pyharbinger 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/PKG-INFO +2 -2
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/README.md +1 -1
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/pyproject.toml +1 -1
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/__init__.py +17 -2
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/anomaly.py +106 -1
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/utils.py +15 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/.gitignore +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/LICENSE +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/changepoint.py +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/classification.py +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/clustering.py +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/data/examples_anomalies.csv +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/data/examples_changepoints.csv +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/datasets.py +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/evaluation.py +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/harbinger.py +0 -0
- {pyharbinger-0.2.0 → pyharbinger-0.3.0}/src/pyharbinger/multivariate.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pyharbinger
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A unified time series event detection framework (Python port of the R harbinger framework)
|
|
5
5
|
Project-URL: Homepage, https://github.com/cefet-rj-dal/pyharbinger
|
|
6
6
|
Project-URL: Repository, https://github.com/cefet-rj-dal/pyharbinger
|
|
@@ -93,7 +93,7 @@ Every detector extends `Harbinger` and returns a detection table with columns
|
|
|
93
93
|
|---|---|
|
|
94
94
|
| **Core** | `Harbinger` (base contract), deviation / filter / candidate-selection utilities |
|
|
95
95
|
| **Evaluation** | `HarEval` (hard), `HarEvalSoft` (SoftED, soft matching via the Hungarian algorithm) |
|
|
96
|
-
| **Anomaly** | `HanrMl` (forecasting residuals, wraps a pytspredit regressor), `HanrFft`, `HanrHistogram`, `HanrFbiad` |
|
|
96
|
+
| **Anomaly** | `HanrMl` (forecasting residuals, wraps a pytspredit regressor), `HanrFft`, `HanrFftAmoc`, `HanrFftBinseg`, `HanrFftAmocCusum`, `HanrFftBinsegCusum`, `HanrHistogram`, `HanrFbiad` |
|
|
97
97
|
| **Change point** | `HcpAmoc`, `HcpBinseg`, `HcpPelt` (via `ruptures`), `HcpKswin`, `HcpPageHinkley` |
|
|
98
98
|
| **Clustering** | `HanctKmeans` (anomalies / discords) |
|
|
99
99
|
| **Classification** | `HancMl` (supervised; wraps a pydaltoolbox classifier) |
|
|
@@ -57,7 +57,7 @@ Every detector extends `Harbinger` and returns a detection table with columns
|
|
|
57
57
|
|---|---|
|
|
58
58
|
| **Core** | `Harbinger` (base contract), deviation / filter / candidate-selection utilities |
|
|
59
59
|
| **Evaluation** | `HarEval` (hard), `HarEvalSoft` (SoftED, soft matching via the Hungarian algorithm) |
|
|
60
|
-
| **Anomaly** | `HanrMl` (forecasting residuals, wraps a pytspredit regressor), `HanrFft`, `HanrHistogram`, `HanrFbiad` |
|
|
60
|
+
| **Anomaly** | `HanrMl` (forecasting residuals, wraps a pytspredit regressor), `HanrFft`, `HanrFftAmoc`, `HanrFftBinseg`, `HanrFftAmocCusum`, `HanrFftBinsegCusum`, `HanrHistogram`, `HanrFbiad` |
|
|
61
61
|
| **Change point** | `HcpAmoc`, `HcpBinseg`, `HcpPelt` (via `ruptures`), `HcpKswin`, `HcpPageHinkley` |
|
|
62
62
|
| **Clustering** | `HanctKmeans` (anomalies / discords) |
|
|
63
63
|
| **Classification** | `HancMl` (supervised; wraps a pydaltoolbox classifier) |
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pyharbinger"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.3.0"
|
|
8
8
|
description = "A unified time series event detection framework (Python port of the R harbinger framework)"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -10,7 +10,16 @@ scikit-learn / scipy / ruptures with thin adapters.
|
|
|
10
10
|
from __future__ import annotations
|
|
11
11
|
|
|
12
12
|
from . import datasets, utils
|
|
13
|
-
from .anomaly import
|
|
13
|
+
from .anomaly import (
|
|
14
|
+
HanrFbiad,
|
|
15
|
+
HanrFft,
|
|
16
|
+
HanrFftAmoc,
|
|
17
|
+
HanrFftAmocCusum,
|
|
18
|
+
HanrFftBinseg,
|
|
19
|
+
HanrFftBinsegCusum,
|
|
20
|
+
HanrHistogram,
|
|
21
|
+
HanrMl,
|
|
22
|
+
)
|
|
14
23
|
from .changepoint import HcpAmoc, HcpBinseg, HcpKswin, HcpPageHinkley, HcpPelt
|
|
15
24
|
from .classification import HancMl
|
|
16
25
|
from .clustering import HanctKmeans
|
|
@@ -30,15 +39,20 @@ from .utils import (
|
|
|
30
39
|
har_filter_mad,
|
|
31
40
|
har_filter_none,
|
|
32
41
|
har_filter_ratio,
|
|
42
|
+
mas,
|
|
33
43
|
)
|
|
34
44
|
|
|
35
|
-
__version__ = "0.
|
|
45
|
+
__version__ = "0.3.0"
|
|
36
46
|
|
|
37
47
|
__all__ = [
|
|
38
48
|
"HancMl",
|
|
39
49
|
"HanctKmeans",
|
|
40
50
|
"HanrFbiad",
|
|
41
51
|
"HanrFft",
|
|
52
|
+
"HanrFftAmoc",
|
|
53
|
+
"HanrFftAmocCusum",
|
|
54
|
+
"HanrFftBinseg",
|
|
55
|
+
"HanrFftBinsegCusum",
|
|
42
56
|
"HanrHistogram",
|
|
43
57
|
"HanrMl",
|
|
44
58
|
"HarEval",
|
|
@@ -63,5 +77,6 @@ __all__ = [
|
|
|
63
77
|
"har_filter_mad",
|
|
64
78
|
"har_filter_none",
|
|
65
79
|
"har_filter_ratio",
|
|
80
|
+
"mas",
|
|
66
81
|
"utils",
|
|
67
82
|
]
|
|
@@ -15,12 +15,25 @@ All of them reuse the shared :class:`~pyharbinger.harbinger.Harbinger` hooks
|
|
|
15
15
|
"""
|
|
16
16
|
from __future__ import annotations
|
|
17
17
|
|
|
18
|
+
from math import log
|
|
19
|
+
|
|
18
20
|
import numpy as np
|
|
21
|
+
import ruptures as rpt
|
|
19
22
|
from pytspredit import ts_data, ts_projection
|
|
20
23
|
|
|
21
24
|
from .harbinger import Harbinger
|
|
25
|
+
from .utils import har_candidate_selection_highgroup
|
|
22
26
|
|
|
23
|
-
__all__ = [
|
|
27
|
+
__all__ = [
|
|
28
|
+
"HanrFbiad",
|
|
29
|
+
"HanrFft",
|
|
30
|
+
"HanrFftAmoc",
|
|
31
|
+
"HanrFftAmocCusum",
|
|
32
|
+
"HanrFftBinseg",
|
|
33
|
+
"HanrFftBinsegCusum",
|
|
34
|
+
"HanrHistogram",
|
|
35
|
+
"HanrMl",
|
|
36
|
+
]
|
|
24
37
|
|
|
25
38
|
|
|
26
39
|
# --- hanr_ml ----------------------------------------------------------------
|
|
@@ -110,6 +123,98 @@ class HanrFft(Harbinger):
|
|
|
110
123
|
return self._restore_refs(anomalies=selection.mask, res=res, threshold=selection.threshold)
|
|
111
124
|
|
|
112
125
|
|
|
126
|
+
# --- hanr_fft_* (spectral change-point cutoff) ------------------------------
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _spectrum_cut_index(freqs: np.ndarray, binseg: bool) -> int:
|
|
130
|
+
"""Cutoff index from a change point in the power spectrum (R ``compute_cut_index``).
|
|
131
|
+
|
|
132
|
+
R uses ``changepoint::cpt.mean`` (change in mean) with method AMOC or BinSeg
|
|
133
|
+
and returns the *last* detected change point. Here ``ruptures`` reproduces it
|
|
134
|
+
with the L2 (mean-shift) cost: AMOC = a single breakpoint; BinSeg = a
|
|
135
|
+
``3*log(n)`` penalty (matching :class:`HcpPelt`'s calibration and R's cpts).
|
|
136
|
+
"""
|
|
137
|
+
k = len(freqs)
|
|
138
|
+
signal = np.asarray(freqs, dtype=float)
|
|
139
|
+
if binseg:
|
|
140
|
+
bkps = rpt.Binseg(model="l2", min_size=1, jump=1).fit(signal).predict(pen=3.0 * log(k))
|
|
141
|
+
else:
|
|
142
|
+
bkps = rpt.Dynp(model="l2", min_size=1, jump=1).fit(signal).predict(n_bkps=1)
|
|
143
|
+
cpts = [b for b in bkps if b < k]
|
|
144
|
+
return cpts[-1] if cpts else 1
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class _HanrFftCut(Harbinger):
|
|
148
|
+
"""Shared machinery for the FFT high-pass detectors with a spectral cutoff.
|
|
149
|
+
|
|
150
|
+
Subclasses set :attr:`_binseg` (AMOC vs. BinSeg change point) and
|
|
151
|
+
:attr:`_cusum` (whether the cutoff is found on the cumulative spectrum).
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
_binseg = False
|
|
155
|
+
_cusum = False
|
|
156
|
+
|
|
157
|
+
def detect(self, serie, **kwargs):
|
|
158
|
+
if serie is None:
|
|
159
|
+
raise ValueError("No data was provided for computation")
|
|
160
|
+
self._store_refs(serie)
|
|
161
|
+
|
|
162
|
+
fft_signal = np.fft.fft(self.serie)
|
|
163
|
+
spectrum = np.abs(fft_signal) ** 2
|
|
164
|
+
half_spectrum = spectrum[: len(self.serie) // 2 + 1]
|
|
165
|
+
|
|
166
|
+
freqs = np.cumsum(half_spectrum) if self._cusum else half_spectrum
|
|
167
|
+
cutindex = _spectrum_cut_index(freqs, self._binseg)
|
|
168
|
+
n = len(fft_signal)
|
|
169
|
+
|
|
170
|
+
# Zero out low frequencies (high-pass), both symmetric halves.
|
|
171
|
+
fft_signal[:cutindex] = 0
|
|
172
|
+
fft_signal[n - cutindex - 1 :] = 0
|
|
173
|
+
|
|
174
|
+
filtered_series = np.real(np.fft.ifft(fft_signal))
|
|
175
|
+
|
|
176
|
+
res = self.har_distance(filtered_series)
|
|
177
|
+
outliers = self.har_outliers(res)
|
|
178
|
+
selection = self.har_outliers_check(outliers, res, self.serie)
|
|
179
|
+
|
|
180
|
+
return self._restore_refs(anomalies=selection.mask, res=res, threshold=selection.threshold)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class HanrFftAmoc(_HanrFftCut):
|
|
184
|
+
"""FFT high-pass detector; cutoff = AMOC change point of the power spectrum."""
|
|
185
|
+
|
|
186
|
+
_binseg = False
|
|
187
|
+
_cusum = False
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class HanrFftBinseg(_HanrFftCut):
|
|
191
|
+
"""FFT high-pass detector; cutoff = last BinSeg change point of the power spectrum."""
|
|
192
|
+
|
|
193
|
+
_binseg = True
|
|
194
|
+
_cusum = False
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class HanrFftAmocCusum(_HanrFftCut):
|
|
198
|
+
"""FFT high-pass detector; cutoff = AMOC change point of the cumulative spectrum."""
|
|
199
|
+
|
|
200
|
+
_binseg = False
|
|
201
|
+
_cusum = True
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class HanrFftBinsegCusum(_HanrFftCut):
|
|
205
|
+
"""FFT high-pass detector; cutoff = last BinSeg change point of the cumulative spectrum.
|
|
206
|
+
|
|
207
|
+
Uses the high-group candidate selection (as in R ``hanr_fft_binseg_cusum``).
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
_binseg = True
|
|
211
|
+
_cusum = True
|
|
212
|
+
|
|
213
|
+
def __init__(self):
|
|
214
|
+
super().__init__()
|
|
215
|
+
self.har_outliers_check = har_candidate_selection_highgroup
|
|
216
|
+
|
|
217
|
+
|
|
113
218
|
# --- hanr_histogram ---------------------------------------------------------
|
|
114
219
|
|
|
115
220
|
|
|
@@ -38,9 +38,24 @@ __all__ = [
|
|
|
38
38
|
"har_filter_mad",
|
|
39
39
|
"har_filter_none",
|
|
40
40
|
"har_filter_ratio",
|
|
41
|
+
"mas",
|
|
41
42
|
]
|
|
42
43
|
|
|
43
44
|
|
|
45
|
+
def mas(x, order: int) -> np.ndarray:
|
|
46
|
+
"""Simple moving-average smoother (Python port of R ``mas``).
|
|
47
|
+
|
|
48
|
+
Returns the length-``(len(x) - order + 1)`` vector of window means
|
|
49
|
+
``mean(x[t:t+order])`` for ``t = 0 .. len(x)-order``.
|
|
50
|
+
"""
|
|
51
|
+
x = np.asarray(x, dtype=float)
|
|
52
|
+
n = len(x)
|
|
53
|
+
if order < 1 or order > n:
|
|
54
|
+
raise ValueError("order must be between 1 and len(x).")
|
|
55
|
+
cumulative = np.concatenate(([0.0], np.cumsum(x)))
|
|
56
|
+
return (cumulative[order:] - cumulative[:-order]) / order
|
|
57
|
+
|
|
58
|
+
|
|
44
59
|
@dataclass
|
|
45
60
|
class FilterResult:
|
|
46
61
|
"""Candidate outlier positions and the (inf, sup) threshold that produced them.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|