Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / tests /test_cis_features.py
Zipeng365's picture
wisp-har 0.2.0: accelerated implementation with speed (exact/fast) and HMM sequence (subject/recording) options
ac7f7cb verified
Raw History Blame Contribute Delete
2.85 kB
"""Backends for faithful finite normalization, ACF, symbolic thresholds and rank queries."""
import unittest
from unittest.mock import patch
import warnings
import numpy as np
from wisp_fast import features as fx
class FaithfulCIS(unittest.TestCase):
@unittest.skipIf(fx._family_kernels is None,"native kernels unavailable")
def test_normalization_acf(self):
rng=np.random.default_rng(643)
for t in (1,7,8,9,127,128,129,160,257,2049):
for c in (1,2,3,7):
x=rng.normal(size=(4,t,c));x[1]=3e4;x[2]=np.rint(x[2])
x[3,::3,0]=-0.
ref=fx.ref._safe_zscore_per_window(x)
np.testing.assert_array_equal(fx._normalize(x),ref)
lags=(1,0,t,3,7,1)
np.testing.assert_array_equal(fx._autocorr(x,lags),fx.ref._autocorr_features(x,lags))
neg=np.full((2,129,1),-0.)
self.assertEqual(fx._normalize(neg).tobytes(),fx.ref._safe_zscore_per_window(neg).tobytes())
neg[:,::2]=0.
self.assertEqual(fx._autocorr(neg,(1,2)).tobytes(),fx.ref._autocorr_features(neg,(1,2)).tobytes())
x=np.array([[[np.nan,0],[np.inf,1],[2,3]],[[0,1],[0,2],[0,3]]])
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
np.testing.assert_array_equal(fx._normalize(x),fx.ref._safe_zscore_per_window(x))
with self.assertRaises(ValueError):fx._family_kernels.normalize(ref,ref)
with self.assertRaises(ValueError):fx._family_kernels.autocorr(ref,(1,),ref)
@unittest.skipIf(fx._cis_intervals is None,"native interval kernels unavailable")
def test_range_select_boundaries_and_degenerate(self):
rng=np.random.default_rng(137)
for t in (17,63,64,65,127,128,129,160,255,256,257):
x=np.round(rng.normal(size=(5,t,3)),1);x[0,:,:]=1.25
x[1,:,0]=np.linspace(0,100,t);x[2,::2,1]=-0.
for turbo in (True,False):
m=fx.IntervalDistributionSketch(n_random_intervals=17,quantiles=(0.,.1,.25,.5,.75,.9,1.),
fast_profile=turbo,zscore=False,random_state=7).fit(x)
m.intervals_.extend([(0,t),(0,1),(t-1,t),(1,t-1)])
got=m.transform_normalized(x)
with patch.object(fx,"_cis_intervals",None):
reference=m.transform_normalized(x)
np.testing.assert_array_equal(got,reference)
@unittest.skipIf(fx._cis_intervals is None,"native symbolic counts unavailable")
def test_short_sort_respects_ties(self):
rng=np.random.default_rng(66)
for t in (8,32,127,256,512,513):
v=np.round(rng.normal(size=(4,t,3)),1);v[0]=-.0
z=fx._symbolic_bundle([("ch",v)],6)["ch"]
np.testing.assert_array_equal(z,fx._state_counts(v,6))