-
Notifications
You must be signed in to change notification settings - Fork 89
/
summarize.py
943 lines (816 loc) · 33.1 KB
/
summarize.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
"""Implement transformers for summarizing a time series."""
__author__ = ["mloning", "RNKuhns", "danbartl", "grzegorzrut"]
__all__ = ["SummaryTransformer", "WindowSummarizer"]
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from aeon.transformations.base import BaseTransformer
from aeon.utils.multiindex import flatten_multiindex
class WindowSummarizer(BaseTransformer):
"""
Transformer for extracting time series features.
The WindowSummarizer transforms input series to features based
on a provided dictionary of window summarizer, window shifts
and window lengths.
Parameters
----------
n_jobs : int, optional (default=-1)
The number of jobs to run in parallel for applying the window functions.
``-1`` means using all processors.
target_cols : list of str, optional (default = None)
Specifies which columns in X to target for applying the window functions.
``None`` will target the first column
lag_feature : dict of str and list, optional (default = dict containing first lag)
Dictionary specifying as key the type of function to be used and as value
the argument `window`.
For the function `lag`, the argument `window` is an integer or a list of
integers giving the `lag` values to be used.
For all other functions, the argument `window` is a list with the arguments
`lag` and `window length`. `lag` defines how far back in the past the window
starts, `window length` gives the length of the window across which to apply the
function. For multiple different windows, provide a list of lists.
Please see below a graphical representation of the logic using the following
symbols:
``z`` = time stamp that the window is summarized *to*.
Part of the window if `lag` is between 0 and `1-window_length`, otherwise
not part of the window.
``x`` = (other) time stamps in the window which is summarized
``*`` = observations, past or future, not part of the window
The summarization function is applied to the window consisting of x and
potentially z.
For `window = [1, 3]`, we have a `lag` of 1 and
`window_length` of 3 to target the three last days (exclusive z) that were
observed. Summarization is done across windows like this:
|-------------------------- |
| * * * * * * * * x x x z * |
|---------------------------|
For `window = [0, 3]`, we have a `lag` of 0 and
`window_length` of 3 to target the three last days (inclusive z) that
were observed. Summarization is done across windows like this:
|-------------------------- |
| * * * * * * * * x x z * * |
|---------------------------|
Special case ´lag´: Since lags are frequently used and window length is
redundant, you only need to provide a list of `lag` values.
So `window = [1]` will result in the first lag:
|-------------------------- |
| * * * * * * * * * * x z * |
|---------------------------|
And `window = [1, 4]` will result in the first and fourth lag:
|-------------------------- |
| * * * * * * * x * * x z * |
|---------------------------|
key: either custom function call (to be
provided by user) or str corresponding to native pandas window function:
* "sum",
* "mean",
* "median",
* "std",
* "var",
* "kurt",
* "min",
* "max",
* "corr",
* "cov",
* "skew",
* "sem"
See also: https://pandas.pydata.org/docs/reference/window.html.
The column generated will be named after the key provided, followed by the
lag parameter and the window_length (if not a lag).
second value (window): list of integers
List containg lag and window_length parameters.
truncate : str, optional default = None
Defines how to deal with NAs that were created as a result of applying the
functions in the lag_feature dict across windows that are longer than
the remaining history of data.
For example a lag config of [14, 7] cannot be fully applied for the first 20
observations of the targeted column.
A lag_feature of [[8, 14], [1, 28]] cannot be correctly applied for the
first 21 resp. 28 observations of the targeted column. Possible values
to deal with those NAs:
* None
* "bfill"
None will keep the NAs generated, and would leave it for the user to choose
an estimator that can correctly deal with observations with missing values,
"bfill" will fill the NAs by carrying the first observation backwards.
Attributes
----------
truncate_start : int
See section Parameters - truncate for a more detailed explanation of truncation
as a result of applying windows of certain lengths across past observations.
Truncate_start will give the maximum of observations that are filled with NAs
across all arguments of the lag_feature when truncate is set to None.
Returns
-------
X: pd.DataFrame
Contains all transformed columns as well as non-transformed columns.
The raw inputs to transformed columns will be dropped.
self: reference to self
Examples
--------
>>> import pandas as pd
>>> from aeon.transformations.summarize import WindowSummarizer
>>> from aeon.datasets import load_airline, load_longley
>>> from aeon.forecasting.naive import NaiveForecaster
>>> from aeon.forecasting.base import ForecastingHorizon
>>> from aeon.forecasting.compose import ForecastingPipeline
>>> from aeon.forecasting.model_selection import temporal_train_test_split
>>> y = load_airline()
>>> kwargs = {
... "lag_feature": {
... "lag": [1],
... "mean": [[1, 3], [3, 6]],
... "std": [[1, 4]],
... }
... }
>>> transformer = WindowSummarizer(**kwargs)
>>> y_transformed = transformer.fit_transform(y)
Example with transforming multiple columns of exogeneous features
>>> y, X = load_longley()
>>> y_train, y_test, X_train, X_test = temporal_train_test_split(y, X)
>>> fh = ForecastingHorizon(X_test.index, is_relative=False)
>>> # Example transforming only X
>>> pipe = ForecastingPipeline(
... steps=[
... ("a", WindowSummarizer(n_jobs=1, target_cols=["POP", "GNPDEFL"])),
... ("b", WindowSummarizer(n_jobs=1, target_cols=["GNP"], **kwargs)),
... ("forecaster", NaiveForecaster(strategy="drift")),
... ]
... )
>>> pipe_return = pipe.fit(y_train, X_train)
>>> y_pred1 = pipe_return.predict(fh=fh, X=X_test)
Example with transforming multiple columns of exogeneous features
as well as the y column
>>> Z_train = pd.concat([X_train, y_train], axis=1)
>>> Z_test = pd.concat([X_test, y_test], axis=1)
>>> pipe = ForecastingPipeline(
... steps=[
... ("a", WindowSummarizer(n_jobs=1, target_cols=["POP", "TOTEMP"])),
... ("b", WindowSummarizer(**kwargs, n_jobs=1, target_cols=["GNP"])),
... ("forecaster", NaiveForecaster(strategy="drift")),
... ]
... )
>>> pipe_return = pipe.fit(y_train, Z_train)
>>> y_pred2 = pipe_return.predict(fh=fh, X=Z_test)
"""
_tags = {
"input_data_type": "Series",
"output_data_type": "Series",
"instancewise": True,
"capability:inverse_transform": False,
"transform_labels": False,
"X_inner_type": [
"pd-multiindex",
"pd.DataFrame",
"pd_multiindex_hier",
], # which mtypes do _fit/_predict support for X?
"skip-inverse-transform": True, # is inverse-transform skipped when called?
"univariate-only": False, # can the transformer handle multivariate X?
"capability:missing_values": True, # can estimator handle missing data?
"X-y-must-have-same-index": False, # can estimator handle different X/y index?
"enforce_index_type": None, # index type that needs to be enforced in X/y
"fit_is_empty": False, # is fit empty and can be skipped? Yes = True
"transform-returns-same-time-index": False,
# does transform return have the same time index as input X
"remember_data": True, # remember all data seen as _X
}
def __init__(
self,
lag_feature=None,
n_jobs=-1,
target_cols=None,
truncate=None,
):
self.lag_feature = lag_feature
self.n_jobs = n_jobs
self.target_cols = target_cols
self.truncate = truncate
super(WindowSummarizer, self).__init__()
def _fit(self, X, y=None):
"""Fit transformer to X and y.
Private _fit containing the core logic, called from fit
Attributes
----------
truncate_start : int
See section class WindowSummarizer - Parameters - truncate for a more
detailed explanation of truncation as a result of applying windows of
certain lengths across past observations.
Truncate_start will give the maximum of observations that are filled
with NAs across all arguments of the lag_feature when truncate is
set to None.
Returns
-------
X: pd.DataFrame
Contains all transformed columns as well as non-transformed columns.
The raw inputs to transformed columns will be dropped.
self: reference to self
"""
X_name = get_name_list(X)
if self.target_cols is not None:
if not all(x in X_name for x in self.target_cols):
missing_cols = [x for x in self.target_cols if x not in X_name]
raise ValueError(
"target_cols "
+ " ".join(missing_cols)
+ " specified that do not exist in X."
)
if self.target_cols is None:
self._target_cols = [X_name[0]]
else:
self._target_cols = self.target_cols
# Convert lag config dictionary to pandas dataframe
if self.lag_feature is None:
func_dict = pd.DataFrame(
{
"lag": [1],
}
).T.reset_index()
else:
func_dict = pd.DataFrame.from_dict(
self.lag_feature, orient="index"
).reset_index()
func_dict = pd.melt(
func_dict, id_vars="index", value_name="window", ignore_index=False
)
func_dict.sort_index(inplace=True)
func_dict.drop("variable", axis=1, inplace=True)
func_dict.rename(
columns={"index": "summarizer"},
inplace=True,
)
func_dict = func_dict.dropna(axis=0, how="any")
# Identify lags (since they can follow special notation)
lags = func_dict["summarizer"] == "lag"
# Convert lags to default list notation with window_length 1
boost_lag = func_dict.loc[lags, "window"].apply(lambda x: [int(x), 1])
func_dict.loc[lags, "window"] = boost_lag
self.truncate_start = func_dict["window"].apply(lambda x: x[0] + x[1] - 1).max()
self._func_dict = func_dict
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
Parameters
----------
X : pd.DataFrame
y : None
Returns
-------
transformed version of X
"""
idx = X.index
X = X.combine_first(self._X)
func_dict = self._func_dict
target_cols = self._target_cols
X.columns = X.columns.map(str)
Xt_out = []
if self.truncate == "bfill":
bfill = True
else:
bfill = False
for cols in target_cols:
if isinstance(X.index, pd.MultiIndex):
hier_levels = list(range(X.index.nlevels - 1))
X_grouped = X.groupby(level=hier_levels)[cols]
df = Parallel(n_jobs=self.n_jobs)(
delayed(_window_feature)(X_grouped, **kwargs, bfill=bfill)
for index, kwargs in func_dict.iterrows()
)
else:
df = Parallel(n_jobs=self.n_jobs)(
delayed(_window_feature)(X.loc[:, [cols]], **kwargs, bfill=bfill)
for _index, kwargs in func_dict.iterrows()
)
Xt = pd.concat(df, axis=1)
Xt = Xt.add_prefix(str(cols) + "_")
Xt_out.append(Xt)
Xt_out_df = pd.concat(Xt_out, axis=1)
Xt_return = pd.concat([Xt_out_df, X.drop(target_cols, axis=1)], axis=1)
Xt_return = Xt_return.loc[idx]
return Xt_return
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`
"""
params1 = {
"lag_feature": {
"lag": [1],
"mean": [[1, 3], [1, 12]],
"std": [[1, 4]],
}
}
params2 = {
"lag_feature": {
"lag": [3, 6],
}
}
params3 = {
"lag_feature": {
"mean": [[1, 7], [8, 7]],
"cov": [[1, 28]],
}
}
return [params1, params2, params3]
# List of native pandas rolling window function.
# In the future different engines for pandas will be investigated
pd_rolling = [
"sum",
"mean",
"median",
"std",
"var",
"kurt",
"min",
"max",
"corr",
"cov",
"skew",
"sem",
]
def get_name_list(Z):
"""Get names of pd.Series or pd.Dataframe."""
if isinstance(Z, pd.DataFrame):
Z_name = Z.columns.to_list()
else:
if Z.name is not None:
Z_name = [Z.name]
else:
Z_name = None
Z_name = [str(z) for z in Z_name]
return Z_name
def _window_feature(Z, summarizer=None, window=None, bfill=False):
"""Compute window features and lag.
Apply summarizer passed over a certain window
of past observations, e.g. the mean of a window of length 7 days, lagged by 14 days.
Z: pandas Dataframe with a single column.
name : str, base string of the derived features, will be appended by
`lag` and window length parameters defined in window.
summarizer: either str corresponding to pandas window function, currently
* "sum",
* "mean",
* "median",
* "std",
* "var",
* "kurt",
* "min",
* "max",
* "corr",
* "cov",
* "skew",
* "sem"
or custom function call. See for the native window functions also
https://pandas.pydata.org/docs/reference/window.html.
window: list of integers
List containg window_length and lag parameters, see WindowSummarizer
class description for in-depth explanation.
"""
lag = window[0]
window_length = window[1]
if summarizer in pd_rolling:
if isinstance(Z, pd.core.groupby.generic.SeriesGroupBy):
if bfill is False:
feat = getattr(
Z.shift(lag).rolling(
window=window_length, min_periods=window_length
),
summarizer,
)()
else:
feat = getattr(
Z.shift(lag)
.fillna(method="bfill")
.rolling(window=window_length, min_periods=window_length),
summarizer,
)()
feat = pd.DataFrame(feat)
else:
if bfill is False:
feat = Z.apply(
lambda x: getattr(
x.shift(lag).rolling(
window=window_length, min_periods=window_length
),
summarizer,
)()
)
else:
feat = Z.apply(
lambda x: getattr(
x.shift(lag)
.fillna(method="bfill")
.rolling(window=window_length, min_periods=window_length),
summarizer,
)()
)
else:
if bfill is False:
feat = Z.shift(lag)
else:
feat = Z.shift(lag).fillna(method="bfill")
if isinstance(Z, pd.core.groupby.generic.SeriesGroupBy) and callable(
summarizer
):
feat = feat.rolling(window_length).apply(summarizer, raw=True)
elif not isinstance(Z, pd.core.groupby.generic.SeriesGroupBy) and callable(
summarizer
):
feat = feat.apply(
lambda x: x.rolling(
window=window_length, min_periods=window_length
).apply(summarizer, raw=True)
)
feat = pd.DataFrame(feat)
if bfill is True:
feat = feat.fillna(method="bfill")
if callable(summarizer):
name = summarizer.__name__
else:
name = summarizer
if name == "lag":
feat.rename(
columns={feat.columns[0]: name + "_" + str(window[0])},
inplace=True,
)
else:
feat.rename(
columns={
feat.columns[0]: name + "_" + "_".join([str(item) for item in window])
},
inplace=True,
)
return feat
ALLOWED_SUM_FUNCS = [
"mean",
"min",
"max",
"median",
"sum",
"skew",
"kurt",
"var",
"std",
"sem",
"nunique",
"count",
]
def _check_summary_function(summary_function):
"""Validate summary_function.
Parameters
----------
summary_function : str, list or tuple
Either a string or list/tuple of strings indicating the pandas summary
functions ("mean", "min", "max", "median", "sum", "skew", "kurtosis",
"var", "std", "sem", "nunique", "count") that is used to summarize
each column of the dataset.
Returns
-------
summary_function : list or tuple
The summary functions that will be used to summarize the dataset.
"""
msg = f"""`summary_function` must be None, or str or a list or tuple made up of
{ALLOWED_SUM_FUNCS}.
"""
if isinstance(summary_function, str):
if summary_function not in ALLOWED_SUM_FUNCS:
raise ValueError(msg)
summary_function = [summary_function]
elif isinstance(summary_function, (list, tuple)):
if not all([func in ALLOWED_SUM_FUNCS for func in summary_function]):
raise ValueError(msg)
elif summary_function is not None:
raise ValueError(msg)
return summary_function
def _check_quantiles(quantiles):
"""Validate quantiles.
Parameters
----------
quantiles : str, list, tuple or None
Either a string or list/tuple of strings indicating the pandas summary
functions ("mean", "min", "max", "median", "sum", "skew", "kurtosis",
"var", "std", "sem", "nunique", "count") that is used to summarize
each column of the dataset.
Returns
-------
quantiles : list or tuple
The validated quantiles that will be used to summarize the dataset.
"""
msg = """`quantiles` must be None, int, float or a list or tuple made up of
int and float values that are between 0 and 1.
"""
if isinstance(quantiles, (int, float)):
if not 0.0 <= quantiles <= 1.0:
raise ValueError(msg)
quantiles = [quantiles]
elif isinstance(quantiles, (list, tuple)):
if len(quantiles) == 0 or not all(
[isinstance(q, (int, float)) and 0.0 <= q <= 1.0 for q in quantiles]
):
raise ValueError(msg)
elif quantiles is not None:
raise ValueError(msg)
return quantiles
class SummaryTransformer(BaseTransformer):
"""Calculate summary value of a time series.
For :term:`univariate time series` a combination of summary functions and
quantiles of the input series are calculated. If the input is a
:term:`multivariate time series` then the summary functions and quantiles
are calculated separately for each column.
Parameters
----------
summary_function : str, list, tuple, or None, default=("mean", "std", "min", "max")
If not None, a string, or list or tuple of strings indicating the pandas
summary functions that are used to summarize each column of the dataset.
Must be one of ("mean", "min", "max", "median", "sum", "skew", "kurt",
"var", "std", "sem", "nunique", "count").
If None, no summaries are calculated, and quantiles must be non-None.
quantiles : str, list, tuple or None, default=(0.1, 0.25, 0.5, 0.75, 0.9)
Optional list of series quantiles to calculate. If None, no quantiles
are calculated, and summary_function must be non-None.
flatten_transform_index : bool, optional (default=True)
if True, columns of return DataFrame are flat, by "variablename__feature"
if False, columns are MultiIndex (variablename__feature)
has no effect if return mtype is one without column names
See Also
--------
WindowSummarizer:
Extracting features across (shifted) windows from series
Notes
-----
This provides a wrapper around pandas DataFrame and Series agg and
quantile methods.
Examples
--------
>>> from aeon.transformations.summarize import SummaryTransformer
>>> from aeon.datasets import load_airline
>>> y = load_airline()
>>> transformer = SummaryTransformer()
>>> y_mean = transformer.fit_transform(y)
"""
_tags = {
"input_data_type": "Series",
# what is the scitype of X: Series, or Panel
"output_data_type": "Primitives",
# what scitype is returned: Primitives, Series, Panel
"instancewise": True, # is this an instance-wise transform?
"X_inner_type": ["pd.DataFrame", "pd.Series"],
# which mtypes do _fit/_predict support for X?
"y_inner_type": "None", # which mtypes do _fit/_predict support for X?
"fit_is_empty": True,
}
def __init__(
self,
summary_function=("mean", "std", "min", "max"),
quantiles=(0.1, 0.25, 0.5, 0.75, 0.9),
flatten_transform_index=True,
):
self.summary_function = summary_function
self.quantiles = quantiles
self.flatten_transform_index = flatten_transform_index
super(SummaryTransformer, self).__init__()
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
private _transform containing the core logic, called from transform
Parameters
----------
X : pd.Series or pd.DataFrame
Data to be transformed
y : ignored argument for interface compatibility
Additional data, e.g., labels for transformation
Returns
-------
summary_value : scalar or pd.Series
If `series_or_df` is univariate then a scalar is returned. Otherwise,
a pd.Series is returned.
"""
if self.summary_function is None and self.quantiles is None:
raise ValueError(
"One of `summary_function` and `quantiles` must not be None."
)
summary_function = _check_summary_function(self.summary_function)
quantiles = _check_quantiles(self.quantiles)
if summary_function is not None:
summary_value = X.agg(summary_function)
if quantiles is not None:
quantile_value = X.quantile(quantiles)
quantile_value.index = [str(s) for s in quantile_value.index]
if summary_function is not None and quantiles is not None:
summary_value = pd.concat([summary_value, quantile_value])
elif summary_function is None:
summary_value = quantile_value
if isinstance(X, pd.Series):
summary_value.name = X.name
summary_value = pd.DataFrame(summary_value)
Xt = summary_value.T
if len(Xt) > 1:
# move the row index as second level to column
Xt = pd.DataFrame(Xt.T.unstack()).T
if self.flatten_transform_index:
Xt.columns = flatten_multiindex(Xt.columns)
return Xt
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`
"""
params1 = {}
params2 = {"summary_function": ["mean", "std", "skew"], "quantiles": None}
params3 = {"summary_function": None, "quantiles": (0.1, 0.2, 0.25)}
return [params1, params2, params3]
class PlateauFinder(BaseTransformer):
"""
Plateau finder transformer.
Transformer that finds segments of the same given value, plateau in
the time series, and returns the starting indices and lengths.
Parameters
----------
value : {int, float, np.nan, np.inf}
Value for which to find segments
min_length : int
Minimum lengths of segments with same value to include.
If min_length is set to 1, the transformer can be used as a value
finder.
"""
_tags = {
"fit_is_empty": True,
"univariate-only": True,
"output_data_type": "Series",
"instancewise": False,
"X_inner_type": "numpy3D",
"y_inner_type": "None",
}
def __init__(self, value=np.nan, min_length=2):
self.value = value
self.min_length = min_length
super(PlateauFinder, self).__init__(_output_convert=False)
def _transform(self, X, y=None):
"""Transform X.
Parameters
----------
X : numpy3D array shape (n_cases, 1, series_length)
Returns
-------
X : pandas data frame
"""
_starts = []
_lengths = []
# find plateaus (segments of the same value)
for x in X[:, 0]:
# find indices of transition
if np.isnan(self.value):
i = np.where(np.isnan(x), 1, 0)
elif np.isinf(self.value):
i = np.where(np.isinf(x), 1, 0)
else:
i = np.where(x == self.value, 1, 0)
# pad and find where segments transition
transitions = np.diff(np.hstack([0, i, 0]))
# compute starts, ends and lengths of the segments
starts = np.where(transitions == 1)[0]
ends = np.where(transitions == -1)[0]
lengths = ends - starts
# filter out single points
starts = starts[lengths >= self.min_length]
lengths = lengths[lengths >= self.min_length]
_starts.append(starts)
_lengths.append(lengths)
# put into dataframe
Xt = pd.DataFrame()
column_prefix = "%s_%s" % (
"channel_",
"nan" if np.isnan(self.value) else str(self.value),
)
Xt["%s_starts" % column_prefix] = pd.Series(_starts)
Xt["%s_lengths" % column_prefix] = pd.Series(_lengths)
Xt = Xt.applymap(lambda x: pd.Series(x))
return Xt
class FittedParamExtractor(BaseTransformer):
"""Fitted parameter extractor.
Extract parameters of a fitted forecaster as features for a subsequent
tabular learning task.
This class first fits a forecaster to the given time series and then
returns the fitted parameters.
The fitted parameters can be used as features for a tabular estimator
(e.g. classification).
Parameters
----------
forecaster : estimator object
An aeon estimator to extract features from.
param_names : str
Name of parameters to extract from the forecaster.
n_jobs : int, optional (default=None)
Number of jobs to run in parallel.
None means 1 unless in a joblib.parallel_backend context.
-1 means using all processors.
"""
_tags = {
"fit_is_empty": True,
"univariate-only": True,
"input_data_type": "Series",
# what is the scitype of X: Series, or Panel
"output_data_type": "Primitives",
# what is the scitype of y: None (not needed), Primitives, Series, Panel
"instancewise": True, # is this an instance-wise transform?
"X_inner_type": "numpy3D", # which mtypes do _fit/_predict support for X?
"y_inner_type": "None", # which mtypes do _fit/_predict support for y?
}
def __init__(self, forecaster, param_names, n_jobs=None):
self.forecaster = forecaster
self.param_names = param_names
self.n_jobs = n_jobs
super(FittedParamExtractor, self).__init__(_output_convert=True)
def _transform(self, X, y=None):
"""Transform X.
Parameters
----------
X: np.ndarray shape (n_time_series, 1, series_length)
The training input samples.
y : ignored argument for interface compatibility
Additional data, e.g., labels for transformation
Returns
-------
Xt : pd.DataFrame
Extracted parameters; columns are parameter values
"""
param_names = self._check_param_names(self.param_names)
n_instances = X.shape[0]
def _fit_extract(forecaster, x, param_names):
forecaster.fit(x)
params = forecaster.get_fitted_params()
return np.hstack([params[name] for name in param_names])
def _get_instance(X, key):
# assuming univariate data
if isinstance(X, pd.DataFrame):
return X.iloc[key, 0]
else:
return pd.Series(X[key, 0])
# iterate over rows
extracted_params = Parallel(n_jobs=self.n_jobs)(
delayed(_fit_extract)(
self.forecaster.clone(), _get_instance(X, i), param_names
)
for i in range(n_instances)
)
return pd.DataFrame(extracted_params, columns=param_names)
@staticmethod
def _check_param_names(param_names):
if isinstance(param_names, str):
param_names = [param_names]
elif isinstance(param_names, (list, tuple)):
for param in param_names:
if not isinstance(param, str):
raise ValueError(
f"All elements of `param_names` must be strings, "
f"but found: {type(param)}"
)
else:
raise ValueError(
f"`param_names` must be str, or a list or tuple of strings, "
f"but found: {type(param_names)}"
)
return param_names
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`
"""
from aeon.forecasting.trend import TrendForecaster
# accessing a nested parameter
params = [
{
"forecaster": TrendForecaster(),
"param_names": ["regressor__intercept"],
}
]
return params