"""WISPSearch(strategy="funnel"): public API against the engine, workers, fallback and >64 channels.""" import numpy as np import pytest from numpy.testing import assert_array_equal from wisp import WISPClassifier, WISPSearch from wisp.selection import _har KW = dict(budget=10, population=4) def _har_pair(train, valid, **info): names = ["0", "1", "2"] X, y, g, t = train Xv, yv, gv, tv = valid return _har(X, y, g, t, names, "train", **info), _har(Xv, yv, gv, tv, names, "validation", **info) def test_funnel_api_equals_engine(train_windows, valid_windows, test_windows, tmp_path): """decoder="recipe": the public estimator predicts with the engine's own refitted winners.""" from wisp_fast.funnel.joint import fit_joint_funnel X, y, g, t = train_windows Xv, yv, gv, tv = valid_windows Xq = test_windows[0] info = dict(sampling_rate_hz=25.0, study_setting="free_living") # recording descriptors reach the fingerprint search = WISPSearch("funnel", decoder="recipe", random_state=7, n_jobs=1, **KW).fit( X, y, groups=g, time_index=t, validation=(Xv, yv, gv, tv), **info) tr, va = _har_pair(train_windows, valid_windows, **info) assert tr.metadata == {"study_setting": "free_living"} and tr.sampling_rate_hz == 25.0 models, summaries, receipt = fit_joint_funnel(tr, va, output=tmp_path / "engine", seed=7, evaluations=10, population=4, workers=1) for c in ("random", "evolution"): est = search.funnel_estimators_[c] assert search.funnel_summary_[c]["winner_id"] == summaries[f"wisp_{c}"]["best"]["id"] assert_array_equal(est.predict_proba(Xq), models[f"wisp_{c}"].predict_proba(Xq)) assert est.decoder_reason_ == "fixed" and est.has_paper_results_ is False assert search.best_estimator_ is search.funnel_estimators_["random"] assert search.has_paper_results_ is False and search.selection_seconds_ > 0 assert len(search.history_) == 10 and search.funnel_receipt_["evaluations"] == 10 def test_funnel_workers_do_not_change_results(train_windows, valid_windows, test_windows): X, y, g, t = train_windows Xv, yv, gv, tv = valid_windows a = WISPSearch("funnel", controller="evolution", n_jobs=1, **KW).fit(X, y, groups=g, time_index=t, validation=(Xv, yv, gv, tv)) b = WISPSearch("funnel", controller="evolution", n_jobs=2, **KW).fit(X, y, groups=g, time_index=t, validation=(Xv, yv, gv, tv)) assert a.best_estimator_ is a.funnel_estimators_["evolution"] for c in ("random", "evolution"): assert_array_equal(a.funnel_estimators_[c].predict_proba(test_windows[0]), b.funnel_estimators_[c].predict_proba(test_windows[0])) assert a.funnel_estimators_[c].decoder_ == b.funnel_estimators_[c].decoder_ @pytest.mark.parametrize("mode", ["tuple", "size"]) def test_funnel_no_candidate_fallback(mode, train_windows, valid_windows, test_windows, monkeypatch): import wisp_fast.funnel.joint as joint def fail(*args, **kwargs): raise RuntimeError("No full-validation candidate passed; see verified.json") monkeypatch.setattr(joint, "fit_joint_funnel", fail) X, y, g, t = train_windows Xv, yv, gv, tv = valid_windows fit_kw = {"validation": (Xv, yv, gv, tv)} if mode == "tuple" else {"validation_size": 0.34} with pytest.raises(RuntimeError, match="No full-validation candidate passed"): WISPSearch("funnel", **KW).fit(X, y, groups=g, time_index=t, **fit_kw) s = WISPSearch("funnel", on_no_candidate="default", decoder="recipe", **KW).fit(X, y, groups=g, time_index=t, **fit_kw) assert s.fallback_["reason"].startswith("No full-validation candidate passed") assert s.best_recipe_.id == "cis" and s.funnel_estimators_["evolution"] is s.best_estimator_ if mode == "tuple": ref = WISPClassifier("cis", decoder="recipe").fit(np.concatenate([X, Xv]), np.concatenate([y, yv]), groups=np.concatenate([g, gv]), time_index=np.concatenate([t, tv])) else: # all of X in its original row order ref = WISPClassifier("cis", decoder="recipe").fit(X, y, groups=g, time_index=t) assert_array_equal(s.predict_proba(test_windows[0]), ref.predict_proba(test_windows[0])) def test_funnel_options(): with pytest.raises(ValueError, match="sequence='subject'"): WISPSearch("funnel", sequence="recording").fit(np.zeros((8, 16, 3)), np.arange(8) % 2, groups=np.arange(8)) with pytest.raises(ValueError, match="controller"): WISPSearch("funnel", controller="optuna").fit(np.zeros((8, 16, 3)), np.arange(8) % 2, groups=np.arange(8)) with pytest.raises(ValueError, match="on_no_candidate"): WISPSearch("funnel", on_no_candidate="skip").fit(np.zeros((8, 16, 3)), np.arange(8) % 2, groups=np.arange(8)) def test_feature_dictionary_channels(monkeypatch): """At most 64 channels: the compiled kernel (when built); above 64: the portable route.""" from wisp_fast.funnel import cis_lite rng = np.random.default_rng(0) wide = rng.normal(size=(20, 32, 70)) m = cis_lite.CISLite("wide", seed=3) m._plan(wide.shape[1:]) calls = [] if cis_lite._cis_lite is not None: real = cis_lite._cis_lite class Spy: def features(self, *args): calls.append(args[0].shape) return real.features(*args) monkeypatch.setattr(cis_lite, "_cis_lite", Spy()) out = m.transform(wide) x = m._inputs(wide) ref = cis_lite._numpy_features(x, m.index_, m.channels_, m.dilation_, m.weights_, m.bias_, m.lags_, m.parts_, cis_lite.PRESETS["wide"].stride, m.mask_) assert_array_equal(out, ref) and not calls narrow = rng.normal(size=(20, 32, 3)) m3 = cis_lite.CISLite("wide", seed=3) m3._plan(narrow.shape[1:]) assert m3.transform(narrow).shape == (20, m3.n_features_) assert len(calls) == (1 if cis_lite._cis_lite is not None else 0) def test_funnel_more_than_64_channels(): rng = np.random.default_rng(1) n, T, C = 60, 24, 66 y = np.resize(np.arange(3), n) X = rng.normal(size=(n, T, C)) + y[:, None, None] * 0.8 g = np.repeat(np.arange(12), 5) s = WISPSearch("funnel", budget=6, population=4, decoder="none").fit(X, y, groups=g, validation_size=0.25) assert s.predict_proba(X[:5]).shape == (5, 3)