Skip to content
Snippets Groups Projects
Commit 5a0dec23 authored by s200431's avatar s200431
Browse files

local changes to preamble and preprocessing to load eeg data on server

parent 1ad2894b
No related branches found
No related tags found
No related merge requests found
...@@ -29,14 +29,15 @@ import statsmodels # multipletest ...@@ -29,14 +29,15 @@ import statsmodels # multipletest
import fooof # Peak Alpha Freq and 1/f exponents import fooof # Peak Alpha Freq and 1/f exponents
import pandas as pd # Dataframes import pandas as pd # Dataframes
import seaborn as sns # Plotting library import seaborn as sns # Plotting library
# import autoreject # Automatic EEG artifact detection from h5io import read_hdf5, write_hdf5
import autoreject # Automatic EEG artifact detection
import mlxtend # Sequential Forward Selection import mlxtend # Sequential Forward Selection
from mne.time_frequency import * from mne.time_frequency import *
from mne.preprocessing import (ICA, create_eog_epochs, create_ecg_epochs, corrmap) from mne.preprocessing import (ICA, create_eog_epochs, create_ecg_epochs, corrmap)
from mne.stats import spatio_temporal_cluster_test, permutation_cluster_test from mne.stats import spatio_temporal_cluster_test, permutation_cluster_test
from mne.channels import find_ch_adjacency from mne.channels import find_ch_adjacency
from mne.connectivity import spectral_connectivity #from mne.connectivity import spectral_connectivity
import nitime.analysis as nta import nitime.analysis as nta
import nitime.timeseries as nts import nitime.timeseries as nts
...@@ -61,6 +62,7 @@ from matplotlib import cm ...@@ -61,6 +62,7 @@ from matplotlib import cm
from statsmodels.tsa.stattools import adfuller from statsmodels.tsa.stattools import adfuller
from statsmodels.formula.api import mixedlm from statsmodels.formula.api import mixedlm
from h5io import read_hdf5, write_hdf5
from autoreject import AutoReject from autoreject import AutoReject
......
...@@ -25,33 +25,46 @@ Link to the demonstration data: www.bci2000.org ...@@ -25,33 +25,46 @@ Link to the demonstration data: www.bci2000.org
# Set working directory # Set working directory
import os import os
wkdir = "/Users/benj3542/Desktop/Uni/Noter/Semester_6/Bachelor/resting-state-eeg-analysis/" wkdir = "/home/s200431"
os.chdir(wkdir) os.chdir(wkdir)
# Load all libraries from the Preamble # Load all libraries from the Preamble
from Preamble import * from Preamble import *
from mne.datasets import eegbci
# To demonstrate the script, a publically available EEG dataset are used # To demonstrate the script, a publically available EEG dataset are used
# EEG recordings from 2 subjects are used as an example # EEG recordings from 2 subjects are used as an example
# The EEGBCI200 is task based, but we will treat it as "resting-state" # The EEGBCI200 is task based, but we will treat it as "resting-state"
# And the 2 runs as Eyes Closed and Eyes Open # And the 2 runs as Eyes Closed and Eyes Open
n_subjects = 2
Subject_id = [1,2] n_subjects = 78
Subject_id = list(range(1,n_subjects+1))
# Download and get filenames # Download and get filenames
from mne.datasets import eegbci data_path = "/data/may2020/QEEG"
files = [] files = []
for i in range(n_subjects): for r, d, f in os.walk(data_path):
raw_fnames = eegbci.load_data(Subject_id[i], [1,2]) # The first 2 runs for file in f:
files.append(raw_fnames) files.append(os.path.join(r,file))
# # Original code to get filenames in a folder files = [files[i:i+2] for i in range(0, len(files), 2)]
# data_path = "EEG_folder"
# # Get filenames
# files = [] """
# for r, d, f in os.walk(data_path): n_subjects = 51
# for file in f: Subject_id = list(range(1,n_subjects+1))
# if ".bdf" in file: # Original code to get filenames in a folder
# files.append(os.path.join(r, file)) data_path = "/data/raw/FOR_DTU/rawEEGforDTU"
# Get filenames
holder = []
files = []
for r, d, f in os.walk(data_path):
for file in f:
if ".bdf" in file:
holder.append(os.path.join(r, file))
files.append(holder)
holder = []
files.pop(0)
"""
# Eye status # Eye status
anno_to_event = {'Eyes Closed': 1, 'Eyes Open': 2} # manually defined event id anno_to_event = {'Eyes Closed': 1, 'Eyes Open': 2} # manually defined event id
...@@ -66,13 +79,13 @@ n_trials = 2 # 1 eyes closed followed by 1 eyes open ...@@ -66,13 +79,13 @@ n_trials = 2 # 1 eyes closed followed by 1 eyes open
# Montage settings # Montage settings
montage = mne.channels.make_standard_montage('standard_1005') montage = mne.channels.make_standard_montage('standard_1005')
#montage = mne.channels.read_custom_montage(filename) # custom montage file #montage = mne.channels.read_custom_montage(filename) # custom montage file
#mne.io.read_raw_eeglab(files[0][1], preload = True)
# %% Load, filter and epoch (Steps 1 to 5) # %% Load, filter and epoch (Steps 1 to 5)
# Pre-allocate memory # Pre-allocate memory
epochs = [0]*n_subjects epochs = [0]*n_subjects
for i in range(n_subjects): for i in range(n_subjects):
# MNE python supports many EEG formats. Make sure to use the proper one # MNE python supports many EEG formats. Make sure to use the proper one
raw = mne.io.concatenate_raws([mne.io.read_raw_edf(f, preload=True) for f in files[i]]) raw = mne.io.concatenate_raws([mne.io.read_raw_bdf(f,preload = True) for f in files[i]])
# Fix EEGBCI channel names # Fix EEGBCI channel names
eegbci.standardize(raw) eegbci.standardize(raw)
# Set montage # Set montage
...@@ -250,10 +263,13 @@ artifacts[1] = [0, 2] # eye blinks, eye movement ...@@ -250,10 +263,13 @@ artifacts[1] = [0, 2] # eye blinks, eye movement
corrected_epochs = cleaned_epochs.copy() corrected_epochs = cleaned_epochs.copy()
for n in range(len(cleaned_epochs)): for n in range(len(cleaned_epochs)):
try:
# Define the components with artifacts # Define the components with artifacts
ica[n].exclude = artifacts[n] ica[n].exclude = artifacts[n]
# Remove on corrected data # Remove on corrected data
ica[n].apply(corrected_epochs[n].load_data()) ica[n].apply(corrected_epochs[n].load_data())
except:
print("An error occurred")
# Inspect how the ICA worked # Inspect how the ICA worked
n=0; corrected_epochs[n].plot(scalings=200e-6, n_epochs = 10) n=0; corrected_epochs[n].plot(scalings=200e-6, n_epochs = 10)
......
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment