-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_base_end.py
404 lines (390 loc) · 15.3 KB
/
plot_base_end.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import pdb
import sys
import os
from os.path import isfile
import numpy as np
import pickle
import time
import shutil
import warnings
import h5py
import argparse
import matplotlib.pyplot as plt
import multiprocessing as mp
from scipy.stats import zscore
from sklearn import preprocessing
import networkx as nx
from networkx.algorithms import community
from mpl_toolkits.axes_grid1 import make_axes_locatable
from ExpGTE import ExpGTE
from utils_cabmi import *
from plotting_functions import *
from analysis_functions import *
from utils_gte import *
from utils_clustering import *
def count_experiments():
"""
Counts the number of experiments and animals for which a reward end GTE
file is created.
"""
num_ITs = 0
num_PTs = 0
num_IT_base = 0
num_IT_expend = 0
num_PT_base = 0
num_PT_expend = 0
processed_dir = './processed/'
for animal_dir in os.listdir(processed_dir):
animal_path = processed_dir + animal_dir + '/'
if not os.path.isdir(animal_path):
continue
include_animal = False
num_baseline = 0
num_expend = 0
for day_dir in os.listdir(animal_path):
day_path = animal_path + day_dir + '/'
baseline_path = day_path + 'baseline.p'
expend_path = day_path + 'experiment_end.p'
if os.path.isfile(baseline_path):
num_baseline += 1
include_animal = True
if os.path.isfile(expend_path):
num_expend += 1
include_animal = True
if include_animal:
if animal_dir.startswith('IT'):
num_ITs +=1
num_IT_base += num_baseline
num_IT_expend += num_expend
else:
num_PTs += 1
num_PT_base += num_baseline
num_PT_expend += num_expend
print('Number of ITs: ' + str(num_ITs))
print('Baselines: ' + str(num_IT_base))
print('Experiment Ends: ' + str(num_IT_expend))
print()
print('Number of PTs: ' + str(num_PTs))
print('Baselines: ' + str(num_PT_base))
print('Experiment Ends: ' + str(num_PT_expend))
def plot_learning():
"""
Comparing info. transfer in baseline vs experiment end for learning vs
non-learning days.
"""
learning_baseline = OnlineNormalEstimator()
learning_expend = OnlineNormalEstimator()
non_learning_baseline = OnlineNormalEstimator()
non_learning_expend = OnlineNormalEstimator()
num_learning = 0
num_non_learning = 0
processed_dir = './processed/'
for animal_dir in os.listdir(processed_dir):
animal_path = processed_dir + animal_dir + '/'
if not os.path.isdir(animal_path):
continue
for day_dir in os.listdir(animal_path):
day_path = animal_path + day_dir + '/'
baseline_path = day_path + 'baseline.p'
expend_path = day_path + 'experiment_end.p'
if not isfile(baseline_path) or not isfile(expend_path):
continue
with open(baseline_path, 'rb') as f:
baseline = pickle.load(f)[0]
with open(expend_path, 'rb') as f:
expend = pickle.load(f)[0]
baseline = [val for val in baseline.flatten() if not np.isnan(val)]
expend = [val for val in expend.flatten() if not np.isnan(val)]
try:
_, _, reg = learning_params('./', animal_dir, day_dir, bin_size=5)
except: # In case another process is already accessing this file
continue
slope = reg.coef_[0]
if slope < 0.2:
for val in baseline:
non_learning_baseline.handle(val)
for val in expend:
non_learning_expend.handle(val)
num_non_learning += 1
if slope > 0.4:
for val in baseline:
learning_baseline.handle(val)
for val in expend:
learning_expend.handle(val)
num_learning += 1
means = [
learning_baseline.mean(), learning_expend.mean(),
non_learning_baseline.mean(), non_learning_expend.mean()
]
stds = [
learning_baseline.std(), learning_expend.std(),
non_learning_baseline.std(), non_learning_expend.std()
]
fig, ax = plt.subplots(1, 1, figsize=(10,5))
labels = [
'Learning: Baseline\n(%d Total)'%num_learning,
'Learning: Exp End\n(%d Total)'%num_learning,
'Non-Learning: Baseline\n(%d Total)'%num_non_learning,
'Non-Learning: Exp End\n(%d Total)'%num_non_learning
]
x_pos = np.arange(len(labels))
ax.bar(x_pos, means, yerr=stds, align='center',
alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Information Transfer')
ax.set_xticks(x_pos)
ax.set_xticklabels(labels)
ax.set_title('GTE Distribution')
ax.yaxis.grid(True)
plt.show(block=True)
def plot_ITPT():
"""
Comparing info. transfer in baseline vs experiment end for IT vs PT animals.
"""
IT_baseline = OnlineNormalEstimator()
IT_expend = OnlineNormalEstimator()
PT_baseline = OnlineNormalEstimator()
PT_expend = OnlineNormalEstimator()
num_it = 0
num_pt = 0
processed_dir = './processed/'
for animal_dir in os.listdir(processed_dir):
animal_path = processed_dir + animal_dir + '/'
if not os.path.isdir(animal_path):
continue
for day_dir in os.listdir(animal_path):
day_path = animal_path + day_dir + '/'
baseline_path = day_path + 'baseline.p'
expend_path = day_path + 'experiment_end.p'
if not isfile(baseline_path) or not isfile(expend_path):
continue
with open(baseline_path, 'rb') as f:
baseline = pickle.load(f)[0]
with open(expend_path, 'rb') as f:
expend = pickle.load(f)[0]
baseline = [val for val in baseline.flatten() if not np.isnan(val)]
expend = [val for val in expend.flatten() if not np.isnan(val)]
if animal_dir.startswith('PT'):
for val in baseline:
PT_baseline.handle(val)
for val in expend:
PT_expend.handle(val)
num_pt += 1
if animal_dir.startswith('IT'):
for val in baseline:
IT_baseline.handle(val)
for val in expend:
IT_expend.handle(val)
num_it += 1
means = [
IT_baseline.mean(), IT_expend.mean(),
PT_baseline.mean(), PT_expend.mean()
]
stds = [
IT_baseline.std(), IT_expend.std(),
PT_baseline.std(), PT_expend.std()
]
fig, ax = plt.subplots(1, 1, figsize=(10,5))
labels = [
'IT: Baseline\n(%d Total)'%num_it,
'IT: Exp End\n(%d Total)'%num_it,
'PT: Baseline\n(%d Total)'%num_pt,
'PT: Exp End\n(%d Total)'%num_pt
]
x_pos = np.arange(len(labels))
ax.bar(x_pos, means, yerr=stds, align='center',
alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Information Transfer')
ax.set_xticks(x_pos)
ax.set_xticklabels(labels)
ax.set_title('GTE Distribution')
ax.yaxis.grid(True)
plt.show(block=True)
def plot_E2_learning():
"""
Comparing E2 info. transfer in baseline vs experiment end for learning vs
non-learning experiments in IT/PT
"""
IT_learning_baseline = OnlineNormalEstimator()
IT_learning_expend = OnlineNormalEstimator()
IT_non_learning_baseline = OnlineNormalEstimator()
IT_non_learning_expend = OnlineNormalEstimator()
PT_learning_baseline = OnlineNormalEstimator()
PT_learning_expend = OnlineNormalEstimator()
PT_non_learning_baseline = OnlineNormalEstimator()
PT_non_learning_expend = OnlineNormalEstimator()
num_it_learning = 0
num_pt_learning = 0
num_it_non_learning = 0
num_pt_non_learning = 0
processed_dir = './processed/'
for animal_dir in os.listdir(processed_dir):
animal_path = processed_dir + animal_dir + '/'
if not os.path.isdir(animal_path):
continue
for day_dir in os.listdir(animal_path):
day_path = animal_path + day_dir + '/'
baseline_path = day_path + 'baseline.p'
expend_path = day_path + 'experiment_end.p'
if not isfile(baseline_path) or not isfile(expend_path):
continue
with open(baseline_path, 'rb') as f:
baseline = pickle.load(f)[0]
with open(expend_path, 'rb') as f:
expend = pickle.load(f)[0]
num_neurons = baseline.shape[0]
try:
f = h5py.File(day_path + 'full_' + animal_dir + '_' + day_dir +\
'__data.hdf5')
_, _, _, reg = learning_params('./', animal_dir, day_dir, bin_size=5)
except: # In case another process is already accessing the file
continue
try:
e2_neur = np.array(f['e2_neur'])
except:
print(day_path)
continue
ens_neur = np.array(f['ens_neur'])
e2_neur = ens_neur[e2_neur]
nerden = np.array(f['nerden'])
e2_mask = np.zeros(nerden.size)
e2_mask[e2_neur] = 1
e2_mask = e2_mask[nerden]
baseline = group_result(baseline, e2_mask, ignore_diagonal=False)
expend = group_result(expend, e2_mask, ignore_diagonal=False)
slope = reg.coef_[0]
if slope < 0:
if animal_dir.startswith('IT'):
IT_non_learning_baseline.handle(baseline[0,1])
IT_non_learning_baseline.handle(baseline[1,0])
IT_non_learning_expend.handle(expend[0,1])
IT_non_learning_expend.handle(expend[1,0])
num_it_non_learning += 1
else:
PT_non_learning_baseline.handle(baseline[0,1])
PT_non_learning_baseline.handle(baseline[1,0])
PT_non_learning_expend.handle(expend[0,1])
PT_non_learning_expend.handle(expend[1,0])
num_pt_non_learning += 1
if slope > 0.2:
if animal_dir.startswith('IT'):
IT_learning_baseline.handle(baseline[0,1])
IT_learning_baseline.handle(baseline[1,0])
IT_learning_expend.handle(expend[0,1])
IT_learning_expend.handle(expend[1,0])
num_it_learning += 1
else:
PT_learning_baseline.handle(baseline[0,1])
PT_learning_baseline.handle(baseline[1,0])
PT_learning_expend.handle(expend[0,1])
PT_learning_expend.handle(expend[1,0])
num_pt_learning += 1
means = [
IT_learning_baseline.mean(), IT_learning_expend.mean(),
IT_non_learning_baseline.mean(), IT_non_learning_expend.mean(),
PT_learning_baseline.mean(), PT_learning_expend.mean(),
PT_non_learning_baseline.mean(), PT_non_learning_expend.mean()
]
stds = [
IT_learning_baseline.std(), IT_learning_expend.std(),
IT_non_learning_baseline.std(), IT_non_learning_expend.std(),
PT_learning_baseline.std(), PT_learning_expend.std(),
PT_non_learning_baseline.std(), PT_non_learning_expend.std()
]
fig, ax = plt.subplots(1, 1, figsize=(9,3))
labels = [
'IT Learning:\nBaseline\n(%d Total)'%num_it_learning,
'IT Learning:\nExp End\n(%d Total)'%num_it_learning,
'IT Non-Learning:\nBaseline\n(%d Total)'%num_it_non_learning,
'IT Non-Learning:\nExp End\n(%d Total)'%num_it_non_learning,
'PT Learning:\nBaseline\n(%d Total)'%num_pt_learning,
'PT Learning:\nExp End\n(%d Total)'%num_pt_learning,
'PT Non-Learning:\nBaseline\n(%d Total)'%num_pt_non_learning,
'PT Non-Learning:\nExp End\n(%d Total)'%num_pt_non_learning
]
x_pos = np.arange(len(labels))
ax.bar(x_pos, means, yerr=stds, align='center',
alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Information Transfer')
ax.set_xticks(x_pos)
ax.set_xticklabels(labels, {'fontsize': 12})
ax.set_title('GTE into/out-of E2 Neurons')
ax.yaxis.grid(True)
pdb.set_trace()
plt.show(block=True)
def plot_E2_ITPT():
"""
Comparing E2 info. transfer in baseline vs experiment end for
IT vs PT animals.
"""
IT_baseline = OnlineNormalEstimator()
IT_expend = OnlineNormalEstimator()
PT_baseline = OnlineNormalEstimator()
PT_expend = OnlineNormalEstimator()
num_it = 0
num_pt = 0
processed_dir = './processed/'
for animal_dir in os.listdir(processed_dir):
animal_path = processed_dir + animal_dir + '/'
if not os.path.isdir(animal_path):
continue
for day_dir in os.listdir(animal_path):
day_path = animal_path + day_dir + '/'
baseline_path = day_path + 'baseline.p'
expend_path = day_path + 'experiment_end.p'
if not isfile(baseline_path) or not isfile(expend_path):
continue
with open(baseline_path, 'rb') as f:
baseline = pickle.load(f)[0]
with open(expend_path, 'rb') as f:
expend = pickle.load(f)[0]
num_neurons = baseline.shape[0]
try:
f = h5py.File(day_path + 'full_' + animal_dir + '_' + day_dir +\
'__data.hdf5')
except: # In case another process is already accessing the file
continue
e2_neur = np.array(f['e2_neur'])
ens_neur = np.array(f['ens_neur'])
e2_neur = ens_neur[e2_neur]
nerden = np.array(f['nerden'])
e2_mask = np.zeros(nerden.size)
e2_mask[e2_neur] = 1
e2_mask = e2_mask[nerden]
baseline = group_result(baseline, e2_mask, ignore_diagonal=False)
expend = group_result(expend, e2_mask, ignore_diagonal=False)
if animal_dir.startswith('PT'):
PT_baseline.handle(baseline[0,1])
PT_baseline.handle(baseline[1,0])
PT_expend.handle(expend[0,1])
PT_expend.handle(expend[1,0])
num_pt += 1
else:
IT_baseline.handle(baseline[0,1])
IT_baseline.handle(baseline[1,0])
IT_expend.handle(expend[0,1])
IT_expend.handle(expend[1,0])
num_it += 1
means = [
IT_baseline.mean(), IT_expend.mean(),
PT_baseline.mean(), PT_expend.mean()
]
stds = [
IT_baseline.std(), IT_expend.std(),
PT_baseline.std(), PT_expend.std()
]
fig, ax = plt.subplots(1, 1, figsize=(5,5))
labels = [
'IT: Baseline\n(%d Total)'%num_it,
'IT: Exp End\n(%d Total)'%num_it,
'PT: Baseline\n(%d Total)'%num_pt,
'PT: Exp End\n(%d Total)'%num_pt
]
x_pos = np.arange(len(labels))
ax.bar(x_pos, means, yerr=stds, align='center',
alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Information Transfer')
ax.set_xticks(x_pos)
ax.set_xticklabels(labels)
ax.set_title('GTE into/out-of E2 Neurons')
ax.yaxis.grid(True)
plt.show(block=True)