Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / tests /test_conv_kernels.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.95 kB
"""Packed random-convolution parity with the previous release backend."""
from unittest import TestCase, skipIf
from unittest.mock import patch
import pickle
import warnings
import numpy as np
from wisp_fast import features as fx
@skipIf(fx._cis_conv is None or fx._kernels is None, 'native convolution unavailable')
class TestPackedConvolution(TestCase):
def test_exact_tails_and_pairwise_reductions(self):
rng = np.random.default_rng(707)
cases = [15, 16, 17, 18, 19, 23, 32, 35, 37, 63, 96,
126, 127, 128, 129, 130, 131, 159, 160, 161, 259, 2049]
for index, length in enumerate(cases):
for c in [1, 2, 3, 5]:
with self.subTest(length=length, channels=c):
x = rng.normal(size=(3, length, c))
if index % 4 == 0:
x = np.rint(x)
if index % 3 == 0:
x[0, 3, 0] = np.nan
x[2, :, -1] = 0.
for turbo in [False, True]:
model = fx.RandomConvSketch(n_kernels=16, kernel_lengths=(1, 5, 7, 9, 15),
fast_profile=turbo, random_state=312)
model.fit(x)
with patch.object(fx, '_cis_conv', None):
old = model.transform(x)
with warnings.catch_warnings():
warnings.simplefilter('ignore', RuntimeWarning)
out = model.transform(x)
np.testing.assert_array_equal(out, old)
def test_validation_and_fallback(self):
x = np.random.default_rng(874).normal(size=(3, 32, 3))
m = fx.RandomConvSketch(n_kernels=8).fit(x)
expected = m.transform(x)
with patch.object(fx, '_verified_conv_blas', return_value=False):
np.testing.assert_array_equal(m.transform(x), expected)
kernel = m.kernels_[0]
kernel['weights'] *= 0.
kernel['bias'] = 0.
m.invalidate_plan()
np.testing.assert_array_equal(m.transform(x)[:, :4], np.zeros((3,4)))
p = pickle.loads(pickle.dumps(m))
self.assertNotIn('_conv_plan', vars(p))
np.testing.assert_array_equal(m.transform(x), p.transform(x))
layout, weights, bias, _, _ = m._conv_plan[1]
for wrong in ('channel', 'length', 'stride', 'output', 'dtype'):
l = layout.copy()
if wrong == 'channel': l[0,3] = 4
if wrong == 'length': l[1,0] = 0
out = np.empty((3,8,5 if wrong == 'output' else 4))
with self.assertRaises((ValueError, BufferError)):
fx._cis_conv.transform(np.ascontiguousarray(x.transpose(0,2,1)),l,
weights.astype('f4') if wrong=='dtype' else weights,
bias,40 if wrong=='stride' else 1,False,False,out)