-
Notifications
You must be signed in to change notification settings - Fork 89
/
interpolate.py
100 lines (84 loc) · 3.29 KB
/
interpolate.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
# -*- coding: utf-8 -*-
"""Time series interpolator/re-sampler."""
import numpy as np
from aeon.transformations.base import BaseTransformer
__all__ = ["TSInterpolator"]
__author__ = ["mloning", "TonyBagnall"]
class TSInterpolator(BaseTransformer):
"""Time series interpolator/re-sampler.
Transformer that rescales series for another number of points.
For each series, np.interp is fitted on each channel independently.
After transformation each series will be a 2D numpy array (n_channels, length).
Parameters
----------
length : integer, the length of time series to resize to.
Example
-------
>>> import numpy as np
>>> from aeon.transformations.collection.interpolate import TSInterpolator
>>> # Unequal length collection of time series
>>> X_list = []
>>> for i in range(10): X_list.append(np.random.rand(5,10+i))
>>> # Equal length collection of time series
>>> X_array = np.random.rand(10,3,30)
>>> trans = TSInterpolator(length = 50)
>>> X_new = trans.fit_transform(X_list)
>>> X_new.shape
(10, 5, 50)
>>> X_new = trans.fit_transform(X_array)
>>> X_new.shape
(10, 3, 50)
"""
_tags = {
"scitype:transform-output": "Series",
"scitype:instancewise": False,
"X_inner_mtype": ["np-list", "numpy3D"],
"y_inner_mtype": "None",
"capability:multivariate": True,
"capability:unequal_length": True,
"fit_is_empty": True,
}
def __init__(self, length):
"""Initialize estimator.
Parameters
----------
length : integer, the length of time series to resize to.
"""
if length <= 0 or (not isinstance(length, int)):
raise ValueError("resizing length must be integer and > 0")
self.length = length
super(TSInterpolator, self).__init__(_output_convert=False)
def _transform(self, X, y=None):
"""Take series in each cell, train linear interpolation and samples n.
Parameters
----------
X : 3D np.ndarray of shape = (n_cases, n_channels, series_length) or
list size [n_cases] of 2D nump arrays, case i has shape (n_channels,
length_i). Collection of time series to transform
y : ignored argument for interface compatibility
Returns
-------
3D numpy array of shape (n_cases, n_channels, self.length)
"""
Xt = []
for x in X:
x_new = np.zeros((x.shape[0], self.length))
x2 = np.linspace(0, 1, x.shape[1])
x3 = np.linspace(0, 1, self.length)
for i, row in enumerate(x):
x_new[i] = np.interp(x3, x2, row)
Xt.append(x_new)
return np.array(Xt)
@classmethod
def get_test_params(cls):
"""Return testing parameter settings for the estimator.
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 = {"length": 10}
return params