-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFigure3.py
334 lines (291 loc) · 8.87 KB
/
Figure3.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
import random
import sklearn
import seaborn as sns
import scipy as sp
from matplotlib import rc
import matplotlib.ticker as mtick
import matplotlib as mpl
from scipy import signal
from sklearn.linear_model import LinearRegression
from sklearn.impute import SimpleImputer
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import ElasticNetCV
from sklearn.datasets import make_regression
from sklearn.decomposition import PCA
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import Lasso
from sklearn.model_selection import train_test_split
import os
import glob
import pickle
import statsmodels.api as sm
from statsmodels.formula.api import ols
from statsmodels.tsa.stattools import acf
from statsmodels.stats.multicomp import pairwise_tukeyhsd
import statsmodels.formula.api as smf
from matplotlib.patches import Circle
from matplotlib.colors import ListedColormap
from matplotlib import cm
# from FileManagement import *
# from GlmModule import *
from DataProcessing import *
from Visualisation import *
from GeneralRunners import *
from PCA import *
from StatTests import *
from Controls import *
from Behaviour import GetChoicePerAnimal, GetReactionTime
saveDir = "C:\\TaskProject\\2pData\\"
if not os.path.isdir(saveDir):
os.makedirs(saveDir)
mpl.rcParams["pdf.fonttype"] = 42
mpl.rcParams["ps.fonttype"] = 42
mpl.rcParams["font.family"] = "Arial"
# Load Data
AllData, AllSubjects, AllSessions = LoadAllFiles("D:\\task_2p\\")
for i in range(17, 23):
AllData[i]["planeDelays"] = AllData[i]["planeDelays"].T
sessId, nId = GetNeuronDetails(AllData)
tups = np.vstack((sessId, nId)).T
tups = list(tuple(map(tuple, tups)))
with open(saveDir + "responsiveness.pickle", "rb") as f:
res = pickle.load(f)
resp = res["Resp"]
p = res["pVals"]
z = res["zScores"]
r = p <= 0.05 / 8
modelsBase = GetOneFunctionVarianceDifferentModels(
dataDir="D:\\Figures\\OneFunction_raw\\",
cutoff=0.00001,
shuffles=True,
resp=tups_,
)
#%% Panels A-D
plt.close("all")
reps = 10
N = 16 # len(AllData)
stateText = ["rand", "pupil", "go", "correct", "reward"]
predictionImprov = np.zeros((5, N)) * np.nan
dfs = []
i = 0
ms = []
control = None
shuffleNumber = 100
shuffleDist = np.zeros((4, shuffleNumber))
ress = []
# States = 2: pupil, 3: Action, 4: outcome, 5: reward
for p in [0, 2, 3, 4, 5]:
results, m = PredictionPerformanceSpecificPopulation(
testStat=p,
reps=reps,
split=0.5,
controlStat=control,
C=5,
controlState=0,
preSplit=(False),
)
results = results[
[0, 1, 2, 3, 4, 5, 6, 7, 11, 15, 16, 17, 18, 19, 20, 21], :, :
]
ms.append(m)
# results/=np.tile(np.max(np.max(results,2),1),(results.shape[-1],results.shape[-2],1)).T #normalised to highest
Id = np.tile(np.arange(N), (reps, 2, 1)).T
Reward = np.tile(np.tile([0, 1], (N, 1)).T, (reps, 1, 1)).T
# # NeuronNum = np.tile(np.tile([1,5,10,25,50,100,1000],(500,1)).T,(16,2,1,1))
RepsNum = np.tile(np.arange(reps), (N, 2, 1))
dfs.append(
pd.DataFrame(
{
"Animal": Id.flatten(),
"Reward": Reward.flatten(),
"RepNum": RepsNum.flatten(),
"score": results.flatten(),
}
)
)
f, ax = plt.subplots(1)
Id = np.tile(np.arange(N), (2, 1)).T
Reward = np.tile(np.tile([0, 1], (N, 1)).T, (1, 1)).T
d_tmp = pd.DataFrame(
{
"Animal": Id.flatten(),
"Reward": Reward.flatten(),
"score": np.nanmean(results, -1).flatten(),
}
)
ax.scatter(
np.nanmean(results, -1)[:, 0],
np.nanmean(results, -1)[:, 1],
c="k",
edgecolors="black",
)
r = sp.stats.ttest_rel(
np.nanmean(results, -1)[:, 0],
np.nanmean(results, -1)[:, 1],
nan_policy="omit",
)
ress.append(r)
ax.plot(np.arange(-1, 1, 0.1), np.arange(-1, 1, 0.1), "k--")
ax.set_aspect(1.0 / ax.get_data_ratio(), adjustable="box")
f.set_size_inches(8, 8)
ax.set_ylim(-0.2, 1)
ax.set_xlim(-0.2, 1)
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
# res = results[:,1,:]-results[:,0,:]
res = np.nanmean(results, -1)
res = res[:, 1] - res[:, 0]
predictionImprov[i, :] = res
i += 1
allDataSets = pd.concat(dfs[1:])
allTexts = []
for i in range(1, len(stateText)):
allTexts.append(np.tile(stateText[i], (len(dfs[i]), 1)))
alltext = np.vstack(allTexts)
allDataSets["State"] = alltext
#%% Panel E
plt.close("all")
reps = 10
N = 16 # len(AllData)
stateText = ["rand", "pupil", "go", "correct", "reward"]
predictionImprov = np.zeros((5, N)) * np.nan
dfs = []
i = 0
ms = []
control = 2
shuffleNumber = 100
shuffleDist = np.zeros((4, shuffleNumber))
ress = []
# States = 2: pupil, 3: Action, 4: outcome, 5: reward
for p in [2, 3, 4]:
for cs in [0, 1]:
results, m = PredictionPerformanceSpecificPopulation(
testStat=5,
reps=reps,
split=0.5,
controlStat=p,
C=5,
controlState=cs,
preSplit=(False),
)
results = results[
[0, 1, 2, 3, 4, 5, 6, 7, 11, 15, 16, 17, 18, 19, 20, 21], :, :
]
ms.append(m)
# results/=np.tile(np.max(np.max(results,2),1),(results.shape[-1],results.shape[-2],1)).T #normalised to highest
Id = np.tile(np.arange(N), (reps, 2, 1)).T
Reward = np.tile(np.tile([0, 1], (N, 1)).T, (reps, 1, 1)).T
# # NeuronNum = np.tile(np.tile([1,5,10,25,50,100,1000],(500,1)).T,(16,2,1,1))
RepsNum = np.tile(np.arange(reps), (N, 2, 1))
dfs.append(
pd.DataFrame(
{
"Animal": Id.flatten(),
"Reward": Reward.flatten(),
"RepNum": RepsNum.flatten(),
"score": results.flatten(),
}
)
)
f, ax = plt.subplots(1)
Id = np.tile(np.arange(N), (2, 1)).T
Reward = np.tile(np.tile([0, 1], (N, 1)).T, (1, 1)).T
d_tmp = pd.DataFrame(
{
"Animal": Id.flatten(),
"Reward": Reward.flatten(),
"score": np.nanmean(results, -1).flatten(),
}
)
ax.scatter(
np.nanmean(results, -1)[:, 0],
np.nanmean(results, -1)[:, 1],
c="k",
edgecolors="black",
)
ress.append(r)
ax.plot(np.arange(-1, 1, 0.1), np.arange(-1, 1, 0.1), "k--")
ax.set_aspect(1.0 / ax.get_data_ratio(), adjustable="box")
f.set_size_inches(8, 8)
ax.set_ylim(-0.2, 1)
ax.set_xlim(-0.2, 1)
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
# res = results[:,1,:]-results[:,0,:]
res = np.nanmean(results, -1)
res = res[:, 1] - res[:, 0]
# predictionImprov[i, :] = res
i += 1
#%% Panel F + G
s, c, p, shuff = PredictCorrectness(AllData[:16], opt=1)
plt.close("all")
shuffScore = np.nanpercentile(shuff, 95)
so = s[s > shuffScore]
su = s[s <= shuffScore]
cvalid = c[s > shuffScore, :]
pvalid = p[s > shuffScore, :]
f, ax = plt.subplots(1)
ax.scatter(np.zeros_like(so), so, c="k", s=60)
ax.scatter(np.zeros_like(su), su, c="grey", s=60)
ax.hlines(shuffScore, -0.5, 0.5, "k")
ax.set_xlim(-2, 2)
# ax.set_ylim(-0.1,0.45)
ax.set_ylabel("MCC")
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
f.set_size_inches(8, 8)
pabs = pvalid.copy()
pabs[pvalid < 0.05] = 1
pabs[pvalid >= 0.05] = 0
coefTitles = np.tile(
[
"Baseline",
"Zero",
"L",
"R",
"Feedback N-1",
"Feedback N-2",
"Feedback N-3",
],
(pvalid.shape[0], 1),
)
[0, 0.125, 0.25, 0.5, 0.75, 1]
df = pd.DataFrame(
{
"coefs": cvalid.flatten(),
"param": coefTitles.flatten(),
"pval": pabs.flatten(),
}
)
f1, ax1 = plt.subplots(1)
ax1.hlines(0, -100, 100, "k", linestyles="dashed")
sns.stripplot(
data=df,
x="param",
y="coefs",
hue="pval",
ax=ax1,
palette=["grey", "black"],
s=10,
)
sns.boxplot(
data=df,
x="param",
y="coefs",
ax=ax1,
palette=["black"],
medianprops={"visible": False},
whiskerprops={"visible": False},
showfliers=False,
showbox=False,
showcaps=False,
showmeans=True,
meanline=True,
meanprops={"color": (0.4, 0.4, 0.4), "ls": "-", "lw": 2},
)
ax1.spines["right"].set_visible(False)
ax1.spines["top"].set_visible(False)
f1.set_size_inches(8, 8)