Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 6,649 Bytes
ac7f7cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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"]))


@pytest.mark.parametrize("speed", ["exact", "fast"])
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()