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_representation.py from Zipeng365/WISP: direct link, hf CLI and curl.
- Browser
- Download file 4.92 kB
-
https://huggingface.co/Zipeng365/WISP/resolve/main/tests/test_api_representation.py
- Command line
-
hf download hf://Zipeng365/WISP/tests/test_api_representation.py
-
curl -L -o test_api_representation.py https://huggingface.co/Zipeng365/WISP/resolve/main/tests/test_api_representation.py
4.92 kB
| """WISPRepresentation, wisp.blocks and HMMSmoother on fixture windows (E4 and E7 in-process).""" | |
| import numpy as np | |
| import pytest | |
| from numpy.testing import assert_array_equal | |
| from sklearn.base import BaseEstimator, TransformerMixin | |
| from wisp import HMMSmoother, WISPRepresentation, blocks, recipes | |
| from wisp._engine import build_kernel | |
| def family_features(model, recipe, X): | |
| """Feature matrix computed inside a fitted reference family object.""" | |
| if recipe.family == "rocket_sketch_ridge_hmm": | |
| return model.sketch_.transform(X) | |
| if recipe.family in ("har_sculpt_forest_hmm", "symbolic_interval_forest"): | |
| return model.feature_extractor_.transform(X) | |
| return model._features(X) | |
| def test_e4_features_bit_identical_to_family(name, seed, train_windows, test_windows): | |
| X, y, g, t = train_windows | |
| Xq = test_windows[0] | |
| recipe = recipes.get(name) | |
| rep = WISPRepresentation(name, random_state=seed).fit(X) | |
| model = build_kernel(recipe, use_hmm=True, engine="reference", seed=seed).fit(X, y, groups=g, time_index=t, | |
| n_classes=3) | |
| assert_array_equal(rep.transform(Xq), family_features(model, recipe, Xq)) | |
| assert_array_equal(rep.transform(X), family_features(model, recipe, X)) | |
| assert rep.n_features_out_ == rep.transform(Xq).shape[1] | |
| assert len(rep.get_feature_names_out()) == rep.n_features_out_ | |
| def test_ensembles_have_no_single_representation(name, train_windows): | |
| with pytest.raises(ValueError, match="predict_proba"): | |
| WISPRepresentation(name).fit(train_windows[0]) | |
| class MeanBlock(TransformerMixin, BaseEstimator): | |
| def fit(self, X, y=None): | |
| self.fitted_ = True | |
| return self | |
| def transform(self, X): | |
| return np.asarray(X).mean(axis=1) | |
| def test_extra_blocks_and_slices(train_windows): | |
| X = train_windows[0] | |
| rep = WISPRepresentation("cis:RS", extra_blocks=[MeanBlock(), ("energy", MeanBlock())]).fit(X) | |
| sl = rep.block_slices_ | |
| assert list(sl) == ["R", "S", "extra0", "energy"] | |
| Z = rep.transform(X) | |
| assert_array_equal(Z[:, sl["extra0"]], X.mean(axis=1)) | |
| assert sl["energy"].stop == Z.shape[1] == rep.n_features_out_ | |
| assert rep.is_catalog_representation_ is False | |
| plain = WISPRepresentation("cis:RS").fit(X) | |
| assert_array_equal(Z[:, :plain.n_features_out_], plain.transform(X)) | |
| assert rep.extra_blocks[0] is not rep.extra_blocks_[0][1] # parameters are not fitted in place | |
| def test_dtype_and_layout(train_windows): | |
| X = train_windows[0] | |
| a = WISPRepresentation("rc").fit(X) | |
| b = WISPRepresentation("rc", layout="NCT", dtype="float32").fit(X.transpose(0, 2, 1)) | |
| Zb = b.transform(X.transpose(0, 2, 1)) | |
| assert Zb.dtype == np.float32 | |
| assert_array_equal(Zb, a.transform(X).astype(np.float32)) | |
| def test_blocks_match_cis_blocks(train_windows): | |
| X = train_windows[0] | |
| rep = WISPRepresentation("cis", random_state=11).fit(X) | |
| Z = rep.transform(X) | |
| for name, block in (("R", blocks.RandomConvolution(random_state=11)), ("I", blocks.IntervalSummary(random_state=11)), | |
| ("S", blocks.StateSpectral())): | |
| assert_array_equal(block.fit(X).transform(X), Z[:, rep.block_slices_[name]]) | |
| def test_e7_hmm_smoother_equals_kernel(with_time, train_windows, test_windows): | |
| from wisp.cpu.hmm import TransitionSmoother | |
| _, y, g, t = train_windows | |
| _, yq, gq, tq = test_windows | |
| rng = np.random.default_rng(3) | |
| scores = rng.dirichlet(np.ones(4), size=len(yq)) | |
| labels = np.array(["a", "b", "c", "d"]) | |
| sm = HMMSmoother().fit(labels[y], groups=g, time_index=t if with_time else None, classes=labels) | |
| ref = TransitionSmoother().fit(y, groups=g, time_index=t if with_time else None, n_classes=4) | |
| assert_array_equal(sm.transition_, ref.transition_) | |
| got = sm.decode(scores, groups=gq, time_index=tq if with_time else None) | |
| exp = ref.predict_from_proba(scores, groups=gq, time_index=tq if with_time else None) | |
| assert_array_equal(got, labels[exp]) | |
| assert_array_equal(sm.decode(scores), labels[ref.predict_from_proba(scores)]) | |
| def test_hmm_smoother_validation(train_windows): | |
| _, y, g, t = train_windows | |
| sm = HMMSmoother().fit(y, groups=g) | |
| with pytest.raises(ValueError, match="shape"): | |
| sm.decode(np.ones((5, 4))) | |
| with pytest.raises(ValueError, match="one entry per window"): | |
| sm.decode(np.ones((5, 3)) / 3, groups=[1, 2]) | |
| with pytest.raises(ValueError, match="one entry per window"): | |
| HMMSmoother().fit(y, groups=g[:-1]) | |