Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
File size: 5,580 Bytes
93a9282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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


@lru_cache(maxsize=1)
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


@lru_cache(maxsize=1)
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