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
Download tests/test_api_sklearn.py from Zipeng365/WISP: direct link, hf CLI and curl.
- Browser
- Download file 5.49 kB
-
https://huggingface.co/Zipeng365/WISP/resolve/main/tests/test_api_sklearn.py
- Command line
-
hf download hf://Zipeng365/WISP/tests/test_api_sklearn.py
-
curl -L -o test_api_sklearn.py https://huggingface.co/Zipeng365/WISP/resolve/main/tests/test_api_sklearn.py
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 | |
| 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] | |