-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalysis.py
1590 lines (1182 loc) · 65.8 KB
/
analysis.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 22 14:18:35 2020
@author: svc_ccg
"""
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import FormatStrFormatter
import matplotlib.patches as mpatches
import os, json, shutil, re
import analysis
from numba import njit
import visual_behavior
from get_sessions import glob_file
import ecephys
from probeSync_qc import get_sync_line_data
import probeSync_qc as probeSync
import cv2
import pandas as pd
import plotly
import plotly.tools as tls
import scipy.signal
import copy
probe_color_dict = {'A': 'orange',
'B': 'r',
'C': 'k',
'D': 'g',
'E': 'b',
'F': 'm'}
def find_spikes_per_trial(spikes, trial_starts, trial_ends):
tsinds = np.searchsorted(spikes, trial_starts)
teinds = np.searchsorted(spikes, trial_ends)
return teinds - tsinds
@njit
def makePSTH_numba(spikes, startTimes, windowDur, binSize=0.001, convolution_kernel=0.05, avg=True):
spikes = spikes.flatten()
startTimes = startTimes - convolution_kernel/2
windowDur = windowDur + convolution_kernel
bins = np.arange(0,windowDur+binSize,binSize)
convkernel = np.ones(int(convolution_kernel/binSize))
counts = np.zeros(bins.size-1)
for i,start in enumerate(startTimes):
startInd = np.searchsorted(spikes, start)
endInd = np.searchsorted(spikes, start+windowDur)
counts = counts + np.histogram(spikes[startInd:endInd]-start, bins)[0]
counts = counts/startTimes.size
counts = np.convolve(counts, convkernel)/(binSize*convkernel.size)
return counts[convkernel.size-1:-convkernel.size], bins[:-convkernel.size-1]
def makePSTH(spikes,startTimes,windowDur,binSize=0.01, avg=True):
bins = np.arange(0,windowDur+binSize,binSize)
counts = np.zeros((len(startTimes),bins.size-1))
for i,start in enumerate(startTimes):
counts[i] = np.histogram(spikes[(spikes>=start) & (spikes<=start+windowDur)]-start,bins)[0]
if avg:
counts = counts.mean(axis=0)
counts /= binSize
return counts
def map_newscale_SNs_to_probes(motor_locs):
serial_numbers = motor_locs['serialNum'].unique()
# Known serial number to probe mappings for NP rigs. Update here if new motors are added.
NP0_serialToProbeDict = {' SN32148': 'A', ' SN32142': 'B', ' SN32144':'C', ' SN32149':'D', ' SN32135':'E', ' SN24273':'F'}
NP1_serialToProbeDict = {' SN34027': 'A', ' SN31056': 'B', ' SN32141':'C', ' SN32146':'D', ' SN32139':'E', ' SN32145':'F'}
NP3_serialToProbeDict = {' SN31212': 'A', ' SN34029': 'B', ' SN31058':'C', ' SN24272':'D', ' SN32152':'E', ' SN36800':'F'}
known_serial_to_probe_mapping = {}
[known_serial_to_probe_mapping.update(d) for d in [NP0_serialToProbeDict, NP1_serialToProbeDict, NP3_serialToProbeDict]]
# Grab the probe mapping for all known serial numbers and leave unknown serial numbers unmapped
try:
assert(all([s in known_serial_to_probe_mapping for s in serial_numbers]))
except Exception as e:
unknown = []
for s in serial_numbers:
if s not in known_serial_to_probe_mapping:
unknown.append(s)
known_serial_to_probe_mapping[s] = ''
warning_string = ('\nWARNING: Unknown newscale serial numbers {} encountered, '
'please update serial number dictionary in data_io.py file'.format(unknown))
print(warning_string)
finally:
serialToProbeDict = {s:known_serial_to_probe_mapping[s] for s in serial_numbers}
serialToProbeDict = {k: v for k, v in sorted(serialToProbeDict.items(), key=lambda item: item[1])}
print('\nUsing following mapping between serial numbers and probe IDs: {}'.format(serialToProbeDict))
return serialToProbeDict
def read_motor_locs_into_dataframe(motor_locs_csv_path):
motor_locs = pd.read_csv(motor_locs_csv_path, header=None, names=['time', 'serialNum', 'x', 'y', 'z', 'relx', 'rely', 'relz'])
motor_locs['time'] = pd.to_datetime(motor_locs['time'])
motor_locs = motor_locs.set_index('time')
return motor_locs.dropna()
def find_motor_coords_at_time(motor_locs_path, time):
time = pd.to_datetime(time)
motor_locs = read_motor_locs_into_dataframe(motor_locs_path)
serialToProbeDict = map_newscale_SNs_to_probes(motor_locs)
pcoordsDict = {}
for pSN in serialToProbeDict:
pid = serialToProbeDict[pSN]
probe_locs = motor_locs.loc[motor_locs.serialNum==pSN]
probe_locs['relz'] = 6000-probe_locs['relz'] #correct for weird z logging
probe_locs = probe_locs.loc[(probe_locs.index<time)]
closest_motor_log_index = np.argmin(np.abs(probe_locs.index - time))
closest_motor_log = probe_locs.iloc[closest_motor_log_index]
#print('motor time: ', closest_motor_log)
pcoordsDict[pid] = closest_motor_log[['relx', 'rely', 'relz']].to_list()
return {pid: pcoordsDict[pid] for pid in 'ABCDEF' if pid in pcoordsDict}
def calculate_probe_noise(datfilepath, chrange=[0, 384], sampleRate=30000,
chunk_size=5, offset = 10, channelNumber=384, return_chunk=False):
'''Read in raw AP band data and find noise for a chunk of the data
INPUTS:
datfilepath: path to binary file with raw data
chrange: channels over which to calculate noise
sampleRate: nominal probe sample rate
chunk_size: length of chunk to read in (in seconds)
offset: when to start reading the data (in seconds)
channelNumber: total number of recording channels in data file
'''
d = np.memmap(datfilepath, dtype = 'int16', mode = 'r+')
d = np.reshape(d, (int(d.size/channelNumber), channelNumber))
chunk = d[offset*sampleRate:(offset+chunk_size)*sampleRate, chrange[0]:chrange[1]]
channel_std = np.std(chunk, axis=0)*0.195 #get standard deviation for each channel and convert to uV
if return_chunk:
return chunk, channel_std
else:
return channel_std
def plot_raw_AP_band(datachunk, probeID, probe_info_dict, FIG_SAVE_DIR, skip_interval = 20,
sampleRate=30000, channelNumber=384, yrange=[-400, 400], prefix='', savefig=True):
channels_to_plot = np.arange(0, datachunk.shape[1], skip_interval)
num_channels = len(channels_to_plot)
time = np.linspace(0, datachunk.shape[0]/sampleRate, datachunk.shape[0])
surface_channel = probe_info_dict['surface_channel']
fig, axes = plt.subplots(num_channels)
fig.set_size_inches([6, 12])
for ic, chan in enumerate(channels_to_plot[::-1]):
chan_data = datachunk[:, chan] * 0.195
color = 'k' if chan<surface_channel else '0.6'
ax = axes[ic]
ax.set_ylabel(chan, rotation=0)
ax.plot(time, chan_data, color)
ax.set_ylim(yrange)
ax.yaxis.set_label_position('right')
if ic == len(axes)-1:
[ax.spines[pos].set_visible(False) for pos in ['right', 'top']]
ax.set_xlabel('Time (s)')
else:
ax.set_yticks([])
ax.xaxis.set_visible(False)
[ax.spines[pos].set_visible(False) for pos in ['right', 'top', 'left', 'bottom']]
if savefig:
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'Probe' + probeID + ' AP band raw snippet'))
def plot_AP_band_noise(probe_dirs, probes_to_run, probe_info_dicts, FIG_SAVE_DIR,
data_chunk_size = 5, skip_interval = 20, prefix=''):
for pid in probes_to_run:
pdir = [d for d in probe_dirs if 'probe'+pid in d][0]
datfilepath = glob_file(os.path.join(pdir, 'continuous\\Neuropix-PXI-100.0'), 'continuous.dat')
chunk, chan_std = calculate_probe_noise(datfilepath, chunk_size= data_chunk_size, return_chunk=True)
fig, ax = plt.subplots()
fig.set_size_inches([3, 12])
ax.plot(chan_std, np.arange(len(chan_std)), 'k')
ax.set_ylabel('channel_number')
ax.set_xlabel('standard dev (uV)')
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'Probe' + pid + ' AP band channel noise'))
plot_raw_AP_band(chunk, pid, probe_info_dicts[pid], FIG_SAVE_DIR, skip_interval = skip_interval, prefix=prefix)
def probe_insertion_report(motor_locs_path, insertion_start_time, experiment_start_time,
FIG_SAVE_DIR, prefix=''):
start_coords = find_motor_coords_at_time(motor_locs_path, insertion_start_time)
end_coords = find_motor_coords_at_time(motor_locs_path, experiment_start_time)
report = {
'insertion_start_coords': start_coords,
'insertion_end_coords': end_coords
}
for pid in start_coords:
zstart = start_coords[pid][2]
zend = end_coords[pid][2]
report[pid + '_insertion_depth'] = zend-zstart
save_json(report, os.path.join(FIG_SAVE_DIR, prefix+'probe_insertion_report.json'))
return report
def plot_rf(mapping_pkl_data, spikes, first_frame_offset, frameAppearTimes, resp_latency=0.025, plot=True, returnMat=False, stimulus_index=0):
rfFlashStimDict = mapping_pkl_data
rfStimParams = rfFlashStimDict['stimuli'][stimulus_index]
rf_pre_blank_frames = int(rfFlashStimDict['pre_blank_sec']*rfFlashStimDict['fps'])
first_rf_frame = first_frame_offset + rf_pre_blank_frames
rf_frameTimes = frameAppearTimes[first_rf_frame:]
rf_trial_start_times = rf_frameTimes[np.array([f[0] for f in np.array(rfStimParams['sweep_frames'])]).astype(np.int)]
#extract trial stim info (xpos, ypos, ori)
sweep_table = np.array(rfStimParams['sweep_table'], dtype=object) #table with rfstim parameters, indexed by sweep order to give stim for each trial
sweep_order = np.array(rfStimParams['sweep_order'], dtype=object).astype(int) #index of stimuli for sweep_table for each trial
#sweep_table = np.array(rfStimParams['sweep_table']) #table with rfstim parameters, indexed by sweep order to give stim for each trial
#sweep_order = np.array(rfStimParams['sweep_order']) #index of stimuli for sweep_table for each trial
trial_xpos = np.array([pos[0] for pos in sweep_table[sweep_order, 0]])
trial_ypos = np.array([pos[1] for pos in sweep_table[sweep_order, 0]])
trial_ori = sweep_table[sweep_order, 3]
xpos = np.unique(trial_xpos)
ypos = np.unique(trial_ypos)
ori = np.unique(trial_ori)
respInds = tuple([(np.where(ypos==y)[0][0], np.where(xpos==x)[0][0], np.where(ori==o)[0][0]) for (y,x,o) in zip(trial_ypos, trial_xpos, trial_ori)])
trial_spikes = find_spikes_per_trial(spikes, rf_trial_start_times+resp_latency, rf_trial_start_times+resp_latency+0.2)
respMat = np.zeros([ypos.size, xpos.size, ori.size])
for (respInd, tspikes) in zip(respInds, trial_spikes):
respMat[respInd] += tspikes
# bestOri = np.unravel_index(np.argmax(respMat), respMat.shape)[-1]
return respMat
def plot_psth_change_flashes(change_times, spikes, preTime = 0.05, postTime = 0.55, sdfSigma=0.005):
sdf, t = makePSTH_numba(spikes,change_times-preTime,preTime+postTime, convolution_kernel=sdfSigma*2)
return sdf, t
def filterTrace(trace, highcutoff=0.2, sampleFreq=2500):
to_filter = np.copy(trace.T)
#highFreqCutoff = 0.2 #frequency below which the signal will get attenuated
b,a = scipy.signal.butter(4, highcutoff/(sampleFreq/2.), btype='high') #I made it a fourth order filter, not actually sure what the best way to determine this is...
for ic, channel in enumerate(to_filter):
to_filter[ic] = scipy.signal.filtfilt(b,a,channel)
return to_filter.T
def processLFP(lfp_array, baseline_samps = None, agarChRange=None):
lfp = np.copy(lfp_array)
# print('min {} max {} range{}'.format(lfp.min()*0.195,
# lfp.max()*0.195, 0.195*(lfp.max()-lfp.min())))
if baseline_samps is not None:
lfp = lfp - np.median(lfp[:baseline_samps], axis=0)[None, :]
if agarChRange is not None:
agar = np.median(lfp[:,agarChRange[0]:agarChRange[1]],axis=1)
lfp = lfp-agar[:,None]
return lfp
def lickTriggeredLFP(lick_times, lfp, lfp_time, agarChRange=None, num_licks=20,
windowBefore=1, windowAfter=1, min_inter_lick_time = 0.5, behavior_duration=3600):
first_lick_times = lick_times[lick_times>lfp_time[0]+windowBefore]
first_lick_times = first_lick_times[:np.min([len(first_lick_times), num_licks])]
probeSampleRate = 1./np.median(np.diff(lfp_time))
samplesBefore = int(round(windowBefore * probeSampleRate))
samplesAfter = int(round(windowAfter * probeSampleRate))
#last_lick_ind = np.where(lfp_time<=first_lick_times[-1])[0][-1]
#lfp = lfp[:last_lick_ind+samplesAfter+1]
#lfp = lfp - np.mean(lfp, axis=0)[None, :]
# if agarChRange is not None:
# agar = np.median(lfp[:,agarChRange[0]:agarChRange[1]],axis=1)
# lfp = lfp-agar[:,None]
lickTriggeredAv = np.full([first_lick_times.size, samplesBefore+samplesAfter, lfp.shape[1]], np.nan)
filt_lickTriggeredAv = np.full([first_lick_times.size, samplesBefore+samplesAfter, lfp.shape[1]], np.nan)
lick_inds = np.searchsorted(lfp_time, first_lick_times)
for il, li in enumerate(lick_inds):
ll = processLFP(lfp[li-samplesBefore:li+samplesAfter], baseline_samps = samplesBefore, agarChRange=agarChRange)
lickTriggeredAv[il, :, :] = ll
filt_lickTriggeredAv[il, :, :] = filterTrace(ll, highcutoff=1)
m = np.nanmean(lickTriggeredAv, axis=0)*0.195 #convert to uV
m_filt = np.nanmean(filt_lickTriggeredAv, axis=0)*0.195
mtime = np.linspace(-windowBefore, windowAfter, m.size)
return m, m_filt, mtime, first_lick_times
def plot_lick_triggered_LFP(lfp_dict, agar_chan_dict, lick_times, FIG_SAVE_DIR, prefix='',
agarChRange=None, num_licks=20, windowBefore=1,
windowAfter=1.5, min_inter_lick_time = 0.5, behavior_duration=3600):
for p in lfp_dict:
plfp = lfp_dict[p]['lfp']
plfp_time = lfp_dict[p]['time']
# get absolute channel range over licks to check for saturation
lick_inds = np.searchsorted(plfp_time, lick_times)
last_lick_ind = lick_inds[num_licks] if num_licks<len(lick_inds) else lick_inds[-1]
chmax = np.max(plfp[:last_lick_ind], axis=0)
chmin = np.min(plfp[:last_lick_ind], axis=0)
chrange = (chmax-chmin)*0.195
agarChRange = np.array(agar_chan_dict[p]).astype(int)
print('Using agar range {} for probe {}'.format(agarChRange, p))
lta, lta_filt, ltime, first_lick_times = analysis.lickTriggeredLFP(lick_times, plfp, lfp_dict[p]['time'],
agarChRange=agarChRange, num_licks=20, windowBefore=windowBefore,
windowAfter=windowAfter, min_inter_lick_time=0.5)
fig, axes = plt.subplots(4,1)
fig.set_size_inches([12, 8])
fig.suptitle(p + ' Lick-triggered LFP, ' + str(len(first_lick_times)) + ' rewarded lick bouts')
im_raw = axes[0].imshow(lta.T, aspect='auto')
im_filt = axes[1].imshow(lta_filt.T, aspect='auto')
plt.colorbar(im_raw, ax=axes[0])
plt.colorbar(im_filt, ax = axes[1])
for ax in axes[2:]:
dummy = plt.colorbar(im_filt, ax = ax)
dummy.remove()
axes[2].plot(np.mean(lta, axis=1), 'k')
for a in axes[:-1]:
a.set_xticks(np.arange(0, windowBefore+windowAfter, windowBefore)*2500)
a.set_xticklabels(np.round(np.arange(-windowBefore, windowAfter, windowBefore), decimals=2))
axes[2].set_xlim(axes[0].get_xlim())
axes[2].set_ylabel('Mean across channels')
[a.tick_params(bottom=False, labelbottom=False) for a in axes[:2]]
[a.set_ylabel('channel') for a in axes[:2]]
axes[0].set_title('raw')
axes[1].set_title('high pass filtered > 1Hz')
axes[3].plot(np.arange(384), chrange)
axes[3].set_xlabel('channel')
axes[3].set_ylabel('abs range, uV')
axes[2].set_xlabel('Time from lick bout (s)')
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'Probe' + p + ' lick-triggered LFP'))
def get_first_lick_times(lick_times, min_inter_lick_time=0.5, rewarded=True):
first_lick_times = lick_times[np.insert(np.diff(lick_times)>=min_inter_lick_time, 0, True)]
return first_lick_times
def get_rewarded_lick_times(lickTimes, frameTimes, trials, min_inter_lick_time=0.5):
trial_start_frames = np.array(trials['startframe'])
trial_end_frames = np.array(trials['endframe'])
trial_start_times = frameTimes[trial_start_frames]
trial_end_times = frameTimes[trial_end_frames]
first_lick_times = lickTimes[np.insert(np.diff(lickTimes)>=min_inter_lick_time, 0, True)]
first_lick_trials = get_trial_by_time(first_lick_times, trial_start_times, trial_end_times)
hit = np.array(trials['response_type']=='HIT')
hit_lick_times = first_lick_times[np.where(hit[first_lick_trials])[0]]
return hit_lick_times
def get_trial_by_time(times, trial_start_times, trial_end_times):
trials = []
for time in times:
if trial_start_times[0]<=time<trial_end_times[-1]:
trial = np.where((trial_start_times<=time)&(trial_end_times>time))[0][0]
else:
trial = -1
trials.append(trial)
return np.array(trials)
def vectorize_edgetimes(on_times, off_times, sampleperiod = 0.001):
on_times_samp = np.round(on_times/sampleperiod, 0).astype(int)
off_times_samp = np.round(off_times/sampleperiod, 0).astype(int)
last_time = np.max([on_times_samp.max(), off_times_samp.max()])
vector = np.zeros(int(last_time))
times = np.arange(len(vector))*sampleperiod
if off_times_samp[0] < on_times_samp[0]:
on_times_samp = np.insert(on_times_samp, 0, 0)
if on_times[-1]>off_times[-1]:
off_times_samp = np.append(off_times_samp, int(last_time))
on_intervals = [slice(on, off) for on, off in zip(on_times_samp, off_times_samp)]
#off_intervals = [slice(off, on) for on, off in zip(on_times[1:], off_times)]
for interval in on_intervals:
vector[interval] = 1
return vector, times
def plot_vsync_and_diode(syncDataset, FIG_SAVE_DIR, prefix=''):
monitor_lag = get_monitor_lag(syncDataset)
# dioder, diodef = probeSync.get_diode_times(syncDataset)
# vf = probeSync.get_vsyncs(syncDataset)
#
# start_session_diode_vector, dtimes = vectorize_edgetimes(dioder[:10], diodef[:10])
#
# fig, ax = plt.subplots(1, 2)
# fig.set_size_inches([15, 6])
# fig.suptitle('vsync/diode alignment')
# ax[0].plot(dtimes, start_session_diode_vector, 'k')
# ax[0].plot(vf[:500], 0.5*np.ones(500), 'r|', ms=20)
# ax[0].plot(vf[60], 0.5, 'g|', ms=30)
# ax[0].set_xlim([vf[0]-1, vf[60]+0.5])
# ax[0].plot([vf[60], vf[60]+monitor_lag], [0.75,0.75], 'b-')
# ax[0].set_xlabel('Experiment time (s)')
#
# ax[1].plot(dtimes, start_session_diode_vector, 'k')
# ax[1].plot(vf[50:70], 0.5*np.ones(20), 'r|', ms=20)
# ax[1].plot(vf[60], 0.5, 'g|', ms=30)
# ax[1].set_xlim([vf[50], vf[70]])
# ax[1].plot([vf[60], vf[60]+monitor_lag], [0.75,0.75], 'b-')
# ax[1].set_xlabel('Experiment time (s)')
#
# ax[1].legend(['diode', 'vf', 'frame 60', 'lag'], markerscale=0.5)
#save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'vsync_with_diode.png'))
#save_as_plotly_json(fig, os.path.join(FIG_SAVE_DIR, prefix+'vsync_with_diode.plotly.json'))
sd = syncDataset
stim_ons, stim_offs = probeSync.get_stim_starts_ends(sd) # These are the on and off times for each stimulus (behavior, mapping, replay)
all_vsyncs = sd.get_falling_edges(2, units='seconds')
# break the vsyncs up into three chunks (one for each stimulus)
vsyncs = []
diode_vsyncs = []
for son, soff in zip(stim_ons, stim_offs):
stim_vsyncs = all_vsyncs[(all_vsyncs>son)&(all_vsyncs<soff)]
stim_diode_vsyncs = stim_vsyncs[::60]
vsyncs.append(stim_vsyncs)
diode_vsyncs.append(stim_diode_vsyncs)
#plot beginning of stims
fig, axes = plt.subplots(len(stim_ons))
if not isinstance(axes, list) and not isinstance(axes, np.ndarray):
axes = [axes]
fig.suptitle('Stim Starts')
for ind, (son, vs, dvs) in enumerate(zip(stim_ons, vsyncs, diode_vsyncs)):
print(ind)
axes[ind].plot(vs, 0.5*np.ones(len(vs)), '|')
axes[ind].plot(dvs, 0.5*np.ones(len(dvs)), '|', ms=20)
sd.plot_bit(4, son-1, son+2, axes=axes[ind], auto_show=False)
sd.plot_bit(5, son-1, son+2, axes=axes[ind], auto_show=False)
axes[ind].set_xlim([son-1, son+2])
axes[ind].legend(['vsyncs', 'diode_vsyncs', 'diode', 'stim_running'])
axes[ind].get_legend().remove()
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'stim_starts_vsync_with_diode.png'))
#plot end of stims
fig, axes = plt.subplots(len(stim_offs))
if not isinstance(axes, list) and not isinstance(axes, np.ndarray):
axes = [axes]
fig.suptitle('Stim Ends')
for ind, (soff, vs, dvs) in enumerate(zip(stim_offs, vsyncs, diode_vsyncs)):
axes[ind].plot(vs, 0.5*np.ones(len(vs)), '|')
axes[ind].plot(dvs, 0.5*np.ones(len(dvs)), '|', ms=20)
sd.plot_bit(4, soff-2, soff+1, axes=axes[ind], auto_show=False)
sd.plot_bit(5, soff-2, soff+1, axes=axes[ind], auto_show=False)
axes[ind].set_xlim([soff-2, soff+1])
axes[ind].legend(['vsyncs', 'diode_vsyncs', 'diode', 'stim_running'])
axes[ind].get_legend().remove()
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'stim_ends_vsync_with_diode.png'))
def get_monitor_lag(syncDataset):
dioder, diodef = probeSync.get_diode_times(syncDataset)
vf = probeSync.get_vsyncs(syncDataset)
lag = np.min([np.min(np.abs(d-vf[60])) for d in [diodef, dioder]])
return lag
def plot_frame_intervals(vsyncs, behavior_frame_count, mapping_frame_count,
behavior_start_frame, mapping_start_frame,
replay_start_frame, save_dir=None, prefix=''):
fig, ax = plt.subplots()
fig.suptitle('stim frame intervals')
ax.plot(np.diff(vsyncs))
ax.set_ylim([0, 0.2])
vline_locs = [behavior_start_frame, mapping_start_frame,
replay_start_frame, replay_start_frame+behavior_frame_count]
for v in vline_locs:
ax.axvline(v, color='k', linestyle='--')
ax.set_xlabel('frames')
ax.set_ylabel('interval, s (capped at 0.2)')
ax.text(behavior_start_frame + behavior_frame_count/2, 0.15,
'behavior', horizontalalignment='center')
ax.text(mapping_start_frame+mapping_frame_count/2, 0.15,
'rf', horizontalalignment='center')
ax.text(replay_start_frame+behavior_frame_count/2, 0.15, 'replay', horizontalalignment='center')
if save_dir is not None:
save_figure(fig, os.path.join(save_dir, prefix+'stim_frame_intervals.png'))
def plot_diode_intervals(sync, FIG_SAVE_DIR=None, prefix=''):
fig, ax = plt.subplots()
fig.suptitle('Diode intervals')
stim_running_r, stim_running_f = (sync.get_rising_edges('stim_running', 'seconds'),
sync.get_falling_edges('stim_running', 'seconds'))
#Get vsyncs that tell us when the graphics card buffer was flipped
vsyncs = sync.get_falling_edges('vsync_stim', units='seconds')
vsyncs = vsyncs[(stim_running_r[0]<=vsyncs) & (vsyncs<stim_running_f[0])]
photodiode_times = np.sort(np.concatenate([
sync.get_rising_edges('stim_photodiode', 'seconds'),
sync.get_falling_edges('stim_photodiode', 'seconds')
]))
photodiode_times = photodiode_times[(stim_running_r[0]<=photodiode_times) & (photodiode_times<stim_running_f[0])]
photodiode_times_on_off = probeSync.correct_on_off_effects(photodiode_times)
#removes blinking at beginning and end of each stimulus
photodiode_times_trimmed = probeSync.trim_border_pulses(
photodiode_times, vsyncs
)
# not totally sure... correcting for on/off photodiode asymmetry
photodiode_times_on_off = probeSync.correct_on_off_effects(
photodiode_times_trimmed
)
# fix blips in the line
photodiode_times_fixed = probeSync.fix_unexpected_edges(
photodiode_times_on_off, cycle=60)
ax.plot(photodiode_times_fixed[:-1], np.diff(photodiode_times_fixed))
if FIG_SAVE_DIR is not None:
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'diode_intervals.png'))
def plot_vsync_interval_histogram(vf, FIG_SAVE_DIR=None, prefix=''):
fig, ax = plt.subplots(constrained_layout=True)
fig.suptitle('Vsync interval histogram')
bins = np.arange(12, 40, 0.1)
ax.hist(np.diff(vf)*1000, bins=bins)
v = ax.axvline(16.667, color='k', linestyle='--')
ax.set_ylabel('number of frame intervals')
ax.set_xlabel('frame interval (ms)')
ax.legend([v], ['expected interval'])
if FIG_SAVE_DIR is not None:
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'vsync_interval_histogram.png'))
#save_as_plotly_json(fig, os.path.join(FIG_SAVE_DIR, prefix+'vsync_interval_histogram.plotly.json'))
def vsync_report(syncDataset, total_pkl_frames, FIG_SAVE_DIR, prefix=''):
vf = probeSync.get_vsyncs(syncDataset)
report = {}
intervals = np.diff(vf)
report['sync_vsync_frame_count'] = len(vf)
report['pkl frame count'] = total_pkl_frames
report['sync_pkl_framecount_match'] = 'TRUE' if len(vf)==total_pkl_frames else 'FALSE'
report['mean interval'] = intervals.mean()
report['median interval'] = np.median(intervals)
report['std of interval'] = intervals[intervals<1].std()
report['num dropped frames'] = int(np.sum(intervals>0.025))-2
report['num intervals 0.1 <= x < 1'] = int(np.sum((intervals<1)&(intervals>=0.1)))
report['num intervals >= 1 (expected = 2)'] = int(np.sum(intervals>=1))
report['monitor lag'] = get_monitor_lag(syncDataset)
save_json(report, os.path.join(FIG_SAVE_DIR, prefix+'vsync_report.json'))
def evoked_rates(probe_dict, behavior_data, behavior_start_frame, FRAME_APPEAR_TIMES):
draw_log = behavior_data['items']['behavior']['stimuli']['images']['draw_log']
flash_frames = np.where(draw_log)[0]
flash_starts = np.insert(np.where(np.diff(flash_frames)>1)[0]+1, 0, 0)
flash_start_frames = flash_frames[flash_starts]
flash_start_times = FRAME_APPEAR_TIMES[flash_start_frames+behavior_start_frame]
preTime = 0.4
postTime = 0.3
presamples = int(preTime*1000)
for p in probe_dict:
u_df = probe_dict[p]
spikes = u_df['times']
sdfs = []
for s in spikes:
s = s.flatten()
sdf,t = plot_psth_change_flashes(flash_start_times, s, preTime=preTime, postTime=postTime)
sdfs.append(sdf)
sdfs = np.array(sdfs)
baselines = np.mean(sdfs[:, presamples-300:presamples], 1)
response_peaks = np.mean(sdfs[:, presamples:presamples+100], 1)
evoked = response_peaks - baselines
u_df['evoked'] = evoked
probe_dict[p].update(u_df)
return probe_dict
def plot_population_change_response(probe_dict, behavior_start_frame, replay_start_frame,
change_frames, FRAME_APPEAR_TIMES, FIG_SAVE_DIR, ctx_units_percentile=66, prefix=''):
#change_frames = np.array(trials['change_frame'].dropna()).astype(int)+1
active_change_times = FRAME_APPEAR_TIMES[change_frames+behavior_start_frame]
try:
passive_change_times = FRAME_APPEAR_TIMES[change_frames+replay_start_frame]
except:
passive_change_times = []
lfig, lax = plt.subplots()
preTime = 0.75
postTime = 0.55
for p in probe_dict:
try:
u_df = probe_dict[p]
#good_units = u_df[(u_df['quality']=='good')&(u_df['snr']>1)]
good_units = u_df[(u_df['snr']>1)&(u_df['isi_viol']<1)&(u_df['firing_rate']>0.1)]
# max_chan = good_units['peak_channel'].max()
# # take spikes from the top n channels as proxy for cortex
# spikes = good_units.loc[good_units['peak_channel']>max_chan-num_channels_to_take_from_top]['times']
ctx_bottom_chan = np.percentile(good_units['peak_channel'], 100-ctx_units_percentile)
spikes = good_units.loc[good_units['peak_channel']>ctx_bottom_chan]['times']
sdfs = [[],[]]
for s in spikes:
s = s.flatten()
if s.size>3600:
for icts, cts in enumerate([active_change_times, passive_change_times]):
if len(cts)>0:
sdf,t = analysis.plot_psth_change_flashes(cts, s, preTime=preTime, postTime=postTime)
sdfs[icts].append(sdf)
# plot population change response
fig, ax = plt.subplots()
title = p + ' population change response'
fig.suptitle(title)
for sdf, color in zip(sdfs, ['k', 'g']):
if len(sdf)>0:
ax.plot(t, np.mean(sdf, axis=0), color)
#ax.plot(t, np.mean(sdfs[1], axis=0), 'g')
ax.legend(['active', 'passive'])
ax.axvline(preTime, c='k')
ax.axvline(preTime+0.25, c='k')
ax.set_xticks(np.arange(0, preTime+postTime, 0.05))
ax.set_xticklabels(np.round(np.arange(-preTime, postTime, 0.05), decimals=2))
ax.set_xlabel('Time from change (s)')
ax.set_ylabel('Mean population response')
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix + title + '.png'))
#fig.savefig(os.path.join(FIG_SAVE_DIR, title + '.png'))
mean_active = np.mean(sdfs[0], axis=0)
mean_active_baseline = mean_active[:int(preTime*1000)].mean()
baseline_subtracted = mean_active - mean_active_baseline
lax.plot(t, baseline_subtracted/baseline_subtracted.max(), c=probe_color_dict[p])
except Exception as e:
print('Failed to run probe {} due to error {}'.format(p, e))
lax.legend(probe_dict.keys())
lax.set_xlim([preTime, preTime+0.1])
lax.set_xticks(np.arange(preTime, preTime+0.1, 0.02))
lax.set_xticklabels(np.arange(0, 0.1, 0.02))
lax.set_xlabel('Time from change (s)')
lax.set_ylabel('Normalized response')
save_figure(lfig, os.path.join(FIG_SAVE_DIR, prefix+'pop_change_response_latency_comparison.png'))
#lfig.savefig(os.path.join(FIG_SAVE_DIR, 'pop_change_response_latency_comparison.png'))
def plot_change_response_DR(probe_dict, behavior_start_frame,
block_change_frames, FRAME_APPEAR_TIMES,
FIG_SAVE_DIR, prefix='',
ctx_units_percentile=66):
block_change_times = [np.array(bl)[~np.isnan(bl)].astype(int) for bl in block_change_frames]
block_change_times = [FRAME_APPEAR_TIMES[bl+behavior_start_frame] for bl in block_change_times]
lfig, lax = plt.subplots()
preTime = 0.75
postTime = 0.55
for p in probe_dict:
#try:
u_df = probe_dict[p]
#good_units = u_df[(u_df['quality']=='good')&(u_df['snr']>1)]
good_units = u_df[(u_df['snr']>1)&(u_df['isi_viol']<1)&(u_df['firing_rate']>0.1)]
# max_chan = good_units['peak_channel'].max()
# # take spikes from the top n channels as proxy for cortex
# spikes = good_units.loc[good_units['peak_channel']>max_chan-num_channels_to_take_from_top]['times']
ctx_bottom_chan = np.percentile(good_units['peak_channel'], 100-ctx_units_percentile)
spikes = good_units.loc[good_units['peak_channel']>ctx_bottom_chan]['times']
sdfs = [[] for i in range(len(block_change_frames))]
for s in spikes:
s = s.flatten()
if s.size>3600:
for icts, cts in enumerate(block_change_times):
if len(cts)>0:
sdf,t = analysis.plot_psth_change_flashes(cts, s, preTime=preTime, postTime=postTime)
sdfs[icts].append(sdf)
# plot population change response
fig, ax = plt.subplots()
title = p + ' population change response'
colors = ['k', 'g', '0.5', 'r', 'b', 'orange', 'teal', 'm']
fig.suptitle(title)
for sdf, color in zip(sdfs, colors):
if len(sdf)>0:
ax.plot(t, np.mean(sdf, axis=0), color)
#ax.plot(t, np.mean(sdfs[1], axis=0), 'g')
ax.legend(['block: '+str(b) for b in range(len(block_change_frames))])
ax.axvline(preTime, c='k')
ax.axvline(preTime+0.25, c='k')
ax.set_xticks(np.arange(0, preTime+postTime, 0.05))
ax.set_xticklabels(np.round(np.arange(-preTime, postTime, 0.05), decimals=2))
ax.set_xlabel('Time from change (s)')
ax.set_ylabel('Mean population response')
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix + title + '.png'))
#fig.savefig(os.path.join(FIG_SAVE_DIR, title + '.png'))
mean_active = np.mean(sdfs[0], axis=0)
mean_active_baseline = mean_active[:int(preTime*1000)].mean()
baseline_subtracted = mean_active - mean_active_baseline
lax.plot(t, baseline_subtracted/baseline_subtracted.max(), c=probe_color_dict[p])
# except Exception as e:
# print('Failed to run probe {} due to error {}'.format(p, e))
lax.legend(probe_dict.keys())
lax.set_xlim([preTime, preTime+0.1])
lax.set_xticks(np.arange(preTime, preTime+0.1, 0.02))
lax.set_xticklabels(np.arange(0, 0.1, 0.02))
lax.set_xlabel('Time from change (s)')
lax.set_ylabel('Normalized response')
save_figure(lfig, os.path.join(FIG_SAVE_DIR, prefix+'pop_change_response_latency_comparison.png'))
def plot_running_wheel(pkl_list, FIG_SAVE_DIR, save_plotly=True, prefix=''):
'''
INPUTS: pkl_list should be list of pkl data objects in the order in which
they were run.
'''
### Plot Running Wheel Data ###
rfig, rax = plt.subplots(2,1)
rfig.set_size_inches(12, 4)
rfig.suptitle('Running')
time_offset = 0
colors = ['k', 'g', 'r']
for ri, rpkl in enumerate(pkl_list):
key = 'behavior' if 'behavior' in rpkl['items'] else 'foraging'
intervals = rpkl['items']['behavior']['intervalsms'] if 'intervalsms' not in rpkl else rpkl['intervalsms']
time = np.insert(np.cumsum(intervals), 0, 0)/1000.
dx,vsig,vin = [rpkl['items'][key]['encoders'][0][rkey] for rkey in ('dx','vsig','vin')]
# cum_dist = np.cumsum(dx)
# cum_dist = cum_dist[:len(time)]
run_speed = visual_behavior.analyze.compute_running_speed(dx[:len(time)],time,vsig[:len(time)],vin[:len(time)])
cum_dist = np.cumsum(run_speed)*0.01667*0.01 # dist = speed*time then convert to meters
rax[0].plot(time+time_offset, run_speed, colors[ri])
rax[1].plot(time+time_offset, cum_dist, colors[ri])
time_offset = time_offset + time[-1]
#rax[0].set_xlabel('Time (s)')
rax[0].set_ylabel('Run Speed (cm/s)')
rax[0].legend(['behavior', 'rf map', 'passive'])
rax[1].set_ylabel('Cumulative Distance (m)')
rax[0].set_xlabel('Time (s)')
save_figure(rfig, os.path.join(FIG_SAVE_DIR, prefix+'run_speed.png'))
if save_plotly:
save_as_plotly_json(rfig, os.path.join(FIG_SAVE_DIR, prefix+'run_speed.plotly.json'))
#rfig.savefig(os.path.join(FIG_SAVE_DIR, 'run_speed.png'))
def plot_unit_quality_hist(metrics_dict, FIG_SAVE_DIR, prefix=''):
fig, ax = plt.subplots()
legend_artist = []
legend_label = []
for ip, probe in enumerate(metrics_dict):
p = metrics_dict[probe]
labels = np.sort(np.unique(p['quality']))
count = []
colors = ['g', '0.4']
bottom = 0
for il, l in enumerate(labels):
num_units = np.sum(p['quality']==l)
count.append(num_units)
if l not in legend_label:
b = ax.bar(ip, num_units, bottom=bottom, color=colors[il])
legend_artist.append(b)
legend_label.append(l)
else:
ax.bar(ip, num_units, bottom=bottom, color=colors[il])
bottom = np.cumsum(count)
ax.set_xticks(np.arange(len(metrics_dict)))
ax.set_xticklabels([p for p in metrics_dict])
ax.legend(legend_artist, legend_label)
ax.set_xlabel('Probe')
ax.set_ylabel('Unit count')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'unit_quality_hist.png'))
#fig.savefig(os.path.join(FIG_SAVE_DIR, 'unit_quality_hist.png'))
def plot_unit_distribution_along_probe(metrics_dict, info_dict, paths, FIG_SAVE_DIR, prefix=''):
for ip, probe in enumerate(metrics_dict):
p = metrics_dict[probe]
good_units = p[p['quality']=='good']
noise_units = p[p['quality']=='noise']
fig, axes = plt.subplots(ncols=1, nrows=2, constrained_layout=True, sharex=True,
gridspec_kw={'width_ratios':[1], 'height_ratios':[1,6]})
fig.suptitle('Probe {} unit distribution'.format(probe))
bins = np.arange(0, 384, 20)
goodhist = axes[1].hist(good_units['peak_channel'], bins=bins, color='g')
noisehist = axes[1].hist(noise_units['peak_channel'], bins=bins, color='k', alpha=0.8)
kilosort_channel_map = np.load(paths['probe'+probe + '_channel_map'])
kilosort_mask = [np.isin(c, kilosort_channel_map) for c in np.arange(384)]
mask = np.array(info_dict[probe]['mask']).astype(int)
surface_channel = info_dict[probe]['surface_channel']
axes[0].plot(np.arange(384), mask, 'k')
axes[0].plot(np.arange(384), kilosort_mask, 'g')
axes[0].axis('off')
axes[0].axvline(surface_channel)
axes[0].legend(['kilosort in mask', 'kilosort out mask', 'surface'])
axes[1].set_xlabel('peak channel')
axes[1].set_ylabel('unit count')
axes[1].legend([goodhist[2][0], noisehist[2][0]], ['good', 'noise'])
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'Probe_{}_unit_distribution.png'.format(probe)))
#fig.savefig(os.path.join(FIG_SAVE_DIR, 'Probe_{}_unit_distribution.png'.format(probe)))
def probe_yield_report(metrics_dict, info_dict, FIG_SAVE_DIR, prefix=''):
report = {p:{} for p in metrics_dict}
for probe in metrics_dict:
p = metrics_dict[probe]
report[probe]['num_good_units'] = int(np.sum(p['quality']=='good'))
report[probe]['num_noise_units'] = int(np.sum(p['quality']=='noise'))
info = info_dict[probe]
report[probe]['surface_channel'] = info['surface_channel']
report[probe]['air_channel'] = info['air_channel']
report[probe]['num_masked_channels'] = int(np.sum(info['mask']))
save_json(report, os.path.join(FIG_SAVE_DIR, prefix+'probe_yield_report.json'))
def all_spike_hist(probe_data):
u_df = probe_data
good_units = u_df[(u_df['quality']=='good')&(u_df['snr']>1)]
flatten = lambda l: [item[0] for sublist in l for item in sublist]
spikes = flatten(good_units['times'].to_list())
binwidth = 1
bins = np.arange(0, np.max(spikes), binwidth)
hist, bin_e = np.histogram(spikes, bins)
return hist, bin_e
def plot_all_spike_hist(probe_dict, FIG_SAVE_DIR, return_hist = False, prefix=''):
flatten = lambda l: [item[0] for sublist in l for item in sublist]
ash = {}
for p in probe_dict:
u_df = probe_dict[p]
good_units = u_df[(u_df['quality']=='good')&(u_df['snr']>1)]
spikes = flatten(good_units['times'].to_list())
binwidth = 1
bins = np.arange(0, np.max(spikes), binwidth)
hist, bin_e = np.histogram(spikes, bins)
ash[p] = hist
if FIG_SAVE_DIR is not None:
fig, ax = plt.subplots()
fig.suptitle('spike histogram (good units), Probe ' + p)
ax.plot(bin_e[1:-1], hist[1:])
ax.set_xlabel('Time (s)')
ax.set_ylabel('Spike Count per ' + str(binwidth) + ' second bin')
save_figure(fig, os.path.join(FIG_SAVE_DIR, prefix+'Probe' + p + ' spike histogram'))
save_as_plotly_json(fig, os.path.join(FIG_SAVE_DIR, prefix+'Probe' + p + ' spike histogram.plotly.json'))
if return_hist:
return ash
# for ip, probe in enumerate(probe_dirs):
# p_name = probe.split('_')[-2][-1]
# base = os.path.join(os.path.join(probe, 'continuous'), 'Neuropix-PXI-100.0')
# times_file = glob_file(base, 'spike_times.npy')
# if times_file is not None:
# times = np.load(times_file)
# times = times/30000.
#
# fig, ax = plt.subplots()
# bins = np.arange(0, times.max(), 1)
# hist, b = np.histogram(times, bins=bins)