-
Notifications
You must be signed in to change notification settings - Fork 89
/
hog1d.py
168 lines (135 loc) · 5.34 KB
/
hog1d.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
"""HOG1D transform."""
import math
import numbers
import numpy as np
from aeon.transformations._split import SplitsTimeSeries
from aeon.transformations.collection import BaseCollectionTransformer
class HOG1DTransformer(BaseCollectionTransformer, SplitsTimeSeries):
"""HOG1D transform.
This transformer calculates the HOG1D transform [1] of a collection of time series.
HOG1D splits each time series n_intervals times, and finds a histogram of
gradients within each interval.
Parameters
----------
n_intervals : int
Length of interval.
n_bins : int
Number of bins in the histogram.
scaling_factor : float
A constant that is multiplied to modify the distribution.
Notes
-----
[1] J. Zhao and L. Itti "Classifying time series using local descriptors with
hybrid sampling", IEEE Transactions on Knowledge and Data Engineering 28(3), 2015.
"""
_tags = {
"fit_is_empty": True,
}
def __init__(self, n_intervals=2, n_bins=8, scaling_factor=0.1):
self.n_intervals = n_intervals
self.n_bins = n_bins
self.scaling_factor = scaling_factor
super(HOG1DTransformer, self).__init__()
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
Parameters
----------
X : 3D np.ndarray of shape = [n_instances, 1, series_length]
collection of time series to transform
y : ignored argument for interface compatibility
Returns
-------
X : 3D np.ndarray of shape = [n_instances, 1, feature_length]
collection of time series to transform
"""
# Get information about the dataframe
n_cases, n_channels, series_length = X.shape
if n_channels > 1:
raise ValueError("HOG1D does not support multivariate time series.")
# Check the parameters are appropriate
self._check_parameters(series_length)
transX = []
for i in range(n_cases):
# Get the HOG1Ds of each time series
inst = self._calculate_hog1ds(X[i][0])
transX.append(inst)
# Convert to numpy array
transX = np.asarray(transX)
transX = np.reshape(transX, [transX.shape[0], 1, transX.shape[1]])
return transX
def _calculate_hog1ds(self, X):
"""Calculate the HOG1Ds given a time series.
Parameters
----------
X : a numpy array of shape = [time_series_length]
Returns
-------
HOG1Ds : a numpy array of shape = [num_intervals*num_bins].
It contains the histogram of each gradient within
each interval.
"""
# Firstly, split the time series into approx equal
# length intervals
splitTimeSeries = self._split(X)
HOG1Ds = []
for x in range(len(splitTimeSeries)):
HOG1Ds.extend(self._get_hog1d(splitTimeSeries[x]))
return HOG1Ds
def _get_hog1d(self, X):
"""Get the HOG1D given a portion of a time series.
X : a numpy array of shape = [interval_size]
Returns
-------
histogram : a numpy array of shape = [num_bins].
"""
# First step is to pad the portion on both ends once.
gradients = [0.0] * (len(X))
X = np.pad(X, 1, mode="edge")
histogram = [0.0] * self.n_bins
# Calculate the gradients of each element
for i in range(1, len(X) - 1):
gradients[(i - 1)] = self.scaling_factor * 0.5 * (X[(i + 1)] - X[(i - 1)])
# Calculate the orientations
orients = [math.degrees(math.atan(x)) for x in gradients]
# Calculate the boundaries of the histogram
hisBoundaries = [
-90 + (180 / self.n_bins) + ((180 / self.n_bins) * x)
for x in range(self.n_bins)
]
# Construct the histogram
for x in range(len(orients)):
orientToAdd = orients[x]
for y in range(len(hisBoundaries)):
if orientToAdd <= hisBoundaries[y]:
histogram[y] += 1.0
break
return histogram
def _check_parameters(self, series_length):
"""Check the values of parameters inserted into HOG1D.
Throws
------
ValueError or TypeError if a parameters input is invalid.
"""
if isinstance(self.n_intervals, int):
if self.n_intervals <= 0:
raise ValueError("num_intervals must have the value of at least 1")
if self.n_intervals > series_length:
raise ValueError("num_intervals cannot be higher than serie_length")
else:
raise TypeError(
f"num_intervals must be an 'int' Found {type(self.n_intervals)} "
f"instead."
)
if isinstance(self.n_bins, int):
if self.n_bins <= 0:
raise ValueError("num_bins must have the value of at least 1")
else:
raise TypeError(
f"num_bins must be an 'int'. Found"
f" {type(self.n_bins).__name__}instead."
)
if not isinstance(self.scaling_factor, numbers.Number):
raise TypeError(
f"scaling_factor must be a 'number'. Found"
f" {type(self.scaling_factor).__name__}instead."
)