-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg_tfr_graph.py
179 lines (161 loc) · 7.7 KB
/
avg_tfr_graph.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
import mne
from mne.time_frequency import read_tfrs
import argparse
import pandas as pd
from os.path import isdir
import re
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
import pickle
import seaborn as sns
import matplotlib
font = {'weight' : 'bold',
'size' : 26}
matplotlib.rc('font', **font)
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
if isdir("/home/jev"):
root_dir = "/home/jev/hdd/sfb/"
elif isdir("/home/jeff"):
root_dir = "/home/jeff/hdd/jeff/sfb/"
elif isdir("/home/jeffhanna/"):
root_dir = "/scratch/jeffhanna/sfb/"
proc_dir = root_dir+"proc/"
img_dir = root_dir+"images/"
n_jobs = 8
chan = "central"
baseline = "zscore"
osc = "SO"
durs = ["30s","2m","5m"]
syncs = ["async", "sync"]
#syncs = ["sync"]
conds = ["sham","eig","fix"]
balance_conds = False
vmin, vmax = -2.5, 2.5
sync_titles = {"async":"non-synchronised", "sync":"synchronised",
"all":"all subjects"}
toi = .26
foi = 15
toi = None
foi = None
tfr = read_tfrs("{}grand_central_{}-tfr.h5".format(proc_dir, baseline))[0]
tfr = tfr["OscType=='{}' and PrePost=='Post'".format(osc)]
epo = mne.read_epochs(proc_dir+"grand_central-epo.fif")
epo = epo["OscType=='{}' and PrePost=='Post'".format(osc)]
epo.resample(tfr.info["sfreq"], n_jobs="cuda")
epo.crop(tmin=tfr.times[0], tmax=tfr.times[-1])
# calculate global ERP min and max for scaling later on
evo = epo.average()
ev_min, ev_max = evo.data.min(), evo.data.max()
mask = None
if toi != None and foi != None:
toi_idx = find_nearest(tfr.times, toi)
foi_idx = find_nearest(tfr.freqs, foi)
mask = np.zeros(tfr.data.shape[2:])
mask[...,foi_idx,toi_idx] = 1
for prepost in ["Pre", "Post"]:
for sync in syncs:
tfr = read_tfrs("{}grand_central_{}-tfr.h5".format(proc_dir, baseline))[0]
tfr = tfr["OscType=='{}' and PrePost=='{}'".format(osc, prepost)]
avg_fig, avg_axes = plt.subplots(3, 3, figsize=(38.4, 21.6))
std_fig, std_axes = plt.subplots(3, 3, figsize=(38.4, 21.6))
hist_data = {"Subj":[], "Cond":[], "TFR":[], "Dur":[]}
mods = []
for dur_idx,dur in enumerate(durs):
with open("{}main_fits_{}_cond_{}_{}_{}.pickle".format(proc_dir,baseline,osc,dur,sync), "rb") as f:
fits = pickle.load(f)
subjs = np.unique(tfr.metadata["Subj"].values)
col = "Cond"
bad_subjs = []
# if sync then remove all subjects recorded under asynchronous conditions (<31)
if sync == "sync":
for subj in list(subjs):
if int(subj) < 31:
bad_subjs.append(subj)
if sync == "async":
for subj in list(subjs):
if int(subj) >= 31:
bad_subjs.append(subj)
if balance_conds:
# check for missing conditions in each subject
for subj in subjs:
this_df = tfr.metadata.query("Subj=='{}'".format(subj))
these_conds = list(np.unique(this_df[col].values))
checks = [c in these_conds for c in list(np.unique(tfr.metadata[col].values))]
if not all(checks):
bad_subjs.append(subj)
# remove all subjects with missing conditions or not meeting synchronicity criterion
bad_subjs = list(set(bad_subjs))
for bs in bad_subjs:
print("Removing subject {}".format(bs))
tfr = tfr["Subj!='{}'".format(bs)]
epo = epo["Subj!='{}'".format(bs)]
subjs = np.unique(tfr.metadata["Subj"].values)
for cond_idx, cond in enumerate(conds):
# get osc ERP and normalise
evo = epo["Cond=='{}{}'".format(cond,dur)].average()
evo_data = evo.data
evo_data = (evo_data - ev_min) / (ev_max - ev_min)
evo_data = evo_data*3 + 13
this_tfr = tfr["Cond=='{}{}'".format(cond,dur)]
this_avg = this_tfr.average()
this_avg.plot(picks="central", axes=avg_axes[dur_idx][cond_idx],
colorbar=False, vmin=vmin, vmax=vmax, cmap="viridis",
mask=mask, mask_style="contour")
avg_axes[dur_idx][cond_idx].plot(tfr.times, evo_data[0,],
color="gray", alpha=0.8,
linewidth=10)
this_std = this_avg.copy()
this_std.data = this_tfr.data.std(axis=0)
this_std.plot(picks="central", axes=std_axes[dur_idx][cond_idx],
colorbar=False, vmin=0, vmax=15, cmap="hot",
mask=mask, mask_style="contour")
std_axes[dur_idx][cond_idx].plot(tfr.times, evo_data[0,],
color="gray", alpha=0.8,
linewidth=10)
if dur_idx == 0:
avg_axes[dur_idx][cond_idx].set_title("{}".format(cond))
std_axes[dur_idx][cond_idx].set_title("{}".format(cond))
if cond_idx == len(conds)-1:
avg_rax = avg_axes[dur_idx][cond_idx].twinx()
avg_rax.set_ylabel("{}".format(dur))
avg_rax.set_yticks([])
std_rax = std_axes[dur_idx][cond_idx].twinx()
std_rax.set_ylabel("{}".format(dur))
std_rax.set_yticks([])
if toi != None and foi != None:
for trial_idx in range(len(this_tfr.data)):
hist_data["Subj"].append(this_tfr.metadata["Subj"].iloc[trial_idx])
hist_data["Cond"].append(cond)
hist_data["Dur"].append(dur)
hist_data["TFR"].append(this_tfr.data[trial_idx,0,foi_idx,toi_idx])
mods.append(fits["fits"][foi_idx*toi_idx])
if toi != None and foi != None:
hist_data = pd.DataFrame.from_dict(hist_data)
for cond in conds:
this_hist_data = hist_data.query("Cond=='{}'".format(cond))
plt.figure(figsize=(38.4,21.6))
sns.histplot(data=this_hist_data, bins="auto", x="TFR", hue="Dur")
plt.title("TFR point value histogram, {} {} {}".format(osc, cond, sync_titles[sync]))
plt.savefig("../images/{}_{}_{}_hist.tif".format(osc, cond, sync))
plt.figure(figsize=(38.4,21.6))
sns.countplot("Subj", hue="Dur", order=list(subjs),
hue_order=["30s","2m","5m"], data=this_hist_data)
plt.title("Counts of {} {} {}".format(osc, cond, sync_titles[sync]))
plt.savefig("../images/{}_{}_{}_subjcount.tif".format(osc, cond, sync))
plt.figure(figsize=(38.4,21.6))
sns.countplot(x="Dur",data=this_hist_data)
plt.title("Counts of {} {} {}".format(osc, cond, sync_titles[sync]))
plt.savefig("../images/{}_{}_{}_count.tif".format(osc, cond, sync))
avg_fig.suptitle("Raw average {}, {}, {}".format(osc, sync_titles[sync], prepost))
avg_fig.savefig("../images/{}_{}_{}_rawavg.tif".format(osc, sync, prepost))
std_fig.suptitle("Standard deviation {}, {}, {}".format(osc, sync_titles[sync], prepost))
std_fig.savefig("../images/{}_{}_{}_rawstd.tif".format(osc, sync, prepost))
df = tfr.metadata.copy()
condlist = list(np.unique(df["Cond"].values))
trial_ns = {}
for cond in condlist:
trial_ns[cond] = len(df.query("Cond=='{}'".format(cond)))