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_speed_and_sequence.py from Zipeng365/WISP: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/Zipeng365/WISP/resolve/main/tests/test_speed_and_sequence.py
- Command line
-
hf download hf://Zipeng365/WISP/tests/test_speed_and_sequence.py
-
curl -L -o test_speed_and_sequence.py https://huggingface.co/Zipeng365/WISP/resolve/main/tests/test_speed_and_sequence.py
6.65 kB
| """The two public options: speed (exact/fast) and HMM smoothing sequence (subject/recording).""" | |
| from __future__ import annotations | |
| import json | |
| import numpy as np | |
| import pytest | |
| from wisp_release.cli import main | |
| from wisp_release.methods import build_family | |
| from wisp_fast.decoder import (CONFIDENCE_SCALE_GRID, FastSmoother, fit_confidence_scale, recording_ids, | |
| scale_confidence) | |
| def test_speed_selects_ridge_solver_only_where_a_ridge_head_exists(): | |
| assert build_family("wisp_cis", seed=1).ridge_solver == "original" | |
| assert build_family("wisp_cis", seed=1, speed="fast").ridge_solver == "screened" | |
| assert build_family("wisp_rc", seed=1, speed="fast").ridge_solver == "screened" | |
| assert build_family("wisp_ss", seed=1, speed="fast").ridge_solver == "original" | |
| assert build_family("wisp_ese", seed=1, speed="fast").member_ridge_solver == "screened" | |
| with pytest.raises(ValueError, match="speed"): | |
| build_family("wisp_cis", seed=1, speed="turbo") | |
| with pytest.raises(ValueError, match="sequence"): | |
| build_family("wisp_cis", seed=1, sequence="session") | |
| def test_recordings_start_when_time_index_goes_backwards(): | |
| groups = np.array(["a", "a", "a", "b", "a", "a", "b"]) | |
| time = np.array([0, 1, 2, 5, 0, 1, 0]) | |
| assert recording_ids(groups, time).tolist() == ["a|0", "a|0", "a|0", "b|0", "a|1", "a|1", "b|1"] | |
| def test_subject_sequence_ignores_recording_boundaries(): | |
| rng = np.random.default_rng(3) | |
| y = rng.integers(0, 3, 40) | |
| groups = np.repeat(["p", "q"], 20) | |
| time = np.tile(np.r_[np.arange(10), np.arange(10)], 2) | |
| P = rng.dirichlet(np.ones(3), 40) | |
| plain = FastSmoother().fit(y, groups=groups, time_index=time, n_classes=3) | |
| explicit = FastSmoother("subject").fit(y, groups=groups, time_index=time, n_classes=3) | |
| np.testing.assert_array_equal(plain.transition_, explicit.transition_) | |
| np.testing.assert_array_equal(plain.predict_from_proba(P, groups=groups, time_index=time), | |
| explicit.predict_from_proba(P, groups=groups, time_index=time)) | |
| recording = FastSmoother("recording").fit(y, groups=groups, time_index=time, n_classes=3) | |
| rec = recording_ids(groups, time) | |
| same = FastSmoother().fit(y, groups=rec, time_index=time, n_classes=3) | |
| np.testing.assert_array_equal(recording.transition_, same.transition_) | |
| assert recording.active | |
| np.testing.assert_array_equal(recording.predict_from_proba(P, groups=groups, time_index=time), | |
| same.predict_from_proba(P, groups=rec, time_index=time)) | |
| def test_recording_smoothing_skipped_when_recordings_hold_one_activity(): | |
| groups = np.repeat(["p"], 12) | |
| time = np.tile(np.arange(4), 3) | |
| y = np.repeat([0, 1, 2], 4) | |
| smoother = FastSmoother("recording").fit(y, groups=groups, time_index=time, n_classes=3) | |
| assert smoother.single_label_fraction_ == 1.0 and not smoother.active | |
| P = np.random.default_rng(0).dirichlet(np.ones(3), 12) | |
| np.testing.assert_array_equal(smoother.predict_from_proba(P, groups=groups, time_index=time), P.argmax(1)) | |
| def test_confidence_scale_is_chosen_on_the_grid(): | |
| rng = np.random.default_rng(1) | |
| P = rng.dirichlet(np.ones(4), 60) | |
| y = rng.integers(0, 4, 60) | |
| scale = fit_confidence_scale(P, y) | |
| assert np.isclose(CONFIDENCE_SCALE_GRID, scale).any() | |
| np.testing.assert_array_equal(scale_confidence(P, 1.0), P) | |
| np.testing.assert_allclose(scale_confidence(P, 2.0).sum(1), 1.0) | |
| def _write(path, rng, n, prefix): | |
| labels = np.repeat(np.arange(3), 2) | |
| y = np.tile(labels, n // 6).astype(np.int64) | |
| X = rng.normal(size=(n, 32, 3)) + y[:, None, None] | |
| np.savez(path, X=X, y=y, subject=np.array([f"{prefix}{i // 12}" for i in range(n)]), | |
| time_index=np.tile(np.arange(6), n // 6), label_names=np.array(["a", "b", "c"])) | |
| def test_cli_recording_fit_requires_validation_and_records_options(tmp_path, capsys, speed): | |
| rng = np.random.default_rng(5) | |
| _write(tmp_path / "train.npz", rng, 36, "t") | |
| _write(tmp_path / "valid.npz", rng, 12, "v") | |
| base = ["family-fit", "--method", "wisp_cis", "--input", str(tmp_path / "train.npz"), "--seed", "1", | |
| "--n-jobs", "1", "--overrides", '{"n_kernels": 8, "n_intervals": 4}', "--speed", speed, | |
| "--sequence", "recording", "--execute"] | |
| with pytest.raises(SystemExit): | |
| main([*base, "--output", str(tmp_path / "missing.pkl")]) | |
| capsys.readouterr() | |
| assert main([*base, "--output", str(tmp_path / "model.pkl"), "--valid", str(tmp_path / "valid.npz")]) == 0 | |
| capsys.readouterr() | |
| metadata = json.loads((tmp_path / "model.metadata.json").read_text()) | |
| assert metadata["speed"] == speed and metadata["sequence"] == "recording" | |
| assert metadata["ridge_solver"] == ("original" if speed == "exact" else "screened") | |
| assert metadata["sequence_smoothing"] is True | |
| assert metadata["confidence_scale"] in CONFIDENCE_SCALE_GRID | |
| def test_cli_selector_and_search_accept_both_options(tmp_path, capsys): | |
| rng = np.random.default_rng(7) | |
| for part, n, prefix in (("train", 36, "t"), ("valid", 12, "v"), ("test", 12, "s")): | |
| _write(tmp_path / f"{part}.npz", rng, n, prefix) | |
| paths = {part: str(tmp_path / f"{part}.npz") for part in ("train", "valid", "test")} | |
| options = ["--speed", "fast", "--sequence", "recording", "--seed", "3", "--n-jobs", "1", "--execute"] | |
| assert main(["select-fit", "--method", "wisp_select5", "--train", paths["train"], "--valid", paths["valid"], | |
| "--output", str(tmp_path / "selected.pkl"), *options]) == 0 | |
| capsys.readouterr() | |
| report = json.loads((tmp_path / "selected.selection.json").read_text()) | |
| assert report["speed"] == "fast" and report["sequence"] == "recording" | |
| assert report["selection_uses_test"] is False | |
| output = tmp_path / "search" | |
| assert main(["search", "--method", "wisp_random", "--train", paths["train"], "--valid", paths["valid"], | |
| "--test", paths["test"], "--output", str(output), "--population", "2", "--generations", "1", | |
| "--max-evaluations", "3", *options]) == 0 | |
| capsys.readouterr() | |
| rows = [json.loads(line) for line in (output / "program_database.jsonl").read_text().splitlines()] | |
| assert len(rows) == 3 | |
| assert all(row["result"].get("sequence") == "recording" for row in rows if row["status"] == "ok") | |
| metadata = json.loads((output / "selected_model.metadata.json").read_text()) | |
| assert metadata["speed"] == "fast" and metadata["sequence"] == "recording" | |
| assert (output / "selected_model.pkl").is_file() | |