Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / tests /test_api_datasets.py
Zipeng365's picture
wisp-har 0.3.0: scikit-learn interface, recipe catalog, Optuna search, cost-accuracy figures
4731b11 verified
Raw History Blame Contribute Delete
2.82 kB
"""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)