function example#

get motor image data#

[7]:
def get_data_example_motor_image():
    tmin, tmax = -1., 4.
    event_id = dict(hands=2, feet=3)
    subject = 1
    runs = [6, 10, 14]  # motor imagery: hands vs feet
    raw_fnames = eegbci.load_data(subject, runs)
    raw = concatenate_raws([read_raw_edf(f, preload=True) for f in raw_fnames])
    eegbci.standardize(raw)  # set channel names
    montage = make_standard_montage('standard_1005')
    raw.set_montage(montage)

    # Apply band-pass filter
    raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')

    events, _ = events_from_annotations(raw, event_id=dict(T1=2, T2=3))

    picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
                       exclude='bads')

    # Read epochs (train will be done only between 1 and 2s)
    # Testing will be done with a running classifier
    epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                    baseline=None, preload=True)
    epochs_train = epochs.copy().crop(tmin=1., tmax=2.)
    labels = epochs.events[:, -1] - 2
    data = epochs.get_data()
    print(data.shape)
    return data, labels
[ ]:
data, _ = get_data_example_motor_image()
# 7-channel data are used.
data = data[:,:7,:]
print(data.shape)

calculate feature#

example for Tsallis_Entropy, Renyi_Entropy and Shannon_entropy feature in scuteegfe !!!!!!! if sfreq is needed, it should also be input by funcs_params

Tsallis, Renyi, Shannon_entropy#

[9]:
fea = Feature(data, sfreq=160, selected_funcs=['Renyi_Entropy', 'Tsallis_Entropy', 'Shannon_entropy'],
              funcs_params={"Renyi_Entropy__sfreq":160, "Tsallis_Entropy__sfreq":160,"Shannon_entropy__sfreq":160})
[10]:
n_epochs, n_channels, n_features = fea.features.shape
print(f'n_epochs:{n_epochs},n_channels:{n_channels},n_features:{n_features}')
n_epochs:45,n_channels:7,n_features:15
[11]:
df = pd.DataFrame(fea.features[0,:,:], columns=fea.feature_names)
df.index = [f'Chan {i+1}' for i in range(n_channels)]
df
[11]:
Renyi_Entropy0 Renyi_Entropy1 Renyi_Entropy2 Renyi_Entropy3 Renyi_Entropy4 Shannon_entropy0 Shannon_entropy1 Shannon_entropy2 Shannon_entropy3 Shannon_entropy4 Tsallis_Entropy0 Tsallis_Entropy1 Tsallis_Entropy2 Tsallis_Entropy3 Tsallis_Entropy4
Chan 1 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 0.99375 0.99375 0.99375 0.99375 0.99375
Chan 2 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 0.99375 0.99375 0.99375 0.99375 0.99375
Chan 3 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 0.99375 0.99375 0.99375 0.99375 0.99375
Chan 4 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 0.99375 0.99375 0.99375 0.99375 0.99375
Chan 5 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 0.99375 0.99375 0.99375 0.99375 0.99375
Chan 6 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 0.99375 0.99375 0.99375 0.99375 0.99375
Chan 7 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 7.321928 0.99375 0.99375 0.99375 0.99375 0.99375

fuzzy_entropy#

[12]:
fea_fuzzy_entropy = Feature(data, sfreq = 160, selected_funcs=['fuzzy_entropy'], funcs_params={"fuzzy_entropy__m":3})
n_epochs, n_channels, n_features = fea_fuzzy_entropy.features.shape
print(f'n_epochs:{n_epochs},n_channels:{n_channels},n_features:{n_features}')
df = pd.DataFrame(fea_fuzzy_entropy.features[0,:,:], columns=fea_fuzzy_entropy.feature_names)
df.index = [f'Chan {i+1}' for i in range(n_channels)]
df
n_epochs:45,n_channels:7,n_features:3
[12]:
fuzzy_entropy0 fuzzy_entropy1 fuzzy_entropy2
Chan 1 2.904107e-10 8.936084e-10 1.258567e-09
Chan 2 3.565500e-10 1.099328e-09 1.552804e-09
Chan 3 3.744488e-10 1.150933e-09 1.608585e-09
Chan 4 3.964832e-10 1.216509e-09 1.695435e-09
Chan 5 3.683379e-10 1.137737e-09 1.619108e-09
Chan 6 3.068711e-10 9.541093e-10 1.390436e-09
Chan 7 2.037004e-10 6.300999e-10 8.990770e-10

correlation_matrix#

various functional connectivity calculation

[ ]:
fea = Feature(data, sfreq=160, selected_funcs=['correlation_matrix'], funcs_params={"correlation_matrix__kind":"coh"})
[16]:
print(fea.features.shape)
fig, ax = plt.subplots(figsize=(8, 8),  facecolor="Black", subplot_kw=dict(polar=True))
plot_connectivity_circle(fea.features[0], [f'Chan {i+1}' for i in range(n_channels)],
                         title=f'coh function connectivity of motor image',
                    colormap="hot",  colorbar_size=0.35, colorbar_pos=(0, 0.5), ax=ax)
Connectivity computation...
only using indices for lower-triangular matrix
    computing connectivity for 0 connections
    using t=0.000s..3.200s for estimation (801 points)
    frequencies: 1.6Hz..124.8Hz (396 points)
    connectivity scores will be averaged for each band
    Using multitaper spectrum estimation with 7 DPSS windows
    the following metrics will be computed: Coherence
    computing cross-spectral density for epoch 1
feature connectivity jump
(45, 7, 7)
../_images/example_function_15_1.png
[16]:
(<Figure size 800x800 with 2 Axes>,
 <PolarAxes: title={'center': 'coh function connectivity of motor image'}>)

correlation_dimension#

[17]:
fea_cor_dim = Feature(data, sfreq=160,
               selected_funcs=['correlation_dimension'],
               funcs_params={"correlation_dimension__emb_dim":10})

print(fea_cor_dim.features.shape)

n_epochs, n_channels, n_features = fea_cor_dim.features.shape
print(f'n_epochs:{n_epochs},n_channels:{n_channels},n_features:{n_features}')

df = pd.DataFrame(fea_cor_dim.features[0,:,:], columns=fea_cor_dim.feature_names)
df.index = [f'Chan {i+1}' for i in range(n_channels)]
df
(45, 7, 1)
n_epochs:45,n_channels:7,n_features:1
[17]:
correlation_dimension
Chan 1 -1.512330e-15
Chan 2 5.425256e-02
Chan 3 9.946799e-17
Chan 4 2.335001e-15
Chan 5 2.038676e-15
Chan 6 8.864243e-16
Chan 7 -9.007042e-16

dispersion_entropy#

[18]:
fea_dis_entropy = Feature(data, sfreq=160,
               selected_funcs = ['dispersion_entropy'])

print(fea_dis_entropy.features.shape)

n_epochs, n_channels, n_features = fea_dis_entropy.features.shape
print(f'n_epochs:{n_epochs},n_channels:{n_channels},n_features:{n_features}')

df = pd.DataFrame(fea_dis_entropy.features[0,:,:], columns=fea_dis_entropy.feature_names)
df.index = [f'Chan {i+1}' for i in range(n_channels)]
df
(45, 7, 1)
n_epochs:45,n_channels:7,n_features:1
[18]:
dispersion_entropy
Chan 1 4.019468
Chan 2 4.031106
Chan 3 4.048759
Chan 4 4.062390
Chan 5 4.017145
Chan 6 3.998640
Chan 7 4.019405

detrended_fluctuation#

[20]:
fea_dfa = Feature(data, sfreq = 160,selected_funcs=['DFA'],funcs_params={"DFA__sfreq":160})
print(fea_dfa.features.shape)

n_epochs, n_channels, n_features = fea_dfa.features.shape
print(f'n_epochs:{n_epochs},n_channels:{n_channels},n_features:{n_features}')

df = pd.DataFrame(fea_dfa.features[0,:,:], columns=fea_dfa.feature_names)
df.index = [f'Chan {i+1}' for i in range(n_channels)]
df
(45, 7, 5)
n_epochs:45,n_channels:7,n_features:5
[20]:
DFA0 DFA1 DFA2 DFA3 DFA4
Chan 1 1.253521 1.256652 1.484165 1.462949 1.469708
Chan 2 1.337364 1.233087 1.455033 1.494680 1.438344
Chan 3 1.304206 1.246399 1.456391 1.443759 1.403382
Chan 4 1.346469 1.266654 1.425789 1.402753 1.389862
Chan 5 1.374896 1.243114 1.469548 1.444954 1.443983
Chan 6 1.385378 1.252074 1.503014 1.437226 1.491487
Chan 7 1.344746 1.221566 1.437606 1.422594 1.463696

alpha_asymetry#

calcualte only for “F3” and “F4”, feature for all channel is for use in the scuteegfe.

[ ]:
fea_alpha_asymetry = Feature(data=data[:5,:], sfreq=160,selected_funcs=['alpha_asymetry'],
               funcs_params={"alpha_asymetry__mode": "eeglab","alpha_asymetry__sfreq":160,
                             "alpha_asymetry__channel":['Fp1', 'Fp2', 'F7', 'F3', 'Fz', 'F4', 'F8']})
[28]:
print(f'alpha_asymetry is {fea_alpha_asymetry.features[:,0]} for all epochs')
alpha_asymetry is [[0.93169937]
 [0.72956181]
 [0.51149582]
 [0.4686063 ]
 [0.5005556 ]] for all epochs

wavelet_entropy#

[32]:
fea_wavelet_entropy = Feature(data=data[:5,:], sfreq=160, selected_funcs=['wavelet_entropy'],
               funcs_params={"wavelet_entropy__sfreq":160,
                             "wavelet_entropy__band":np.array([[2, 3.8], [4, 7], [8, 13], [14, 30], [31, 48]])})
[34]:
n_epochs, n_channels, n_features = fea_wavelet_entropy.features.shape
print(f'n_epochs:{n_epochs},n_channels:{n_channels},n_features:{n_features}')

df = pd.DataFrame(fea_wavelet_entropy.features[0,:,:], columns=fea_wavelet_entropy.feature_names)
df.index = [f'Chan {i+1}' for i in range(n_channels)]
df
n_epochs:5,n_channels:7,n_features:7
[34]:
wavelet_entropy0 wavelet_entropy1 wavelet_entropy2 wavelet_entropy3 wavelet_entropy4 wavelet_entropy5 wavelet_entropy6
Chan 1 -1.065854e-06 -0.000004 -0.000002 -7.472665e-07 -5.035163e-08 9.287985 33.252703
Chan 2 -1.287955e-06 -0.000004 -0.000003 -9.563370e-07 -6.311899e-08 10.101545 33.679903
Chan 3 -1.334464e-06 -0.000004 -0.000003 -9.772300e-07 -6.602383e-08 10.266217 33.867515
Chan 4 -1.437448e-06 -0.000004 -0.000003 -1.058710e-06 -7.177793e-08 10.542468 34.123227
Chan 5 -1.523095e-06 -0.000004 -0.000003 -1.044548e-06 -6.740835e-08 11.084058 34.309465
Chan 6 -1.349469e-06 -0.000004 -0.000003 -9.456489e-07 -5.863428e-08 12.161545 34.840794
Chan 7 -9.318971e-07 -0.000002 -0.000002 -6.041882e-07 -3.890704e-08 11.502448 34.871220