Instructions to use Zipeng365/WISP with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use Zipeng365/WISP with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("Zipeng365/WISP", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
File size: 7,794 Bytes
4731b11 | 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 | """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:<stream>"`` (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
|