Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 29,645 Bytes
ac7f7cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
"""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

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=4)
def _state_table(length):
    den = np.maximum(np.arange(length + 1), 1)[:, None]
    p = np.arange(length + 1, dtype=np.float64)[None, :] / den
    return np.stack((p, p * np.log(p + 1e-12)), axis=-1)


@lru_cache(maxsize=128)
def _state_levels(length,bins):
    pos = (length-1)*np.linspace(0,1,bins+1)[1:-1]
    lo = pos.astype(np.intp)
    return lo, np.minimum(lo+1,length-1), pos-lo


@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 _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.)


@lru_cache(maxsize=64)
def _frequency_plan(time,bands):
    freq=np.fft.rfftfreq(time)
    pairs=[(0,0) if np.isnan(lo) or np.isnan(hi) else
           (int(freq.searchsorted(lo)),int(freq.searchsorted(hi))) for lo,hi in bands]
    return freq,np.array([(l,max(l,r)) for l,r in pairs],dtype=np.float64).reshape(-1,2)


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)<<np.uint64(63)):
        part=np.sort(x,axis=-1)
    else:
        part=np.partition(x,np.unique(np.r_[lower,upper]),axis=-1)
    a, b, frac = part[:, :, lower], part[:, :, upper], pos - lower
    # NumPy's linear interpolation evaluates from the closer endpoint.
    q = np.where(frac >= .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<length) if self.include_autocorr else ()
            if self.fast_profile:lags=lags[:2]
            if (self.include_spectral and self.include_autocorr and _family_kernels is not None
                    and v.dtype==np.float64 and v.flags.c_contiguous and v.flags.aligned
                    and 2<=length<=8192 and len(lags)<=64):
                centered=np.empty_like(v);acf=np.empty((n,len(lags),v.shape[2]))
                _family_kernels.autocorr(v,lags,acf,centered)
                emit(f'{prefix}_fft',_spectrum_centered(centered,self.spectral_bands))
                emit(f'{prefix}_acf',acf)
            else:
                if self.include_spectral:emit(f'{prefix}_fft',ref._spectral_features(v,self.spectral_bands))
                if self.include_autocorr:emit(f'{prefix}_acf',_autocorr(v,lags))
            if self.include_symbolic and length >= 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


try:
    from . import _cis_conv
except ImportError:
    _cis_conv = None


@lru_cache(maxsize=1)
def _verified_conv_blas():
    import hashlib
    library = _cis_conv.fused_library() if _cis_conv is not None else None
    if not library or np.__version__ != "2.3.5":
        return False
    try:
        with open(library, "rb") as f:
            actual = hashlib.file_digest(f, "sha256").hexdigest()
    except OSError:
        return False
    return actual == "ee2039cf13a45e1e89f04d0cea9585657d93ffaa425594e1935161013fdf4e12"


def _pack_kernels(kernels):
    count = len(kernels)
    max_len = max(spec["length"] for spec in kernels)
    layout = np.zeros((count, 6), dtype=np.int64)
    weights = np.zeros((count, max_len, 3))
    biases = np.empty(count)
    for j, spec in enumerate(kernels):
        chan = spec["channels"]
        length = spec["length"]
        layout[j, :3] = (length, spec["dilation"], len(chan))
        layout[j, 3:3+len(chan)] = chan
        weights[j, :length, :len(chan)] = spec["weights"]
        biases[j] = spec["bias"]
    max_offset = int(np.max((layout[:, 0]-1)*layout[:, 1]))
    return layout, weights, biases, max_offset, bool(np.any(layout[:, 2] > 1))


@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)

    def __getstate__(self):
        state = super().__getstate__().copy()
        state.pop("_conv_plan", None)
        state.pop("_quant_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:
            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)
        if (_cis_conv is not None and _kernels is not None and
                self.kernels_ and n > 0 and stride <= t <= 8192):
            plan = self.__dict__.get("_conv_plan")
            if plan is None or plan[0] is not self.kernels_:
                plan = (self.kernels_, _pack_kernels(self.kernels_))
                self._conv_plan = plan
            layout, weights, biases, max_offset, multi = plan[1]
            if t > max_offset and (stride>1 or self.fast_profile or not multi or
                                   (t-max_offset > 1 and _cis_conv.has_blas())):
                # A single N,C,T copy is shared by all kernels. Projection and
                # pooling scratch storage is per window, independent of bank size.
                data = np.ascontiguousarray(x.transpose(0,2,1), dtype=np.float64)
                args = (data, layout, weights, biases, stride, self.fast_profile or stride>1,
                        stride==1 and not self.fast_profile and _verified_conv_blas(), f,
                        bool(getattr(self, "response_stride_", None)))
                if getattr(self, 'projection_precision_', None) == 'float32':
                    _cis_conv.transform(*args, True)
                else:
                    _cis_conv.transform(*args)
                return _clean(f.reshape(n,-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):
        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 (native_plan is None and n>=8 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 _cis_intervals.levels(x,bounds,qs,out,int(self.fast_profile)):
                    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.)