forked from neuromodulation/icn_bursts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathberlin_pipeline.py
195 lines (170 loc) · 5.65 KB
/
berlin_pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from typing import Union
import runpy
from turtle import done
import pandas as pd
from bids import BIDSLayout
import mne_bids
import matplotlib.pyplot as plt
import seaborn as sns
from src import pipeline, plot_utils, preprocessing, postprocessing
import seaborn as sns
import numpy as np
from scipy.stats import wilcoxon
# SCRIPT START #
def main():
"""Run this script."""
# Load project constants
project_constants = runpy.run_path("berlin_constants.py")
data_path = project_constants["data_path"]
path_bids = project_constants["PATH_BIDS"]
m1_ids = project_constants["M1_IDS"]
new_ch_names_map = project_constants["NEW_CH_NAMES_MAP"]
files = project_constants["files"]
remove_subjects: Union[str, None] = ["001", "002"] #'010']
if remove_subjects:
for remove_subject in remove_subjects:
files = [file for file in files if remove_subject not in file]
files = preprocessing.pick_runs(files)
files_x = [f for f in files if "010" in f]
# Define variables
burst_char_pd_all = []
M1_burst_dynamics_all = []
npow_list_all = []
# Process runs #
for path_run in files:
entities = mne_bids.get_entities_from_fname(path_run)
sub = entities["subject"]
session = entities["session"]
task = entities["task"]
acquisition = entities["acquisition"]
run = entities["run"]
med = "On" if "MedOn" in entities["session"] else "Off"
(burst_char_pd, M1_burst_dynamics, npow,) = pipeline.bursts_single_run(
path_run=path_run,
path_bids=path_bids,
sub=sub,
m1=m1_ids[sub],
new_ch_names=new_ch_names_map[sub],
med=med,
session=session,
task=task,
acquisition=acquisition,
run=run,
)
burst_char_pd["Subject"] = sub
burst_char_pd["Medication"] = med
burst_char_pd["Run"] = run
M1_burst_dynamics["Subject"] = sub
M1_burst_dynamics["Medication"] = med
M1_burst_dynamics["Run"] = run
npow["Subject"] = sub
npow["Medication"] = med
npow["Run"] = run
burst_char_pd_all.append(burst_char_pd)
M1_burst_dynamics_all.append(M1_burst_dynamics)
npow_list_all.append(npow)
return (
burst_char_pd_all,
M1_burst_dynamics_all,
npow_list_all,
)
if __name__ == "__main__":
(burst_char_pd_all, M1_burst_dynamics_all, npow_list_all,) = main()
# Structure Results in DataFrame
features = pd.concat(burst_char_pd_all)
dist = pd.concat(M1_burst_dynamics_all)
psd = pd.concat(npow_list_all)
#print("done")
# Average Runs (multiple subs)
avg_features = postprocessing.avg_features_sub(burst_char_pd_all)
df_gavg_dist, df_sub_dist = postprocessing.avg_distribution(M1_burst_dynamics_all)
plot_utils.plot_avgm1_burst_features(avg_features)
plot_utils.plot_distribution(df_gavg_dist, df_sub_dist)
print("done")
on = features[features['Medication']=='On']
off = features[features['Medication']=='Off']
duration_on = on['Duration (s)']
duration_off = off['Duration (s)']
res = wilcoxon(duration_off, duration_on)
res.statistic, res.pvalue
print("done")
# Plot Distribution single subject
dist3 = df_sub_dist[df_sub_dist['Subject']==3]
dist4 = df_sub_dist[df_sub_dist['Subject']==4]
dist5 = df_sub_dist[df_sub_dist['Subject']==5]
dist6 = df_sub_dist[df_sub_dist['Subject']==6]
dist7 = df_sub_dist[df_sub_dist['Subject']==7]
dist8 = df_sub_dist[df_sub_dist['Subject']==8]
dist9 = df_sub_dist[df_sub_dist['Subject']==9]
dist10 = df_sub_dist[df_sub_dist['Subject']==10]
dist11 = df_sub_dist[df_sub_dist['Subject']==11]
dist12 = df_sub_dist[df_sub_dist['Subject']==12]
dist13 = df_sub_dist[df_sub_dist['Subject']==13]
dist14 = df_sub_dist[df_sub_dist['Subject']==14]
dist15 = df_sub_dist[df_sub_dist['Subject']==15]
#plot_utils.plot_distribution_sub3(dist3)
#plot_utils.plot_distribution_sub4(dist4)
#plot_utils.plot_distribution_sub5(dist5)
#plot_utils.plot_distribution_sub6(dist6)
#plot_utils.plot_distribution_sub7(dist7)
#plot_utils.plot_distribution_sub8(dist8)
#plot_utils.plot_distribution_sub9(dist9)
#plot_utils.plot_distribution_sub10(dist10)
#plot_utils.plot_distribution_sub11(dist11)
#plot_utils.plot_distribution_sub12(dist12)
#plot_utils.plot_distribution_sub13(dist13)
#plot_utils.plot_distribution_sub14(dist14)
#plot_utils.plot_distribution_sub15(dist15)
(
psd_s3off,
psd_s3on,
psd_s4off,
psd_s4on,
psd_s5off,
psd_s5on,
psd_s6off,
psd_s6on,
psd_s7off,
psd_s7on,
psd_s8off,
psd_s8on,
psd_s9off,
psd_s9on,
psd_s10off,
psd_s10on,
psd_s11off,
psd_s11on,
psd_s12off,
psd_s12on,
psd_s13off,
psd_s13on,
psd_s14off,
psd_s14on,
psd_s15off,
psd_s15on,
psd_off,
psd_on,
) = postprocessing.arrange_psd(npow_list_all)
# PLOTS #
# Features
plot_utils.plot_avgm1_burst_features(avg_features)
plot_utils.plot_m1_burst_features(features)
# Distribution of Duration
plot_utils.plot_distribution(df_gavg_dist, df_sub_dist)
print("done")
# PSD
plot_utils.plot_gavg_psd(psd_off, psd_on)
plot_utils.plot_psd_s3(psd_s3off, psd_s3on)
plot_utils.plot_psd_s4(psd_s4off, psd_s4on)
plot_utils.plot_psd_s5(psd_s5off, psd_s5on)
plot_utils.plot_psd_s6(psd_s6off, psd_s6on)
plot_utils.plot_psd_s7(psd_s7off, psd_s7on)
plot_utils.plot_psd_s8(psd_s8off, psd_s8on)
plot_utils.plot_psd_s9(psd_s9off, psd_s9on)
plot_utils.plot_psd_s10(psd_s10off, psd_s10on)
plot_utils.plot_psd_s11(psd_s11off, psd_s11on)
plot_utils.plot_psd_s12(psd_s12off, psd_s12on)
plot_utils.plot_psd_s13(psd_s13off, psd_s13on)
plot_utils.plot_psd_s14(psd_s14off, psd_s14on)
plot_utils.plot_psd_s15(psd_s15off, psd_s15on)
print("done")