"""Accelerated execution of funnel candidates (full-split verification and the winner refit).""" from contextvars import ContextVar from dataclasses import dataclass import time import numpy as np from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier from sklearn.linear_model import RidgeClassifier from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.utils.class_weight import compute_sample_weight from wisp.cpu.algorithms import _classifier, build_cpu_estimator from wisp.cpu.probability import align_proba, estimator_proba, global_classes, labels_from_proba from wisp.registry import build_method, list_specs from . import features as fx from .cache import _ACTIVE, _SEARCH, transform_blocks from .decoder import FastSmoother from .forest import ForestPlan, local_proba CLASS_TO_ID = {spec.code_class: spec.method_id for spec in list_specs()} # Candidate decoder override: None keeps each candidate's own HMM flag (the default and the # benchmark record); True/False forces HMM/argmax decoding in the proxy, verification and refit. FORCE_HMM = ContextVar("wisp_funnel_force_hmm", default=None) def decoder_spec(spec_dict): """The candidate specification with its HMM flag overridden when ``FORCE_HMM`` is set.""" force = FORCE_HMM.get() if force is None or not isinstance(spec_dict, dict): return spec_dict return {**spec_dict, "params": {**spec_dict.get("params", {}), "use_hmm": bool(force)}} @dataclass(frozen=True) class TurboBudget: """Upper limits. The canonical HMM sees all training labels and timestamps.""" kernels: int = 32 intervals: int = 4 shapelets: int = 6 trees: int = 24 max_steps: int = 96 max_train: int = 4096 ridge_alpha: float = 1.0 def __post_init__(self): for field in ("kernels", "intervals", "shapelets", "trees", "max_steps", "max_train"): val = getattr(self, field) if not isinstance(val, (int, np.integer)) or val < 1: raise ValueError(f"{field} must be a positive integer") if self.max_steps < 15: raise ValueError("max_steps must allow the longest standard convolution (15)") if self.ridge_alpha <= 0 or not np.isfinite(self.ridge_alpha): raise ValueError("ridge_alpha must be finite and positive") def build_candidate(spec_dict, *, mode="exact", budget=None): spec_dict = decoder_spec(spec_dict) if isinstance(spec_dict, dict) and spec_dict.get("route", "cpu") != "cpu": raise ValueError("Only CPU WISP candidates are released") return FastFamily(build_cpu_estimator(spec_dict), mode, budget) def _training_rows(y, cap, seed): """Roughly proportional stratified random sample, retaining every observed class.""" labels, inverse, counts = np.unique(y, return_counts=True, return_inverse=True) if cap < len(labels): raise ValueError("max_train is smaller than the number of observed classes") # Reserve one per class, then allocate remaining slots proportionally to capacity. quota = (counts - 1) * ((cap - len(labels)) / (len(y) - len(labels))) floor = np.floor(quota).astype(int) sizes = floor + 1 order = np.argsort(-(quota - floor), kind="stable") sizes[order[:cap - sizes.sum()]] += 1 rng = np.random.default_rng(seed) return np.sort(np.concatenate([ rng.choice(np.flatnonzero(inverse == j), size=size, replace=False) for j, size in enumerate(sizes)])) class FastFamily: """fit/predict API of the release. Inference batching never splits a sequence decoder.""" def __init__(self, template, mode="exact", budget=None, ridge_solver="original", feature_workers=1): if not isinstance(feature_workers,(int,np.integer)) or feature_workers!=1: raise ValueError("feature_workers must be 1 (features are computed in the calling thread)") self.feature_workers = feature_workers if ridge_solver not in ("original","screened"): raise ValueError("Unknown Ridge solver") if ridge_solver!="original" and (mode!="exact" or CLASS_TO_ID[type(template).__name__] not in ("wisp_rc","wisp_cis") or template.classifier!="ridge"): raise ValueError("Custom solvers are supported for exact RC/CIS Ridge models and CIS recipes") self.ridge_solver = ridge_solver if mode not in {"exact", "turbo"}: raise ValueError("mode must be exact or turbo") if budget is not None and mode != "turbo": raise ValueError("budget is only meaningful in turbo mode") if budget is not None and not isinstance(budget, TurboBudget): raise TypeError("budget must be a TurboBudget") self.template = template self.method_id = CLASS_TO_ID[type(template).__name__] self.mode = mode self.budget = (budget or TurboBudget()) if mode == "turbo" else None self.n_jobs = template.n_jobs self.smoother_ = None self.members_ = [] self.blocks_ = [] self.model_ = None self.classes_ = None self.input_shape_ = None self.checkpoint_only_ = False def __getstate__(self): # Process-local pointers cannot be serialized or included in a size tiebreak. state = self.__dict__.copy() state.pop("_forest_plan", None) return state def design_profile(self): return self.template.design_profile() def _blocks(self): m, family, b = self.template, self.method_id, self.budget fast = {"fast_profile": self.mode == "turbo"} if family == "wisp_ss": return [fx.WearableFeatureExtractor(segment_sizes=m.segment_sizes, symbolic_bins=m.symbolic_bins, **fast)] if family == "wisp_so": return [fx.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, **fast)] if family == "wisp_gis": return [fx.InvariantPhysicsFeatureExtractor( segment_sizes=m.segment_sizes, symbolic_bins=m.symbolic_bins, random_intervals=min(24, b.intervals) if b else 24, random_state=m.seed, include_axis_features=True, **fast), fx.RandomShapeletSketch( n_shapelets=min(m.n_shapelets, b.shapelets) if b else m.n_shapelets, random_state=m.seed+17, **fast)] if family in {"wisp_cis", "wisp_rc"}: kernels = min(m.n_kernels, b.kernels) if b else m.n_kernels blocks = [] if getattr(m, "use_rocket", True): blocks.append(fx.RandomConvSketch(n_kernels=kernels, random_state=m.seed, **fast)) if family == "wisp_cis" and getattr(m, "use_intervals", True): blocks.append(fx.IntervalDistributionSketch( n_random_intervals=min(m.n_intervals, b.intervals) if b else m.n_intervals, random_state=m.seed+29, **fast)) if family == "wisp_cis" and getattr(m, "use_state", True): blocks.append(fx.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, **fast)) return blocks raise ValueError(f"{family} has no base feature blocks") def _inputs(self, X): X = np.asarray(X) if X.ndim == 2: X = X[..., None] if X.ndim != 3 or min(X.shape) < 1: raise ValueError("X must have nonempty shape (N,T,C) or (N,T)") return X def _times(self, X): if self.budget and X.shape[1] > self.budget.max_steps: def sample(): take = np.linspace(0, X.shape[1]-1, self.budget.max_steps, dtype=np.intp) return np.ascontiguousarray(X[:, take, :]) memory = _SEARCH.get() return sample() if memory is None else memory.sampled(X, self.budget.max_steps, sample) return X def _fit_base(self, X, y): self.blocks_ = self._blocks() for block in self.blocks_: block.fit(X, y) self._fit_head(self._features(X), y) def _fit_head(self, F, y): m, b = self.template, self.budget kind = getattr(m, "classifier", "extratrees") if self.method_id == "wisp_so": head = ExtraTreesClassifier(n_estimators=160, max_features="sqrt", class_weight="balanced", random_state=m.seed, n_jobs=self.n_jobs) elif b and kind == "ridge": head = RidgeClassifier(alpha=b.ridge_alpha, solver="cholesky") elif getattr(self,"ridge_solver","original") != "original": from .ridge_fast import ScreenedRidgeClassifierCV head = ScreenedRidgeClassifierCV(alphas=np.logspace(-3,3,9)) elif hasattr(self, "recipe_id"): from wisp.cis_algorithms import _classifier as recipe_classifier head = recipe_classifier(kind, seed=m.seed, n_jobs=self.n_jobs, class_balance=m.class_balance) else: head = _classifier(kind, seed=m.seed, n_jobs=self.n_jobs) if b: if kind in {"rf", "extratrees"}: head.set_params(n_estimators=min(head.n_estimators, b.trees)) elif kind == "logreg": head.set_params(max_iter=250, tol=1e-3) weighted = kind == "ridge" and getattr(m, "class_balance", "balanced") == "balanced" kwargs = {"clf__sample_weight": compute_sample_weight("balanced", y)} if weighted else {} if self.method_id in {"wisp_cis", "wisp_rc"} or kind == "logreg": head = Pipeline([("scale", StandardScaler()), ("clf", head)]) # Upstream only uses ridge via the RC/CIS scaled pipeline in its search grammar. if kind == "ridge" and not isinstance(head, Pipeline): kwargs = {} forest = head.steps[-1][1] if isinstance(head, Pipeline) else head if _SEARCH.get() is not None and type(forest) in (ExtraTreesClassifier,RandomForestClassifier): from .search_forest import forest_fit_plan with forest_fit_plan(forest): self.model_ = head.fit(F, y, **kwargs) else: self.model_ = head.fit(F, y, **kwargs) self.__dict__.pop("_forest_plan", None) def _features(self, X): return self._feature_batch(X) def _feature_batch(self, X): blocks = transform_blocks(self.blocks_, X) return blocks[0] if len(blocks) == 1 else np.concatenate(blocks, axis=1) def _member_models(self): seed = self.template.seed if self.method_id == "wisp_cse": configs = [("wisp_ss", {}), ("wisp_rc", {"n_kernels": 256}), ("wisp_so", {})] else: configs = [("wisp_cis", {"n_kernels": 256, "n_intervals": 16}), ("wisp_gis", {"n_shapelets": 24}), ("wisp_so", {})] members = [] for j, (name, params) in enumerate(configs): template = build_method(name, seed=seed+j, n_jobs=self.n_jobs, overrides={"use_hmm": False, **params}) members.append(FastFamily(template, self.mode, self.budget)) return members def fit(self, X, y, *, groups=None, time_index=None, n_classes=None): if self.checkpoint_only_: raise ValueError("Construct a new family for refitting an imported checkpoint") start = time.perf_counter() X = self._inputs(X) y = np.asarray(y, dtype=np.int64) if y.shape != (len(X),): raise ValueError("y must have one label per window") self.classes_ = global_classes(y, n_classes) self.trained_classes_ = np.unique(y) self.input_shape_ = X.shape[1:] k = len(self.classes_) self.smoother_ = (FastSmoother().fit(y, groups=groups, time_index=time_index, n_classes=k) if self.template.use_hmm else None) if self.budget and len(y) > self.budget.max_train: rows = _training_rows(y, self.budget.max_train, self.template.seed) X, y = X[rows], y[rows] self.fit_windows_ = len(y) if self.method_id in {"wisp_cse", "wisp_ese"}: self.members_ = self._member_models() for member in self.members_: member.fit(X, y, n_classes=k) else: self._fit_base(self._times(X), y) self.fit_seconds_ = time.perf_counter() - start return self def _head_proba(self,features,*,private=False): head=self.model_ # Standalone fitted model only. Shared panels, disk cache mappings, # and externally supplied feature arrays take the read-only path. if (private and type(head) is Pipeline and len(head.steps)==2 and type(head.steps[0][1]) is StandardScaler and features.dtype==np.float64 and features.flags.writeable and features.flags.owndata): scaled=head.steps[0][1].transform(features,copy=False) return estimator_proba(head.steps[1][1],scaled,self.classes_) if isinstance(head,(ExtraTreesClassifier,RandomForestClassifier)) and head.n_jobs==1: plan=getattr(self,"_forest_plan",None) if plan is None:plan=self._forest_plan=ForestPlan(head) return align_proba(local_proba(head,features,plan),head.classes_,self.classes_) return estimator_proba(head,features,self.classes_) def _proba(self, X): if not self.members_: private=(self.method_id in ("wisp_cis","wisp_rc") and len(self.blocks_)>=2 and _SEARCH.get() is None and _ACTIVE.get() is None) return self._head_proba(self._features(self._times(X)),private=private) return self._combine(lambda member:member._proba(X),len(X)) def _combine(self,score,n): # Accumulate in canonical order without retaining all member scores. if self.method_id == "wisp_cse": out = score(self.members_[0]).copy() for member in self.members_[1:]: out += score(member) out /= len(self.members_) else: w = np.asarray(self.template.weights, dtype=np.float64) if w.size != len(self.members_): w = np.ones(len(self.members_)) w = w / np.maximum(w.sum(), 1e-12) out = np.zeros((n, len(self.classes_))) for wi, member in zip(w, self.members_): if wi != 0: out += wi * score(member) return out / np.maximum(out.sum(axis=1, keepdims=True), 1e-12) def predict_proba(self, X, *, batch_size=None): if self.classes_ is None: raise RuntimeError("Unfitted estimator") X = self._inputs(X) if self.input_shape_ is not None and X.shape[1:] != self.input_shape_: raise ValueError(f"Fitted T,C={self.input_shape_}; got {X.shape[1:]}") if batch_size is None: return self._proba(X) if not isinstance(batch_size, (int, np.integer)) or batch_size < 1: raise ValueError("batch_size must be a positive integer") out = np.empty((len(X), len(self.classes_)), dtype=np.float64) for start in range(0, len(X), batch_size): stop = min(len(X), start + batch_size) out[start:stop] = self._proba(X[start:stop]) return out def decode_proba(self, proba, *, groups=None, time_index=None): if self.smoother_ is not None and groups is not None: return self.smoother_.predict_from_proba(proba, groups=groups, time_index=time_index) return labels_from_proba(proba, self.classes_) def predict(self, X, *, groups=None, time_index=None, batch_size=None): return self.decode_proba(self.predict_proba(X, batch_size=batch_size), groups=groups, time_index=time_index)