Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / src /wisp /_engine.py
Zipeng365's picture
wisp-har 0.3.0: scikit-learn interface, recipe catalog, Optuna search, cost-accuracy figures
4731b11 verified
Raw History Blame Contribute Delete
4.39 kB
"""Construct the frozen-kernel estimator for a recipe and a speed tier (no fitting here).
``speed="exact"``: the 0.2.0 accelerated engine (``wisp_fast``) with the original Ridge
solver. Its decoded labels were verified identical to the paper code; see docs/SPEED.md
for the cells, score differences and timings behind this choice.
``speed="fast"``: the same engine with the screened Ridge solver (Ridge strength chosen on
a 512-row training subsample, then one fit on all rows). Only Ridge readouts change.
Every path below only *constructs* kernel objects through existing builders; the
numerical work is done by unmodified kernel code.
"""
from __future__ import annotations
from typing import Any
from .recipes import Recipe
#: speed tier -> Ridge solver name of the accelerated engine
RIDGE_SOLVER = {"exact": "original", "fast": "screened"}
def _reference_kernel(recipe: Recipe, *, use_hmm: bool, seed: int, n_jobs: int) -> Any:
"""Paper (unaccelerated) kernel object for a recipe."""
if recipe.source == "fixed":
from wisp.registry import build_method
return build_method(recipe.provenance["method_id"], seed=seed, n_jobs=n_jobs,
overrides={"use_hmm": bool(use_hmm)})
if recipe.source == "cis_subset":
from wisp.ablations import build_recipe
return build_recipe(recipe.provenance["paper_ids"]["hmm" if use_hmm else "none"], seed=seed, n_jobs=n_jobs)
from wisp.cis_algorithms import build_cpu_estimator
spec = recipe.kernel_spec(seed=seed, n_jobs=n_jobs)
spec["params"]["use_hmm"] = bool(use_hmm)
return build_cpu_estimator(spec)
def build_kernel(recipe: Recipe, *, use_hmm: bool, speed: str = "exact", sequence: str = "subject",
seed: int = 42, n_jobs: int = 1, engine: str | None = None) -> Any:
"""Kernel estimator for ``recipe`` with fit/predict/predict_proba on encoded labels.
``engine="reference"`` returns the paper code itself (used by the equivalence tests).
"""
if engine == "reference":
if speed != "exact" or sequence != "subject":
raise ValueError("The reference kernel supports speed='exact' and sequence='subject' only")
return _reference_kernel(recipe, use_hmm=use_hmm, seed=seed, n_jobs=n_jobs)
if engine not in (None, "accelerated"):
raise ValueError(f"Unknown engine {engine!r}")
if recipe.params.get("class_balance", "balanced") != "balanced":
# The accelerated engine implements the balanced heads of the paper grammar only.
if speed != "exact" or sequence != "subject":
raise ValueError("class_balance='none' recipes run on the reference kernel only")
return _reference_kernel(recipe, use_hmm=use_hmm, seed=seed, n_jobs=n_jobs)
solver = RIDGE_SOLVER[speed]
if recipe.source == "fixed":
from wisp_release.methods import build_family
return build_family(recipe.provenance["method_id"], seed=seed, n_jobs=n_jobs,
overrides={"use_hmm": bool(use_hmm)}, speed=speed, sequence=sequence)
if recipe.source == "cis_subset":
from wisp_fast.engine import build_recipe
return build_recipe(recipe.provenance["paper_ids"]["hmm" if use_hmm else "none"], seed=seed,
n_jobs=n_jobs, ridge_solver=solver if recipe.params.get("classifier") == "ridge"
else "original", sequence=sequence)
spec = recipe.kernel_spec(seed=seed, n_jobs=n_jobs)
spec["params"]["use_hmm"] = bool(use_hmm)
if recipe.family == "state_rocket_interval_hmm" and any(
k in spec["params"] for k in ("use_rocket", "use_intervals", "use_state", "class_balance", "use_anchor_stats")):
# CIS block subsets exist only in the wisp.cis_algorithms template (as for wisp.ablations);
# wrap it exactly as wisp_fast.engine.build_recipe does.
from wisp.cis_algorithms import build_cpu_estimator
from wisp_fast.engine import FastFamily
template = build_cpu_estimator(spec)
model = FastFamily(template, "exact", None, ridge_solver=solver if template.classifier == "ridge" else "original",
sequence=sequence)
model.recipe_id = "custom"
return model
from wisp_fast.engine import build_candidate
return build_candidate(spec, ridge_solver=solver, sequence=sequence)