File size: 19,788 Bytes
4e21335 f110a8c 4e21335 f110a8c 4e21335 f110a8c 4e21335 5f6f9fc 4e21335 5f6f9fc 4e21335 5f6f9fc c63ab27 4e21335 c63ab27 4e21335 d074242 f110a8c d074242 f110a8c d074242 f110a8c d074242 f110a8c d074242 f110a8c d074242 f110a8c d074242 f110a8c d074242 1ca5c15 d074242 1ca5c15 d074242 1ca5c15 d074242 1ca5c15 d074242 1ca5c15 d074242 1ca5c15 d074242 1ca5c15 f110a8c 1ca5c15 d074242 1ca5c15 b3e10d5 d074242 1ca5c15 d074242 4e21335 d074242 f110a8c 4e21335 d074242 5f6f9fc f110a8c 4e21335 d074242 f110a8c d074242 5f6f9fc f110a8c d074242 4e21335 5f6f9fc f110a8c 5f6f9fc d074242 4e21335 1ca5c15 d074242 1ca5c15 d074242 1ca5c15 d074242 b3e10d5 f110a8c 4e21335 d074242 4e21335 d074242 4e21335 d074242 4e21335 f110a8c 4e21335 f110a8c 4e21335 f110a8c 4e21335 f110a8c 4e21335 f110a8c 4e21335 28e23b5 f110a8c 28e23b5 4e21335 28e23b5 f110a8c 28e23b5 f110a8c 28e23b5 4e21335 | 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | """Data loading utilities: synthetic generation, built-in datasets, CSV upload."""
from __future__ import annotations
import math
from dataclasses import dataclass
from typing import Literal, Optional, Sequence
import numpy as np
import pandas as pd
PatternType = Literal["constant", "sinusoidal", "step", "stairs", "linear", "mixed", "gp"]
MAX_UPLOAD_POINTS = 200_000 # safety limit for HF Spaces
@dataclass
class TimeSeriesData:
"""Container for a loaded time series with optional ground truth."""
signal: np.ndarray # shape (n_timestamps, n_dims)
ground_truth: Optional[np.ndarray] = None # state labels, shape (n_timestamps,)
change_points: Optional[list[int]] = None
name: str = "Unknown"
n_dims: int = 1
def __post_init__(self):
if self.signal.ndim == 1:
self.signal = self.signal[:, np.newaxis]
self.n_dims = self.signal.shape[1]
if self.ground_truth is not None and self.change_points is None:
self.change_points = _states_to_cps(self.ground_truth)
@property
def length(self) -> int:
return self.signal.shape[0]
@property
def n_states(self) -> int:
if self.ground_truth is not None:
return int(self.ground_truth.max()) + 1
return 0
@property
def has_ground_truth(self) -> bool:
return self.ground_truth is not None
def _states_to_cps(states: np.ndarray) -> list[int]:
diffs = np.diff(states)
return list(np.nonzero(diffs)[0] + 1)
# ---------------------------------------------------------------------------
# Synthetic Data
# ---------------------------------------------------------------------------
@dataclass
class SyntheticSeries:
signal: np.ndarray
change_points: list[int]
change_points_one_hot: np.ndarray
states: np.ndarray
def _generate_boundaries(
length: int,
n_segments: int,
rng: np.random.Generator,
min_segment_length: int = 20,
) -> list[int]:
if n_segments < 1:
raise ValueError("n_segments must be >= 1")
if length <= n_segments:
raise ValueError("length must exceed number of segments")
if min_segment_length * n_segments > length:
min_segment_length = max(1, length // n_segments)
proportions = rng.dirichlet(alpha=np.ones(n_segments))
segment_lengths = np.maximum(min_segment_length, (proportions * length).astype(int))
# Redistribute excess/deficit so that no segment becomes negative
delta = length - int(segment_lengths.sum())
if delta > 0:
# Distribute extra samples round-robin
for i in range(delta):
segment_lengths[i % n_segments] += 1
elif delta < 0:
# Remove excess samples from the largest segments first
for _ in range(-delta):
idx = int(np.argmax(segment_lengths))
if segment_lengths[idx] <= min_segment_length:
# All segments are at minimum; shrink the largest anyway
idx = int(np.argmax(segment_lengths))
segment_lengths[idx] -= 1
boundaries: list[int] = []
cursor = 0
for seg_len in segment_lengths[:-1]:
cursor += int(seg_len)
boundaries.append(min(cursor, length - 1))
return [cp for cp in boundaries if cp < length]
def _sine_segment(start, stop, n_dims, mean_offset, amplitude, freq, phase, noise_level, rng):
t = np.linspace(0, 1, stop - start, endpoint=False)
base = amplitude * np.sin(2 * math.pi * (freq * t + phase)) + mean_offset
noise_scale = (0.05 * amplitude if amplitude else 0.05) * noise_level
noise = rng.normal(scale=max(noise_scale, 1e-12), size=(stop - start, n_dims))
return np.tile(base[:, None], (1, n_dims)) + noise
def _complex_segment(start, stop, n_dims, trend, curvature, freq, noise_level, rng):
t = np.linspace(0, 1, stop - start, endpoint=False)
base = trend * t + curvature * (t - 0.5) ** 2
seasonal = np.sin(2 * math.pi * freq * t)
features = base + 0.6 * seasonal
out = np.empty((stop - start, n_dims))
for dim in range(n_dims):
noise = rng.normal(scale=max(0.1 * noise_level, 1e-12), size=features.shape)
drift = rng.normal(scale=max(0.2 * noise_level, 1e-12)) * t
out[:, dim] = features + drift + noise
return out
# -- Pattern-based segment generators --
def _gen_constant(length: int, n_dims: int, level: float, noise_level: float, rng) -> np.ndarray:
"""Flat segment at a given level."""
noise = rng.normal(scale=max(0.05 * noise_level, 1e-12), size=(length, n_dims))
return np.full((length, n_dims), level) + noise
def _gen_sinusoidal(
length: int, n_dims: int, amplitude: float, freq: float, phase: float, offset: float, noise_level: float, rng
) -> np.ndarray:
"""Sinusoidal segment."""
t = np.linspace(0, 1, length, endpoint=False)
base = amplitude * np.sin(2 * math.pi * (freq * t + phase)) + offset
noise = rng.normal(scale=max(0.05 * amplitude * noise_level, 1e-12), size=(length, n_dims))
return np.tile(base[:, None], (1, n_dims)) + noise
def _gen_step(length: int, n_dims: int, levels: Sequence[float], noise_level: float, rng) -> np.ndarray:
"""Piecewise-constant (step function) within a single segment."""
n_steps = len(levels)
step_len = length // n_steps
out = np.empty((length, n_dims))
for i, lvl in enumerate(levels):
start = i * step_len
stop = (i + 1) * step_len if i < n_steps - 1 else length
out[start:stop] = lvl
noise = rng.normal(scale=max(0.05 * noise_level, 1e-12), size=(length, n_dims))
return out + noise
def _gen_stairs(
length: int, n_dims: int, start_level: float, step_size: float, n_stairs: int, noise_level: float, rng
) -> np.ndarray:
"""Staircase pattern — monotonically increasing within the segment."""
stair_len = length // max(n_stairs, 1)
out = np.empty((length, n_dims))
for i in range(n_stairs):
s = i * stair_len
e = (i + 1) * stair_len if i < n_stairs - 1 else length
out[s:e] = start_level + i * step_size
noise = rng.normal(scale=max(0.05 * noise_level, 1e-12), size=(length, n_dims))
return out + noise
def _gen_linear(length: int, n_dims: int, start_val: float, end_val: float, noise_level: float, rng) -> np.ndarray:
"""Linear trend."""
t = np.linspace(start_val, end_val, length)
noise = rng.normal(scale=max(0.05 * noise_level, 1e-12), size=(length, n_dims))
return np.tile(t[:, None], (1, n_dims)) + noise
_PATTERN_GENERATORS = {
"constant": "constant",
"sinusoidal": "sinusoidal",
"step": "step",
"stairs": "stairs",
"linear": "linear",
"gp": "gp",
}
# -- Gaussian-process based segment (smooth, state-dependent) --
def _rbf_kernel(t: np.ndarray, lengthscale: float, variance: float = 1.0) -> np.ndarray:
"""Squared-exponential covariance matrix on a 1-D grid."""
diff = t[:, None] - t[None, :]
return variance * np.exp(-0.5 * (diff / max(lengthscale, 1e-3)) ** 2)
def _sample_gp(length: int, n_dims: int, lengthscale: float, variance: float, rng: np.random.Generator) -> np.ndarray:
"""Sample ``n_dims`` independent GP draws on a unit-interval grid of length ``length``.
Uses a low-rank Cholesky with a tiny jitter for numerical stability.
"""
if length <= 1:
return rng.normal(size=(length, n_dims))
t = np.linspace(0.0, 1.0, length)
K = _rbf_kernel(t, lengthscale=lengthscale, variance=variance)
K = K + 1e-6 * np.eye(length)
try:
L = np.linalg.cholesky(K)
except np.linalg.LinAlgError:
# Fall back to eigendecomposition if Cholesky fails
w, V = np.linalg.eigh(K)
w = np.clip(w, 1e-10, None)
L = V * np.sqrt(w)
z = rng.normal(size=(length, n_dims))
return L @ z
def _gen_gp(
length: int,
n_dims: int,
lengthscale: float,
variance: float,
offset: float,
noise_level: float,
rng: np.random.Generator,
) -> np.ndarray:
"""Smooth Gaussian-process segment with state-dependent statistics."""
base = _sample_gp(length, n_dims, lengthscale=lengthscale, variance=variance, rng=rng)
noise = rng.normal(scale=max(0.05 * noise_level, 1e-12), size=(length, n_dims))
return base + offset + noise
def _generate_segment_by_pattern(
pattern: str,
state_idx: int,
length: int,
n_dims: int,
noise_level: float,
shape_rng: np.random.Generator,
noise_rng: np.random.Generator | None = None,
) -> np.ndarray:
"""Generate one segment according to the chosen pattern type.
Parameters
----------
shape_rng : numpy Generator
Drives all *shape* draws (waveform parameters, GP hyper-parameters,
mixed-pattern choice, GP base realization). Seed it from the state id
so that recurring segments of the same state share the same generative
process.
noise_rng : numpy Generator, optional
Drives the additive observation noise. If ``None``, ``shape_rng`` is
reused (pixel-identical recurring states). Pass a per-segment RNG to
keep the shape consistent across recurrences while varying noise.
"""
if noise_rng is None:
noise_rng = shape_rng
if pattern == "constant":
level = shape_rng.uniform(-2, 2)
return _gen_constant(length, n_dims, level, noise_level, noise_rng)
elif pattern == "sinusoidal":
amp = shape_rng.uniform(0.5, 2.0)
freq = shape_rng.uniform(1.0, 4.0)
phase = shape_rng.uniform(0, 1)
offset = shape_rng.uniform(-1, 1)
return _gen_sinusoidal(length, n_dims, amp, freq, phase, offset, noise_level, noise_rng)
elif pattern == "step":
n_steps = int(shape_rng.integers(2, 5))
levels = shape_rng.uniform(-2, 2, size=n_steps).tolist()
return _gen_step(length, n_dims, levels, noise_level, noise_rng)
elif pattern == "stairs":
start = shape_rng.uniform(-2, 1)
step_size = shape_rng.uniform(0.3, 1.0) * shape_rng.choice([-1, 1])
n_stairs = int(shape_rng.integers(3, 7))
return _gen_stairs(length, n_dims, start, step_size, n_stairs, noise_level, noise_rng)
elif pattern == "linear":
s = shape_rng.uniform(-2, 2)
e = shape_rng.uniform(-2, 2)
return _gen_linear(length, n_dims, s, e, noise_level, noise_rng)
elif pattern == "gp":
# All shape draws (hyper-parameters AND base GP sample) use shape_rng,
# so two segments sharing the same state produce the same realization.
lengthscale = float(shape_rng.uniform(0.04, 0.20))
variance = float(shape_rng.uniform(0.4, 1.8))
offset = float(shape_rng.uniform(-1.5, 1.5))
base = _sample_gp(length, n_dims, lengthscale=lengthscale, variance=variance, rng=shape_rng)
noise = noise_rng.normal(scale=max(0.05 * noise_level, 1e-12), size=(length, n_dims))
return base + offset + noise
elif pattern == "mixed":
# Pick a sub-pattern deterministically from shape_rng so two segments
# of the same recurring state pick the SAME sub-pattern.
choice = shape_rng.choice(["constant", "sinusoidal", "step", "stairs", "linear", "gp"])
return _generate_segment_by_pattern(choice, state_idx, length, n_dims, noise_level, shape_rng, noise_rng)
else:
# Fallback to constant
return _gen_constant(length, n_dims, float(state_idx), noise_level, noise_rng)
def generate_synthetic_series(
length: int = 1000,
n_segments: int = 3,
n_dims: int = 2,
pattern: PatternType = "gp",
noise_level: float = 0.5,
recurring_states: bool = False,
min_segment_length: int = 20,
continuous: bool = True,
random_state: int | None = None,
) -> TimeSeriesData:
"""Generate a synthetic multivariate time series with labelled segments.
Parameters
----------
pattern : str
Intra-segment waveform: 'constant', 'sinusoidal', 'step', 'stairs',
'linear', 'gp' (Gaussian process, smooth and state-dependent),
or 'mixed' (random per segment).
recurring_states : bool
If True, state labels can repeat (fewer distinct states than segments).
Useful for testing state-detection algorithms.
min_segment_length : int
Minimum length of each segment (default 20).
continuous : bool
If True, shift each segment so its first value matches the last value
of the previous segment, removing artificial discontinuities at the
boundaries (purely cosmetic; does not affect state labels).
"""
if length <= 0:
raise ValueError("length must be positive")
if n_dims <= 0:
raise ValueError("n_dims must be positive")
if noise_level < 0:
raise ValueError("noise_level must be non-negative")
rng = np.random.default_rng(random_state)
change_points = _generate_boundaries(
length=length,
n_segments=n_segments,
rng=rng,
min_segment_length=min_segment_length,
)
boundaries = [0, *change_points, length]
# Assign state labels
if recurring_states and n_segments >= 3:
# Use fewer distinct states than segments (at least 2)
n_distinct = max(2, n_segments // 2)
state_ids: list[int] = []
for i in range(n_segments):
# Avoid same state as previous
candidates = list(range(n_distinct))
if state_ids:
candidates = [c for c in candidates if c != state_ids[-1]]
state_ids.append(int(rng.choice(candidates)))
else:
state_ids = list(range(n_segments))
states = np.zeros(length, dtype=int)
signal = np.zeros((length, n_dims), dtype=float)
# One seed per *distinct state* drives all shape draws (waveform params,
# GP hyper-params, mixed sub-pattern choice). Two segments sharing the
# same state thus share the same generative process. A separate per-segment
# seed drives only the additive observation noise, so recurring segments
# look alike without being pixel-identical.
distinct_states = sorted(set(state_ids))
state_seeds: dict[int, int] = {s: int(rng.integers(0, 2**31)) for s in distinct_states}
noise_seeds: list[int] = [int(rng.integers(0, 2**31)) for _ in range(len(state_ids))]
for seg_idx, (start, stop) in enumerate(zip(boundaries[:-1], boundaries[1:])):
sid = state_ids[seg_idx]
seg_len = stop - start
shape_rng = np.random.default_rng(state_seeds[sid])
noise_rng = np.random.default_rng(noise_seeds[seg_idx])
segment = _generate_segment_by_pattern(pattern, sid, seg_len, n_dims, noise_level, shape_rng, noise_rng)
# Stitch segment to previous endpoint to suppress visual discontinuities
# at the boundary (does not change the state label or boundary index).
if continuous and seg_idx > 0 and seg_len > 0:
prev_end = signal[start - 1] # shape (n_dims,)
shift = prev_end - segment[0]
segment = segment + shift
signal[start:stop] = segment
states[start:stop] = sid
recur_label = ", recurring" if recurring_states else ""
return TimeSeriesData(
signal=signal,
ground_truth=states,
change_points=change_points,
name=f"Synthetic ({pattern}{recur_label}, {n_segments} seg, {n_dims}D)",
)
# ---------------------------------------------------------------------------
# Built-in Datasets
# ---------------------------------------------------------------------------
MOCAP_TRIALS = list(range(9))
def load_mocap_dataset(trial: int = 0) -> TimeSeriesData:
"""Load a sample CMU MoCap 86 trial from tsseg built-in datasets."""
from tsseg.data.datasets import load_mocap
X, y = load_mocap(trial=trial, return_X_y=True)
X = np.asarray(X, dtype=float)
y = np.asarray(y, dtype=int)
return TimeSeriesData(
signal=X,
ground_truth=y,
name=f"Sample dataset (CMU MoCap 86, trial {trial})",
)
def get_builtin_datasets() -> dict[str, dict]:
"""Return available built-in datasets metadata."""
return {
"Sample dataset": {
"description": (
"A real-world trial from CMU MoCap 86 — humeral & femoral angles, 4 dimensions, 9 labelled trials."
),
"loader": load_mocap_dataset,
"params": {"trial": {"type": "int", "choices": MOCAP_TRIALS, "default": 0}},
},
}
# ---------------------------------------------------------------------------
# CSV Upload
# ---------------------------------------------------------------------------
def load_csv(
file_path: str,
separator: str = ",",
has_header: bool = True,
label_column: str | None = None,
) -> TimeSeriesData:
"""Load a time series from a CSV file.
Parameters
----------
file_path : str
Path to the CSV file.
separator : str
Column separator.
has_header : bool
Whether the first row is a header.
label_column : str or None
Name of the ground truth column (if any).
"""
header = 0 if has_header else None
df = pd.read_csv(file_path, sep=separator, header=header)
if len(df) > MAX_UPLOAD_POINTS:
raise ValueError(
f"File too large: {len(df)} rows (max {MAX_UPLOAD_POINTS}). Please truncate or subsample your data."
)
ground_truth = None
if label_column and label_column in df.columns:
ground_truth = df[label_column].values.astype(int)
df = df.drop(columns=[label_column])
elif label_column and label_column not in df.columns:
# Try auto-detect common names
for candidate in [
"label",
"labels",
"ground_truth",
"gt",
"state",
"states",
"class",
"segment",
"is_anomaly",
"anomaly",
"target",
"y",
]:
if candidate in df.columns:
ground_truth = df[candidate].values.astype(int)
df = df.drop(columns=[candidate])
break
else:
# No label_column specified — still try auto-detect
for candidate in [
"label",
"labels",
"ground_truth",
"gt",
"state",
"states",
"class",
"segment",
"is_anomaly",
"anomaly",
"target",
"y",
]:
if candidate in df.columns:
ground_truth = df[candidate].values.astype(int)
df = df.drop(columns=[candidate])
break
# Drop common index / timestamp columns that are not signal data
_INDEX_COLS = {
"timestamp",
"timestamps",
"time",
"date",
"datetime",
"index",
"idx",
"id",
"row",
"step",
}
cols_to_drop = [c for c in df.columns if str(c).lower().strip() in _INDEX_COLS]
if cols_to_drop:
df = df.drop(columns=cols_to_drop)
# Drop non-numeric columns
numeric_df = df.select_dtypes(include=[np.number])
if numeric_df.empty:
raise ValueError("No numeric columns found in the CSV file.")
signal = numeric_df.values.astype(float)
# Check for NaN
nan_ratio = np.isnan(signal).mean()
if nan_ratio > 0.1:
raise ValueError(f"Too many NaN values ({nan_ratio:.1%}). Please clean your data.")
if nan_ratio > 0:
# Forward-fill NaN
df_filled = pd.DataFrame(signal).ffill().bfill()
signal = df_filled.values
return TimeSeriesData(
signal=signal,
ground_truth=ground_truth,
name="Uploaded CSV",
)
|