Spaces:
Sleeping
Sleeping
File size: 8,580 Bytes
5f38e95 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | r"""Supervised datasets built from Phase-1 simulations or Phase-3 ingested logs.
The canonical inverse-dynamics dataset packages each time step as
* **inputs** :math:`\mathbf{x} = [\mathbf{p}, \mathbf{q}, \mathbf{v},
\boldsymbol\omega, \dot{\mathbf{v}}_\text{des}, \dot{\boldsymbol\omega}_\text{des}]
\in \mathbb{R}^{19}`,
* **target** :math:`\mathbf{y} = \boldsymbol\tau \in \mathbb{R}^m`.
Desired accelerations are computed by finite-differencing the velocity
arrays of the source data; this is what a sensor-equipped lab would also
need to estimate, so the dataset realistically reflects what a deployed
inverse-dynamics model receives at inference time.
Normalisation is per-feature z-score, computed on the *training* split
only --- the validation / test splits use the same transform so a
publishable mean / std bias diagnostic remains comparable across folds.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING
import numpy as np
from numpy.typing import NDArray
from cdpr.learn._lazy import require_torch
if TYPE_CHECKING: # pragma: no cover
import torch
from torch.utils.data import Dataset
from cdpr.dynamics.simulator import SimulationResult
from cdpr.ingest.containers import IngestedExperiment
# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------
def _finite_difference(arr: NDArray[np.float64], dt: float) -> NDArray[np.float64]:
"""Central-difference time derivative; one-sided at the endpoints."""
out = np.empty_like(arr)
out[1:-1] = (arr[2:] - arr[:-2]) / (2 * dt)
out[0] = (arr[1] - arr[0]) / dt
out[-1] = (arr[-1] - arr[-2]) / dt
return out
def _stack_inputs(
positions: NDArray[np.float64],
quaternions: NDArray[np.float64],
linear_velocities: NDArray[np.float64],
angular_velocities: NDArray[np.float64],
linear_accel: NDArray[np.float64],
angular_accel: NDArray[np.float64],
) -> NDArray[np.float64]:
return np.concatenate(
[positions, quaternions, linear_velocities, angular_velocities,
linear_accel, angular_accel],
axis=-1,
)
# ---------------------------------------------------------------------------
# Normalisation
# ---------------------------------------------------------------------------
@dataclass(slots=True)
class Normalizer:
"""Per-feature z-score (fit once on training data, apply elsewhere)."""
mean: NDArray[np.float64] = field(default_factory=lambda: np.zeros(0))
std: NDArray[np.float64] = field(default_factory=lambda: np.ones(0))
def fit(self, X: NDArray[np.float64]) -> "Normalizer":
self.mean = X.mean(axis=0)
std = X.std(axis=0)
std[std < 1e-9] = 1.0 # avoid division by zero on constant channels
self.std = std
return self
def transform(self, X: NDArray[np.float64]) -> NDArray[np.float64]:
return (X - self.mean) / self.std
def inverse_transform(self, X: NDArray[np.float64]) -> NDArray[np.float64]:
return X * self.std + self.mean
# ---------------------------------------------------------------------------
# Dataset
# ---------------------------------------------------------------------------
@dataclass(slots=True)
class TrajectoryDataset:
"""In-memory inverse-dynamics dataset.
The PyTorch ``Dataset`` interface is built lazily via :meth:`as_torch`;
holding the numpy arrays directly is useful for non-torch
benchmarking and unit tests.
"""
inputs: NDArray[np.float64]
targets: NDArray[np.float64]
time: NDArray[np.float64]
feature_names: tuple[str, ...]
normalizer: Normalizer | None = None
@property
def n_features(self) -> int:
return int(self.inputs.shape[-1])
@property
def n_targets(self) -> int:
return int(self.targets.shape[-1])
def __len__(self) -> int:
return int(self.inputs.shape[0])
def split(self, fraction: float = 0.8, shuffle: bool = True,
seed: int = 0) -> tuple["TrajectoryDataset", "TrajectoryDataset"]:
"""Train / validation split with shared time-stamp ordering preserved."""
n = len(self)
idx = np.arange(n)
if shuffle:
np.random.default_rng(seed).shuffle(idx)
cut = int(round(fraction * n))
train_idx, val_idx = np.sort(idx[:cut]), np.sort(idx[cut:])
train = TrajectoryDataset(
inputs=self.inputs[train_idx], targets=self.targets[train_idx],
time=self.time[train_idx], feature_names=self.feature_names,
)
val = TrajectoryDataset(
inputs=self.inputs[val_idx], targets=self.targets[val_idx],
time=self.time[val_idx], feature_names=self.feature_names,
)
return train, val
def fit_normalizer(self) -> "TrajectoryDataset":
self.normalizer = Normalizer().fit(self.inputs)
return self
def transform_with(self, normalizer: Normalizer) -> "TrajectoryDataset":
return TrajectoryDataset(
inputs=normalizer.transform(self.inputs),
targets=self.targets,
time=self.time,
feature_names=self.feature_names,
normalizer=normalizer,
)
def as_torch(self):
"""Return a ``torch.utils.data.TensorDataset`` view of the arrays."""
torch = require_torch()
from torch.utils.data import TensorDataset
return TensorDataset(
torch.as_tensor(self.inputs, dtype=torch.float32),
torch.as_tensor(self.targets, dtype=torch.float32),
)
# ---------------------------------------------------------------------------
# Factories
# ---------------------------------------------------------------------------
def _common_feature_names(n_cables: int) -> tuple[str, ...]:
return (
"p_x", "p_y", "p_z",
"q_x", "q_y", "q_z", "q_w",
"v_x", "v_y", "v_z",
"w_x", "w_y", "w_z",
"a_x", "a_y", "a_z",
"alpha_x", "alpha_y", "alpha_z",
)
def dataset_from_simulation(
result: "SimulationResult", *, dt: float | None = None,
) -> TrajectoryDataset:
"""Build an inverse-dynamics dataset from a :class:`SimulationResult`."""
if dt is None:
if len(result.time) < 2:
raise ValueError("SimulationResult is too short to estimate dt.")
dt = float(result.time[1] - result.time[0])
lin_accel = _finite_difference(result.linear_velocities, dt)
ang_accel = _finite_difference(result.angular_velocities, dt)
X = _stack_inputs(
result.positions, result.quaternions_xyzw,
result.linear_velocities, result.angular_velocities,
lin_accel, ang_accel,
)
return TrajectoryDataset(
inputs=X,
targets=result.cable_tensions.astype(np.float64),
time=result.time.astype(np.float64),
feature_names=_common_feature_names(result.cable_tensions.shape[1]),
)
def dataset_from_experiment(
experiment: "IngestedExperiment",
) -> TrajectoryDataset:
"""Build an inverse-dynamics dataset from an :class:`IngestedExperiment`.
Requires the experiment to carry positions, quaternions, velocities,
and cable tensions. Velocities are differentiated to produce
accelerations; if the experiment also recorded velocities directly,
those are used as-is (the differentiator only fills in accelerations).
"""
needed = ("positions", "quaternions_xyzw", "linear_velocities",
"angular_velocities", "cable_tensions")
missing = [n for n in needed if getattr(experiment, n) is None]
if missing:
raise ValueError(f"experiment is missing required channels: {missing}")
time = experiment.time
if len(time) < 2:
raise ValueError("experiment is too short to estimate dt.")
dt = float(np.median(np.diff(time)))
lin_accel = _finite_difference(experiment.linear_velocities, dt)
ang_accel = _finite_difference(experiment.angular_velocities, dt)
X = _stack_inputs(
experiment.positions, experiment.quaternions_xyzw,
experiment.linear_velocities, experiment.angular_velocities,
lin_accel, ang_accel,
)
return TrajectoryDataset(
inputs=X,
targets=experiment.cable_tensions.astype(np.float64),
time=time.astype(np.float64),
feature_names=_common_feature_names(experiment.cable_tensions.shape[1]),
)
|