-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_erp.py
228 lines (157 loc) · 7.02 KB
/
stats_erp.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from compute_phase_freq import erp_concat_job
from compute_resp_features import respiration_features_job
from bibliotheque import init_nan_da
from params import *
from configuration import *
import os
import ghibtools as gh
import jobtools
def get_N_resp_cycles(run_keys):
concat = []
for run_key in run_keys:
participant, session = run_key.split('_')
resp = respiration_features_job.get(run_key).to_dataframe()
concat.append(resp)
all_resp = pd.concat(concat)
N_cycles = all_resp.value_counts(subset = ['participant','session']).to_frame().reset_index().rename(columns ={'count':'N'}).set_index(['participant','session'])
return N_cycles
# GLOBAL
def global_erp_fig(chan, center, **p):
N_cycles = get_N_resp_cycles(run_keys)
N_cycles_pooled = N_cycles.groupby(['session']).sum(numeric_only = True)
all_erp = erp_concat_job.get(global_key)['erp_concat']
sessions = all_erp['session'].values
# COLORBAR POS
ax_x_start = 1.05
ax_x_width = 0.02
ax_y_start = 0
ax_y_height = 1
# FIGS TEXT PRELOAD VARIABLES
sup_fontsize=20
sup_pos=1.05
yticks = [4,8,12, p['max_freq']]
baseline_mode = p["baseline_mode"]
low_q_clim = p['delta_colorlim']
high_q_clim = 1 - p['delta_colorlim']
delta_clim= f'{low_q_clim} - {high_q_clim}'
clim_fontsize = 10
clim_title = f'Power\n({baseline_mode} vs baseline)\nDelta clim : {delta_clim}'
figsize = (15,7)
cmap = p['cmap']
global_erp = all_erp.mean('participant')
vmin = global_erp.quantile(low_q_clim)
vmax = global_erp.quantile(high_q_clim)
fig, axs = plt.subplots(ncols = len(sessions), figsize = figsize, constrained_layout = True)
suptitle = f'Mean ERP-like time-frequency map across {len(subject_keys)} subjects in electrode {chan} ({center})'
fig.suptitle(suptitle, fontsize = sup_fontsize, y = sup_pos)
for c, ses in enumerate(sessions):
ax = axs[c]
im = ax.pcolormesh(global_erp.coords['time'].values,
global_erp.coords['freq'].values,
global_erp.loc[ses, chan , center, : ,:].values,
cmap = cmap,
norm = 'linear',
vmin = vmin,
vmax = vmax
)
ax.set_yscale('log')
ax.set_yticks(ticks = yticks, labels = yticks)
ax.minorticks_off()
ax.set_xlabel('Time [sec]')
ax.axvline(x = 0, color = 'k', ls = '--', lw = 0.5)
N = N_cycles_pooled.loc[ses, 'N']
ax.set_title(f'{ses} - N : {N}')
cbar_ax = fig.add_axes([ax_x_start, ax_y_start, ax_x_width, ax_y_height])
clb = fig.colorbar(im, cax=cbar_ax)
clb.ax.set_title(clim_title,fontsize=clim_fontsize)
folder = base_folder / 'Figures' / 'ERP' / 'global' / center
fig.savefig(folder / f'{chan}.png', bbox_inches = 'tight', dpi = 100)
plt.close()
return xr.Dataset()
def test_global_fig_erp():
chan, center = ('P7','inspi_time')
ds = global_erp_fig(chan, center, **erp_fig_params)
print(ds)
global_erp_fig_job = jobtools.Job(precomputedir, 'global_erp_fig', erp_fig_params, global_erp_fig)
jobtools.register_job(global_erp_fig_job)
# BY SUBJECT
def subject_erp_fig(participant, chan, center, **p):
N_cycles = get_N_resp_cycles(run_keys)
N_cycles_pooled = N_cycles.groupby(['session']).sum(numeric_only = True)
all_erp = erp_concat_job.get(global_key)['erp_concat']
sessions = all_erp['session'].values
# COLORBAR POS
ax_x_start = 1.05
ax_x_width = 0.02
ax_y_start = 0
ax_y_height = 1
# FIGS TEXT PRELOAD VARIABLES
sup_fontsize=20
sup_pos=1.05
yticks = [4,8,12, p['max_freq']]
baseline_mode = p["baseline_mode"]
low_q_clim = p['delta_colorlim']
high_q_clim = 1 - p['delta_colorlim']
delta_clim= f'{low_q_clim} - {high_q_clim}'
clim_fontsize = 10
clim_title = f'Power\n({baseline_mode} vs baseline)\nDelta clim : {delta_clim}'
figsize = (15,7)
cmap = p['cmap']
# vmin = all_erp.quantile(low_q_clim)
# vmax = all_erp.quantile(high_q_clim)
vmin = all_erp.loc[participant,:,chan,:,:,:].quantile(low_q_clim)
vmax = all_erp.loc[participant,:,chan,:,:,:].quantile(high_q_clim)
fig, axs = plt.subplots(ncols = len(sessions), figsize = figsize, constrained_layout = True)
fig.suptitle(f'ERP-like time-frequency power map in {participant} in electrode {chan} ({center})', fontsize = sup_fontsize, y = sup_pos)
for c, ses in enumerate(sessions):
ax = axs[c]
im = ax.pcolormesh(all_erp.coords['time'].values,
all_erp.coords['freq'].values,
all_erp.loc[participant, ses, chan , center, : ,:].values,
cmap = cmap,
norm = 'linear',
vmin = vmin,
vmax = vmax
)
ax.set_yscale('log')
ax.set_yticks(ticks = yticks, labels = yticks)
ax.minorticks_off()
ax.set_xlabel('Time [sec]')
ax.axvline(x = 0, color = 'k', ls = '--', lw = 0.5)
N = N_cycles.loc[(participant,ses), 'N']
ax.set_title(f'{ses} - N : {N}')
cbar_ax = fig.add_axes([ax_x_start, ax_y_start, ax_x_width, ax_y_height])
clb = fig.colorbar(im, cax=cbar_ax)
clb.ax.set_title(clim_title,fontsize=clim_fontsize)
folder_path = base_folder / 'Figures' / 'ERP' / 'by_subject' / center / chan
fig.savefig(folder_path / f'{participant}.png', bbox_inches = 'tight')
plt.close()
return xr.Dataset()
def test_subject_erp_fig():
participant, chan , center = ('P01','P7','inspi_time')
ds = subject_erp_fig(participant, chan , center , **erp_fig_params)
print(ds)
subject_erp_fig_job = jobtools.Job(precomputedir, 'subject_erp_fig', erp_fig_params, subject_erp_fig)
jobtools.register_job(subject_erp_fig_job)
# COMPUTE
def compute_all():
chan_keys = power_params['chans']
centers = ['inspi_time','expi_time']
global_erp_keys = [(chan, center) for chan in chan_keys for center in centers]
jobtools.compute_job_list(global_erp_fig_job, global_erp_keys, force_recompute=True, engine='slurm',
slurm_params={'cpus-per-task':'20', 'mem':'30G', },
module_name='stats_erp',
)
subject_erp_keys = [(sub_key, chan_key, center) for center in centers for chan_key in chan_keys for sub_key in subject_keys]
jobtools.compute_job_list(subject_erp_fig_job, subject_erp_keys, force_recompute=True, engine='slurm',
slurm_params={'cpus-per-task':'20', 'mem':'30G', },
module_name='stats_erp',
)
if __name__ == '__main__':
# test_global_fig_erp()
# test_subject_erp_fig()
compute_all()