a b/features.py
1
import mne_features.univariate as mne_f
2
import numpy as np
3
4
5
def time_series_features(data):
6
    '''
7
    Computes the features variance, RMS and peak-to-peak amplitude using the package mne_features.
8
9
    Args:
10
        data (ndarray): EEG data.
11
12
    Returns:
13
        ndarray: Computed features.
14
15
    '''
16
17
    n_trials, n_secs, n_channels, _ = data.shape
18
    features_per_channel = 3
19
20
    features = np.empty([n_trials, n_secs, n_channels * features_per_channel])
21
    for i, trial in enumerate(data):
22
        for j, second in enumerate(trial):
23
            variance = mne_f.compute_variance(second)
24
            rms = mne_f.compute_rms(second)
25
            ptp_amp = mne_f.compute_ptp_amp(second)
26
            features[i][j] = np.concatenate([variance, rms, ptp_amp])
27
    features = features.reshape(
28
        [n_trials*n_secs, n_channels*features_per_channel])
29
    return features
30
31
32
def freq_band_features(data, freq_bands):
33
    '''
34
    Computes the frequency bands delta, theta, alpha, beta and gamma using the package mne_features.
35
36
    Args:
37
        data (ndarray): EEG data.
38
        freq_bands (ndarray): The frequency bands to compute.
39
40
    Returns:
41
        ndarray: Computed features.
42
    '''
43
    n_trials, n_secs, n_channels, sfreq = data.shape
44
    features_per_channel = len(freq_bands)-1
45
46
    features = np.empty([n_trials, n_secs, n_channels * features_per_channel])
47
    for i, trial in enumerate(data):
48
        for j, second in enumerate(trial):
49
            psd = mne_f.compute_pow_freq_bands(
50
                sfreq, second, freq_bands=freq_bands)
51
            features[i][j] = psd
52
    features = features.reshape(
53
        [n_trials*n_secs, n_channels*features_per_channel])
54
    return features
55
56
57
def hjorth_features(data):
58
    '''
59
    Computes the features Hjorth mobility (spectral) and Hjorth complexity (spectral) using the package mne_features.
60
61
    Args:
62
        data (ndarray): EEG data.
63
64
    Returns:
65
        ndarray: Computed features.
66
    '''
67
    n_trials, n_secs, n_channels, sfreq = data.shape
68
    features_per_channel = 2
69
70
    features = np.empty([n_trials, n_secs, n_channels * features_per_channel])
71
    for i, trial in enumerate(data):
72
        for j, second in enumerate(trial):
73
            mobility_spect = mne_f.compute_hjorth_mobility_spect(sfreq, second)
74
            complexity_spect = mne_f.compute_hjorth_complexity_spect(
75
                sfreq, second)
76
            features[i][j] = np.concatenate([mobility_spect, complexity_spect])
77
    features = features.reshape(
78
        [n_trials*n_secs, n_channels*features_per_channel])
79
    return features
80
81
82
def fractal_features(data):
83
    '''
84
    Computes the Higuchi Fractal Dimension and Katz Fractal Dimension using the package mne_features.
85
86
    Args:
87
        data (ndarray): EEG data.
88
89
    Returns:
90
        ndarray: Computed features.
91
92
    '''
93
    n_trials, n_secs, n_channels, _ = data.shape
94
    features_per_channel = 2
95
96
    features = np.empty([n_trials, n_secs, n_channels * features_per_channel])
97
    for i, trial in enumerate(data):
98
        for j, second in enumerate(trial):
99
            higuchi = mne_f.compute_higuchi_fd(second)
100
            katz = mne_f.compute_katz_fd(second)
101
            features[i][j] = np.concatenate([higuchi, katz])
102
    features = features.reshape(
103
        [n_trials*n_secs, n_channels*features_per_channel])
104
    return features
105
106
107
def entropy_features(data):
108
    '''
109
    Computes the features Approximate Entropy, Sample Entropy, Spectral Entropy and SVD entropy using the package mne_features.
110
111
    Args:
112
        data (ndarray): EEG data.
113
114
    Returns:
115
        ndarray: Computed features.
116
117
    '''
118
    n_trials, n_secs, n_channels, sfreq = data.shape
119
    features_per_channel = 4
120
121
    features = np.empty([n_trials, n_secs, n_channels * features_per_channel])
122
    for i, trial in enumerate(data):
123
        for j, second in enumerate(trial):
124
            app_entropy = mne_f.compute_app_entropy(second)
125
            samp_entropy = mne_f.compute_samp_entropy(second)
126
            spect_entropy = mne_f.compute_spect_entropy(sfreq, second)
127
            svd_entropy = mne_f.compute_svd_entropy(second)
128
            features[i][j] = np.concatenate(
129
                [app_entropy, samp_entropy, spect_entropy, svd_entropy])
130
    features = features.reshape(
131
        [n_trials*n_secs, n_channels*features_per_channel])
132
    return features