Instructions to use Zipeng365/WISP with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use Zipeng365/WISP with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("Zipeng365/WISP", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Download src/wisp_fast/funnel/decoder.py from Zipeng365/WISP: direct link, hf CLI and curl.
- Browser
- Download file 5.58 kB
-
https://huggingface.co/Zipeng365/WISP/resolve/main/src/wisp_fast/funnel/decoder.py
- Command line
-
hf download hf://Zipeng365/WISP/src/wisp_fast/funnel/decoder.py
-
curl -L -o decoder.py https://huggingface.co/Zipeng365/WISP/resolve/main/src/wisp_fast/funnel/decoder.py
5.58 kB
| """Grouped transition counting and Viterbi decoding on the release class axis.""" | |
| from functools import lru_cache | |
| import ctypes | |
| from .native import lookup | |
| import numpy as np | |
| def _native(): | |
| fn = lookup("wisp_viterbi") | |
| if fn is None: | |
| return None | |
| arr = np.ctypeslib.ndpointer | |
| f64 = arr(dtype=np.float64, flags="C_CONTIGUOUS") | |
| i32 = arr(dtype=np.int32, flags="C_CONTIGUOUS") | |
| i64 = arr(dtype=np.int64, flags="C_CONTIGUOUS") | |
| fn.argtypes = [f64, f64, f64, ctypes.c_int64, ctypes.c_int64, i32, f64, f64, i64] | |
| fn.restype = None | |
| return fn | |
| def _native_groups(): | |
| fn = lookup("wisp_viterbi_groups") | |
| if fn is not None: | |
| arr = np.ctypeslib.ndpointer | |
| f64 = arr(dtype=np.float64, flags="C_CONTIGUOUS") | |
| i32 = arr(dtype=np.int32, flags="C_CONTIGUOUS") | |
| i64 = arr(dtype=np.int64, flags="C_CONTIGUOUS") | |
| fn.argtypes = [f64, f64, f64, i64, ctypes.c_int64, ctypes.c_int64, i32, f64, f64, i64] | |
| fn.restype = None | |
| return fn | |
| def _order(n, groups, time_index): | |
| """Preserve original order within a group when timestamps are absent or tied.""" | |
| if groups is None: | |
| return np.arange(n), np.array([0, n], dtype=np.intp) | |
| g = np.asarray(groups) | |
| if g.shape != (n,): | |
| raise ValueError("groups must have one entry per window") | |
| if np.any(g != g): | |
| raise ValueError("Missing group ID") | |
| if time_index is None: | |
| order = np.argsort(g, kind="stable") | |
| else: | |
| time = np.asarray(time_index) | |
| if time.shape != (n,): | |
| raise ValueError("time_index must have one entry per window") | |
| order = np.lexsort((time, g)) | |
| sorted_groups = g[order] | |
| bounds = np.r_[0, np.flatnonzero(sorted_groups[1:] != sorted_groups[:-1]) + 1, n] | |
| return order, bounds | |
| class FastSmoother: | |
| def fit(self, y, *, groups=None, time_index=None, n_classes=None): | |
| y = np.asarray(y, dtype=np.int64) | |
| k = int(n_classes) if n_classes is not None else int(y.max()) + 1 | |
| if y.ndim != 1 or y.size == 0 or np.any((y < 0) | (y >= k)): | |
| raise ValueError("Invalid y or n_classes") | |
| A = np.ones((k, k), dtype=np.float64) | |
| if groups is not None: | |
| order, bounds = _order(len(y), groups, time_index) | |
| labels = y[order] | |
| edges = labels[:-1] * k + labels[1:] | |
| valid = np.ones(edges.size, dtype=bool) | |
| valid[bounds[1:-1] - 1] = False | |
| A += np.bincount(edges[valid], minlength=k*k).reshape(k, k) | |
| # Upstream adds persistence twice for ungrouped training and once otherwise. | |
| A.flat[::k + 1] += 4.0 if groups is None else 2.0 | |
| p = np.bincount(y, minlength=k).astype(np.float64) + 1.0 | |
| self.transition_ = A / A.sum(axis=1, keepdims=True) | |
| self.prior_ = p / p.sum() | |
| self.log_A_ = np.log(np.clip(self.transition_, 1e-12, 1.0)) | |
| self.log_pi_ = np.log(np.clip(self.prior_, 1e-12, 1.0)) | |
| return self | |
| def predict_from_proba(self, proba, *, groups=None, time_index=None): | |
| P = np.asarray(proba, dtype=np.float64) | |
| if P.ndim != 2 or P.shape[1] != len(self.prior_) or not np.isfinite(P).all(): | |
| raise ValueError("Invalid emissions") | |
| n, k = P.shape | |
| if n == 0: | |
| return np.empty(0, dtype=np.int64) | |
| if n==1: | |
| if groups is not None: | |
| g=np.asarray(groups) | |
| if g.shape!=(1,) or np.any(g!=g):raise ValueError("Invalid single-window group") | |
| if time_index is not None and np.shape(time_index)!=(1,):raise ValueError("Invalid time index") | |
| # A one-frame trellis uses the fitted prior and has no transitions. | |
| e=np.clip(P,1e-12,1.0) | |
| e/=e.sum(axis=1,keepdims=True) | |
| np.clip(e,1e-12,1.0,out=e);np.log(e,out=e) | |
| return np.argmax(e+self.log_pi_,axis=1).astype(np.int64,copy=False) | |
| order, bounds = _order(n, groups, time_index) | |
| return self._decode_ordered(P,order,bounds) | |
| def _decode_ordered(self,P,order,bounds): | |
| n,k=P.shape | |
| P = np.clip(P[order], 1e-12, 1.0) | |
| P /= P.sum(axis=1, keepdims=True) | |
| E = np.ascontiguousarray(np.log(np.clip(P, 1e-12, 1.0))) | |
| back = np.empty((np.diff(bounds).max(), k), dtype=np.int32) | |
| prev, nxt = np.empty(k), np.empty(k) | |
| labels = np.empty(n, dtype=np.int64) | |
| grouped = _native_groups() if _native() is not None else None | |
| if grouped is not None: | |
| grouped(E, self.log_A_, self.log_pi_, np.ascontiguousarray(bounds, dtype=np.int64), | |
| len(bounds)-1, k, back, prev, nxt, labels) | |
| out = np.empty_like(labels) | |
| out[order] = labels | |
| return out | |
| fn = _native() | |
| for lo, hi in zip(bounds[:-1], bounds[1:]): | |
| if fn is not None: | |
| fn(E[lo:hi], self.log_A_, self.log_pi_, hi-lo, k, back, prev, nxt, labels[lo:hi]) | |
| else: | |
| prev[:] = self.log_pi_ + E[lo] | |
| cols = np.arange(k) | |
| for i in range(lo + 1, hi): | |
| scores = prev[:, None] + self.log_A_ | |
| pred = np.argmax(scores, axis=0) | |
| back[i-lo] = pred | |
| prev[:] = scores[pred, cols] + E[i] | |
| labels[hi-1] = np.argmax(prev) | |
| for i in range(hi-2, lo-1, -1): | |
| labels[i] = back[i-lo+1, labels[i+1]] | |
| out = np.empty_like(labels) | |
| out[order] = labels | |
| return out | |