"""Ephemeral clean CIS/RC designs for a winner refit on the same immutable splits.""" from collections import OrderedDict from contextvars import ContextVar import json import hashlib from threading import RLock import numpy as np from .cache import _SEARCH from .selection import fit_on from . import features as fx _ACTIVE_SPEC=ContextVar("wisp_finalist_design_spec",default=None) _FAMILIES={"rocket_sketch_ridge_hmm","state_rocket_interval_hmm"} def spec_key(spec): return json.dumps([spec['name'],spec['params']],sort_keys=True) if spec['name'] in _FAMILIES else None def _signature(blocks): return tuple((type(b),tuple(sorted(b.get_params(deep=False).items()))) for b in blocks) def _digest(x): return hashlib.blake2b(np.ascontiguousarray(x).view(np.uint8),digest_size=16).digest() class RefitDesign: def __init__(self,train,valid,limit_mb): if not np.isfinite(limit_mb) or limit_mb<0:raise ValueError("Invalid design budget") self.panels=(train.X,valid.X) self.limit=int(limit_mb*1024**2) self.hashes=[_digest(x) for x in self.panels] if self.limit else [] self.store=OrderedDict() self.used=self.peak=self.evictions=0 self.lock=RLock() def capture(self,x,blocks,values): key=_ACTIVE_SPEC.get() if key is None or not x.flags.c_contiguous or not self.limit:return side=next((k for k,p in enumerate(self.panels) if x is p),None) if side is None:return if any(type(b) not in (fx.RandomConvSketch,fx.IntervalDistributionSketch, fx.WearableFeatureExtractor) for b in blocks):return size=sum(v.nbytes for v in values) with self.lock: if size>self.limit or (key,side) in self.store:return while size+self.used>self.limit: _,(_,old,_)=self.store.popitem(last=False) self.used-=old;self.evictions+=1 self.store[(key,side)]=(tuple(values),size,_signature(blocks));self.used+=size self.peak=max(self.peak,self.used) def refit(self,model,spec,combined): key=spec_key(spec) records=[self.store.get((key,i)) for i in (0,1)] reusable=(all(records) and model.mode=="exact" and model.method_id in ("wisp_rc","wisp_cis") and all(rec[2]==_signature(model._blocks()) for rec in records)) n=len(self.panels[0]) reused=bool(reusable and all(_digest(x)==h for x,h in zip((combined.X[:n],combined.X[n:]),self.hashes))) rows=[rec[0] for rec in records] if reused else [] del records self.store.clear();self.used=0 if reused: # Inputs are the same train and validation objects used to construct # combined. R/I/S fit depends only on seed and T,C. The fresh head # and temporal decoder still fit the complete labeled training set. assembled=np.empty((len(combined.y),sum(v.shape[1] for v in rows[0])),dtype=np.float64) n=len(self.panels[0]);start=0 for a,b in zip(*rows): stop=start+a.shape[1] assembled[:n,start:stop]=a;assembled[n:,start:stop]=b start=stop del rows,a,b class Plan: def transform(self,blocks,x): if x is not combined.X:raise ValueError("Unexpected refit panel") for block in blocks: if hasattr(block,"feature_names_") and not block.feature_names_:block.transform(x[:1]) return [assembled] token=_SEARCH.set(Plan()) try:fit_on(model,combined) finally:_SEARCH.reset(token) else:fit_on(model,combined) return dict(reused=reused,reused_clean_windows=len(combined.y) if reused else 0, limit_bytes=self.limit,peak_bytes=self.peak,evictions=self.evictions)