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: 5,487 Bytes
4731b11 93a9282 4731b11 93a9282 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 | """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]
|