Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 2,820 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
"""wisp.datasets: make_windows rules and reading exported folds."""
import os

import numpy as np
import pytest
from numpy.testing import assert_array_equal

from conftest import frozen_root, requires_frozen
from wisp.datasets import load_exported_fold, load_wristharbench, make_windows


def test_window_count_and_time_index():
    sig = np.arange(1000, dtype=float).reshape(-1, 2)  # 500 samples, 2 channels
    X, y, g, t = make_windows(sig, fs=50, window_s=2.0, step_s=1.0)
    assert X.shape == (9, 100, 2) and y is None
    assert_array_equal(t, np.arange(9) * 1.0)
    assert_array_equal(X[1, 0], sig[50])
    with pytest.raises(ValueError):
        make_windows(sig, fs=50, window_s=None, step_s=1.0)


def test_majority_rule_and_fraction():
    labels = np.array(["a"] * 90 + ["b"] * 10 + ["b"] * 100)
    X, y, g, t = make_windows(np.zeros(200), fs=10, window_s=10, step_s=10, labels=labels)
    assert list(y) == ["a", "b"]
    X, y, g, t = make_windows(np.zeros(200), fs=10, window_s=10, step_s=10, labels=labels, min_label_fraction=0.95)
    assert list(y) == ["b"] and list(t) == [10.0]
    lab = np.r_[np.full(50, np.nan), np.ones(50)]
    _, y, _, _ = make_windows(np.zeros(100), fs=10, window_s=10, step_s=10, labels=lab)
    assert list(y) == [1.0]  # missing annotations do not count against the fraction


def test_constant_rule_and_group_boundaries():
    labels = np.r_[np.zeros(60), np.ones(60)]
    groups = np.r_[np.full(50, 7), np.full(70, 8)]
    X, y, g, t = make_windows(np.zeros(120), fs=1, window_s=20, step_s=10, labels=labels, group=groups,
                              label_rule="constant")
    # windows never cross the group change at 50 or mix labels across 60
    for start, gid, lab in zip(t.astype(int), g, y):
        assert (groups[start:start + 20] == gid).all() and (labels[start:start + 20] == lab).all()
    assert set(g) == {7, 8} and len(y) == 9


@requires_frozen
def test_load_exported_fold_adl():
    data = load_exported_fold(frozen_root(), "adl", 0)
    assert (len(data.train), len(data.val), len(data.test)) == (3035, 446, 861)
    assert len(data.label_names) == 14 and data.train.X.shape[1:] == (160, 3)
    assert not set(data.train.groups) & set(data.test.groups)


@pytest.mark.skipif(os.environ.get("WISP_TEST_HUB") != "1", reason="set WISP_TEST_HUB=1 to download from the Hub")
def test_load_wristharbench_adl(tmp_path):
    data = load_wristharbench("adl", fold=0, cache_dir=os.environ.get("WISP_TEST_HUB_CACHE", tmp_path))
    assert (len(data.train), len(data.val), len(data.test)) == (3035, 446, 861)
    assert len(data.label_names) == 14
    root = frozen_root()
    if root is not None:
        frozen = load_exported_fold(root, "adl", 0)
        assert_array_equal(data.test.X, frozen.test.X)
        assert_array_equal(data.test.y, frozen.test.y)