Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 4,907 Bytes
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
"""Recipe catalog: contents, names, canonical format and the search best recipes (E5 part 1)."""
import copy
import json
import pickle
import subprocess
import sys

import pytest

from conftest import ROOT
from wisp import recipes
from wisp.recipes import Recipe

# docs/architecture section 3.1 table: stream -> (family, selected, folds covered, same as)
EXPECTED_BEST = {
    "adl": ("rocket_sketch_ridge_hmm", 9, 3, None),
    "capture24": ("state_rocket_interval_hmm", 8, 4, None),
    "domino": ("state_rocket_interval_hmm", 11, 4, "cis"),
    "gotov": ("state_rocket_interval_hmm", 20, 5, "cis"),
    "iuwds": ("state_rocket_interval_hmm", 12, 5, "cis"),
    "harmes_left": ("state_rocket_interval_hmm", 8, 5, "cis with decoder='none'"),
    "harmes_right": ("state_rocket_interval_hmm", 7, 4, "cis with decoder='none'"),
    "handy": ("symbolic_interval_forest", 9, 3, "so"),
    "wisdm": ("invariant_interval_shape_forest", 15, 3, "gis"),
    "paal_adl": ("har_sculpt_forest_hmm", 8, 4, None),
    "pamap2": ("rocket_sketch_ridge_hmm", 6, 3, None),
    "mhealth": ("symbolic_interval_forest", 6, 1, "so"),
}


def test_catalog_contents():
    ids = set(recipes._catalog())
    assert {"ss", "rc", "so", "gis", "cis", "cse", "ese"} <= ids
    assert {"cis:R", "cis:I", "cis:S", "cis:RI", "cis:RS", "cis:IS"} <= ids
    assert len([i for i in ids if i.startswith("best:")]) == 12
    assert len(ids) == 7 + 6 + 12
    assert all(r.in_catalog for r in recipes._catalog().values())


def test_aliases_and_names():
    assert recipes.get("cis:RIS") == recipes.get("cis")
    assert recipes.get("cis:SR") == recipes.get("cis:RS")
    assert recipes.best("capture24") == recipes.get("best:capture24")
    assert recipes.best("capture24_wearable_activity_v1") == recipes.best("capture24")
    assert recipes.dataset_id("PAAL-ADL") == "paal_adl_wrist_accel_v1"
    with pytest.raises(KeyError):
        recipes.get("not_a_recipe")
    with pytest.raises(KeyError):
        recipes.best("unknown_stream")


def test_fixed_recipes_match_registry():
    from wisp.registry import get_spec
    for short, mid in (("ss", "wisp_ss"), ("cis", "wisp_cis"), ("ese", "wisp_ese")):
        r = recipes.get(short)
        assert r.params == dict(get_spec(mid).params)
        assert r.paper_name == get_spec(mid).paper_name
        assert r.provenance["method_id"] == mid


def test_cis_subsets_match_ablations():
    from wisp.ablations import get_recipe_spec
    for tag in ("R", "I", "S", "RI", "RS", "IS"):
        r = recipes.get(f"cis:{tag}")
        spec = get_recipe_spec(f"cis_ablation_{tag}_viterbi")
        assert (r.params["use_rocket"], r.params["use_intervals"], r.params["use_state"]) == \
            (spec.use_rocket, spec.use_intervals, spec.use_state)
        assert r.provenance["paper_ids"] == {"hmm": f"cis_ablation_{tag}_viterbi", "none": f"cis_ablation_{tag}_direct"}


def test_recipe_roundtrip_and_immutability():
    r = recipes.get("best:paal_adl")
    d = r.to_dict()
    assert d == {"name": "har_sculpt_forest_hmm", "params": {"classifier": "rf", "segment_sizes": [5, 10, 20],
                                                             "symbolic_bins": 8, "use_hmm": True}, "route": "cpu"}
    again = Recipe.from_json(r.to_json())
    assert again.to_dict() == d and again.source == "custom"
    assert pickle.loads(pickle.dumps(r)) == r and copy.deepcopy(r) == r
    with pytest.raises(AttributeError):
        r.family = "x"
    params = r.params
    params["use_hmm"] = False
    assert r.params["use_hmm"] is True
    assert "seed" not in Recipe("rocket_sketch_ridge_hmm", {"seed": 3, "n_jobs": 2}).params
    with pytest.raises(ValueError):
        Recipe("not_a_family")


@pytest.mark.parametrize("stream", sorted(EXPECTED_BEST))
def test_search_best_table(stream):
    family, selected, folds, same = EXPECTED_BEST[stream]
    r = recipes.best(stream)
    prov = r.provenance
    assert (r.family, prov["selected"], prov["folds_covered"], prov["same_as"]) == (family, selected, folds, same)
    assert prov["searches"] == 30


def test_search_best_regenerates_identically():
    script = ROOT / "scripts" / "build_search_best.py"
    if not script.exists() or not (ROOT / "results" / "search_selections.csv").exists():
        pytest.skip("source tree scripts/results not available")
    out = subprocess.run([sys.executable, str(script), "--check"], capture_output=True, text=True)
    assert out.returncode == 0, out.stdout + out.stderr


def test_search_runs_and_list():
    runs = recipes.search_runs()
    assert len(runs) == 360
    table = recipes.list()
    assert len(table) == 25
    from collections import Counter
    texts = runs["canonical_recipe"].tolist() if hasattr(runs, "columns") else [r["canonical_recipe"] for r in runs]
    text, count = Counter(texts).most_common(1)[0]
    assert json.loads(text)["name"] == "state_rocket_interval_hmm" and count == 53