-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.py
143 lines (108 loc) · 5.19 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
import numpy as np
import mne_features.univariate as mnf
import utils.variables as var
def hjorth_features(data):
'''
Computes the features Hjorth mobility (spectral) and Hjorth complexity (spectral) using the package mne_features.
Parameters
----------
data : dict
data (list of dicts): A list of dictionaries, where each dictionary contains EEG data for multiple trials. The keys in each dictionary represent trial IDs, and the values are numpy arrays of shape (n_channels, n_samples).
Returns
-------
features : list of ndarrays
list of ndarrays with the computed features.
'''
sfreq = var.SFREQ
features_per_channel = 2
n_recordings, n_channels, n_samples = data.shape
features = np.empty([n_recordings, n_channels*features_per_channel])
for i in range(n_recordings):
samples = data[i]
mobility_spect = mnf.compute_hjorth_mobility_spect(sfreq=sfreq, data=samples)
complexity_spect = mnf.compute_hjorth_complexity_spect(sfreq=sfreq, data=samples)
features[i] = np.concatenate([mobility_spect, complexity_spect])
features = features.reshape([n_recordings, n_channels*features_per_channel])
return features
def entropy_features(data):
'''
Computes the features Approximate Entropy, Sample Entropy, Spectral Entropy and SVD entropy using the package mne_features.
Parameters
----------
data : dict
A list of dictionaries, where each dictionary contains EEG data for multiple trials. The keys in each dictionary represent trial IDs, and the values are numpy arrays of shape (n_channels, n_samples).
Returns
-------
features : list of ndarrays
list of ndarrays of the computed features.
'''
sfreq = var.SFREQ
features_per_channel = 4
n_recordings, n_channels, n_samples = data.shape
features = np.empty([n_recordings, n_channels*features_per_channel])
for i in range(n_recordings):
samples = data[i]
app_entropy = mnf.compute_app_entropy(data=samples)
samp_entropy = mnf.compute_samp_entropy(data=samples)
spect_entropy = mnf.compute_spect_entropy(sfreq=sfreq, data=samples)
svd_entropy = mnf.compute_svd_entropy(data=samples)
features[i] = np.concatenate([app_entropy, samp_entropy, spect_entropy, svd_entropy])
features = features.reshape([n_recordings, n_channels*features_per_channel])
return features
def time_series_features(data):
'''
Compute time-series features from data.
Parameters
----------
data : ndarray, shape(n_recordings, n_channels, n_samples)
An ndarray containing the data.
Returns
-------
features : ndarray, shape(n_recordings, n_channels*features_per_channel)
An ndarray of the computed features
'''
sfreq = var.SFREQ
features_per_channel = 3
n_recordings, n_channels, n_samples = data.shape
features = np.empty([n_recordings, n_channels*features_per_channel])
for i in range(n_recordings):
samples = data[i]
ptp_amp = mnf.compute_ptp_amp(data=samples)
variance = mnf.compute_variance(data=samples)
rms = mnf.compute_rms(data=samples)
features[i] = np.concatenate([ptp_amp, variance, rms])
features = features.reshape([n_recordings, n_channels*features_per_channel])
return features
def psd_features(data):
sfreq = var.SFREQ
n_frequencies = 5 #default number of (freqs-1) for mnf.compute_pow_freq_bands
n_recordings, n_channels, n_samples = data.shape
features = np.empty([n_recordings, n_channels*n_frequencies])
for i in range(n_recordings):
samples = data[i]
psd = mnf.compute_pow_freq_bands(sfreq=sfreq, data=samples)
features[i] = np.concatenate([psd])
features = features.reshape([n_recordings, n_channels*n_frequencies])
return features
def all_features(data):
'''
Compute hjorth, entropy and time-series features in one
'''
sfreq = var.SFREQ
features_per_channel = 10
n_recordings, n_channels, n_samples = data.shape
features = np.empty([n_recordings, n_channels*features_per_channel])
for i in range(n_recordings):
samples = data[i]
mobility_spect = mnf.compute_hjorth_mobility_spect(sfreq=sfreq, data=samples)
complexity_spect = mnf.compute_hjorth_complexity_spect(sfreq=sfreq, data=samples)
app_entropy = mnf.compute_app_entropy(data=samples)
samp_entropy = mnf.compute_samp_entropy(data=samples)
spect_entropy = mnf.compute_spect_entropy(sfreq=sfreq, data=samples)
svd_entropy = mnf.compute_svd_entropy(data=samples)
ptp_amp = mnf.compute_ptp_amp(data=samples)
variance = mnf.compute_variance(data=samples)
rms = mnf.compute_rms(data=samples)
mean = mnf.compute_mean(data=samples)
features[i] = np.concatenate([mobility_spect, complexity_spect, app_entropy, samp_entropy, spect_entropy, svd_entropy, ptp_amp, variance, rms, mean])
features = features.reshape([n_recordings, n_channels*features_per_channel])