"""Accelerated transforms for the pinned WISP release. Faithful mode retains NumPy's arithmetic on scale-sensitive features. The fast_profile parameter is a training-time choice and changes the features. """ from collections import defaultdict from dataclasses import dataclass from functools import lru_cache import numpy as np from numpy.lib.stride_tricks import sliding_window_view from scipy.stats import kurtosis, skew from wisp.cpu import features as ref from .numeric import _frequency_plan,_state_levels,_state_table try: from . import _kernels except ImportError: _kernels = None try: from . import _cis_intervals except ImportError: _cis_intervals = None def _state_counts(x, n_bins): n, length, channels = x.shape blocks = [] levels = np.linspace(0, 1, n_bins + 1)[1:-1] offset = np.arange(n)[:, None] for c in range(channels): xc = x[:, :, c] q = np.quantile(xc, levels, axis=1).T states = np.zeros((n, length), dtype=np.intp) for border in q.T: states += xc > border[:, None] h = np.bincount((states + offset * n_bins).ravel(), minlength=n * n_bins) trans = (offset * n_bins + states[:, :-1]) * n_bins + states[:, 1:] mat = np.bincount(trans.ravel(), minlength=n * n_bins * n_bins) mat = mat.reshape(n, n_bins, n_bins) flat = (mat / np.maximum(mat.sum(axis=2, keepdims=True), 1)).reshape(n, -1) ent = -(flat * np.log(flat + 1e-12)).sum(axis=1, keepdims=True) blocks.extend((h.reshape(n, n_bins) / length, flat, ent)) return np.concatenate(blocks, axis=1) @lru_cache(maxsize=256) def _column_names(prefix,width): return tuple(f"{prefix}_{j}" for j in range(width)) def _symbolic_bundle(variants, bins): """Quantiles and transition counts for every signal view in the CIS state block.""" slices = [v[:,:,:3].transpose(0,2,1) for _, v in variants] n, _, length = slices[0].shape x = np.empty((n, sum(v.shape[1] for v in slices), length)) offset = 0 for v in slices: x[:,offset:offset+v.shape[1]] = v offset += v.shape[1] n, width, length = x.shape lo, hi, frac = _state_levels(length, bins) part = (np.sort(x,axis=2) if length <= 512 else np.partition(x, np.unique(np.r_[lo,hi]), axis=2)) a, b = np.take(part,lo,axis=2), np.take(part,hi,axis=2) q = np.ascontiguousarray(np.where(frac>=.5,b-(b-a)*(1-frac),a+(b-a)*frac)) if length<=512 and bins<=16 and hasattr(_cis_intervals, 'state_pool'): out = np.empty((n,width,bins+bins*bins+1)) _cis_intervals.state_pool(x,q,_state_table(length),out) else: counts = np.empty((n,width,bins+bins*bins)) _cis_intervals.state_counts(x,q,counts) h = counts[:,:,:bins] / length mat = counts[:,:,bins:].reshape(n,width,bins,bins) flat = (mat / np.maximum(mat.sum(axis=3,keepdims=True),1)).reshape(n,width,-1) e = -(flat*np.log(flat+1e-12)).sum(axis=2,keepdims=True) out = np.concatenate((h,flat,e),axis=2) boundaries = np.cumsum([v.shape[1] for v in slices])[:-1] return {name:block.reshape(n,-1) for (name,_),block in zip(variants,np.split(out,boundaries,axis=1))} try: from . import _family_kernels except ImportError: _family_kernels = None def _time_major_view(x): """Natural N,T,C stride order, possibly sliced/padded/reversed.""" steps=[abs(s) for s,n in zip(x.strides,x.shape) if n>1] return all(v>=w for v,w in zip(steps,steps[1:])) def _normalize(x): if (_family_kernels is not None and x.dtype == np.float64 and x.flags.c_contiguous and x.flags.aligned and 0 < x.shape[1] <= 8192 and 0 < x.shape[2] <= 64): out = np.empty_like(x) if _family_kernels.normalize(x, out): return out return ref._safe_zscore_per_window(x) def _autocorr(v, lags): if (_family_kernels is not None and v.dtype == np.float64 and v.flags.c_contiguous and v.flags.aligned and 0 < v.shape[1] <= 8192 and len(lags) <= 64): out = np.empty((len(v),len(lags),v.shape[2])) _family_kernels.autocorr(v, tuple(int(lag) for lag in lags), out) return out.reshape(len(v),len(lags)*v.shape[2]) return ref._autocorr_features(v, lags) def _clean(out): if _family_kernels is not None and out.flags.c_contiguous and out.flags.aligned and out.dtype==np.float64 and out.ndim==2: _family_kernels.clean(out) return out return np.nan_to_num(out,copy=False,nan=0.,posinf=0.,neginf=0.) def _spectrum_centered(center,bands): n,t,c=center.shape freq,bounds=_frequency_plan(t,tuple(map(tuple,bands))) power=np.abs(np.fft.rfft(center,axis=1)) np.square(power,out=power);power/=t prob=np.empty((n,len(freq)-1,c)) out=np.empty((n,len(bounds)+3,c)) _family_kernels.spectral(power,freq,bounds,prob,out) work=np.log(prob+1.e-12);work*=prob np.copyto(work,0.,where=~(prob>0)) out[:,len(bounds)]=-work.sum(axis=1)/np.log(max(len(freq)-1,2)) return out.reshape(n,-1) def _raw_quantiles(variants, bins, need_symbols): """One time-axis partition supplies the median, quartiles and state boundaries.""" x = np.ascontiguousarray(np.concatenate([v.transpose(0, 2, 1) for _, v in variants], axis=1)) n, width, time = x.shape levels = np.array([.25, .5, .75]) if need_symbols: levels = np.r_[levels, np.linspace(0, 1, bins + 1)[1:-1]] pos = (time - 1) * levels lower = pos.astype(np.intp) upper = np.minimum(lower + 1, time - 1) # Short multi-quantile axes are cheaper to sort. +0 and -0 compare # equal but may have different median bytes after sort vs partition. if time<=512 and not np.any(x.view(np.uint64)==np.uint64(1)<= .5, b - (b-a)*(1-frac), a + (b-a)*frac) median = a[:, :, 1] if time % 2 else (a[:, :, 1] + b[:, :, 1]) * .5 pooled = None if need_symbols: state_x, thresholds = x, q[:, :, 3:] if any(v.shape[2] > 3 for _, v in variants): starts = np.cumsum([0] + [v.shape[2] for _, v in variants[:-1]]) indices = np.concatenate([np.arange(start, start + min(3, v.shape[2])) for start, (_, v) in zip(starts, variants)]) state_x = np.ascontiguousarray(x[:, indices]) thresholds = thresholds[:, indices] state_width = state_x.shape[1] counts = np.empty((n, state_width, bins * (bins+1))) _cis_intervals.state_counts(state_x, np.ascontiguousarray(thresholds), counts) hist = counts[:, :, :bins] / time mat = counts[:, :, bins:].reshape(n, state_width, bins, bins) flat = (mat / np.maximum(mat.sum(axis=3, keepdims=True), 1)).reshape(n, state_width, -1) entropy = -(flat * np.log(flat + 1e-12)).sum(axis=-1, keepdims=True) pooled = np.concatenate((hist, flat, entropy), axis=2) quantiles, symbols = {}, {} start, symbol_start = 0, 0 for name, v in variants: end = start + v.shape[2] quantiles[name] = (median[:, start:end], q[:, start:end, 0], q[:, start:end, 2]) if pooled is not None: symbol_end = symbol_start + min(3, v.shape[2]) symbols[name] = pooled[:, symbol_start:symbol_end].reshape(n, -1) symbol_start = symbol_end start = end return quantiles, symbols def _family_stats(v, quantiles): n, time, channels = v.shape out = np.empty((n, 11, channels)) _family_kernels.stats(v, out) mean, var, low, high, power, dmean, dstd, zcr, slope, third, fourth = out.transpose(1, 0, 2) small = var <= (np.finfo(float).eps * mean)**2 with np.errstate(divide="ignore", invalid="ignore"): skewness = np.where(small, 0, third / var**1.5) excess = np.where(small, 0, fourth / var**2 - 3) if channels == 1: # NumPy's einsum uses a contiguous dot path on univariate windows. slope = ref._slope(v) stats = np.concatenate((mean, np.sqrt(var), low, high, *quantiles, power, dmean, dstd, zcr, slope, skewness, excess), axis=1) return np.nan_to_num(stats, copy=False) @dataclass class WearableFeatureExtractor(ref.WearableFeatureExtractor): fast_profile: bool = False def fit(self, X, y=None): # No fitted statistics: the first real transform supplies feature names. return self def transform(self, X): x = ref._as_3d(X) if not self.fast_profile and not x.flags.c_contiguous: return super().transform(X) x = _normalize(x) if self.zscore else np.nan_to_num( x, nan=0.0, posinf=0.0, neginf=0.0) return self.transform_normalized(x) def transform_normalized(self, x): n, length, channels = x.shape variants = [('ch', x)] if (self.include_jerk and self.include_magnitude and channels>=2 and _family_kernels is not None and x.dtype==np.float64 and x.flags.c_contiguous and x.flags.aligned): mag=np.empty((n,length,1));jerk=np.empty_like(x);jm=np.empty_like(mag) _family_kernels.derived(x,mag,jerk,jm) variants.extend((('mag',mag),('jerk',jerk),('jerk_mag',jm))) else: if self.include_magnitude and channels >= 2: variants.append(('mag', np.linalg.norm(x[:, :, :3], axis=2, keepdims=True))) if self.include_jerk: jerk = np.diff(x, axis=1, prepend=x[:, :1]) variants.append(('jerk', jerk)) if channels >= 2: variants.append(('jerk_mag', np.linalg.norm(jerk[:, :, :3], axis=2, keepdims=True))) blocks, names = [], [] bins = min(4, self.symbolic_bins) if self.fast_profile else self.symbolic_bins raw_native = (self.include_raw_stats and not self.fast_profile and _family_kernels is not None and 1 <= length <= 8192 and all(v.flags.c_contiguous and v.flags.aligned for _, v in variants)) fused_symbols = (self.include_symbolic and length >= 8 and 2 <= bins <= 32 and _cis_intervals is not None) symbols = None if raw_native: raw_q, sym_q = _raw_quantiles(variants, bins, fused_symbols) if fused_symbols: symbols = sym_q elif fused_symbols: symbols = _symbolic_bundle(variants, bins) def emit(name, block): blocks.append(block.reshape(n, -1)) names.extend(_column_names(name, blocks[-1].shape[1])) for prefix, v in variants: if raw_native: emit(f'{prefix}_stat', _family_stats(v, raw_q[prefix])) elif self.include_raw_stats: stats = [v.mean(axis=1), v.std(axis=1), v.min(axis=1), v.max(axis=1)] if not self.fast_profile: quartiles = np.quantile(v, [.25, .75], axis=1) diff = np.diff(v, axis=1, prepend=v[:, :1]) stats += [np.median(v, axis=1), *quartiles, np.mean(v*v, axis=1), diff.mean(axis=1), diff.std(axis=1), ref._zero_crossing_rate(v), ref._slope(v), skew(v, axis=1, nan_policy='omit'), kurtosis(v, axis=1, nan_policy='omit')] emit(f'{prefix}_stat', np.concatenate([np.nan_to_num(s) for s in stats], axis=1)) if self.include_multiscale: sizes = self.segment_sizes[:1] if self.fast_profile else self.segment_sizes for w in sizes: if w > length: continue if (_family_kernels is not None and v.flags.c_contiguous and v.flags.aligned and 1 <= length <= 8192): nf = 3 if self.fast_profile else 4 packed = np.empty((n, nf, w, v.shape[2])) _family_kernels.segments(v, packed, int(w), int(self.fast_profile)) if nf == 4 and v.shape[2] == 1: sv = ref._segment_view(v, int(w)) packed[:, 3] = ref._slope(sv.reshape(n*w, sv.shape[2], 1)).reshape(n, w, 1) emit(f'{prefix}_seg{w}', packed) continue seg = ref._segment_view(v, int(w)) means = seg.mean(axis=2) stats = [means, seg.std(axis=2), seg.max(axis=2) - seg.min(axis=2)] if not self.fast_profile: # Match _slope's per-segment reduction order. The flattened # sample+segment axes are independent. seg_flat = seg.reshape(n*w, seg.shape[2], seg.shape[3]) stats.append(ref._slope(seg_flat).reshape(means.shape)) emit(f'{prefix}_seg{w}', np.concatenate([s.reshape(n,-1) for s in stats], axis=1)) lags=tuple(l for l in self.autocorr_lags if l= 8: counts = symbols[prefix] if symbols is not None else _state_counts(v[:, :, :3], int(bins)) emit(f'{prefix}_sym', counts) if self.include_cross_channel and channels > 1: cols = [x[:, :, c] - x[:, :, c].mean(axis=1, keepdims=True) for c in range(channels)] powers = [(col * col).sum(axis=1) for col in cols] corr = [(cols[i]*cols[j]).sum(axis=1)/(np.sqrt(powers[i]*powers[j])+1e-12) for i in range(channels) for j in range(i+1, channels)] emit('corr', np.column_stack(corr)) if not blocks: raise ValueError('No feature blocks enabled') self.feature_names_ = names return _clean(np.concatenate(blocks, axis=1)) def _pool_response(out, indexes, resp): # (kernel, sample, time) with contiguous time, as in the original. out[:, indexes, 0] = (resp > 0.).mean(axis=2).T out[:, indexes, 1] = resp.max(axis=2).T out[:, indexes, 2] = resp.mean(axis=2).T out[:, indexes, 3] = resp.std(axis=2).T @dataclass class RandomConvSketch(ref.RandomConvSketch): fast_profile: bool = False def fit(self, X, y=None): self.invalidate_plan() return super().fit(X, y) def invalidate_plan(self): """Call after custom in-place edits to learned kernel specifications.""" self.__dict__.pop("_conv_plan", None) self.__dict__.pop("_quant_plan", None) self.__dict__.pop("_edge_plan", None) self.__dict__.pop("_axis_plan", None) def __getstate__(self): state = super().__getstate__().copy() state.pop("_conv_plan", None) state.pop("_quant_plan", None) state.pop("_edge_plan", None) state.pop("_axis_plan", None) return state def transform(self, X): x = ref._as_3d(X) if not self.fast_profile and not getattr(self,"response_stride_",None) and not x.flags.c_contiguous: # Slicing sensors, windows or time can leave gaps in storage. # The reference z-score keeps its original reduction layout but # usually produces ordinary contiguous windows for these views. if self.zscore and _time_major_view(x): z=ref._safe_zscore_per_window(x) if z.flags.c_contiguous:return self.transform_normalized(z) return super().transform(X) if self.zscore: x = _normalize(x) return self.transform_normalized(x) def transform_normalized(self, x): """Apply the bank to an already prepared float64 time-major window tensor.""" n, t, _ = x.shape f = np.empty((n, len(self.kernels_), 4)) stride = getattr(self,"response_stride_",4 if self.fast_profile else 1) groups = defaultdict(list) for k, spec in enumerate(self.kernels_): groups[tuple(spec['channels']), spec['length'], spec['dilation']].append((k, spec)) for (channels, length, dilation), group in groups.items(): valid = t - (length - 1)*dilation if valid <= 0: raise ValueError('Random convolution kernel exceeds window length') frames = (valid + stride - 1)//stride native = _kernels is not None and (stride>1 or self.fast_profile or len(channels) == 1) if native: xch = np.ascontiguousarray(x[:, :, channels].transpose(2, 0, 1)) else: # Same channel-major advanced-index layout as upstream matmul. dat = x[:, :, channels] # Bound simultaneous kernel response storage to about 8 MiB. block_size = max(1, (1 << 20)//max(n*frames, 1)) for start in range(0, len(group), block_size): parts = group[start:start+block_size] idx = [k for k, _ in parts] weights = np.stack([spec['weights'] for _, spec in parts]) biases = np.asarray([spec['bias'] for _, spec in parts]) resp = np.zeros((len(parts), n, frames)) if native: _kernels.project(xch, weights, biases, dilation, stride, resp) else: for i in range(length): sub = dat[None, :, i*dilation:i*dilation+valid:stride, :] resp += (sub @ weights[:, None, i, :, None])[..., 0] resp += biases[:, None, None] _pool_response(f, idx, resp) return np.nan_to_num(f.reshape(n, -1), copy=False, nan=0., posinf=0., neginf=0.) @dataclass class IntervalDistributionSketch(ref.IntervalDistributionSketch): fast_profile: bool = False def transform(self, X): x = ref._as_3d(X) if not self.fast_profile and not x.flags.c_contiguous: return super().transform(X) if self.zscore: x = _normalize(x) return self.transform_normalized(x) def transform_normalized(self, x, *, native_plan=None, mixed_levels=True): n, time, channels = x.shape width = channels * (4 if self.fast_profile else (5 + len(self.quantiles))) eligible=(_cis_intervals is not None and x.flags.c_contiguous and 2 <= channels <= 256 and time <= 2048 and 0 < len(self.quantiles) <= 16 and all(0 <= q <= 1 for q in self.quantiles) and tuple(sorted(self.quantiles)) == tuple(self.quantiles)) if eligible: qs = np.asarray((0.5,) if self.fast_profile else self.quantiles, dtype=np.float64) if (mixed_levels and native_plan is None and (n>=8 or n>0 and hasattr(_cis_intervals,"rank_levels")) and time<=255 and 4<=len(self.intervals_)<=65536 and hasattr(_cis_intervals,"levels")): bounds=np.ascontiguousarray(self.intervals_,np.int64) out=np.empty((n,len(bounds),width)) if hasattr(_cis_intervals,'mixed_levels'): result=_cis_intervals.mixed_levels(x,bounds,qs,out,int(self.fast_profile),True) if isinstance(result,bytes): missing=np.frombuffer(result,dtype=np.uint8)==0 out[missing]=self.transform_normalized(x[missing],mixed_levels=False).reshape(-1,len(bounds),width) return out.reshape(n,-1) else:result=_cis_intervals.levels(x,bounds,qs,out,int(self.fast_profile)) if result:return out.reshape(n,-1) if eligible and np.isfinite(x).all(): def rank(): if time<=256 and n>=8 and hasattr(_cis_intervals,"rank_short"): ordered=np.empty((n,channels,time),dtype=np.int64) if _cis_intervals.rank_short(x,ordered): return ordered return np.argsort(x.swapaxes(1,2), axis=-1).astype(np.int64,copy=False) def evaluate(intervals, order): bounds = np.asarray(intervals, dtype=np.int64).reshape(-1,2) out = np.empty((n,len(bounds),width)) _cis_intervals.intervals(x,bounds,order,qs,out,int(self.fast_profile)) return _clean(out.reshape(n,-1)) if native_plan is not None: return native_plan(self, rank, evaluate) return evaluate(self.intervals_, rank()) out = np.empty((n, len(self.intervals_), width)) intervals = defaultdict(list) for idx, (lo, hi) in enumerate(self.intervals_): intervals[hi-lo].append((idx, lo, hi)) for length, group in intervals.items(): group_size = max(1, (1 << 18) // max(1, n*length*channels)) for start in range(0, len(group), group_size): parts = group[start:start + group_size] if len(parts) == 1: v = x[:, parts[0][1]:parts[0][2]] else: v = np.concatenate([x[:, lo:hi] for _, lo, hi in parts], axis=0) stats = [v.mean(axis=1), v.std(axis=1), np.ptp(v,axis=1)] if self.fast_profile: stats.append(np.median(v, axis=1)) else: stats.extend([(v*v).mean(axis=1), ref._slope(v), ref._quantile_block(v, self.quantiles)]) ix = [idx for idx, _, _ in parts] out[:,ix] = np.concatenate(stats, axis=1).reshape(len(ix), n, width).swapaxes(0,1) return np.nan_to_num(out.reshape(n, -1), copy=False, nan=0., posinf=0., neginf=0.) @dataclass class InvariantPhysicsFeatureExtractor(ref.InvariantPhysicsFeatureExtractor): fast_profile: bool = False def fit(self, X, y=None): self.interval_ = IntervalDistributionSketch(n_random_intervals=self.random_intervals, random_state=self.random_state, zscore=self.zscore, fast_profile=self.fast_profile).fit(X, y) return self def transform(self, X): x = ref._as_3d(X) if not self.fast_profile and not x.flags.c_contiguous: return super().transform(X) x = _normalize(x) if self.zscore else np.nan_to_num(x) blocks = [] common = dict(zscore=False, include_magnitude=False, include_jerk=False, segment_sizes=self.segment_sizes, fast_profile=self.fast_profile) if self.include_axis_features: axis = WearableFeatureExtractor(**common, include_spectral=False, include_autocorr=False, include_symbolic=False) blocks.append(axis.transform(x)) base = x[:, :, :3] mag = np.linalg.norm(base, axis=2, keepdims=True) jerk = np.diff(base, axis=1, prepend=base[:, :1]) jmag = np.linalg.norm(jerk, axis=2, keepdims=True) inv = WearableFeatureExtractor(**common, include_cross_channel=False, symbolic_bins=self.symbolic_bins) blocks.append(inv.transform(np.concatenate([mag, jmag], axis=2))) centered = base - base.mean(axis=1,keepdims=True) gram = centered.swapaxes(1,2) @ centered / max(1, x.shape[1]-1) vals = np.maximum(np.linalg.eigvalsh(gram)[:, ::-1], 0) ratios = vals / (vals.sum(axis=1, keepdims=True)+1e-12) blocks.extend([vals, ratios, np.zeros((len(x), max(6-2*base.shape[2], 0)))]) blocks.append((jmag*jmag).mean(axis=1)/((mag*mag).mean(axis=1)+1e-12)) if self.interval_ is not None: # The upstream interval branch z-scores its already normalized input. blocks.append(self.interval_.transform(x)) return _clean(np.concatenate(blocks, axis=1)) @dataclass class RandomShapeletSketch(ref.RandomShapeletSketch): fast_profile: bool = False def transform(self, X): x = ref._as_3d(X) if not self.fast_profile and not x.flags.c_contiguous: return super().transform(X) if self.zscore: x = _normalize(x) return self.transform_normalized(x) def transform_normalized(self, x, *, window_tile=None): n, time, _ = x.shape groups = defaultdict(list) for k, s in enumerate(self.shapelets_): groups[s['channel'], s['dilation'], len(s['values'])].append((k, s['values'])) f = np.empty((n, len(self.shapelets_)*2)) stride = 4 if self.fast_profile else 1 channels = {} for (c, d, length), group in groups.items(): span = (length-1)*d+1 valid = time-span+1 if valid < 1: raise ValueError('Fitted shapelet exceeds input window length') if _family_kernels is not None and length <= 256 and self.zscore: if c not in channels: channels[c] = np.require(x[:, :, c], dtype=np.float64, requirements=("C", "A")) weights = np.stack([v for _, v in group]) pooled = np.empty((n, len(group), 2)) _family_kernels.shapelets(channels[c], weights, pooled, int(d), stride) for j, (k, _) in enumerate(group): f[:, 2*k:2*k+2] = pooled[:, j] continue grid = sliding_window_view(x[:, :, c], span, axis=1)[:, ::stride, ::d] # Limit materialized normalized windows; sliding_window_view itself # does not copy any data. Each row can be discarded after pooling. chunk = max(1, (1 << 18)//(grid.shape[1]*length)) for lo in range(0, n, chunk): def normalize(): part = np.ascontiguousarray(grid[lo:lo+chunk]) block = part - part.mean(axis=2, keepdims=True) sd = block.std(axis=2, keepdims=True) return block / np.where(sd < 1e-6, 1., sd) block = (normalize() if window_tile is None else window_tile((c,d,length,stride,lo,chunk), normalize)) for k, values in group: delta = block - values np.square(delta, out=delta) dist = np.sqrt(delta.mean(axis=2)) # NaN distances are skipped by upstream's strict '<' scan. if not self.zscore: dist[np.isnan(dist)] = np.inf positions = dist.argmin(axis=1) rows = slice(lo, lo+len(block)) f[rows, 2*k] = dist[np.arange(len(block)), positions] f[rows, 2*k+1] = positions*stride / max(valid-1, 1) return np.nan_to_num(f, copy=False, nan=0., posinf=0., neginf=0.)