-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fig4.py
292 lines (218 loc) · 7.92 KB
/
Fig4.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from __future__ import division
from matplotlib.pylab import *
import scipy.io as io
import glob
import sys
from scipy.stats import *
import seaborn as sns
from scikits import bootstrap
from scipy.signal import detrend
from scipy.io import loadmat
find = lambda x: where(x)[0]
def sig_bar(sigs,axis,y,color):
w=diff(axis)[0]
for s in sigs:
beg =axis[s]
end = beg+w
fill_between([beg,end],[y[0],y[0]],[y[1],y[1]],color=color)
sns.set_style("ticks")
sns.set_context("talk", font_scale=1.25)
sns.set_style({"ytick.direction": "in"})
sns.set_style({"xtick.direction": "in"})
## WOLFF DATA
imponset =0.25 + 0.95;
all_files = sort(glob.glob("Data/Wolff2017_concat/exp2_d1*.mat"))
all_files2 = sort(glob.glob("Data/Wolff2017_concat/exp2_d2*.mat"))
f=io.loadmat(all_files[0])
time = f["data1"]["time"][0][0][0]
imp_i = argmin(abs(time-imponset))
s_vars = []
for f,file in enumerate(all_files):
f1=io.loadmat(file)
f2=io.loadmat(all_files2[f])
data1 = f1["data1"]["trial"][0][0]
data1 = data1 - np.mean(data1,1)[:,None,:]
data2 = f2["data2"]["trial"][0][0]
data2 = data2 - np.mean(data2,1)[:,None,:]
data = concatenate((data1,data2))
var=std(data,0)**2
s_vars.append(mean(var,0))
s_vars_dt = detrend(s_vars,1)
base_line_dt = mean(s_vars_dt[:,imp_i-100:imp_i],1)
base_line = mean(array(s_vars)[:,imp_i-100:imp_i],1)
s_vars_dt_rel = [(s_vars_dt[:,t] - base_line_dt)/base_line * 100 for t in range(len(time))]
m_var_st = mean(s_vars_dt_rel,1)
stderr_st = 2*std(s_vars_dt_rel,1)/sqrt(len(s_vars_dt_rel[0]))
low_st = m_var_st-stderr_st
high_st = m_var_st+stderr_st
ps_st = array([ttest_1samp(v,0)[1] for v in s_vars_dt_rel])
# Wolff 2015 data
all_files = glob.glob("Data/Wolff2015/original/eeg_*.mat")
f=io.loadmat(all_files[0])
time2015 = io.loadmat("Data/Wolff2015/time.mat")['t'][0]
s_vars2015 = []
for file in all_files:
f=io.loadmat(file)
data2015 = f['eeg']
data2015 = data2015 - np.mean(data2015,1)[:,None,:]
var2015=std(data2015,0)**2
s_vars2015.append(mean(var2015,0))
s_vars_2015_dt = detrend(s_vars2015,1)
base_line2015 = mean(array(s_vars2015)[:,25:50],1)
base_line2015_dt = mean(s_vars_2015_dt[:,25:50],1)
s_vars_2015_rel = [(s_vars_2015_dt[:,t] - base_line2015_dt)/base_line2015 * 100 for t in range(len(time2015))]
m_var2015 = mean(s_vars_2015_rel,1)
stderr2015 = std(s_vars_2015_rel,1)/sqrt(len(s_vars_2015_rel[0]))
low2015 = m_var2015-2*stderr2015
high2015 = m_var2015+2*stderr2015
ps_st2015 = array([ttest_1samp(v,0)[1] for v in s_vars_2015_rel])
## ROSE DATA
file_path = "Data/Rose et al/TMS_Data/Exp2"
all_files = glob.glob(file_path+"/*.mat")
#subjs = unique([int(a.split("/")[4].split("_")[0]) for a in all_files])
subjs = [104, 105, 106, 107, 108, 111]
all_vars = []
all_subjs = []
for sub in subjs:
all_data = []
files = glob.glob("%s/%s*.mat" % (file_path,sub))
s_vars = []
s_w_vars = []
for file in files:
f=io.loadmat(file)
good_chan = f["goodchannels"][0]-1
data = f["data"]
times = f["times"][0]
d=data[good_chan]
all_data.append(d)
m_sens = mean(d,0)
d = [d1-m_sens for d1 in d]
var = std(d,-1)**2
baseline_tms = mean(var[:,:200:250],1)
norm_var = [(var[:,t] - baseline_tms) / baseline_tms * 100 for t in range(len(times))]
s_vars.append(mean(norm_var,1))
all_vars.append(mean(s_vars,0))
all_subjs.append(s_vars)
# del(all_subjs[1][7])
# del(all_subjs[1][2])
# del(all_subjs[1][1])
# del(all_subjs[2][6])
# del(all_subjs[2][3])
# del(all_subjs[2][1])
all_vars = np.concatenate(all_subjs)
ps_rose = array([ttest_1samp(v,0)[1] for v in array(all_vars).T])
m_var =mean(all_vars,0)
stderr = 2*std(all_vars,0)/sqrt(len(all_vars))
low = m_var-stderr
high = m_var+stderr
m_fr = loadtxt("simulations_for_plot/wolff_sims_fr.txt")
time_stk = loadtxt("simulations_for_plot/wolff_sims_time.txt")
time_stk -= 0.3/2
m_fr = m_fr[mean(m_fr,1)>0]
baseline = mean(std(m_fr[:,:5],0)/mean(m_fr[:,:5],0))
idx = bootstrap.bootstrap_indexes(m_fr)
diff_ff_nostp = loadtxt("simulations_for_plot/diff_ff_sim_nostp.txt")
time_nostp = loadtxt("simulations_for_plot/time_sim_nostp.txt")
boot = [(std(m_fr[i],0)/mean(m_fr[i],0) - baseline) for i in idx]
#### var split
# wolff 2015 trial by trial analyses
root_dir = "Data/Wolff2015/"
time2015 = io.loadmat("Data/Wolff2015/time.mat")['t'][0]
eegs_imp=[]
# select decoding peak, but allow for a small jitter of +- 6bins
T = 6
idx_imp=range(147-T,147+T)
for sub in range(1,25):
dec_bins_imp=[]
eeg_bins_imp=[]
for bin_n in [1,2]:
dec_pairs_imp = []
eeg_pairs_imp = []
for pair in [1,2]:
# Impulse, same as delay but for impulse period
# load single-trial decoders
files=glob.glob(root_dir+"maha*pair%ibin%i*Impulse%i.mat" % (pair, bin_n,sub))[0]
dec_imp=loadmat(files)["d"]
# select point where to make the split
m_dec_imp = mean(dec_imp[:,idx_imp],1)
#m_dec_imp = amax(dec_imp[:,:200],1)
# do median split
l_idx = m_dec_imp < percentile(m_dec_imp,50)
h_idx = m_dec_imp > percentile(m_dec_imp,50)
# load trial-by-trial voltage traces
files=glob.glob(root_dir+"eeg*pair%ibin%i*Impulse%i.mat" % (pair, bin_n,sub))[0]
eeg_imp=loadmat(files)["dat1_dat2"]
# compute variability for each median split
eeg_imp_low_dec=std(eeg_imp[l_idx],0)
eeg_imp_high_dec=std(eeg_imp[h_idx],0)
# compute the difference
diff_imp = mean(eeg_imp_low_dec,0) - mean(eeg_imp_high_dec,0)
eeg_pairs_imp.append(diff_imp)
eeg_bins_imp.append(mean(eeg_pairs_imp,0))
eegs_imp.append(mean(eeg_bins_imp,0))
ci_split = np.array([bootstrap.ci(e,n_samples=1000) for e in np.array(eegs_imp).T])
stderr_split = np.array([bootstrap.ci(e,n_samples=1000,alpha=1-0.69) for e in np.array(eegs_imp).T])
plt.figure(figsize=(10,10))
subplot(2,2,1)
title("Wolff et al (2017)\nWolff et al (2015)",pad=10)
fill_between(time-imponset,low_st,high_st,alpha=0.5,color="gray")
#plot(time-imponset,(array(s_vars_dt)),"k",alpha=0.15)
plot(time-imponset,m_var_st,"gray",lw=2)
plot(time-imponset,zeros(len(time)),"--",color="black")
fill_between(time2015,low2015,high2015,alpha=0.5,color="black")
plot(time2015, m_var2015, "black")
sig_bar(find(ps_st<0.005),time-imponset,[40*0.95,40],"gray")
sig_bar(find(ps_st2015<0.005),time2015,[40*0.94*0.94,40*0.94],"black")
xlim(-0.2,.4)
ylim(-40,40)
yticks([-40,-20, 0,20,40])
ylabel(r"$\Delta$" "variance (%)")
xlim(-0.2,0.5)
xticks([0,0.5],[])
subplot(2,2,2)
title("Rose et al (2018)",pad=10)
fill_between(times/1000,low,high,alpha=0.5,color="k")
#plot(times/1000,(array(all_vars)).T,"k",alpha=0.2)
plot(times/1000,m_var,"k",lw=2)
plot(times/1000,zeros(len(times)),"k--")
sig_bar(find(ps_rose<0.005),times/1000,[75*0.95,75],"k")
ylim(-25,75)
ylabel("% of " r"$\Delta$" "variance")
yticks([-25,0,25,50,75])
xlim(-0.2,0.5)
xticks([0,0.5],[])
subplot(2,2,3)
title("variability median-split by \n reactivation decoding (2015)",pad=10)
sig_bar(find(ci_split[:,0]>0),time2015,[0.5*0.94,0.5],"black")
fill_between(time2015,ci_split[:,0],ci_split[:,1],alpha=0.5,color="black")
plot(time2015, mean(eegs_imp,0),color="black")
plot([time2015[0],time2015[-1]],[0,0],"k--")
plot(0.38799999999999996,0,"*")
xlim(-0.2, 0.5)
ylim(-0.5, 0.5)
xticks([0,0.5],["0", ".5"])
yticks([-0.5, 0,0.5])
xlabel("time from impulse (s)")
ax1 = subplot(2,2,4)
title("Model",pad=10)
fill_between(time_stk,mean(boot,0)-std(boot,0)*2,mean(boot,0)+std(boot,0)*2,alpha=0.5,color="k")
plot(time_stk,mean(boot,0),"k",lw=2,label="reactivations")
ylabel(r"$\Delta$""fano-factor")
ylim([-2,2])
yticks([-2,-1, 0, 1, 2])
plot(time_stk,zeros(len(time_stk)),"k--")
xlim(-0.2,0.5)
xticks([0,0.5],["0", ".5"])
xlabel("time from impulse (s)")
ax2 = ax1.twinx()
ax2.plot(time_nostp-2,np.percentile(diff_ff_nostp,50,0),color="gray",label="no reactivations")
ax2.fill_between(time_nostp-2,np.percentile(diff_ff_nostp,5,0),np.percentile(diff_ff_nostp,95,0),alpha=0.5,color="gray")
ax2.set_ylim([-.2,.2])
ax2.tick_params(axis='y', labelcolor="gray")
sns.despine(right=False)
plt.tight_layout()
#ax1.legend(frameon=False)
#ax2.legend(frameon=False)
savefig("figures/var_col.png",dpi=300)
savefig("figures/var_col.svg",dpi=300)
plt.show()