Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 7,035 Bytes
4731b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a9282
4731b11
 
 
93a9282
4731b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a9282
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""WISPClassifier: delegation to the frozen kernel (E1 on fixtures), labels, layout, decoder, readout."""
import warnings

import joblib
import numpy as np
import pytest
from numpy.testing import assert_array_equal
from sklearn.linear_model import LogisticRegression
from threadpoolctl import threadpool_limits

from wisp import WISPClassifier, recipes
from wisp._engine import build_kernel

FIXED = ("ss", "rc", "so", "gis", "cis", "cse", "ese")


@pytest.mark.parametrize("name", FIXED + ("cis:RS", "cis:I", "best:adl", "best:paal_adl", "best:harmes_left"))
def test_e1_delegation_matches_reference_kernel(name, train_windows, test_windows):
    """Catalog recipe without readout (decoder="recipe"): decoded labels equal the paper kernel (fixture data)."""
    X, y, g, t = train_windows
    Xq, _, gq, tq = test_windows
    with threadpool_limits(1):
        clf = WISPClassifier(name, decoder="recipe").fit(X, y, groups=g, time_index=t)
        ref = build_kernel(recipes.get(name), use_hmm=clf.decoder_ == "hmm", engine="reference", seed=42)
        ref.fit(X, y, groups=g, time_index=t, n_classes=3)
        assert_array_equal(clf.predict(Xq, groups=gq, time_index=tq), ref.predict(Xq, groups=gq, time_index=tq))
        assert_array_equal(clf.predict(Xq), ref.predict(Xq))
        np.testing.assert_allclose(clf.predict_proba(Xq), ref.predict_proba(Xq), rtol=0, atol=1e-9)
    assert clf.has_paper_results_ is True
    assert clf.model_size_mb_ > 0 and clf.fit_seconds_ > 0


def test_string_labels_missing_class_and_classes(train_windows, test_windows):
    X, y, g, t = train_windows
    names = np.array(["sit", "stand", "walk"])[y]
    clf = WISPClassifier("rc").fit(X, names, groups=g, time_index=t)
    assert list(clf.classes_) == ["sit", "stand", "walk"]
    assert set(clf.predict(test_windows[0])) <= {"sit", "stand", "walk"}
    keep = names != "walk"
    table = ["run", "sit", "stand", "walk"]
    clf = WISPClassifier("cis", classes=table).fit(X[keep], names[keep], groups=g[keep], time_index=t[keep])
    P = clf.predict_proba(test_windows[0])
    assert P.shape == (len(test_windows[0]), 4)
    assert_array_equal(P[:, [0, 3]], 0)
    with pytest.raises(ValueError, match="not in the supplied classes"):
        WISPClassifier("rc", classes=["a", "b"]).fit(X, names, groups=g)


def test_layout_nct_equals_ntc(train_windows, test_windows):
    X, y, g, t = train_windows
    a = WISPClassifier("cis").fit(X, y, groups=g, time_index=t)
    b = WISPClassifier("cis", layout="NCT").fit(X.transpose(0, 2, 1), y, groups=g, time_index=t)
    Xq = test_windows[0]
    assert_array_equal(a.predict_proba(Xq), b.predict_proba(Xq.transpose(0, 2, 1)))


def test_groups_validation(train_windows):
    X, y, g, t = train_windows
    with pytest.raises(ValueError, match="one entry per window"):
        WISPClassifier("rc").fit(X, y, groups=g[:-1])
    clf = WISPClassifier("rc").fit(X, y, groups=g, time_index=t)
    with pytest.raises(ValueError, match="one entry per window"):
        clf.predict(X, groups=g[:-2])
    with pytest.raises(ValueError, match="shape"):
        clf.predict(X[:, :32])


def test_decoder_settings(train_windows, test_windows):
    X, y, g, t = train_windows
    Xq, _, gq, tq = test_windows
    with pytest.raises(ValueError, match="needs groups"):
        WISPClassifier("rc", decoder="hmm").fit(X, y)
    with warnings.catch_warnings(record=True) as caught:
        warnings.simplefilter("always")
        auto = WISPClassifier("rc", decoder="recipe").fit(X, y)
    assert auto.decoder_ == "none" and any("no groups" in str(w.message) for w in caught)
    assert auto.has_paper_results_ is False and auto.decoder_reason_ == "fixed"
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        adaptive = WISPClassifier("rc").fit(X, y)  # default "adaptive": no groups, no warning
    assert adaptive.decoder_ == "none" and adaptive.decoder_reason_ == "no groups"
    assert adaptive.decision_seconds_ == 0
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        none = WISPClassifier("rc", decoder="none").fit(X, y, groups=g, time_index=t)
    assert none.decoder_ == "none"
    assert_array_equal(none.predict(Xq, groups=gq), none.predict(Xq))
    hmm = WISPClassifier("rc", decoder="hmm").fit(X, y, groups=g, time_index=t)
    assert hmm.decoder_ == "hmm"
    assert_array_equal(hmm.predict(Xq), np.argmax(hmm.predict_proba(Xq), 1))  # no groups: per window
    # direct CIS is a paper variant (cis_ablation_RIS_direct); direct RC is not
    assert WISPClassifier("cis", decoder="none").fit(X, y, groups=g).has_paper_results_ is True
    assert none.has_paper_results_ is False


def test_readout_replacement(train_windows, test_windows):
    X, y, g, t = train_windows
    Xq, yq, gq, tq = test_windows
    clf = WISPClassifier("cis", decoder="recipe", readout=LogisticRegression(max_iter=500)).fit(
        X, y, groups=g, time_index=t)
    assert clf.has_paper_results_ is False and clf.decoder_ == "hmm"
    P = clf.predict_proba(Xq)
    assert P.shape == (len(Xq), 3) and np.allclose(P.sum(1), 1)
    assert clf.predict(Xq, groups=gq, time_index=tq).shape == (len(Xq),)
    assert (clf.predict(Xq) == yq).mean() > 0.6
    for name in ("cse", "ese"):
        with pytest.raises(ValueError, match="single-readout"):
            WISPClassifier(name, readout=LogisticRegression()).fit(X, y, groups=g)


def test_speed_fast_and_custom_recipe(train_windows, test_windows):
    X, y, g, t = train_windows
    fast = WISPClassifier("cis", speed="fast").fit(X, y, groups=g, time_index=t)
    assert fast.has_paper_results_ is False
    assert type(fast.model_.model_.steps[-1][1]).__name__ == "ScreenedRidgeClassifierCV"
    custom = WISPClassifier({"name": "state_rocket_interval_hmm", "params": {"n_kernels": 64, "n_intervals": 8}})
    custom.fit(X, y, groups=g, time_index=t)
    assert custom.recipe_.source == "custom" and custom.has_paper_results_ is False
    with pytest.raises(ValueError):
        WISPClassifier("cis", speed="turbo").fit(X, y, groups=g)


def test_joblib_roundtrip(tmp_path, train_windows, test_windows):
    X, y, g, t = train_windows
    Xq, _, gq, tq = test_windows
    for kwargs in ({"recipe": "gis"}, {"recipe": "rc", "readout": LogisticRegression(max_iter=300)}):
        clf = WISPClassifier(**kwargs).fit(X, y, groups=g, time_index=t)
        path = tmp_path / "m.joblib"
        joblib.dump(clf, path)
        back = joblib.load(path)
        assert_array_equal(back.predict(Xq, groups=gq, time_index=tq), clf.predict(Xq, groups=gq, time_index=tq))
        assert_array_equal(back.predict_proba(Xq), clf.predict_proba(Xq))


def test_recording_sequence_option(train_windows, valid_windows, test_windows):
    X, y, g, t = train_windows
    Xv, yv, _, _ = valid_windows
    clf = WISPClassifier("rc", sequence="recording").fit(X, y, groups=g, time_index=t)
    clf.fit_confidence_scale(Xv, yv)
    assert clf.confidence_scale_ is None or clf.confidence_scale_ > 0
    assert clf.has_paper_results_ is False