Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / tests /test_api_sklearn.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
5.49 kB
"""scikit-learn compatibility: clone/params, Pipeline, CV without groups, GridSearchCV,
metadata routing of groups, joblib, and check_estimator with a listed exemption set."""
import numpy as np
import pytest
import sklearn
from numpy.testing import assert_array_equal
from sklearn.base import clone
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV, GroupKFold, cross_val_score
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import StandardScaler
from wisp import HMMSmoother, WISPClassifier, WISPRepresentation, WISPSearch, WISPSelect
from wisp.adapters import FunctionBlock, TorchEncoderBlock
@pytest.mark.parametrize("est", [
WISPClassifier("cis:RS", decoder="none", readout=LogisticRegression(C=0.5), classes=[0, 1, 2], speed="fast"),
WISPRepresentation("gis", extra_blocks=[FunctionBlock(np.mean)], dtype="float32", layout="NCT"),
HMMSmoother(alpha=0.5), WISPSelect(7, n_jobs=2), WISPSearch("evolution", budget=20, population=6),
FunctionBlock(np.mean), TorchEncoderBlock(module=None, batch_size=8),
])
def test_clone_and_params_roundtrip(est):
params = est.get_params(deep=False)
twin = clone(est)
assert type(twin) is type(est)
for key, value in params.items():
other = twin.get_params(deep=False)[key]
if hasattr(value, "get_params"):
assert other.get_params().keys() == value.get_params().keys()
elif isinstance(value, list):
assert [type(v) for v in other] == [type(v) for v in value] and other is not value
else:
assert other == value or (other is None and value is None)
est.set_params(**params)
assert est.get_params(deep=False).keys() == params.keys()
def test_nested_params_and_set_params():
clf = WISPClassifier("cis", readout=LogisticRegression())
assert "readout__C" in clf.get_params(deep=True)
clf.set_params(readout__C=3.0, recipe="rc")
assert clf.readout.C == 3.0 and clf.recipe == "rc"
def test_representation_in_pipeline(train_windows, test_windows):
X, y, g, t = train_windows
Xq, yq, _, _ = test_windows
pipe = Pipeline([("rep", WISPRepresentation("cis")), ("scale", StandardScaler()),
("clf", LogisticRegression(max_iter=500))]).fit(X, y)
assert (pipe.predict(Xq) == yq).mean() > 0.6
Z = WISPRepresentation("cis").fit(X).transform(Xq)
assert_array_equal(pipe.named_steps["rep"].transform(Xq), Z)
def test_cross_val_score_without_groups_and_gridsearch(train_windows):
X, y, g, t = train_windows
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
scores = cross_val_score(WISPClassifier("rc"), X, y, cv=3)
assert scores.shape == (3,)
grid = GridSearchCV(WISPClassifier(), {"recipe": ["rc", "cis:RS"]}, cv=GroupKFold(3))
grid.fit(X, y, groups=g)
assert grid.best_params_["recipe"] in ("rc", "cis:RS")
def test_metadata_routing_passes_groups(train_windows, test_windows):
"""End-to-end: groups/time_index reach WISPClassifier.fit/predict through a Pipeline."""
X, y, g, t = train_windows
Xq, _, gq, tq = test_windows
with sklearn.config_context(enable_metadata_routing=True):
clf = WISPClassifier("rc", decoder="recipe").set_fit_request(groups=True, time_index=True) \
.set_predict_request(groups=True, time_index=True)
pipe = make_pipeline(clf).fit(X, y, groups=g, time_index=t)
routed = pipe.predict(Xq, groups=gq, time_index=tq)
assert pipe[-1].decoder_ == "hmm"
direct = WISPClassifier("rc", decoder="recipe").fit(X, y, groups=g, time_index=t)
assert_array_equal(routed, direct.predict(Xq, groups=gq, time_index=tq))
cv = cross_val_score(WISPClassifier("rc").set_fit_request(groups=True, time_index=True)
.set_predict_request(groups=False, time_index=False),
X, y, cv=GroupKFold(3), params={"groups": g, "time_index": t})
assert cv.shape == (3,)
# check_estimator: WISP works on windows (n, T, C). A 2-D input is read as single-channel
# windows (n, T), so the generic checks run on (n, n_features) data treated as tiny series.
# Each exemption below is a check whose premise does not hold for window classifiers.
EXPECTED_FAILED_CHECKS = {
"check_classifiers_train": (
"Requires >= 83% training accuracy on Gaussian blobs with 2-3 features. Read as windows of 2-3 time "
"samples, every window is standardised per window and channel (a property of the method), which maps "
"each row to a fixed set of values and removes the blob location that carries the class."),
}
def test_check_estimator():
from sklearn.utils.estimator_checks import check_estimator
est = WISPClassifier("so", decoder="none")
results = check_estimator(est, expected_failed_checks=EXPECTED_FAILED_CHECKS, on_fail=None)
failed = [r for r in results if r["status"] == "failed"]
assert not failed, [(r["check_name"], str(r["exception"])[:200]) for r in failed]
def test_check_estimator_representation():
from sklearn.utils.estimator_checks import check_estimator
results = check_estimator(WISPRepresentation("so"), on_fail=None)
failed = [r for r in results if r["status"] == "failed"]
assert not failed, [(r["check_name"], str(r["exception"])[:200]) for r in failed]