Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / src /wisp /_validation.py
Zipeng365's picture
wisp-har 0.4.0: WISP-Funnel search and the adaptive HMM switch (default decoder)
93a9282 verified
Raw History Blame Contribute Delete
6.54 kB
"""Input checks shared by the public interface: window layout, labels, groups and splits."""
from __future__ import annotations
import contextlib
from typing import Any
import numpy as np
LAYOUTS = ("NTC", "NCT")
SPEEDS = ("exact", "fast")
SEQUENCES = ("subject", "recording")
DECODERS = ("adaptive", "recipe", "hmm", "none", "auto") # "auto": deprecated alias of "recipe"
def check_choice(name: str, value: Any, choices: tuple) -> Any:
if value not in choices:
raise ValueError(f"{name} must be one of {', '.join(map(repr, choices))}; got {value!r}")
return value
def as_windows(X: Any, layout: str = "NTC") -> np.ndarray:
"""Return windows as a finite float64 ``(n, T, C)`` array (the kernel convention).
``layout="NCT"`` accepts ``(n, C, T)`` and transposes it. A 2-D array is read as
``(n, T)``, i.e. single-channel windows, as in the kernel. Values must be finite:
impute missing samples before windowing (the kernel would silently zero them).
"""
check_choice("layout", layout, LAYOUTS)
try:
from scipy import sparse
if sparse.issparse(X):
raise TypeError("Sparse input is not supported: WISP needs dense arrays of windows")
except ImportError: # pragma: no cover
pass
arr = np.asarray(X)
if np.iscomplexobj(arr):
raise ValueError("Complex data not supported")
try:
arr = np.asarray(arr, dtype=np.float64)
except (TypeError, ValueError) as exc:
raise TypeError(f"X must be a numeric array of windows ({exc})") from None
if arr.ndim == 1:
raise ValueError("Expected windows of shape (n, T, C) or (n, T), got a 1-D array. Reshape your data, "
"e.g. X[None, :] for a single window.")
if arr.ndim == 2:
if arr.shape[1] == 0:
raise ValueError(f"0 feature(s) (shape={arr.shape}) while a minimum of 1 is required.")
arr = arr[:, :, None] if layout == "NTC" else arr[:, None, :]
if arr.ndim != 3:
raise ValueError(f"X must have shape (n, T, C) for layout='NTC' or (n, C, T) for layout='NCT'; got {arr.shape}")
if arr.shape[0] == 0:
raise ValueError(f"Found array with 0 sample(s) (shape={arr.shape}) while a minimum of 1 is required.")
if layout == "NCT":
arr = arr.transpose(0, 2, 1)
if min(arr.shape) < 1:
raise ValueError(f"X must be nonempty; got shape {arr.shape}")
if not np.isfinite(arr).all():
raise ValueError("Input X contains NaN or infinity; WISP expects finite windows (impute missing samples first)")
return np.ascontiguousarray(arr)
def check_input_shape(estimator: Any, X_raw: Any, Xw: np.ndarray) -> None:
"""Fitted (T, C) must match; 2-D input gets scikit-learn's feature-count message."""
expected = tuple(estimator.input_shape_)
if Xw.shape[1:] != expected:
raw = np.asarray(X_raw) if not hasattr(X_raw, "shape") else X_raw
if getattr(raw, "ndim", 3) == 2:
raise ValueError(f"X has {raw.shape[1]} features, but {type(estimator).__name__} is expecting "
f"{estimator.n_features_in_} features as input.")
raise ValueError(f"Fitted on windows of shape (T, C)={expected}; got {Xw.shape[1:]}")
def check_targets(estimator: Any, y: Any) -> np.ndarray:
"""1-D classification targets (scikit-learn conventions for errors and warnings)."""
from sklearn.utils.multiclass import check_classification_targets
from sklearn.utils.validation import assert_all_finite, column_or_1d
if y is None:
raise ValueError(f"{type(estimator).__name__} requires y to be passed, but the target y is None.")
y = column_or_1d(y, warn=True)
if y.dtype.kind in "fc":
assert_all_finite(y, input_name="y")
check_classification_targets(y)
return y
def check_aligned(name: str, values: Any, n: int) -> np.ndarray | None:
if values is None:
return None
arr = np.asarray(values)
if arr.ndim != 1 or len(arr) != n:
raise ValueError(f"{name} must be one-dimensional with one entry per window ({n}); got shape {arr.shape}")
return arr
def encode_labels(y: Any, classes: Any | None = None) -> tuple[np.ndarray, np.ndarray]:
"""Encode arbitrary hashable labels as ``0..K-1``.
With ``classes`` the order of that global class table is used and every label in
``y`` must belong to it; otherwise the sorted unique labels of ``y`` are used.
"""
y = np.asarray(y)
if y.ndim != 1 or y.size == 0:
raise ValueError("y must be a nonempty one-dimensional array")
if classes is None:
table, codes = np.unique(y, return_inverse=True)
return table, codes.astype(np.int64)
table = np.asarray(classes)
if table.ndim != 1 or table.size == 0:
raise ValueError("classes must be a nonempty one-dimensional table")
if len(np.unique(table)) != table.size:
raise ValueError("classes must not contain duplicates")
lookup = {_key(c): i for i, c in enumerate(table.tolist())}
try:
codes = np.fromiter((lookup[_key(v)] for v in y.tolist()), dtype=np.int64, count=y.size)
except KeyError as exc:
raise ValueError(f"label {exc.args[0]!r} is not in the supplied classes table") from None
return table, codes
def _key(value: Any) -> Any:
if isinstance(value, (np.generic,)):
return value.item()
return value
def check_disjoint_groups(train_groups: Any, valid_groups: Any) -> None:
if train_groups is None or valid_groups is None:
raise ValueError("Selection and search need groups for both training and validation windows")
a = set(np.asarray(train_groups).astype(str).tolist())
b = set(np.asarray(valid_groups).astype(str).tolist())
shared = a & b
if shared:
raise ValueError(f"Training and validation groups overlap ({len(shared)} shared IDs, e.g. "
f"{sorted(shared)[:3]}); validation must come from different sequences")
@contextlib.contextmanager
def kernel_context():
"""Run frozen-kernel code with scikit-learn metadata routing switched off.
The kernel passes ``clf__sample_weight`` to its internal pipelines, which scikit-learn
rejects when a caller has enabled metadata routing globally. Routing of ``groups`` to
the public estimators is unaffected; this only scopes the kernel's own calls
(``sklearn.config_context`` is thread-local).
"""
import sklearn
with sklearn.config_context(enable_metadata_routing=False):
yield