Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Resting-state EEG analysis
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
s200431
Resting-state EEG analysis
Commits
699c82d7
Commit
699c82d7
authored
Mar 14, 2023
by
s200431
Browse files
Options
Downloads
Plain Diff
Merge remote-tracking branch 'refs/remotes/origin/master'
parents
519d755c
bbddfde7
No related branches found
No related tags found
No related merge requests found
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
FeatureEstimation.py
+8
-4
8 additions, 4 deletions
FeatureEstimation.py
Main.py
+14492
-0
14492 additions, 0 deletions
Main.py
Preamble.py
+5
-2
5 additions, 2 deletions
Preamble.py
Preprocessing.py
+43
-14
43 additions, 14 deletions
Preprocessing.py
with
14548 additions
and
20 deletions
FeatureEstimation.py
+
8
−
4
View file @
699c82d7
...
...
@@ -36,14 +36,14 @@ the different parts easier (Default shortcut: Ctrl + Shift + O)
# Set working directory
import
os
wkdir
=
"
/
Users/benj3542/Desktop/Uni/Noter/Semester_6/Bachelor/resting-state-eeg-analysis/
"
wkdir
=
"
/
home/s200431
"
os
.
chdir
(
wkdir
)
# Load all libraries from the Preamble
from
Preamble
import
*
# %% Load preprocessed epochs and questionnaire data
load_path
=
"
.
/PreprocessedData
"
load_path
=
"
home/s200431
/PreprocessedData
"
# Get filenames
files
=
[]
...
...
@@ -79,6 +79,9 @@ Drop_epochs_df = Drop_epochs_df.sort_values(by=["Subject_ID"]).reset_index(drop=
### Load questionnaire data
# For the purposes of this demonstration I will make a dummy dataframe
# The original code imported csv files with questionnaire data and group status
final_qdf
=
pd
.
read_csv
(
r
'
/data/raw/FOR_DTU/Questionnaires_for_DTU.csv
'
)
"""
final_qdf = pd.DataFrame({
"
Subject_ID
"
:Subject_id,
"
Age
"
:[23,26],
"
Gender
"
:[0,0],
...
...
@@ -87,10 +90,11 @@ final_qdf = pd.DataFrame({"Subject_ID":Subject_id,
"
Q1
"
:[1.2, 2.3],
"
Q2
"
:[1.7, 1.5],
"
Qn
"
:[2.1,1.0]})
"""
# Define cases as >= 44 total PCL
# Type: numpy array with subject id
cases
=
np
.
array
(
final_qdf
[
"
Subject_ID
"
][
final_qdf
[
"
PCL_t
otal
"
]
>=
44
])
cases
=
np
.
array
(
final_qdf
[
"
Subject_ID
"
][
final_qdf
[
"
PCL_t
7
"
]
>=
44
])
n_groups
=
2
Groups
=
[
"
CTRL
"
,
"
PTSD
"
]
...
...
@@ -887,7 +891,7 @@ for i in range(len(order)):
maps
[
i
][
m
]
*=
sign_swap
[
i
][
m
]
# Plot the maps and save
save_path
=
"
/
Users/benj3542/Desktop/Uni/Noter/Semester_6/Bachelor/resting-state-eeg-analysis
/Figures/Microstates
"
save_path
=
"
/
home/s200431
/Figures/Microstates
"
labels
=
[
"
EC
"
,
"
EO
"
]
for
i
in
range
(
len
(
labels
)):
fig
,
axarr
=
plt
.
subplots
(
1
,
n_maps
,
figsize
=
(
20
,
5
))
...
...
This diff is collapsed.
Click to expand it.
Main.py
0 → 100755
+
14492
−
0
View file @
699c82d7
This diff is collapsed.
Click to expand it.
Preamble.py
+
5
−
2
View file @
699c82d7
...
...
@@ -29,14 +29,16 @@ import statsmodels # multipletest
import
fooof
# Peak Alpha Freq and 1/f exponents
import
pandas
as
pd
# Dataframes
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
from
mne.time_frequency
import
*
from
mne.time_frequency
import
psd_multitaper
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.channels
import
find_ch_adjacency
from
mne.connectivity
import
spectral_connectivity
#
from mne.connectivity import spectral_connectivity
import
nitime.analysis
as
nta
import
nitime.timeseries
as
nts
...
...
@@ -61,6 +63,7 @@ from matplotlib import cm
from
statsmodels.tsa.stattools
import
adfuller
from
statsmodels.formula.api
import
mixedlm
from
h5io
import
read_hdf5
,
write_hdf5
from
autoreject
import
AutoReject
...
...
This diff is collapsed.
Click to expand it.
Preprocessing.py
+
43
−
14
View file @
699c82d7
...
...
@@ -25,7 +25,7 @@ Link to the demonstration data: www.bci2000.org
# Set working directory
import
os
wkdir
=
"
/
Users/benj3542/Desktop/Uni/Noter/Semester_6/Bachelor/resting-state-eeg-analysis
/
"
wkdir
=
"
/
home/s200431
/
"
os
.
chdir
(
wkdir
)
# Load all libraries from the Preamble
...
...
@@ -36,10 +36,27 @@ from mne.datasets import eegbci
# EEG recordings from 2 subjects are used as an example
# The EEGBCI200 is task based, but we will treat it as "resting-state"
# And the 2 runs as Eyes Closed and Eyes Open
n_subjects
=
2
Subject_id
=
[
1
,
2
]
n_subjects
=
91
Subject_id
=
list
(
range
(
52
,
n_subjects
+
52
))
# Download and get filenames
data_path
=
"
/data/may2020/QEEG
"
files
=
[]
for
r
,
d
,
f
in
os
.
walk
(
data_path
):
for
file
in
f
:
files
.
append
(
os
.
path
.
join
(
r
,
file
))
files
=
[
files
[
i
:
i
+
2
]
for
i
in
range
(
0
,
len
(
files
),
2
)]
"""
n_subjects = 51
Subject_id = list(range(1,n_subjects+1))
# Original code to get filenames in a folder
data_path =
"
/data/raw/FOR_DTU/rawEEGforDTU
"
from mne.datasets import eegbci
files = []
for i in range(n_subjects):
...
...
@@ -47,18 +64,26 @@ for i in range(n_subjects):
files.append(raw_fnames)
"""
# Original code to get filenames in a folder
data_path =
"
/Users/benj3542/Desktop/Uni/Noter/Semester_6/Bachelor/Test_bdf
"
data_path
=
"
/data/sep2020
"
"""
# 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))
)
if
"
.
set
"
in file:
holder.append(os.path.join(r, file))
files.append(holder)
holder = []
files.pop(0)
"""
files
=
[]
for
r
,
d
,
f
in
os
.
walk
(
data_path
):
for
file
in
f
:
if
"
.set
"
in
file
:
files
.
append
(
os
.
path
.
join
(
r
,
file
))
# Eye status
anno_to_event
=
{
'
Eyes Closed
'
:
1
,
'
Eyes Open
'
:
2
}
# manually defined event id
eye_status
=
list
(
anno_to_event
.
keys
())
...
...
@@ -70,15 +95,16 @@ n_epochs_trial = int(60/epoch_len) # number of epochs in a trial
n_trials
=
2
# 1 eyes closed followed by 1 eyes open
# Montage settings
montage
=
mne
.
channels
.
make_standard_montage
(
'
standard_1005
'
)
#montage = mne.channels.read_custom_montage(filename) # custom montage file
fname
=
"
/home/glia/Analysis/Channel_locations/SMARTING_fixed.loc
"
#montage = mne.channels.make_standard_montage('standard_1005')
montage
=
mne
.
channels
.
read_custom_montage
(
fname
)
# custom montage file
#mne.io.read_raw_eeglab(files[0][1], preload = True)
# %% Load, filter and epoch (Steps 1 to 5)
# Pre-allocate memory
epochs
=
[
0
]
*
n_subjects
for
i
in
range
(
n_subjects
):
# 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
.
read_raw_eeglab
(
files
[
i
],
preload
=
True
,
verbose
=
0
)
# Fix EEGBCI channel names
eegbci
.
standardize
(
raw
)
# Set montage
...
...
@@ -256,10 +282,13 @@ artifacts[1] = [0, 2] # eye blinks, eye movement
corrected_epochs
=
cleaned_epochs
.
copy
()
for
n
in
range
(
len
(
cleaned_epochs
)):
try
:
# Define the components with artifacts
ica
[
n
].
exclude
=
artifacts
[
n
]
# Remove on corrected data
ica
[
n
].
apply
(
corrected_epochs
[
n
].
load_data
())
except
:
print
(
"
An error occurred
"
)
# Inspect how the ICA worked
n
=
0
;
corrected_epochs
[
n
].
plot
(
scalings
=
200e-6
,
n_epochs
=
10
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment