Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / tests /test_api_adapters.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
3.45 kB
"""Adapters: FunctionBlock, TorchEncoderBlock, to_torch_dataset and a skorch readout.
The torch/skorch tests are skipped when those optional packages are not installed.
"""
import numpy as np
import pytest
from numpy.testing import assert_array_equal
from wisp import WISPClassifier, WISPRepresentation
from wisp.adapters import FunctionBlock, TorchEncoderBlock, to_torch_dataset
def test_function_block(train_windows):
X = train_windows[0]
block = FunctionBlock(lambda a: a.std(axis=1)).fit(X)
assert_array_equal(block.transform(X), X.std(axis=1))
nct = FunctionBlock(lambda a: a.transpose(0, 2, 1).mean(axis=2))
rep = WISPRepresentation("rc", extra_blocks=[("nct_mean", nct)]).fit(X)
assert_array_equal(rep.transform(X)[:, rep.block_slices_["nct_mean"]], X.mean(axis=1))
with pytest.raises(ValueError):
FunctionBlock(lambda a: a[:3].mean(axis=1)).fit(X).transform(X)
def test_torch_encoder_block(train_windows, test_windows):
torch = pytest.importorskip("torch")
X, y, g, t = train_windows
torch.manual_seed(0)
enc = torch.nn.Sequential(torch.nn.Conv1d(3, 8, 5), torch.nn.ReLU(), torch.nn.AdaptiveAvgPool1d(1))
block = TorchEncoderBlock(enc, batch_size=16)
Z = block.fit(X).transform(X)
assert Z.shape == (len(X), 8)
with torch.no_grad():
ref = enc(torch.from_numpy(X.transpose(0, 2, 1).astype(np.float32))).reshape(len(X), -1).numpy()
np.testing.assert_allclose(Z, ref, rtol=1e-5, atol=1e-6)
assert enc.training is True # mode restored
rep = WISPRepresentation("cis:RS", extra_blocks=[("encoder", block)]).fit(X)
assert rep.block_slices_["encoder"].stop - rep.block_slices_["encoder"].start == 8
clf = WISPClassifier("cis:RS").fit(X, y, groups=g, time_index=t) # unaffected by the encoder
assert clf.predict(test_windows[0]).shape == (len(test_windows[0]),)
def test_to_torch_dataset(train_windows):
pytest.importorskip("torch")
X, y, _, _ = train_windows
Z = WISPRepresentation("rc").fit(X).transform(X)
ds = to_torch_dataset(Z, y)
zb, yb = ds[0]
assert zb.dtype.is_floating_point and yb.item() == y[0] and len(ds) == len(y)
with pytest.raises(ValueError):
to_torch_dataset(Z, y.astype(float))
def test_skorch_readout(train_windows, test_windows):
torch = pytest.importorskip("torch")
skorch = pytest.importorskip("skorch")
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import FunctionTransformer, StandardScaler
X, y, g, t = train_windows
torch.manual_seed(0)
d = WISPRepresentation("rc").fit(X).n_features_out_
class MLP(torch.nn.Module):
def __init__(self):
super().__init__()
self.net = torch.nn.Sequential(torch.nn.Linear(d, 32), torch.nn.ReLU(), torch.nn.Linear(32, 3))
def forward(self, x):
return self.net(x)
net = skorch.NeuralNetClassifier(MLP, max_epochs=15, lr=0.05, criterion=torch.nn.CrossEntropyLoss,
train_split=None, verbose=0)
readout = make_pipeline(StandardScaler(), FunctionTransformer(lambda a: a.astype(np.float32)), net)
clf = WISPClassifier("rc", readout=readout).fit(X, y, groups=g, time_index=t)
P = clf.predict_proba(test_windows[0])
assert P.shape == (len(test_windows[0]), 3) and np.allclose(P.sum(1), 1)
assert clf.predict(test_windows[0], groups=test_windows[2]).shape == (len(test_windows[0]),)