Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 3,814 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
"""Feature-level release conformance, including discrete and singleton windows."""
import warnings

import numpy as np
import unittest
from unittest.mock import patch

from wisp.cpu import features as ref
from wisp_fast import features as fx


IMPLEMENTATIONS = [
    ('WearableFeatureExtractor', {}),
    ('IntervalDistributionSketch', {'n_random_intervals': 7}),
    ('RandomConvSketch', {'n_kernels': 36}),
    ('InvariantPhysicsFeatureExtractor', {'random_intervals': 7}),
    ('RandomShapeletSketch', {'n_shapelets': 12}),
]


def compare_features(name, kw, case):
    rng = np.random.default_rng(781)
    x = rng.standard_normal((1 if case == 'one_window' else 5, 37,
                             4 if case == 'four_channels' else 3))
    if case == 'ties':
        x = np.round(x)
        x[:, :, 1] = 2.
    if case == 'fortran':
        x = np.asfortranarray(x)
    if case == 'univariate':
        x = x[:, :, 1].copy()
    x[0, 5] = np.nan
    original = getattr(ref, name)(**kw)
    replacement = getattr(fx, name)(**kw)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', RuntimeWarning)
        expected = original.fit_transform(x)
        result = replacement.fit_transform(x)
    np.testing.assert_array_equal(result, expected)
    if hasattr(original, 'feature_names_'):
        assert replacement.feature_names_ == original.feature_names_


def compare_turbo(name, kw):
    x = np.random.default_rng(1024).normal(size=(7, 65, 3)).astype(np.float32)
    model = getattr(fx, name)(**kw, fast_profile=True)
    result = model.fit_transform(x)
    assert len(result) == 7 and np.isfinite(result).all()
    np.testing.assert_allclose(result[4:5], model.transform(x[4:5]), rtol=1e-14, atol=1e-14)
    np.testing.assert_array_equal(result, model.fit_transform(x))


class TestFeatures(unittest.TestCase):
    def test_release_conformance(self):
        cases = ['triaxial', 'ties', 'one_window', 'univariate', 'four_channels', 'fortran']
        for name, kw in IMPLEMENTATIONS:
            for case in cases:
                with self.subTest(name=name, case=case):
                    compare_features(name, kw, case)

    def test_turbo_singleton_batch_and_refit(self):
        for name, kw in IMPLEMENTATIONS:
            with self.subTest(name=name):
                compare_turbo(name, kw)

    @patch.object(fx, '_kernels', None)
    def test_conv_numpy_fallback(self):
        x = np.random.default_rng(781).normal(size=(7, 40, 3))
        kw = dict(n_kernels=64, dilations=(1, 2, 5))
        a = ref.RandomConvSketch(**kw).fit_transform(x)
        b = fx.RandomConvSketch(**kw).fit_transform(x)
        np.testing.assert_array_equal(b, a)


    @unittest.skipIf(fx._kernels is None, 'optional extension unavailable')
    def test_native_dimension_guards(self):
        native = fx._kernels
        x = np.zeros((3, 5, 37))
        weights = np.zeros((2, 4, 3))
        biases = np.zeros(2)
        out = np.zeros((2, 5, 34))
        for args in [(x, weights, biases, 0, 1, out),
                     (x, weights, biases[:1], 1, 1, out),
                     (x.astype(np.float32), weights, biases, 1, 1, out),
                     (x[0:2], weights, biases, 1, 1, out),
                     (x, weights, biases, 2, 1, out),
                     (x, weights, biases, 1, 100, out)]:
            with self.assertRaises((BufferError, ValueError)):
                native.project(*args)


    def test_identical_shapelet_matches_choose_first_window(self):
        x = np.tile(np.arange(8), 7).reshape(1, -1, 1).astype(float)
        kw = dict(n_shapelets=12, lengths=(8,), dilations=(1,))
        expected = ref.RandomShapeletSketch(**kw).fit_transform(x)
        actual = fx.RandomShapeletSketch(**kw).fit_transform(x)
        np.testing.assert_array_equal(actual, expected)