"""WISPRepresentation: a recipe's representation as a scikit-learn transformer.""" from __future__ import annotations from typing import Any import numpy as np from sklearn.base import BaseEstimator, TransformerMixin, clone from sklearn.utils.validation import check_is_fitted from ._validation import as_windows, check_choice, check_input_shape, kernel_context from .recipes import Recipe, resolve def kernel_blocks(recipe: Recipe, seed: int) -> list[tuple[str, Any]]: """Unfitted kernel transformers of a single-readout recipe, in the family's column order. The constructor arguments and seed offsets are those used inside the family classes of ``wisp.cis_algorithms`` (the parameter values are read from a constructed family object, so defaults always match the kernel). """ from wisp.cis_algorithms import build_cpu_estimator from wisp.cpu.features import (IntervalDistributionSketch, InvariantPhysicsFeatureExtractor, RandomConvSketch, RandomShapeletSketch, WearableFeatureExtractor) if not recipe.single_readout: raise ValueError(f"{recipe.family} is a score-level ensemble with no single feature vector; " "use WISPClassifier(...).predict_proba for its class scores") m = build_cpu_estimator(recipe.kernel_spec(seed=seed, n_jobs=1)) if recipe.family == "har_sculpt_forest_hmm": return [("statistics", WearableFeatureExtractor(segment_sizes=m.segment_sizes, symbolic_bins=m.symbolic_bins))] if recipe.family == "rocket_sketch_ridge_hmm": return [("R", RandomConvSketch(n_kernels=m.n_kernels, random_state=m.seed))] if recipe.family == "symbolic_interval_forest": return [("symbolic", WearableFeatureExtractor( include_raw_stats=True, include_magnitude=True, include_jerk=False, include_multiscale=True, segment_sizes=(5, 10, 20), include_spectral=False, include_autocorr=False, include_cross_channel=True, include_symbolic=True, symbolic_bins=8))] if recipe.family == "invariant_interval_shape_forest": return [("geometry_interval", InvariantPhysicsFeatureExtractor( segment_sizes=m.segment_sizes, symbolic_bins=int(m.symbolic_bins), random_intervals=24, random_state=m.seed, include_axis_features=True)), ("shapelets", RandomShapeletSketch(n_shapelets=int(m.n_shapelets), random_state=m.seed + 17))] if recipe.family == "state_rocket_interval_hmm": if getattr(m, "use_anchor_stats", False): raise ValueError("Anchor statistics are not part of the paper configuration") blocks = [] if m.use_rocket: blocks.append(("R", RandomConvSketch(n_kernels=int(m.n_kernels), random_state=m.seed))) if m.use_intervals: blocks.append(("I", IntervalDistributionSketch(n_random_intervals=int(m.n_intervals), random_state=m.seed + 29))) if m.use_state: blocks.append(("S", WearableFeatureExtractor( include_raw_stats=False, include_magnitude=True, include_jerk=True, include_multiscale=False, include_spectral=True, include_autocorr=True, include_cross_channel=True, include_symbolic=True, symbolic_bins=6))) if not blocks: raise ValueError("At least one CIS representation must be enabled") return blocks raise ValueError(f"No representation mapping for {recipe.family}") class WISPRepresentation(TransformerMixin, BaseEstimator): """Window-to-vector representation of a WISP recipe (scikit-learn transformer). Parameters ---------- recipe : str, Recipe or dict, default="cis" A single-readout recipe: ``"ss"``, ``"rc"``, ``"so"``, ``"gis"``, ``"cis"``, ``"cis:R"`` ... ``"cis:IS"``, ``"best:"`` (single-readout ones) or a custom recipe of those families. ``"cse"``/``"ese"`` raise ``ValueError``. extra_blocks : list of transformers or (name, transformer) pairs, optional User blocks fitted on the same windows (in ``layout``); their outputs are appended after the recipe's columns. Any window-in, vector-out transformer works. layout : {"NTC", "NCT"}, default="NTC" Axis order of the input windows. dtype : {"float64", "float32"}, default="float64" Output dtype (features are always computed in float64). random_state : int, default=42 Seed of the random kernels/intervals/shapelets (same offsets as the family). Each kernel transformer standardises every window and channel itself; do not add a global standardisation of the raw windows in front of it. """ def __init__(self, recipe="cis", *, extra_blocks=None, layout="NTC", dtype="float64", random_state=42): self.recipe = recipe self.extra_blocks = extra_blocks self.layout = layout self.dtype = dtype self.random_state = random_state def _extra(self): out = [] for i, item in enumerate(self.extra_blocks or ()): name, block = item if isinstance(item, tuple) else (f"extra{i}", item) out.append((str(name), block)) return out def fit(self, X, y=None): check_choice("dtype", self.dtype, ("float64", "float32")) recipe = resolve(self.recipe) Xw = as_windows(X, self.layout) self.recipe_ = recipe self.blocks_ = kernel_blocks(recipe, int(self.random_state)) with kernel_context(): for _, block in self.blocks_: block.fit(Xw, y) self.extra_blocks_ = [(name, clone(block).fit(X, y)) for name, block in self._extra()] self.input_shape_ = Xw.shape[1:] self.n_features_in_ = int(np.prod(self.input_shape_)) probe_kernel = [block.transform(Xw[:2]) for _, block in self.blocks_] probe_extra = [np.asarray(block.transform(np.asarray(X)[:2])) for _, block in self.extra_blocks_] widths, names = [], [] for (name, _), arr in zip(self.blocks_ + self.extra_blocks_, probe_kernel + probe_extra): if arr.ndim != 2: raise ValueError(f"block {name!r} must return a 2-D (n, d) array; got shape {arr.shape}") widths.append(arr.shape[1]) names.append(name) bounds = np.cumsum([0] + widths) self.block_slices_ = {name: slice(int(a), int(b)) for name, a, b in zip(names, bounds[:-1], bounds[1:])} self.n_features_out_ = int(bounds[-1]) self.is_catalog_representation_ = bool(recipe.in_catalog and not self.extra_blocks_) return self def transform(self, X): check_is_fitted(self, "blocks_") Xw = as_windows(X, self.layout) check_input_shape(self, X, Xw) parts = [block.transform(Xw) for _, block in self.blocks_] parts += [np.asarray(block.transform(X), dtype=np.float64) for _, block in self.extra_blocks_] Z = parts[0] if len(parts) == 1 else np.concatenate(parts, axis=1) return Z.astype(self.dtype, copy=False) def get_feature_names_out(self, input_features=None): check_is_fitted(self, "blocks_") names = [] for name, block in self.blocks_ + self.extra_blocks_: sl = self.block_slices_[name] inner = list(getattr(block, "feature_names_", None) or []) width = sl.stop - sl.start if len(inner) != width: inner = [str(i) for i in range(width)] names.extend(f"{name}__{n}" for n in inner) return np.asarray(names, dtype=object) def __sklearn_tags__(self): tags = super().__sklearn_tags__() tags.input_tags.three_d_array = True return tags