/
truncate.py
127 lines (103 loc) · 4.17 KB
/
truncate.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
"""Truncation transformer - truncate unequal length panels to lower/upper bounds."""
__all__ = ["TruncationTransformer"]
__maintainer__ = []
import numpy as np
from aeon.transformations.collection import BaseCollectionTransformer
class TruncationTransformer(BaseCollectionTransformer):
"""Truncate unequal length time series to a lower bounds.
Truncates all series in panel between lower/upper range bounds. This transformer
assumes that all series have the same number of channels (dimensions) and
that all channels in a single series are the same length.
Parameters
----------
truncated_length : int, default=None
bottom range of the values to truncate can also be used to truncate
to a specific length.
if None, will find the shortest sequence and use instead.
Example
-------
>>> from aeon.transformations.collection import TruncationTransformer
>>> import numpy as np
>>> X = []
>>> for i in range(10): X.append(np.random.random((4, 75 + i)))
>>> truncator = TruncationTransformer(truncated_length=10)
>>> X2 = truncator.fit_transform(X)
>>> X2.shape
(10, 4, 10)
"""
_tags = {
"X_inner_type": ["np-list", "numpy3D"],
"capability:multivariate": True,
"capability:unequal_length": True,
"capability:unequal_length:removes": True,
}
def __init__(self, truncated_length=None):
self.truncated_length = truncated_length
super().__init__()
@staticmethod
def _get_min_length(X):
min_length = X[0].shape[1]
for x in X:
if x.shape[1] < min_length:
min_length = x.shape[1]
return min_length
def _fit(self, X, y=None):
"""Fit transformer to X and y.
Parameters
----------
X : list of [n_cases] 2D np.ndarray shape (n_channels, length_i)
where length_i can vary between time series or 3D numpy of equal length
series
y : ignored argument for interface compatibility
Additional data, e.g., labels for transformation
Returns
-------
self : reference to self
"""
# If lower is none, set to the minimum length in X
min_length = self._get_min_length(X)
if self.truncated_length is None:
self.truncated_length_ = min_length
elif min_length < self.truncated_length:
self.truncated_length_ = min_length
else:
self.truncated_length_ = self.truncated_length
return self
def _transform(self, X, y=None):
"""Truncate X and return a transformed version.
Parameters
----------
X : list of [n_cases] 2D np.ndarray shape (n_channels, length_i)
where length_i can vary between time series.
y : ignored argument for interface compatibility
Returns
-------
Xt : numpy3D array (n_cases, n_channels, self.truncated_length_)
truncated time series from X.
"""
min_length = self._get_min_length(X)
if min_length < self.truncated_length_:
raise ValueError(
"Error: min_length of series \
is less than the one found when fit or set."
)
Xt = np.array([x[:, : self.truncated_length_] for x in X])
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`
"""
params = {"truncated_length": 5}
return params