Analysis Code of Subject 22 (Adult)

import os
import numpy as np
import mne
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (5, 5)
cd /directory_of_sub_01
/directory_of_sub_01_eeg_data
ls
sub-22_ses-01_task-hfo_channels.tsv*  sub-22_ses-01_task-hfo_eeg.edf*
sub22_first_run = "sub-22_ses-01_task-hfo_eeg.edf"
raw = mne.io.read_raw_edf(sub22_first_run, preload=True)
raw._data=raw.get_data()*1e-6
Extracting EDF parameters from /mnt/f/Uni_Marburg/Zweites_Semester/Methodisches_Arbeiten_PM/Datasets/Dataset of EEG recordings of pediatric patients with epilepsy based on the 10-20 system/sub-22/ses-01/eeg/sub-22_ses-01_task-hfo_eeg.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
Reading 0 ... 11059199  =      0.000 ... 10799.999 secs...
raw.get_data()
array([[3.75010682e-05, 3.45800532e-05, 3.28097411e-05, ...,
        9.55964184e-06, 6.84516325e-06, 5.87149158e-06],
       [6.38852745e-05, 6.41776563e-05, 6.76862374e-05, ...,
        5.95713835e-06, 2.52165268e-06, 4.78761132e-06],
       [1.98203926e-05, 1.91324864e-05, 2.26580054e-05, ...,
        3.40729482e-05, 3.29980948e-05, 3.12568324e-05],
       ...,
       [4.44341196e-05, 4.70772159e-05, 5.04683583e-05, ...,
        1.00738677e-05, 5.48585150e-06, 6.33363710e-06],
       [2.38147075e-05, 2.74557944e-05, 3.57526974e-05, ...,
        1.58162542e-05, 1.59953241e-05, 1.62937738e-05],
       [2.01063230e-05, 1.97308200e-05, 2.21545212e-05, ...,
        2.07890558e-05, 1.54296039e-05, 1.16063006e-05]])
print(raw)
<RawEDF | sub-22_ses-01_task-hfo_eeg.edf, 23 x 11059200 (10800.0 s), ~1.90 GB, data loaded>
print(raw.info)
<Info | 7 non-empty values
 bads: []
 ch_names: Fp1, A2, Fp2, F7, F3, Fz, F4, F8, T3, C3, Cz, C4, T4, T5, P3, ...
 chs: 23 EEG
 custom_ref_applied: False
 highpass: 0.0 Hz
 lowpass: 512.0 Hz
 meas_date: 2021-02-28 23:10:01 UTC
 nchan: 23
 projs: []
 sfreq: 1024.0 Hz
>
raw.ch_names
raw.set_channel_types(dict(zip(raw.ch_names, ['eeg' if ch not in {'T1', 'T2'} else 'misc' for ch in raw.ch_names])))
raw=raw.set_eeg_reference(['A1', 'A2'])
EEG channel type selected for re-referencing
Applying a custom EEG reference.
print(raw)
<RawEDF | sub-22_ses-01_task-hfo_eeg.edf, 23 x 11059200 (10800.0 s), ~1.90 GB, data loaded>
%matplotlib notebook
raw.plot(n_channels=23, scalings=dict(eeg=200e-6));
raw.notch_filter(60)
Setting up band-stop filter from 59 - 61 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandstop filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 59.35
- Lower transition bandwidth: 0.50 Hz (-6 dB cutoff frequency: 59.10 Hz)
- Upper passband edge: 60.65 Hz
- Upper transition bandwidth: 0.50 Hz (-6 dB cutoff frequency: 60.90 Hz)
- Filter length: 6759 samples (6.601 sec)
Measurement date February 28, 2021 23:10:01 GMT
Experimenter Unknown
Participant Unknown
Digitized points Not available
Good channels 0 magnetometer, 0 gradiometer, and 21 EEG channels
Bad channels
EOG channels Not available
ECG channels Not available
Sampling frequency 1024.00 Hz
Highpass 0.00 Hz
Lowpass 512.00 Hz
Filenames sub-22_ses-01_task-hfo_eeg.edf
Duration 02:59:59 (HH:MM:SS)
raw.filter(l_freq=0.1, h_freq=40.0, method='fir', phase='zero', fir_window='hamming', fir_design='firwin')
raw.plot_psd(average=True) #für Farbe "False"
raw.plot(n_channels=23);
Filtering raw data in 1 contiguous segment
Setting up band-pass filter from 0.1 - 40 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 0.10
- Lower transition bandwidth: 0.10 Hz (-6 dB cutoff frequency: 0.05 Hz)
- Upper passband edge: 40.00 Hz
- Upper transition bandwidth: 10.00 Hz (-6 dB cutoff frequency: 45.00 Hz)
- Filter length: 33793 samples (33.001 sec)
Effective window size : 2.000 (s)
raw.info['sfreq']
1024.0
raw.plot(n_channels=23, scalings=dict(eeg=100e-6));
print(raw.info['chs'])
[{'cal': 1.0, 'logno': 1, 'scanno': 1, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'Fp1', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 2, 'scanno': 2, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'A2', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 3, 'scanno': 3, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'Fp2', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 4, 'scanno': 4, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'F7', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 5, 'scanno': 5, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'F3', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 6, 'scanno': 6, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'Fz', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 7, 'scanno': 7, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'F4', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 8, 'scanno': 8, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'F8', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 9, 'scanno': 9, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'T3', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 10, 'scanno': 10, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'C3', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 11, 'scanno': 11, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'Cz', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 12, 'scanno': 12, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'C4', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 13, 'scanno': 13, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'T4', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 14, 'scanno': 14, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'T5', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 15, 'scanno': 15, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'P3', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 16, 'scanno': 16, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'Pz', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 17, 'scanno': 17, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'P4', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 18, 'scanno': 18, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'T6', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 19, 'scanno': 19, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'O1', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 20, 'scanno': 20, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'A1', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 21, 'scanno': 21, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'O2', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 1 (FIFFV_COIL_EEG), 'kind': 2 (FIFFV_EEG_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 22, 'scanno': 22, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'T1', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 0 (FIFFV_COIL_NONE), 'kind': 502 (FIFFV_MISC_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}, {'cal': 1.0, 'logno': 23, 'scanno': 23, 'range': 1.0, 'unit_mul': 0 (FIFF_UNITM_NONE), 'ch_name': 'T2', 'unit': 107 (FIFF_UNIT_V), 'coord_frame': 4 (FIFFV_COORD_HEAD), 'coil_type': 0 (FIFFV_COIL_NONE), 'kind': 502 (FIFFV_MISC_CH), 'loc': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}]
ten_twenty_montage = mne.channels.make_standard_montage('standard_1020')
print(ten_twenty_montage)
<DigMontage | 0 extras (headshape), 0 HPIs, 3 fiducials, 94 channels>
fig = ten_twenty_montage.plot(kind='3d')
ten_twenty_montage.plot(kind='topomap', show_names=True);
4 duplicate electrode labels found:
T7/T3, T8/T4, P7/T5, P8/T6
Plotting 90 unique labels.
Creating RawArray with float64 data, n_channels=90, n_times=1
    Range : 0 ... 0 =      0.000 ...     0.000 secs
Ready.
4 duplicate electrode labels found:
T7/T3, T8/T4, P7/T5, P8/T6
Plotting 90 unique labels.
Creating RawArray with float64 data, n_channels=90, n_times=1
    Range : 0 ... 0 =      0.000 ...     0.000 secs
Ready.
raw.set_montage(ten_twenty_montage)
Measurement date February 28, 2021 23:10:01 GMT
Experimenter Unknown
Participant Unknown
Digitized points 24 points
Good channels 0 magnetometer, 0 gradiometer, and 21 EEG channels
Bad channels
EOG channels Not available
ECG channels Not available
Sampling frequency 1024.00 Hz
Highpass 0.10 Hz
Lowpass 40.00 Hz
Filenames sub-22_ses-01_task-hfo_eeg.edf
Duration 02:59:59 (HH:MM:SS)
# set up and fit the ICA   
from mne.preprocessing import ICA
num_components = 8 
ica = ICA(n_components=num_components, method='fastica')
raw_copy=raw.copy()
raw_copy=raw_copy.filter(l_freq=1.0, h_freq=None)
ica.fit(raw_copy)
<ipython-input-20-e8cc57d15613>:4: DeprecationWarning: Version 0.23 introduced max_iter="auto", setting max_iter=1000 for `fastica` and max_iter=500 for `infomax` and `picard`. The current default of max_iter=200 will be changed to "auto" in version 0.24.
  ica = ICA(n_components=num_components, method='fastica')
Filtering raw data in 1 contiguous segment
Setting up high-pass filter at 1 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal highpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 1.00
- Lower transition bandwidth: 1.00 Hz (-6 dB cutoff frequency: 0.50 Hz)
- Filter length: 3381 samples (3.302 sec)

Fitting ICA to data using 21 channels (please be patient, this may take a while)
Selecting by number: 8 components
Fitting ICA took 300.2s.
<ICA | raw data decomposition, fit (fastica): 11059200 samples, 8 components, channels used: "eeg">
ica.plot_components(); 
ica.plot_overlay(raw); 
Applying ICA to Raw instance
    Transforming to ICA space (8 components)
    Zeroing out 0 ICA components
    Projecting back using 21 PCA components
ica.apply(raw)
Applying ICA to Raw instance
    Transforming to ICA space (8 components)
    Zeroing out 0 ICA components
    Projecting back using 21 PCA components
Measurement date February 28, 2021 23:10:01 GMT
Experimenter Unknown
Participant Unknown
Digitized points 24 points
Good channels 0 magnetometer, 0 gradiometer, and 21 EEG channels
Bad channels
EOG channels Not available
ECG channels Not available
Sampling frequency 1024.00 Hz
Highpass 0.10 Hz
Lowpass 40.00 Hz
Filenames sub-22_ses-01_task-hfo_eeg.edf
Duration 02:59:59 (HH:MM:SS)
raw.plot(scalings=dict(eeg=100e-6));
from mne import Epochs
from mne import make_fixed_length_epochs
epochs = make_fixed_length_epochs(raw, duration = 2.0, preload = True)
epochs.resample(500.0)
Not setting metadata
Not setting metadata
5400 matching events found
No baseline correction applied
0 projection items activated
Loading data for 5400 events and 2048 original time points ...
0 bad epochs dropped
Number of events 5400
Events 1: 5400
Time range 0.000 – 1.998 sec
Baseline off
#epochs.plot_psd_topomap(ch_type = 'eeg', normalize = True); 
epochs.plot_psd_topomap(ch_type = 'eeg', normalize = True, bands = [(30,45,"Gamma")], vlim=(0.0, 0.1)); #lower, higher, name; min, max 
    Using multitaper spectrum estimation with 7 DPSS windows