Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 2,556 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
from unittest import TestCase,skipIf
from unittest.mock import patch
import numpy as np
from wisp_fast import features as f


@skipIf(f._family_kernels is None,'requires optional native backend')
class NewKernels(TestCase):
    def test_spectral_and_center(self):
        rng=np.random.default_rng(907)
        for n,c,t in ((1,1,160),(9,3,33),(12,1,128),(17,5,513)):
            x=rng.normal(size=(n,t,c));x[0,::2]=0.
            ac=np.empty((n,3,c));center=np.empty_like(x)
            f._family_kernels.autocorr(x,(1,4,8),ac,center)
            bands=((0.,.042),(.17,.17),(.5,.51),(.01,.49))
            self.assertEqual(center.tobytes(),(x-x.mean(axis=1,keepdims=True)).tobytes())
            self.assertEqual(f._spectrum_centered(center,bands).tobytes(),f.ref._spectral_features(x,bands).tobytes())
        x=np.zeros((2,31,3))
        with self.assertRaises(ValueError):f._family_kernels.autocorr(x,(1,),np.empty((2,1,3)),x)
        with self.assertRaises(ValueError):f._family_kernels.derived(x,np.empty((2,31,1)),x,np.empty((2,31,1)))

    def test_window_lane_r(self):
        with np.errstate(over='ignore',invalid='ignore'):
            for t,c in ((18,2),(127,5),(160,3),(193,1),(256,3),(257,5)):
                x=np.random.default_rng(t).normal(size=(17,t,c));x[1]=0.;x[2]*=1.e-130
                m=f.RandomConvSketch(n_kernels=17,kernel_lengths=(1,5,7,9,11,15),random_state=14).fit(x)
                y=m.transform_normalized(x)
                with patch.object(f,'_cis_conv',None):ref=m.transform_normalized(x)
                np.testing.assert_array_equal(y,ref)

    def test_private_standardization(self):
        from wisp_fast.engine import build_family
        from sklearn.pipeline import Pipeline
        from sklearn.preprocessing import StandardScaler
        from sklearn.linear_model import RidgeClassifier
        rng=np.random.default_rng(9247);X=rng.normal(size=(20,6));y=np.arange(20)%3
        m=build_family('wisp_cis');m.model_=Pipeline([('s',StandardScaler()),('h',RidgeClassifier())]).fit(X,y)
        m.classes_=np.arange(3);orig=X.copy()
        a=m._head_proba(X)
        self.assertEqual(orig.tobytes(),X.tobytes())
        b=m._head_proba(X.copy(),private=True);np.testing.assert_array_equal(a,b)
        X.flags.writeable=False;np.testing.assert_array_equal(a,m._head_proba(X,private=True))

    def test_clean_and_noncontiguous(self):
        a=np.array([[np.nan,np.inf,-0.,-np.inf,1.]])
        check=np.nan_to_num(a,nan=0.,posinf=0.,neginf=0.)
        self.assertEqual(f._clean(a).tobytes(),check.tobytes())