-
Notifications
You must be signed in to change notification settings - Fork 0
/
MI_infer.py
141 lines (129 loc) · 6.48 KB
/
MI_infer.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
from os.path import isdir
import pandas as pd
import numpy as np
import seaborn as sns
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
plt.ion()
import matplotlib
font = {'weight' : 'bold',
'size' : 40}
matplotlib.rc('font', **font)
import warnings
warnings.filterwarnings("ignore")
def plot_werr(df, df_bs, x_order, hue_order, width=.3):
fig, axis = plt.subplots(1,1, figsize=(21.6, 21.6))
xtick_arr = np.arange(len(x_order))
xticks = {"ticks":xtick_arr, "labels":x_order}
for hue_idx, hue in enumerate(hue_order):
df_h = df.query("Duration=='{}'".format(hue))
df_bs_h = df_bs.query("Duration=='{}'".format(hue))
vals = [df_h.query("Stimulation=='{}'".format(xo))["PAC"].values[0] for xo in x_order]
vals_bs = [df_bs_h.query("Stimulation=='{}'".format(xo))["PAC"].values for xo in x_order]
vals_bs = np.array(vals_bs)
ci_low = np.quantile(vals_bs, 0.025, axis=1)
ci_high = np.quantile(vals_bs, 0.975, axis=1)
plt.bar(xtick_arr + width*(hue_idx-1), vals, width=width, label=hue,
yerr=ci_high-ci_low)
plt.xticks(**xticks)
return fig, axis
if isdir("/home/jev"):
root_dir = "/home/jev/hdd/sfb/"
elif isdir("/home/jeff"):
root_dir = "/home/jeff/hdd/jeff/sfb/"
proc_dir = root_dir+"proc/"
method= "wavelet"
baseline = "nobl"
baseline = "zscore"
time_win = (275,600)
#time_win = (50,600)
freq_win = (12, 15)
bootstrap = "load"
infile = "{}ModIdx_{}_{}_{}-{}Hz_{}-{}ms.pickle".format(proc_dir, method,
baseline, *freq_win,
*time_win)
print(infile)
df = pd.read_pickle(infile)
for osc in ["SO"]:
for var in ["ND", "MVL", "PLV"]:
fig, ax = plt.subplots(figsize=(38.4, 21.6))
this_df = df.query("OscType=='{}'".format(osc))
sns.barplot(data=this_df, y=var, x="StimType", hue="Dur",
order=["sham", "eig", "fix"], hue_order=["30s", "2m", "5m"],
ax=ax)
plt.suptitle("{} {} ({} transformed)".format(osc, var, method))
plt.savefig("../images/{}_{}_{}".format(var, osc, method))
vc_form = {"Subj": "0 + C(Subj)"}
re_form = "0 + Stim*Dur"
#re_form = None
formula = "{} ~ C(StimType, Treatment('sham'))*C(Dur, Treatment('30s'))".format(var)
mod = smf.mixedlm(formula, data=this_df, groups=this_df["Sync"],
re_formula=re_form, vc_formula=vc_form)
mf = mod.fit(reml=False)
print(mf.summary())
# # model predictions
# predict_df_dict = {"Stimulation":[],"Duration":[],"PAC":[]}
# exog_names = mf.model.exog_names
# for st in ["sham", "eig", "fix"]:
# for dur in ["30s", "2m", "5m"]:
# pac = mf.predict(exog={"StimType":[st], "Dur":[dur]}).values[0]
#
# predict_df_dict["PAC"].append(pac)
# if st == "sham":
# predict_df_dict["Stimulation"].append("Sham")
# elif st == 'eig':
# predict_df_dict["Stimulation"].append("Eigen")
# elif st == 'fix':
# predict_df_dict["Stimulation"].append("Fixed")
# predict_df_dict["Duration"].append(dur)
# predict_df = pd.DataFrame.from_dict(predict_df_dict)
#
# if bootstrap == "load":
# filename = "{}{}_{}_{}_{}-{}_{}-{}_predict_bs.pickle".format(proc_dir,
# var, osc,
# method,
# *freq_win,
# *time_win)
# predict_bs = pd.read_pickle(filename)
# elif type(bootstrap) == int:
# predict_bs_dict = {"Stimulation":[],"Duration":[],"PAC":[]}
# subjs = list(np.unique(this_df["Subj"].values))
# for bs_idx in range(bootstrap):
# print("Bootstrap iteration {} of {}".format(bs_idx, bootstrap))
# this_df_bs = this_df.copy()
# for subj in subjs:
# subj_inds = this_df_bs["Subj"]==subj
# vals = this_df_bs[subj_inds][var].values
# inds = np.random.randint(len(vals), size=len(vals))
# this_df_bs.loc[subj_inds,var] = vals[inds]
#
# mod_bs = smf.mixedlm(formula, data=this_df_bs, groups=this_df_bs["Sync"], re_formula=re_form, vc_formula=vc_form)
# mf_bs = mod_bs.fit(reml=False)
# exog_names = mf_bs.model.exog_names
# for st in ["sham", "eig", "fix"]:
# for dur in ["30s", "2m", "5m"]:
# pac = mf_bs.predict(exog={"StimType":[st],
# "Dur":[dur]}).values[0]
# predict_bs_dict["PAC"].append(pac)
# if st == "sham":
# predict_bs_dict["Stimulation"].append("Sham")
# elif st == 'eig':
# predict_bs_dict["Stimulation"].append("Eigen")
# elif st == 'fix':
# predict_bs_dict["Stimulation"].append("Fixed")
# predict_bs_dict["Duration"].append(dur)
# predict_bs = pd.DataFrame.from_dict(predict_bs_dict)
# predict_bs.to_pickle("{}{}_{}_{}_{}-{}_{}-{}_predict_bs.pickle".format(proc_dir, var, osc, method, *freq_win, *time_win))
#
# fig, ax = plot_werr(predict_df, predict_bs, ["Sham", "Eigen", "Fixed"],
# ["30s", "2m", "5m"])
# plt.ylim((0.15, 0.23))
# ax.text(2, 0.22, "*", fontsize=75)
# #plt.legend()
# plt.ylabel("normalised direct PAC", fontsize=44)
# plt.suptitle("{} PAC LME model predictions, {}-{}Hz, {}-{}ms".format(osc, freq_win[0],
# freq_win[1],
# time_win[0],
# time_win[1]))
# plt.savefig("../images/{}_{}_{}_{}-{}_{}-{}_predict.png".format(var, osc, method, *freq_win, *time_win))
# plt.savefig("../images/{}_{}_{}_{}-{}_{}-{}_predict.svg".format(var, osc, method, *freq_win, *time_win))