-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzola_decoding_overtime_1608.py
512 lines (426 loc) · 22 KB
/
zola_decoding_overtime_1608.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import pickle
from pathlib import Path
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split, StratifiedKFold, LeavePOut
from tqdm import tqdm
from keras import backend as K
from sklearn.utils import resample
import astropy
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from datetime import datetime
from astropy.stats import bootstrap
import sklearn
from instruments.helpers.util import simple_xy_axes, set_font_axes
from instruments.helpers.neural_analysis_helpers import get_word_aligned_raster
from instruments.helpers.euclidean_classification_minimal_function import classify_sweeps
# Import standard packages
import numpy as np
import matplotlib.pyplot as plt
import pickle
# If you would prefer to load the '.h5' example file rather than the '.pickle' example file. You need the deepdish package
# import deepdish as dd
# Import function to get the covariate matrix that includes spike history from previous bins
from Neural_Decoding.preprocessing_funcs import get_spikes_with_history
import Neural_Decoding
# Import metrics
from Neural_Decoding.metrics import get_R2
from Neural_Decoding.metrics import get_rho
# Import decoder functions
from Neural_Decoding.decoders import LSTMDecoder, LSTMClassification
def target_vs_probe(blocks, talker=1, probewords=[20, 22], pitchshift=True):
if talker == 1:
probeword = probewords[0]
else:
probeword = probewords[1]
binsize = 0.01
window = [0, 0.6]
epochs = ['Early', 'Late']
epoch_threshold = 1.5
clust_ids = [st.annotations['cluster_id'] for st in blocks[0].segments[0].spiketrains if
st.annotations['group'] != 'noise']
scores = {'cluster_id': [],
'score': [],
'cm': [],
'bootScore': [],
'lstm_score': [],
'lstm_balanced_avg': [],
'lstm_accuracylist': [],
'lstm_balancedaccuracylist': [],
'history:': [],
'lstm_avg': [],
'history': [],
'time_bin': [], }
# scores['cluster_id'].append(cluster_id)
# scores['score'].append(score)
# scores['lstm_score'].append(np.mean(totalaclist))
# scores['lstm_balanced_avg'].append(np.mean(bal_ac_list))
# scores['bootScore'].append(bootScore)
# scores['lstm_accuracylist'].append(accuracy_list)
# scores['lstm_balancedaccuracylist'].append(bal_ac_list)
# scores['cm'].append(len(unique_trials_targ) + len(unique_trials_probe))
cluster_id_droplist = np.empty([])
for cluster_id in tqdm(clust_ids):
target_filter = ['Target trials', 'No Level Cue'] # , 'Non Correction Trials']
try:
raster_target = get_word_aligned_raster(blocks, cluster_id, word=1, pitchshift=pitchshift,
correctresp=True,
df_filter=[])
raster_target = raster_target[raster_target['talker'] == int(talker)]
if len(raster_target) == 0:
print('no relevant spikes for this talker')
continue
except Exception as error:
print('No relevant target firing')
print(error)
cluster_id_droplist = np.append(cluster_id_droplist, cluster_id)
continue
probe_filter = ['No Level Cue'] # , 'Non Correction Trials']
try:
raster_probe = get_word_aligned_raster(blocks, cluster_id, word=probeword, pitchshift=pitchshift,
correctresp=True,
df_filter=[])
raster_probe = raster_probe[raster_probe['talker'] == talker]
raster_probe['trial_num'] = raster_probe['trial_num'] + np.max(raster_target['trial_num'])
if len(raster_probe) == 0:
print('no relevant spikes for this talker')
continue
except:
print('No relevant probe firing')
cluster_id_droplist = np.append(cluster_id_droplist, cluster_id)
continue
# sample with replacement from target trials and probe trials to boostrap scores and so distributions are equal
lengthofraster = np.sum(len(raster_target['spike_time']) + len(raster_probe['spike_time']))
raster_targ_reshaped = np.empty([])
raster_probe_reshaped = np.empty([])
bins = np.arange(window[0], window[1], binsize)
lengthoftargraster = len(raster_target['spike_time'])
lengthofproberaster = len(raster_probe['spike_time'])
unique_trials_targ = np.unique(raster_target['trial_num'])
unique_trials_probe = np.unique(raster_probe['trial_num'])
raster_targ_reshaped = np.empty([len(unique_trials_targ), len(bins) - 1])
raster_probe_reshaped = np.empty([len(unique_trials_probe), len(bins) - 1])
count = 0
for trial in (unique_trials_targ):
raster_targ_reshaped[count, :] = \
np.histogram(raster_target['spike_time'][raster_target['trial_num'] == trial], bins=bins,
range=(window[0], window[1]))[0]
count += 1
count = 0
for trial in (unique_trials_probe):
raster_probe_reshaped[count, :] = \
np.histogram(raster_probe['spike_time'][raster_probe['trial_num'] == trial], bins=bins,
range=(window[0], window[1]))[0]
count += 1
if len(raster_targ_reshaped) >= len(raster_probe_reshaped) * 2:
print('raster of distractor at least a 1/2 of target raster')
#upsample the probe raster
raster_probe_reshaped = raster_probe_reshaped[np.random.choice(len(raster_probe_reshaped), len(raster_targ_reshaped), replace=False), :]
elif len(raster_probe_reshaped) >= len(raster_targ_reshaped) * 2:
print('raster of target at least a 1/2 of probe raster')
# upsample the target raster
raster_targ_reshaped = raster_targ_reshaped[np.random.choice(len(raster_targ_reshaped), len(raster_probe_reshaped), replace=False), :]
stim0 = np.full(len(raster_target), 0) # 0 = target word
stim1 = np.full(len(raster_probe), 1) # 1 = probe word
stim = np.concatenate((stim0, stim1))
stim0 = np.full(len(raster_targ_reshaped), 0) # 0 = target word
stim1 = np.full(len(raster_probe_reshaped), 1) # 1 = probe word
# if (len(stim0) + len(stim1)) < 5:
# print('less than 5 trials')
# continue
stim_lstm = np.concatenate((stim0, stim1))
raster = np.concatenate((raster_target, raster_probe))
raster_lstm = np.concatenate((raster_targ_reshaped, raster_probe_reshaped))
score, d, bootScore, bootClass, cm = classify_sweeps(raster, stim, binsize=binsize, window=window, genFig=False)
# fit LSTM model to the same data
newraster = raster.tolist()
raster_reshaped = np.reshape(raster_lstm, (np.size(raster_lstm, 0), np.size(raster_lstm, 1), 1)).astype(
'float32')
stim_reshaped = np.reshape(stim_lstm, (len(stim_lstm), 1)).astype('float32')
X = raster_reshaped
y = stim_reshaped
# tf.keras.backend.clear_session()
K.clear_session()
totalaclist = []
totalbalaclist = []
# create a loop that iterates from 1 to n time point and trains the model on n-1 time points
for k in range(1, X.shape[1], 1):
# break X and y into time bins from 1 to k
print('at bin number', k)
X_bin = X[:, 0:k].copy()
y_bin = y[:, 0:k].copy()
outsideloopacclist = []
outsideloopbalacclist = []
for i in range(0, 1):
accuracy_list = []
bal_ac_list = []
kfold = StratifiedKFold(n_splits=3, shuffle=True)
print('iteration', i)
for train, test in kfold.split(X_bin, y_bin):
model_lstm = LSTMClassification(units=400, dropout=0.25, num_epochs=10)
model_lstm.fit(X_bin[train], y_bin[train])
# y_pred = model_lstm.predict(X_bin[test])
y_pred = model_lstm.model(X_bin[test], training=False)
y_pred = np.argmax(y_pred, axis=1)
accuracy = sklearn.metrics.accuracy_score(y_bin[test].flatten(), y_pred.flatten())
balancedacscore = sklearn.metrics.balanced_accuracy_score(y_bin[test].flatten(), y_pred.flatten())
bal_ac_list.append(balancedacscore)
accuracy_list.append(accuracy)
outsideloopacclist.append(np.mean(accuracy_list))
outsideloopbalacclist.append(np.mean(bal_ac_list))
totalaclist.append(np.mean(outsideloopacclist))
totalbalaclist.append(np.mean(outsideloopbalacclist))
# Update the scores dictionary
scores['cluster_id'].append(cluster_id) # Assuming cluster_id is defined somewhere
scores['score'].append(score) # Assuming score is defined somewhere
scores['lstm_score'].append(np.mean(totalaclist))
scores['lstm_balanced_avg'].append(np.mean(totalbalaclist))
scores['bootScore'].append(bootScore) # Assuming bootScore is defined somewhere
scores['lstm_accuracylist'].append(totalaclist)
scores['lstm_avg'].append(np.mean(totalaclist))
scores['lstm_balancedaccuracylist'].append(totalbalaclist)
scores['time_bin'] = k
scores['cm'].append(len(unique_trials_targ) + len(
unique_trials_probe)) # Assuming unique_trials_targ and unique_trials_probe are defined somewhere
return scores
def probe_early_vs_late(blocks, talker=1, noise=True, df_filter=['No Level Cue'],
window=[0, 0.8], binsize=0.02):
epochs = ['Early', 'Late']
epoch_treshold = 1.5
clust_ids = [st.annotations['cluster_id'] for st in blocks[0].segments[0].spiketrains if
st.annotations['group'] != 'noise']
scores = {'cluster_id': [],
'score': [],
'cm': [], }
for cluster_id in tqdm(clust_ids):
# df_filter = ['No Level Cue'] #, 'Non Correction Trials']
raster = get_word_aligned_raster(blocks, cluster_id, noise=noise, df_filter=df_filter)
raster = raster[raster['talker'] == talker]
stim = np.zeros(len(raster), dtype=np.int64)
stim[raster['relStart'] > epoch_treshold] = 1
score, d, bootScore, bootClass, cm = classify_sweeps(raster, stim, binsize=binsize, iterations=100,
window=window, genFig=False)
X_train, X_test, y_train, y_test = train_test_split(raster, stim, test_size=0.33, )
model_lstm = LSTMDecoder(units=400, dropout=0, num_epochs=5)
# Fit model
model_lstm.fit(X_train, y_train)
# Get predictions
y_valid_predicted_lstm = model_lstm.predict(X_test)
# Get metric of fit
R2s_lstm = get_R2(y_test, y_valid_predicted_lstm)
print('R2s:', R2s_lstm)
scores['cluster_id'].append(cluster_id)
scores['score'].append(score)
scores['cm'].append(cm)
return scores
def save_pdf_classification(scores, saveDir, title):
conditions = ['silence']
for talker in [1, 2]:
# talker = 1
# title = f'eucl_classification_{month}_talker{talker}_win_bs_earlylateprobe_leftright_26082022'
comparisons = [comp for comp in scores[f'talker{talker}']]
comp = comparisons[0]
i = 0
clus = scores[f'talker{talker}'][comp]['silence']['cluster_id'][i]
with PdfPages(saveDir / f'{title}_talker{talker}.pdf') as pdf:
for i, clus in enumerate(tqdm(scores[f'talker{talker}'][comp]['silence']['cluster_id'])):
fig, ax = plt.subplots(figsize=(10, 5))
y = {}
yerrmax = {}
yerrmin = {}
x = np.arange(len(comparisons))
width = 0.35
for condition in conditions:
y[condition] = [scores[f'talker{talker}'][comp][condition]['score'][i][0] for comp in comparisons]
yerrmax[condition] = [scores[f'talker{talker}'][comp][condition]['score'][i][1] for comp in
comparisons]
yerrmin[condition] = [scores[f'talker{talker}'][comp][condition]['score'][i][2] for comp in
comparisons]
rects1 = ax.bar(x - width / 2 - 0.01, y[conditions[0]], width, label=conditions[0],
color='cornflowerblue')
ax.set_ylabel('Scores')
ax.set_xticks(x, comparisons)
ax.legend()
ax.scatter(x - width / 2 - 0.01, yerrmax[conditions[0]], c='black', marker='_', s=50)
ax.scatter(x - width / 2 - 0.01, yerrmin[conditions[0]], c='black', marker='_', s=50)
n_trials = {}
trial_string = ''
for comp in comparisons:
n_trials[comp] = {}
for cond in conditions:
n_trials[comp][cond] = np.sum(scores[f'talker{talker}'][comp][cond]['cm'][i])
trial_string += f'{comp} {cond}: {n_trials[comp][cond]}\n'
ax.bar_label(rects1, padding=3)
# ax.bar_label(rects2, padding=3)
ax.set_ylim([0, 1])
simple_xy_axes(ax)
set_font_axes(ax, add_size=10)
fig.suptitle(f'cluster {clus}, \nn_trials: {trial_string}')
fig.tight_layout()
pdf.savefig(fig)
plt.close(fig)
def save_pdf_classification_lstm(scores, saveDir, title, probeword):
conditions = ['pitchshift', 'nopitchshift']
for talker in [1, 2]:
comparisons = [comp for comp in scores[f'talker{talker}']]
comp = comparisons[0]
i = 0
# clus = scores[f'talker{talker}'][comp]['pitchshift']['cluster_id'][i]
if len(scores['talker1'][comp]['pitchshift']) > len(scores['talker1'][comp]['nopitchshift']):
k = 'pitchshift'
else:
k = 'nopitchshift'
with PdfPages(saveDir / f'{title}_talker{talker}_probeword{probeword[0]}.pdf') as pdf:
for i, clus in enumerate(
tqdm(scores[f'talker{talker}'][comp][k]['cluster_id'])): # ['pitchshift']['cluster_id'])):
fig, ax = plt.subplots(figsize=(10, 5))
y = {}
yerrmax = {}
yerrmin = {}
x = np.arange(len(comparisons))
x2 = np.arange(len(conditions))
width = 0.35
for condition in conditions:
try:
y[condition] = [scores[f'talker{talker}'][comp][condition]['lstm_avg'][i] for comp in
comparisons]
except:
print('dimension mismatch')
continue
try:
rects1 = ax.bar(x - width / 2 - 0.01, y[conditions[0]], width, label=conditions[0],
color='cornflowerblue')
rects2 = ax.bar(x + width / 2 + 0.01, y[conditions[1]], width, label=conditions[1],
color='lightcoral')
except:
print('both conditions not satisfied')
continue
ax.set_ylabel('Scores')
ax.set_xticks(x, comparisons)
if talker == 1:
talkestring = 'Female'
else:
talkestring = 'Male'
# plt.title('LSTM classification scores for extracted units,'+ talkestring+' talker')
ax.legend()
#
# ax.scatter(x - width / 2 - 0.01, yerrmax[conditions[0]], c='black', marker='_', s=50)
# ax.scatter(x - width / 2 - 0.01, yerrmin[conditions[0]], c='black', marker='_', s=50)
# ax.scatter(x + width / 2 + 0.01, yerrmax[conditions[1]], c='black', marker='_', s=50)
# ax.scatter(x + width / 2 + 0.01, yerrmin[conditions[1]], c='black', marker='_', s=50)
# ax.scatter(range(len(scores)), yerrmax, c='black', marker='_', s=10)
# ax.scatter(range(len(scores)), yerrmin, c='black', marker='_', s=10)
n_trials = {}
trial_string = ''
for comp in comparisons:
n_trials[comp] = {}
for cond in conditions:
n_trials[comp][cond] = np.sum(scores[f'talker{talker}'][comp][cond]['cm'][i])
trial_string += f'{comp} {cond}: {n_trials[comp][cond]}\n'
ax.bar_label(rects1, padding=3, fmt='%.2f')
ax.bar_label(rects2, padding=3, fmt='%.2f')
ax.set_ylim([0, 1])
simple_xy_axes(ax)
set_font_axes(ax, add_size=10)
fig.suptitle(f'cluster {clus}, \nn_trials: {trial_string}')
fig.tight_layout()
pdf.savefig(fig)
plt.close(fig)
def save_pdf_classification_lstm_bothtalker(scores, saveDir, title):
conditions = ['pitchshift', 'nopitchshift']
for talker in [1, 2]:
# talker = 1
# title = f'eucl_classification_{month}_talker{talker}_win_bs_earlylateprobe_leftright_26082022'
comparisons = [comp for comp in scores[f'talker{talker}']]
comp = comparisons[0]
i = 0
clus = scores[f'talker{talker}'][comp]['pitchshift']['cluster_id'][i]
if len(scores['talker1'][comp]['pitchshift']) > len(scores['talker1'][comp]['nopitchshift']):
k = 'pitchshift'
else:
k = 'nopitchshift'
with PdfPages(saveDir / f'{title}_talker{talker}.pdf') as pdf:
for i, clus in enumerate(
tqdm(scores[f'talker{talker}'][comp][k]['cluster_id'])): # ['pitchshift']['cluster_id'])):
fig, ax = plt.subplots(figsize=(10, 5))
y = {}
yerrmax = {}
yerrmin = {}
x = np.arange(len(comparisons))
x2 = np.arange(len(conditions))
width = 0.35
for condition in conditions:
try:
y[condition] = [scores[f'talker{talker}'][comp][condition]['lstm_avg'][i] for comp in
comparisons]
except:
print('dimension mismatch')
continue
rects1 = ax.bar(x - width / 2 - 0.01, y[conditions[0]], width, label=conditions[0],
color='cornflowerblue')
rects2 = ax.bar(x + width / 2 + 0.01, y[conditions[1]], width, label=conditions[1], color='lightcoral')
ax.set_ylabel('Scores')
ax.set_xticks(x, comparisons)
plt.title('LSTM classification scores for extracted units')
ax.legend()
n_trials = {}
trial_string = ''
for comp in comparisons:
n_trials[comp] = {}
for cond in conditions:
n_trials[comp][cond] = np.sum(scores[f'talker{talker}'][comp][cond]['cm'][i])
trial_string += f'{comp} {cond}: {n_trials[comp][cond]}\n'
ax.bar_label(rects1, padding=3, fmt='%2f')
ax.bar_label(rects2, padding=3, fmt='%2f')
ax.set_ylim([0, 1])
simple_xy_axes(ax)
set_font_axes(ax, add_size=10)
fig.suptitle(f'cluster {clus}, \nn_trials: {trial_string}')
fig.tight_layout()
pdf.savefig(fig)
plt.close(fig)
def run_classification(dir, datapath, ferretid):
fname = 'blocks.pkl'
with open(datapath / 'blocks.pkl', 'rb') as f:
blocks = pickle.load(f)
scores = {}
probewords_list = [(2, 2), (20, 22), (5, 6), (42, 49), (32, 38)]
# probewords_list = [(20, 22), (5, 6), (42, 49), (32, 38)]
now = datetime.now()
dt_string = now.strftime("%d%m%Y_%H_%M_%S")
dt_string = now.strftime("%d%m%Y_%H_%M_%S")
tarDir = Path(
f'D:/Data/decoding_over_time_results/F1702_Zola/')
saveDir = tarDir
saveDir.mkdir(exist_ok=True, parents=True)
for probeword in probewords_list:
print('now starting')
print(probeword)
for talker in [2]:
binsize = 0.01
if talker == 1:
window = [0, 0.6]
else:
window = [0, 0.5]
print(f'talker {talker}')
scores[f'talker{talker}'] = {}
scores[f'talker{talker}']['target_vs_probe'] = {}
scores[f'talker{talker}']['target_vs_probe']['nopitchshift'] = target_vs_probe(blocks, talker=talker,
probewords=probeword,
pitchshift=False)
scores[f'talker{talker}']['target_vs_probe']['pitchshift'] = target_vs_probe(blocks, talker=talker,
probewords=probeword,
pitchshift=True)
np.save(saveDir / f'scores_{dir}_{probeword[0]}_{ferretid}_probe_allpitches_onlymaletalker_bs.npy',
scores)
fname = 'scores_' + dir + f'_probe_earlylate_left_right_win_bs_{binsize}'
def main():
directories = [
'orecchiette_2022'] # , 'Trifle_July_2022']/home/zceccgr/Scratch/zceccgr/ms4output/F1702_Zola/spkenvresults04102022allrowsbut4th
datapath = Path(f'D:\F1702_Zola\spkenvresults04102022allrowsbut4th')
ferretid = 'zola'
for dir in directories:
run_classification(dir, datapath, ferretid)
if __name__ == '__main__':
main()