eeyhsong / EEG-Conformer

EEG Transformer 2.0. i. Convolutional Transformer for EEG Decoding. ii. Novel visualization - Class Activation Topography.
GNU General Public License v3.0
389 stars 55 forks source link

Transforming of original data #6

Closed niwolpert closed 1 year ago

niwolpert commented 1 year ago

It's not clear to me how the original data sets (for example the .gdf files from the motor imagery task) were transformed into the specific format of the .mat files that are called in the script. I would very much appreciate if you could share either the code for the transformation or the transformed data directly (which the script can be directly called on).

anishmrao commented 1 year ago

I'm facing the same problem. Would appreciate it if you could share the data and/or the script.

eeyhsong commented 1 year ago

hi @niwolpert, @anishmrao, You can refer to https://github.com/eeyhsong/EEG-Transformer/blob/main/getData.m 🤝

niwolpert commented 1 year ago

Thanks a lot!

anishmrao commented 1 year ago

Adding python code to convert GDF and MAT files of the BCI Competition IV Dataset 2a if anyone needs it:

import scipy.io as scio
import os
import mne
import numpy as np

SUBJECTS_BCI_IV = ['A{0:02d}'.format(i) for i in range(1, 10)]
data_path = PATH_TO_GDF_AND_MAT_FILES
result_path = PATH_TO_STORE_PREPROCESSED_MATS
session_types = ['T', 'E']

if(not os.path.exists(result_path)):
    os.makedirs(result_path)

for name in SUBJECTS_BCI_IV:
    for session_type in session_types:
        label = scio.loadmat(os.path.join(data_path, name + session_type + '.mat'))['classlabel']
        data = mne.io.read_raw_gdf(os.path.join(data_path, name + session_type + '.gdf'))

        events = mne.events_from_annotations(data)
        epochs = mne.Epochs(data, events[0], event_id=events[1]['768'], tmin=0, tmax=4, baseline=None, detrend=None, preload=True) # Create epochs with start event (code=768) as trigger
        epoched_data = epochs.get_data()

        assert epoched_data.shape[0] == label.shape[0], "Trials and label counts do not match"

        epoched_data = np.nan_to_num(epoched_data)
        epoched_data = epoched_data[:, :22, :1000] # Keep only EEG channels
        epoched_data = np.transpose(epoched_data, (2, 1, 0)) # Expected shape for downstream code

        mat = {'data': epoched_data, 'label': label}
        scio.savemat(os.path.join(result_path, name + session_type + ".mat"), mat)

        print("Finished processing", name + session_type)

print("Finished processing all subjects.")
doukAndy commented 1 year ago

Adding python code to convert GDF and MAT files of the BCI Competition IV Dataset 2a if anyone needs it:

import scipy.io as scio
import os
import mne
import numpy as np

SUBJECTS_BCI_IV = ['A{0:02d}'.format(i) for i in range(1, 10)]
data_path = PATH_TO_GDF_AND_MAT_FILES
result_path = PATH_TO_STORE_PREPROCESSED_MATS
session_types = ['T', 'E']

if(not os.path.exists(result_path)):
    os.makedirs(result_path)

for name in SUBJECTS_BCI_IV:
    for session_type in session_types:
        label = scio.loadmat(os.path.join(data_path, name + session_type + '.mat'))['classlabel']
        data = mne.io.read_raw_gdf(os.path.join(data_path, name + session_type + '.gdf'))

        events = mne.events_from_annotations(data)
        epochs = mne.Epochs(data, events[0], event_id=events[1]['768'], tmin=0, tmax=4, baseline=None, detrend=None, preload=True) # Create epochs with start event (code=768) as trigger
        epoched_data = epochs.get_data()

        assert epoched_data.shape[0] == label.shape[0], "Trials and label counts do not match"

        epoched_data = np.nan_to_num(epoched_data)
        epoched_data = epoched_data[:, :22, :1000] # Keep only EEG channels
        epoched_data = np.transpose(epoched_data, (2, 1, 0)) # Expected shape for downstream code

        mat = {'data': epoched_data, 'label': label}
        scio.savemat(os.path.join(result_path, name + session_type + ".mat"), mat)

        print("Finished processing", name + session_type)

print("Finished processing all subjects.")

@anishmrao Hello! sry to bother, could you please tell me where to get the MAT files of the BCI Competition IV Dataset 2a? I can only get access to the GDF files on http://www.bbci.de/competition/iv/ (>-<) Thanks a lot!!!!!!!!

anishmrao commented 1 year ago

Hi @doukAndy , you can find the true label mats near the bottom of this page: https://www.bbci.de/competition/iv/results/#top

doukAndy commented 1 year ago

Found it! Thanks a lot!!!!!!! @anishmrao