/
base.py
631 lines (516 loc) · 25 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# -*- coding: utf-8 -*-
# copyright: aeon developers, BSD-3-Clause License (see LICENSE file)
"""
Base class template for panel transformers.
class name: BasePanelTransformer
Defining methods:
fitting - fit(self, X, y=None)
transform - transform(self, X, y=None)
fit&transform - fit_transform(self, X, y=None)
updating - update(self, X, y=None)
Inherited 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 - is_fitted (property)
fitted state inspection - check_is_fitted()
"""
__author__ = ["mloning", "fkiraly", "miraep8", "MatthewMiddlehurst"]
__all__ = [
"BaseCollectionTransformer",
]
from abc import ABCMeta, abstractmethod
from aeon.datatypes import check_is_scitype, convert_to, mtype_to_scitype, update_data
from aeon.transformations.base import BaseTransformer, _coerce_to_list
class BaseCollectionTransformer(BaseTransformer, metaclass=ABCMeta):
"""Transformer base class."""
# default tag values - these typically make the "safest" assumption
_tags = {
"scitype:transform-input": "Panel",
"scitype:transform-output": "Panel",
"X_inner_mtype": "numpy3D",
"y_inner_mtype": "None",
"capability:unequal_length": False,
}
# allowed types for transformers - Series and Panel
ALLOWED_INPUT_TYPES = [
"numpy3D",
"numpyflat",
"np-list",
"pd-multiindex",
"df-list",
"nested_univ",
]
def __init__(self):
super(BaseCollectionTransformer, self).__init__(_output_convert=False)
def fit(self, X, y=None):
"""Fit transformer to X, optionally to y.
State change:
Changes state to "fitted".
Writes to self:
_is_fitted : flag is set to True.
_X : X, coerced copy of X, if remember_data tag is True
possibly coerced to inner type or update_data compatible type
by reference, when possible
model attributes (ending in "_") : dependent on estimator
Parameters
----------
X : Series or Panel, any supported mtype
Data to fit transform to, of python type as follows:
Series: pd.Series, pd.DataFrame, or np.ndarray (1D or 2D)
Panel: pd.DataFrame with 2-level MultiIndex, list of pd.DataFrame,
nested pd.DataFrame, or pd.DataFrame in long/wide format
subject to aeon mtype format specifications, for further details see
examples/AA_datatypes_and_datasets.ipynb
y : Series or Panel, default=None
Additional data, e.g., labels for transformation
Returns
-------
self : a fitted instance of the estimator
"""
# input checks and datatype conversion
X_inner, y_inner = self._fit_checks(X, y)
# skip the rest if fit_is_empty is True
if self.get_tag("fit_is_empty"):
self._is_fitted = True
return self
self._fit(X=X_inner, y=y_inner)
# this should happen last: fitted state is set to True
self._is_fitted = True
return self
def transform(self, X, y=None):
"""Transform X and return a transformed version.
State required:
Requires state to be "fitted".
Accesses in self:
_is_fitted : must be True
_X : optionally accessed, only available if remember_data tag is True
fitted model attributes (ending in "_") : must be set, accessed by _transform
Parameters
----------
X : Series or Panel, any supported mtype
Data to be transformed, of python type as follows:
Series: pd.Series, pd.DataFrame, or np.ndarray (1D or 2D)
Panel: pd.DataFrame with 2-level MultiIndex, list of pd.DataFrame,
nested pd.DataFrame, or pd.DataFrame in long/wide format
subject to aeon mtype format specifications, for further details see
examples/AA_datatypes_and_datasets.ipynb
y : Series or Panel, default=None
Additional data, e.g., labels for transformation
Returns
-------
transformed version of X
type depends on type of X and scitype:transform-output tag:
| | `transform` | |
| `X` | `-output` | type of return |
|----------|--------------|------------------------|
| `Series` | `Primitives` | `pd.DataFrame` (1-row) |
| `Panel` | `Primitives` | `pd.DataFrame` |
| `Series` | `Series` | `Series` |
| `Panel` | `Series` | `Panel` |
| `Series` | `Panel` | `Panel` |
instances in return correspond to instances in `X`
combinations not in the table are currently not supported
Explicitly, with examples:
if `X` is `Series` (e.g., `pd.DataFrame`) and `transform-output` is `Series`
then the return is a single `Series` of the same mtype
Example: detrending a single series
if `X` is `Panel` (e.g., `pd-multiindex`) and `transform-output` is `Series`
then the return is `Panel` with same number of instances as `X`
(the transformer is applied to each input Series instance)
Example: all series in the panel are detrended individually
if `X` is `Series` or `Panel` and `transform-output` is `Primitives`
then the return is `pd.DataFrame` with as many rows as instances in `X`
Example: i-th row of the return has mean and variance of the i-th series
if `X` is `Series` and `transform-output` is `Panel`
then the return is a `Panel` object of type `pd-multiindex`
Example: i-th instance of the output is the i-th window running over `X`
"""
# check whether is fitted
self.check_is_fitted()
# input check and conversion for X/y
X_inner, y_inner, metadata = self._check_X_y(X=X, y=y, return_metadata=True)
Xt = self._transform(X=X_inner, y=y_inner)
return Xt
def fit_transform(self, X, y=None):
"""Fit to data, then transform it.
Fits the transformer to X and y and returns a transformed version of X.
State change:
Changes state to "fitted".
Writes to self:
_is_fitted : flag is set to True.
_X : X, coerced copy of X, if remember_data tag is True
possibly coerced to inner type or update_data compatible type
by reference, when possible
model attributes (ending in "_") : dependent on estimator
Parameters
----------
X : Series or Panel, any supported mtype
Data to be transformed, of python type as follows:
Series: pd.Series, pd.DataFrame, or np.ndarray (1D or 2D)
Panel: pd.DataFrame with 2-level MultiIndex, list of pd.DataFrame,
nested pd.DataFrame, or pd.DataFrame in long/wide format
subject to aeon mtype format specifications, for further details see
examples/AA_datatypes_and_datasets.ipynb
y : Series or Panel, default=None
Additional data, e.g., labels for transformation
Returns
-------
transformed version of X
type depends on type of X and scitype:transform-output tag:
| `X` | `tf-output` | type of return |
|----------|--------------|------------------------|
| `Series` | `Primitives` | `pd.DataFrame` (1-row) |
| `Panel` | `Primitives` | `pd.DataFrame` |
| `Series` | `Series` | `Series` |
| `Panel` | `Series` | `Panel` |
| `Series` | `Panel` | `Panel` |
instances in return correspond to instances in `X`
combinations not in the table are currently not supported
Explicitly, with examples:
if `X` is `Series` (e.g., `pd.DataFrame`) and `transform-output` is `Series`
then the return is a single `Series` of the same mtype
Example: detrending a single series
if `X` is `Panel` (e.g., `pd-multiindex`) and `transform-output` is `Series`
then the return is `Panel` with same number of instances as `X`
(the transformer is applied to each input Series instance)
Example: all series in the panel are detrended individually
if `X` is `Series` or `Panel` and `transform-output` is `Primitives`
then the return is `pd.DataFrame` with as many rows as instances in `X`
Example: i-th row of the return has mean and variance of the i-th series
if `X` is `Series` and `transform-output` is `Panel`
then the return is a `Panel` object of type `pd-multiindex`
Example: i-th instance of the output is the i-th window running over `X`
"""
# input checks and datatype conversion
X_inner, y_inner, metadata = self._fit_checks(X, y, False, True)
Xt = self._fit_transform(X=X_inner, y=y_inner)
self._is_fitted = True
return Xt
def inverse_transform(self, X, y=None):
"""Inverse transform X and return an inverse transformed version.
Currently it is assumed that only transformers with tags
"scitype:transform-input"="Series", "scitype:transform-output"="Series",
have an inverse_transform.
State required:
Requires state to be "fitted".
Accesses in self:
_is_fitted : must be True
_X : optionally accessed, only available if remember_data tag is True
fitted model attributes (ending in "_") : accessed by _inverse_transform
Parameters
----------
X : Series or Panel, any supported mtype
Data to be inverse transformed, of python type as follows:
Series: pd.Series, pd.DataFrame, or np.ndarray (1D or 2D)
Panel: pd.DataFrame with 2-level MultiIndex, list of pd.DataFrame,
nested pd.DataFrame, or pd.DataFrame in long/wide format
subject to aeon mtype format specifications, for further details see
examples/AA_datatypes_and_datasets.ipynb
y : Series or Panel, default=None
Additional data, e.g., labels for transformation
Returns
-------
inverse transformed version of X
of the same type as X, and conforming to mtype format specifications
"""
if self.get_tag("skip-inverse-transform"):
return X
if not self.get_tag("capability:inverse_transform"):
raise NotImplementedError(
f"{type(self)} does not implement inverse_transform"
)
# check whether is fitted
self.check_is_fitted()
# input check and conversion for X/y
X_inner, y_inner, metadata = self._check_X_y(X=X, y=y, return_metadata=True)
Xt = self._inverse_transform(X=X_inner, y=y_inner)
return Xt
def update(self, X, y=None, update_params=True):
"""Update transformer with X, optionally y.
State required:
Requires state to be "fitted".
Accesses in self:
_is_fitted : must be True
_X : accessed by _update and by update_data, if remember_data tag is True
fitted model attributes (ending in "_") : must be set, accessed by _update
Writes to self:
_X : updated by values in X, via update_data, if remember_data tag is True
fitted model attributes (ending in "_") : only if update_params=True
type and nature of update are dependent on estimator
Parameters
----------
X : Series or Panel, any supported mtype
Data to fit transform to, of python type as follows:
Series: pd.Series, pd.DataFrame, or np.ndarray (1D or 2D)
Panel: pd.DataFrame with 2-level MultiIndex, list of pd.DataFrame,
nested pd.DataFrame, or pd.DataFrame in long/wide format
subject to aeon mtype format specifications, for further details see
examples/AA_datatypes_and_datasets.ipynb
y : Series or Panel, default=None
Additional data, e.g., labels for transformation
update_params : bool, default=True
whether the model is updated. Yes if true, if false, simply skips call.
argument exists for compatibility with forecasting module.
Returns
-------
self : a fitted instance of the estimator
"""
# check whether is fitted
self.check_is_fitted()
# if requires_y is set, y is required in fit and update
if self.get_tag("requires_y") and y is None:
raise ValueError(f"{self.__class__.__name__} requires `y` in `update`.")
# check and convert X/y
X_inner, y_inner = self._check_X_y(X=X, y=y)
# update memory of X, if remember_data tag is set to True
if self.get_tag("remember_data", False):
self._X = update_data(None, X_new=X_inner)
# skip everything if update_params is False
# skip everything if fit_is_empty is True
if not update_params or self.get_tag("fit_is_empty", False):
return self
self._update(X=X_inner, y=y_inner)
return self
def _fit_checks(self, X, y, early_abandon=True, return_metadata=False):
"""Input checks and conversions for fit and fit_transform."""
self.reset()
X_inner = None
y_inner = None
metadata = None
# skip everything if fit_is_empty is True and we do not need to remember data
if (
not early_abandon
or not self.get_tag("fit_is_empty")
or self.get_tag("remember_data", False)
):
# if requires_y is set, y is required in fit and update
if self.get_tag("requires_y") and y is None:
raise ValueError(f"{self.__class__.__name__} requires `y` in `fit`.")
# check and convert X/y
X_inner, y_inner, metadata = self._check_X_y(X=X, y=y, return_metadata=True)
# memorize X as self._X, if remember_data tag is set to True
if self.get_tag("remember_data", False):
self._X = update_data(None, X_new=X_inner)
if return_metadata:
return X_inner, y_inner, metadata
else:
return X_inner, y_inner
def _check_X_y(self, X=None, y=None, return_metadata=False):
"""Check and coerce X/y for fit/transform functions.
Parameters
----------
X : object of aeon compatible time series type
can be Series, Panel, Hierarchical
y : None (default), or object of aeon compatible time series type
can be Series, Panel, Hierarchical
return_metadata : bool, optional, default=False
whether to return the metadata return object
Returns
-------
X_inner : Series, Panel, or Hierarchical object, or VectorizedDF
compatible with self.get_tag("X_inner_mtype") format
Case 1: self.get_tag("X_inner_mtype") supports scitype of X, then
converted/coerced version of X, mtype determined by "X_inner_mtype" tag
Case 2: self.get_tag("X_inner_mtype") supports *higher* scitype than X
then X converted to "one-Series" or "one-Panel" sub-case of that scitype
always pd-multiindex (Panel) or pd_multiindex_hier (Hierarchical)
Case 3: self.get_tag("X_inner_mtype") supports only *simpler* scitype than X
then VectorizedDF of X, iterated as the most complex supported scitype
y_inner : Series, Panel, or Hierarchical object, or VectorizedDF
compatible with self.get_tag("y_inner_mtype") format
Case 1: self.get_tag("y_inner_mtype") supports scitype of y, then
converted/coerced version of y, mtype determined by "y_inner_mtype" tag
Case 2: self.get_tag("y_inner_mtype") supports *higher* scitype than y
then X converted to "one-Series" or "one-Panel" sub-case of that scitype
always pd-multiindex (Panel) or pd_multiindex_hier (Hierarchical)
Case 3: self.get_tag("y_inner_mtype") supports only *simpler* scitype than y
then VectorizedDF of X, iterated as the most complex supported scitype
Case 4: None if y was None, or self.get_tag("y_inner_mtype") is "None"
Complexity order above: Hierarchical > Panel > Series
metadata : dict, returned only if return_metadata=True
dictionary with str keys, contents as follows
_converter_store_X : dict, metadata from X conversion, for back-conversion
_X_mtype_last_seen : str, mtype of X seen last
_X_input_scitype : str, scitype of X seen last
_convert_case : str, coversion case (see above), one of
"case 1: scitype supported"
"case 2: higher scitype supported"
"case 3: requires vectorization"
Raises
------
TypeError if X is None
TypeError if X or y is not one of the permissible Series mtypes
TypeError if X is not compatible with self.get_tag("univariate_only")
if tag value is "True", X must be univariate
ValueError if self.get_tag("requires_y")=True but y is None
"""
if X is None:
raise TypeError("X cannot be None, but found None")
metadata = dict()
metadata["_converter_store_X"] = dict()
# retrieve supported mtypes
X_inner_mtype = _coerce_to_list(self.get_tag("X_inner_mtype"))
y_inner_mtype = _coerce_to_list(self.get_tag("y_inner_mtype"))
y_inner_scitype = mtype_to_scitype(y_inner_mtype, return_unique=True)
# checking X
X_valid, msg, X_metadata = check_is_scitype(
X,
scitype="Panel",
return_metadata=True,
var_name="X",
)
if not X_valid:
raise TypeError("invalid input type for X")
X_scitype = X_metadata["scitype"]
X_mtype = X_metadata["mtype"]
# remember these for potential back-conversion (in transform etc)
metadata["_X_mtype_last_seen"] = X_mtype
metadata["_X_input_scitype"] = X_scitype
if X_mtype not in self.ALLOWED_INPUT_TYPES:
raise TypeError("invalid input mtype for X")
# check if univariate-only
if self.get_tag("univariate-only") and not X_metadata["is_univariate"]:
raise TypeError("X must be univariate, but found multivariate")
# checking y
if y_inner_mtype != ["None"] and y is not None:
if "Table" in y_inner_scitype:
y_possible_scitypes = "Table"
elif X_scitype == "Series":
y_possible_scitypes = "Series"
elif X_scitype == "Panel":
y_possible_scitypes = "Panel"
elif X_scitype == "Hierarchical":
y_possible_scitypes = ["Panel", "Hierarchical"]
y_valid, _, y_metadata = check_is_scitype(
y, scitype=y_possible_scitypes, return_metadata=True, var_name="y"
)
if not y_valid:
raise TypeError("invalid input mtype for y")
y_scitype = y_metadata["scitype"]
else:
# y_scitype is used below - set to None if y is None
y_scitype = None
X_inner = convert_to(
X,
to_type=X_inner_mtype,
store=metadata["_converter_store_X"],
store_behaviour="reset",
)
# converts y, returns None if y is None
if y_inner_mtype != ["None"] and y is not None:
y_inner = convert_to(
y,
to_type=y_inner_mtype,
as_scitype=y_scitype,
)
else:
y_inner = None
if return_metadata:
return X_inner, y_inner, metadata
else:
return X_inner, y_inner
def _check_X(self, X=None):
"""Shorthand for _check_X_y with one argument X, see _check_X_y."""
return self._check_X_y(X=X)[0]
def _fit(self, X, y=None):
"""Fit transformer to X and y.
private _fit containing the core logic, called from fit
Parameters
----------
X : Series or Panel of mtype X_inner_mtype
if X_inner_mtype is list, _fit must support all types in it
Data to fit transform to
y : Series or Panel of mtype y_inner_mtype, default=None
Additional data, e.g., labels for tarnsformation
Returns
-------
self: a fitted instance of the estimator
See extension_templates/transformer.py for implementation details.
"""
# default fit is "no fitting happens"
return self
@abstractmethod
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
private _transform containing the core logic, called from transform
Parameters
----------
X : Series or Panel of mtype X_inner_mtype
if X_inner_mtype is list, _transform must support all types in it
Data to be transformed
y : Series or Panel, default=None
Additional data, e.g., labels for transformation
Returns
-------
transformed version of X
type depends on type of X and scitype:transform-output tag:
| | `transform` | |
| `X` | `-output` | type of return |
|----------|--------------|------------------------|
| `Series` | `Primitives` | `pd.DataFrame` (1-row) |
| `Panel` | `Primitives` | `pd.DataFrame` |
| `Series` | `Series` | `Series` |
| `Panel` | `Series` | `Panel` |
| `Series` | `Panel` | `Panel` |
instances in return correspond to instances in `X`
combinations not in the table are currently not supported
See extension_templates/transformer.py for implementation details.
"""
def _fit_transform(self, X, y=None):
"""Fit to data, then transform it.
Fits the transformer to X and y and returns a transformed version of X.
private _fit_transform containing the core logic, called from fit_transform
Parameters
----------
X : Series or Panel of mtype X_inner_mtype
if X_inner_mtype is list, _fit_transform must support all types in it
Data to fit transform to
y : Series or Panel of mtype y_inner_mtype, default=None
Additional data, e.g., labels for tarnsformation
Returns
-------
self: a fitted instance of the estimator
See extension_templates/transformer.py for implementation details.
"""
# Non-optimized default implementation; override when a better
# method is possible for a given algorithm.
self._fit(X, y)
return self._transform(X, y)
def _inverse_transform(self, X, y=None):
"""Inverse transform X and return an inverse transformed version.
private _inverse_transform containing core logic, called from inverse_transform
Parameters
----------
X : Series or Panel of mtype X_inner_mtype
if X_inner_mtype is list, _inverse_transform must support all types in it
Data to be transformed
y : Series or Panel, default=None
Additional data, e.g., labels for transformation
Returns
-------
inverse transformed version of X
of the same type as X, and conforming to mtype format specifications
See extension_templates/transformer.py for implementation details.
"""
raise NotImplementedError(
f"{self.__class__.__name__} does not support inverse_transform"
)
def _update(self, X, y=None):
"""Update transformer with X and y.
private _update containing the core logic, called from update
Parameters
----------
X : Series or Panel of mtype X_inner_mtype
if X_inner_mtype is list, _update must support all types in it
Data to update transformer with
y : Series or Panel of mtype y_inner_mtype, default=None
Additional data, e.g., labels for tarnsformation
Returns
-------
self: a fitted instance of the estimator
See extension_templates/transformer.py for implementation details.
"""
# standard behaviour: no update takes place, new data is ignored
return self