-
Notifications
You must be signed in to change notification settings - Fork 1
/
features.py
199 lines (150 loc) · 5.23 KB
/
features.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
import numpy as np
import scipy.stats as stats
import scipy.signal as signal
from scipy.ndimage import median_filter
import statsmodels.tsa.stattools as stattools
MIN_WINDOW_SEC = 2 # seconds
def extract_features(xyz, sample_rate=100):
"""Extract commonly used HAR time-series features. xyz is a window of shape (N,3)"""
if np.isnan(xyz).any():
return {}
if len(xyz) <= MIN_WINDOW_SEC * sample_rate:
return {}
feats = {}
v = np.linalg.norm(xyz, axis=1)
v = v - 1 # detrend: "remove gravity"
v = np.clip(v, -2, 2) # clip abnormaly high values
# Moments features
feats.update(moments_features(v, sample_rate))
# Quantile features
feats.update(quantile_features(v, sample_rate))
# Autocorrelation features
feats.update(autocorr_features(v, sample_rate))
# Spectral features
feats.update(spectral_features(v, sample_rate))
# FFT features
feats.update(fft_features(v, sample_rate))
# Peak features
feats.update(peaks_features(v, sample_rate))
return feats
def moments_features(v, sample_rate=None):
"""Moments"""
avg = np.mean(v)
std = np.std(v)
if std > 0.01:
skew = np.nan_to_num(stats.skew(v))
kurt = np.nan_to_num(stats.kurtosis(v))
else:
skew = kurt = 0
feats = {
"avg": avg,
"std": std,
"skew": skew,
"kurt": kurt,
}
return feats
def quantile_features(v, sample_rate=None):
"""Quantiles (min, 25th, med, 75th, max)"""
feats = {}
feats["min"], feats["q25"], feats["med"], feats["q75"], feats["max"] = np.quantile(
v, (0, 0.25, 0.5, 0.75, 1)
)
return feats
def autocorr_features(v, sample_rate):
"""Autocorrelation features"""
with np.errstate(divide="ignore", invalid="ignore"): # ignore invalid div warnings
u = np.nan_to_num(stattools.acf(v, nlags=2 * sample_rate))
peaks, _ = signal.find_peaks(u, prominence=0.1)
if len(peaks) > 0:
acf_1st_max_loc = peaks[0]
acf_1st_max = u[acf_1st_max_loc]
acf_1st_max_loc /= sample_rate # in secs
else:
acf_1st_max = acf_1st_max_loc = 0.0
valleys, _ = signal.find_peaks(-u, prominence=0.1)
if len(valleys) > 0:
acf_1st_min_loc = valleys[0]
acf_1st_min = u[acf_1st_min_loc]
acf_1st_min_loc /= sample_rate # in secs
else:
acf_1st_min = acf_1st_min_loc = 0.0
acf_zeros = np.sum(np.diff(np.signbit(u)))
feats = {
"acf_1st_max": acf_1st_max,
"acf_1st_max_loc": acf_1st_max_loc,
"acf_1st_min": acf_1st_min,
"acf_1st_min_loc": acf_1st_min_loc,
"acf_zeros": acf_zeros,
}
return feats
def spectral_features(v, sample_rate):
"""Spectral entropy, average power, dominant frequencies"""
feats = {}
freqs, powers = signal.periodogram(
v, fs=sample_rate, detrend="constant", scaling="density"
)
powers /= len(v) / sample_rate # unit/sec
feats["pentropy"] = stats.entropy(powers[powers > 0])
feats["power"] = np.sum(powers)
peaks, _ = signal.find_peaks(powers)
peak_powers = powers[peaks]
peak_freqs = freqs[peaks]
peak_ranks = np.argsort(peak_powers)[::-1]
TOPN = 3
feats.update({f"f{i + 1}": 0 for i in range(TOPN)})
feats.update({f"p{i + 1}": 0 for i in range(TOPN)})
for i, j in enumerate(peak_ranks[:TOPN]):
feats[f"f{i + 1}"] = peak_freqs[j]
feats[f"p{i + 1}"] = peak_powers[j]
return feats
def fft_features(v, sample_rate, nfreqs=5):
"""Power of frequencies 0Hz, 1Hz, 2Hz, ... using Welch's method"""
_, powers = signal.welch(
v,
fs=sample_rate,
nperseg=sample_rate,
noverlap=sample_rate // 2,
detrend="constant",
scaling="density",
average="median",
)
feats = {f"fft{i}": powers[i] for i in range(nfreqs + 1)}
return feats
def peaks_features(v, sample_rate):
"""Features of the signal peaks"""
feats = {}
u = butterfilt(v, 5, fs=sample_rate) # lowpass 5Hz
peaks, peak_props = signal.find_peaks(
u, distance=0.2 * sample_rate, prominence=0.25
)
feats["npeaks"] = len(peaks) / (len(v) / sample_rate) # peaks/sec
if len(peak_props["prominences"]) > 0:
feats["peaks_avg_promin"] = np.mean(peak_props["prominences"])
feats["peaks_min_promin"] = np.min(peak_props["prominences"])
feats["peaks_max_promin"] = np.max(peak_props["prominences"])
else:
feats["peaks_avg_promin"] = feats["peaks_min_promin"] = feats[
"peaks_max_promin"
] = 0
return feats
def butterfilt(x, cutoffs, fs, order=4, axis=0):
"""Butterworth filter"""
nyq = 0.5 * fs
if isinstance(cutoffs, tuple):
hicut, lowcut = cutoffs
if hicut > 0:
btype = "bandpass"
Wn = (hicut / nyq, lowcut / nyq)
else:
btype = "low"
Wn = lowcut / nyq
else:
btype = "low"
Wn = cutoffs / nyq
sos = signal.butter(order, Wn, btype=btype, analog=False, output="sos")
y = signal.sosfiltfilt(sos, x, axis=axis)
return y
def get_feature_names():
"""Hacky way to get the list of feature names"""
feats = extract_features(np.zeros((500, 3)), 100)
return list(feats.keys())