"""Small adapters for plugging other models into WISP (and WISP features into PyTorch). PyTorch is imported only when a torch adapter is used; the rest of the package works without it. """ from __future__ import annotations from typing import Any, Callable import numpy as np from sklearn.base import BaseEstimator, TransformerMixin __all__ = ["FunctionBlock", "TorchEncoderBlock", "to_torch_dataset"] class FunctionBlock(TransformerMixin, BaseEstimator): """Wrap any function ``fn(X) -> (n, d)`` as a stateless representation block. Use it for frozen feature extractors or to adapt layouts, e.g. a transformer that expects ``(n, C, T)``: ``FunctionBlock(lambda X: model.transform(X.transpose(0, 2, 1)))``. """ def __init__(self, fn: Callable[[np.ndarray], Any] | None = None, kw_args: dict | None = None): self.fn = fn self.kw_args = kw_args def fit(self, X, y=None): if self.fn is None or not callable(self.fn): raise ValueError("FunctionBlock needs a callable fn") return self def transform(self, X): out = np.asarray(self.fn(X, **(self.kw_args or {}))) if out.ndim == 1: out = out[:, None] if out.ndim != 2 or len(out) != len(X): raise ValueError(f"fn must return an (n, d) array for n={len(X)} windows; got shape {out.shape}") return out class TorchEncoderBlock(TransformerMixin, BaseEstimator): """A frozen PyTorch encoder as a representation block. Windows arrive as ``(n, T, C)`` (``layout="NTC"``, the WISP convention) or ``(n, C, T)`` and are passed to the module as float32 ``(batch, C, T)`` tensors, the usual PyTorch convention for 1-D convolutions. The module runs in ``eval()`` mode under ``torch.no_grad()`` in batches; its output is flattened to ``(n, d)``. ``fit`` does not train the module. """ def __init__(self, module=None, batch_size: int = 256, device: str = "cpu", layout: str = "NTC"): self.module = module self.batch_size = batch_size self.device = device self.layout = layout def fit(self, X, y=None): import torch # noqa: F401 (fail early with a clear ImportError) if self.module is None: raise ValueError("TorchEncoderBlock needs a torch.nn.Module") if self.layout not in ("NTC", "NCT"): raise ValueError("layout must be 'NTC' or 'NCT'") return self def transform(self, X): import torch arr = np.asarray(X, dtype=np.float32) if arr.ndim == 2: arr = arr[:, :, None] if self.layout == "NTC" else arr[:, None, :] if self.layout == "NTC": arr = arr.transpose(0, 2, 1) arr = np.ascontiguousarray(arr) module = self.module.to(self.device) was_training = module.training module.eval() outs = [] try: with torch.no_grad(): for start in range(0, len(arr), int(self.batch_size)): batch = torch.from_numpy(arr[start:start + int(self.batch_size)]).to(self.device) out = module(batch) if isinstance(out, (tuple, list)): out = out[0] outs.append(out.reshape(out.shape[0], -1).detach().cpu().numpy().astype(np.float64)) finally: module.train(was_training) return np.concatenate(outs, axis=0) if outs else np.empty((0, 0)) def to_torch_dataset(Z, y=None, *, dtype: str = "float32"): """``torch.utils.data.TensorDataset`` of features ``Z`` (and integer labels ``y``). ``y`` must already be encoded as integers ``0..K-1`` (e.g. with ``np.unique(..., return_inverse=True)``). """ import torch from torch.utils.data import TensorDataset Zt = torch.as_tensor(np.asarray(Z, dtype=dtype)) if y is None: return TensorDataset(Zt) yy = np.asarray(y) if not np.issubdtype(yy.dtype, np.integer): raise ValueError("y must be encoded integer class IDs") return TensorDataset(Zt, torch.as_tensor(yy.astype(np.int64)))