-
Notifications
You must be signed in to change notification settings - Fork 89
/
_base.py
306 lines (233 loc) · 7.95 KB
/
_base.py
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
"""
Base class template for annotator base type for time series stream.
class name: BaseSeriesAnnotator
Class defining methods:
fitting - fit(self, X, Y=None)
annotating - predict(self, X)
updating (temporal) - update(self, X, Y=None)
update&annotate - update_predict(self, X)
Inspection methods:
hyper-parameter inspection - get_params()
fitted parameter inspection - get_fitted_params()
State:
fitted model/strategy - by convention, any attributes ending in "_"
fitted state flag - check_is_fitted()
"""
__author__ = ["satya-pattnaik ", "fkiraly"]
__all__ = ["BaseSeriesAnnotator"]
from aeon.base import BaseEstimator
from aeon.utils.validation.annotation import check_fmt, check_labels
from aeon.utils.validation.series import check_series
class BaseSeriesAnnotator(BaseEstimator):
"""Base series annotator.
Parameters
----------
fmt : str {"dense", "sparse"}, optional (default="dense")
annotation output format:
* If "sparse", a sub-series of labels for only the outliers in X is returned,
* If "dense", a series of labels for all values in X is returned.
labels : str {"indicator", "score"}, optional (default="indicator")
annotation output labels:
* If "indicator", returned values are boolean, indicating whether a value is an
outlier,
* If "score", returned values are floats, giving the outlier score.
Notes
-----
Assumes "predict" data is temporal future of "fit"
Single time series in both, no meta-data.
The base series annotator specifies the methods and method
signatures that all annotators have to implement.
Specific implementations of these methods is deferred to concrete
annotators.
"""
_tags = {
"distribution_type": "None", # Tag to determine test in test_all_annotators
} # for unit test cases
def __init__(self, fmt="dense", labels="indicator"):
self.fmt = fmt
self.labels = labels
self._is_fitted = False
self._X = None
self._Y = None
super().__init__()
def fit(self, X, Y=None):
"""Fit to training data.
Parameters
----------
X : pd.DataFrame
Training data to fit model to (time series).
Y : pd.Series, optional
Ground truth annotations for training if annotator is supervised.
Returns
-------
self :
Reference to self.
Notes
-----
Creates fitted model that updates attributes ending in "_". Sets
_is_fitted flag to True.
"""
check_labels(self.labels)
check_fmt(self.fmt)
X = check_series(X)
if Y is not None:
Y = check_series(Y)
self._X = X
self._Y = Y
# fkiraly: insert checks/conversions here, after PR #1012 I suggest
self._fit(X=X, Y=Y)
# this should happen last
self._is_fitted = True
return self
def predict(self, X):
"""Create annotations on test/deployment data.
Parameters
----------
X : pd.DataFrame
Data to annotate (time series).
Returns
-------
Y : pd.Series
Annotations for sequence X exact format depends on annotation type.
"""
self.check_is_fitted()
X = check_series(X)
return self._predict(X=X)
def predict_scores(self, X):
"""Return scores for predicted annotations on test/deployment data.
Parameters
----------
X : pd.DataFrame
Data to annotate (time series).
Returns
-------
Y : pd.Series
Scores for sequence X exact format depends on annotation type.
"""
self.check_is_fitted()
X = check_series(X)
return self._predict_scores(X)
def update(self, X, Y=None):
"""Update model with new data and optional ground truth annotations.
Parameters
----------
X : pd.DataFrame
Training data to update model with (time series).
Y : pd.Series, optional
Ground truth annotations for training if annotator is supervised.
Returns
-------
self :
Reference to self.
Notes
-----
Updates fitted model that updates attributes ending in "_".
"""
self.check_is_fitted()
X = check_series(X)
if Y is not None:
Y = check_series(Y)
self._X = X.combine_first(self._X)
if Y is not None:
self._Y = Y.combine_first(self._Y)
self._update(X=X, Y=Y)
return self
def update_predict(self, X):
"""Update model with new data and create annotations for it.
Parameters
----------
X : pd.DataFrame
Training data to update model with, time series.
Returns
-------
Y : pd.Series
Annotations for sequence X exact format depends on annotation type.
Notes
-----
Updates fitted model that updates attributes ending in "_".
"""
X = check_series(X)
self.update(X=X)
return self.predict(X=X)
def fit_predict(self, X, Y=None):
"""Fit to data, then predict it.
Fits model to X and Y with given annotation parameters
and returns the annotations made by the model.
Parameters
----------
X : pd.DataFrame, pd.Series or np.ndarray
Data to be transformed
Y : pd.Series or np.ndarray, optional (default=None)
Target values of data to be predicted.
Returns
-------
self : pd.Series
Annotations for sequence X exact format depends on annotation type.
"""
# Non-optimized default implementation; override when a better
# method is possible for a given algorithm.
return self.fit(X, Y).predict(X)
def _fit(self, X, Y=None):
"""Fit to training data.
core logic
Parameters
----------
X : pd.DataFrame
Training data to fit model to time series.
Y : pd.Series, optional
Ground truth annotations for training if annotator is supervised.
Returns
-------
self :
Reference to self.
Notes
-----
Updates fitted model that updates attributes ending in "_".
"""
raise NotImplementedError("abstract method")
def _predict(self, X):
"""Create annotations on test/deployment data.
core logic
Parameters
----------
X : pd.DataFrame
Data to annotate, time series.
Returns
-------
Y : pd.Series
Annotations for sequence X exact format depends on annotation type.
"""
raise NotImplementedError("abstract method")
def _predict_scores(self, X):
"""Return scores for predicted annotations on test/deployment data.
core logic
Parameters
----------
X : pd.DataFrame
Data to annotate, time series.
Returns
-------
Y : pd.Series
Annotations for sequence X exact format depends on annotation type.
"""
raise NotImplementedError("abstract method")
def _update(self, X, Y=None):
"""Update model with new data and optional ground truth annotations.
core logic
Parameters
----------
X : pd.DataFrame
Training data to update model with time series
Y : pd.Series, optional
Ground truth annotations for training if annotator is supervised.
Returns
-------
self :
Reference to self.
Notes
-----
Updates fitted model that updates attributes ending in "_".
"""
# default/fallback: re-fit to all data
self._fit(self._X, self._Y)
return self