-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParallel_mu_fit_3.py
1325 lines (956 loc) · 50.2 KB
/
Parallel_mu_fit_3.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
# server // analysis / fit / mu_fit / mu_fit_n.py
# env_2
# 2025-1-30 : this version is parallelized
# define the number of parallel jobs with the variable ; noj
# %%
# run the 'functions=' cell in the pre-req file.
# / Aryo / analysis / general / pre_req.py
# you should have the file : mean_sd_total.pkl
# it's run by the following program : // analysis / fit / mu_fit / sd_mean .py
# create a new destination folder ('mu') in windows explorer.
# fill the values of the 2 consequative cells below
# 1st cell ; fill the directories & the suptitle string
# 2nd cell ; sample_index : beginning_end ; G:\Aryo\analysis\sample \ sample .ods
# %%
# note : if you analyze not all channels, but with leaps : in this program : the initialization of the datframe is by 384 channels
# but those leaped channels are 0s.
# nmz in file's name = normalized.
# re_sig in file's name = response significance.
# also change the baseline for short sois.
# %%
# These columns should be later added to the dataframes :
# 'frequency', 'nsr', 'animal', 'hemisphere',
# 'penetration', 'stimulus', 'ck_pt_n', 'latency_average',
# 'nsr_6soi', 'AP', 'bf', 'rsbf', 'rsbf_abs']
# %%
# %%
# multi-block recordings
# for the extraction of the raw traces from the combined drift-corrected recordings : do not make any difference here,
# since it's already taken into account when merging several single recordings together.
# triggers => refer below : rec = session.recordnodes[0]... .
# for trigger
# change this directory.
directory = r'/home/azare/groups/PrimNeu/Aryo/copy_data/Benny_terminalexp/p9/4/2022-05-18_21-06-41'
# you should 1st create this in windows explorer.
# for pdfs & the database.
dest_dir = r'/home/azare/groups/PrimNeu/Aryo/analysis/Benny/p9/4/mu'
# this will be printed at sup-title.
description_session = 'Benny_terminal , right hemisphere _ p9_4_R (primate probe) , tone , 3.6 kHz'
#######
# these 2 are penetration-dependent , not measurement-dependent.
# for extracting the spikes.
# this is the drift-corrected file
# 1 file for each penetration
rd_pps_d = si.load_extractor(r'/home/azare/groups/PrimNeu/Aryo/analysis/sort/Benny/p9/drift' )
# directory of the silence periods' mean & sd of a particular penetration
# pnt : penetration
# 1 file for each penetration
dir_sil_pnt = r'/home/azare/groups/PrimNeu/Aryo/analysis/Benny/p9'
# %%
# => G:\Aryo\analysis\sample \ sample .ods <= // analysis / general / pre_req .py
sample_index_beginning = 116483904
sample_index_end = 173905944
# %%
# number of jobs ( ___ ) to be executed in Parallel.
# threads
# processes
noj=50
# %%
# %%
# related to the trigger.
# nte : umber of trials (out of 100) to exclude.
nte = 3
# nrt : number of remaining trials.
nrt = 100 - nte
# %%
# related to the channels
# number of vectors (channels) to analyze.
nva = 384
# the step for analysis of channels. Every 'leap' channels will be analysed. 5 channels = 100 microns.
# the total number of channels analyzed would be 384 / 5.
# 1 means : no leap : all channels will be considered.
leap = 1
# %%
# %%
# mean & sd pf the silence periods' of this penetration
# includes the silence periods of the whole 2 hours recording.
mean_sd_total = pd.read_pickle( dir_sil_pnt + '/mean_sd_total.pkl')
# %%
#################
#################
# pre-block rate.
ticks_pbr = np.arange(7) # tick positions : generally automatically set, but specifically needed if you want to set the labels (next line).
labels_pbr = np.arange(1,8)
########
# for significance testing : this version is modified to contain each bin containing 100ms : of course for pre-block part.
# tdm : tandem.
# res_tdm_8_soi : for the response : 10-110ms after each stimulus.
# base_tdm_8_soi : for baseline : during the 1min silence interval.
#############
# bin sizes :
# for response magnitude calculation (normalized ... ) : 25 ms : of course for both response & baseline periods.
# corresponding variables : base_neb_mean_8_100_rint , base_event_8 , m.
# for significance testing (distributions) : 100 ms : of course for both response & baseline periods.
# corresponding variables : they have 'tdm' (tandem) in their name. base_tdm_8_soi , res_tdm_8_soi.
###############
# changes in this version : bottom subfigure : adding another trace : base events as measured before each event (base_event_8).
# changes in this version ; instead subtracting 1 minute silence interval baseline from the max response : here the baseline before the event is subtracted
# in modules . py :
# from open_ephys.analysis import Session
# from scipy.optimize import curve_fit
# def fit_func(SOI, A, tau_0):
# return A*(1-np.exp(-(SOI-0.05)/tau_0))
# sois = np.array([ 0.11 , 0.195 , 0.345 , 0.611 , 1.081 , 1.914 , 3.388 , 6])
################
# for extracting the triggers.
# in case of non-separate recorindgs (multi-block recordings) : example : Elfie p2_2 : due to Michael's mistake : run the below line
# rec = session.recordnodes[0].recordings[1]
# the main 'directory' [ in session = Session(directory) ] is the directory of the parent recording (p2_1 : contains p2_2 ).
session = Session(directory)
rec = session.recordnodes[0].recordings[0]
ap = rec.continuous[0].samples
####################
# triggers.
trg = ap[: , 384]
t , di_t = ss.find_peaks( trg , plateau_size=20)
trg_re = di_t['left_edges']
# you can then check the shape of the array in Ipython console:
# trg_re.shape
#############
#############
# trg_re_r : trigger reshaped.
trg_re_r = trg_re.reshape(8,100)
# the new sorted trigger.
t_8_100 = np.zeros((8,100))
# this is the order of presentation of the sois.
# the order of presentation of the sois were randomized.
# here, one can fetch the actual order.
soi_order = [1,1,1,1,1,1,1,1] # string (below)
soi_order_numeric = [1,1,1,1,1,1,1,1]
# below : diff functions requires an array, not a vector. hence I reshaped it.
for i in range(8) :
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 3000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 3500) ) :
t_8_100[0 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_1'
soi_order_numeric[i] = 1
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 5000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 7000) ) :
t_8_100[1 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_2'
soi_order_numeric[i] = 2
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 10000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 11000) ) :
t_8_100[2 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_3'
soi_order_numeric[i] = 3
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 15000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 20000) ) :
t_8_100[3 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_4'
soi_order_numeric[i] = 4
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 30000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 35000) ) :
t_8_100[4 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_5'
soi_order_numeric[i] = 5
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 50000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 60000) ) :
t_8_100[5 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_6'
soi_order_numeric[i] = 6
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 100000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 110000) ) :
t_8_100[6 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_7'
soi_order_numeric[i] = 7
if ( ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] > 170000) & ( np.diff(trg_re_r[i , :].reshape(1,100))[0,0] < 200000) ) :
t_8_100[7 , :] = trg_re_r[i,:]
soi_order[i] = 'soi_8'
soi_order_numeric[i] = 8
########################
#######################
#######################
#######################
# %% after trigger.
# dataframe
# clm ; columns
# r2s : r2-score (goodness of fit).
# trs : test for response significance (p-value).
# tmp : template waveform.
# kde : the kde curves for all 8 sois.
# res_mag_8_soi' , 'res_abs_8_soi' , 'base_evt_8_soi' : respectively : a-b , a , b.
clm = [
'Tau_6' , 'A_6' , 'y_fit_6' , 'r2s_6' , # last 6 sois. 2 free parameters in the fit function.
'Tau_6_3p' , 'A_6_3p' , 't0_6_3p' , 'y_fit_6_3p' , 'r2s_6_3p' , # last 6 sois. 3 free parameters in the fit function.
'Tau_all' , 'A_all' , 'y_fit_all' , 'r2s_all' , # all sois. 2 parameter.
'Tau_all_3p' , 'A_all_3p' , 't0_all_3p' , 'y_fit_all_3p' , 'r2s_all_3p' , # all sois. 3 parameter.
'idx_sig' , 'Tau_sig' , 'A_sig' , 'y_fit_sig' , 'r2s_sig' , # only sois with statistically significant responses. 2 parameter.
'Tau_sig_3p' , 'A_sig_3p' , 't0_sig_3p' , 'y_fit_sig_3p' , 'r2s_sig_3p' , # only sois with statistically significant responses. 3 parameter.
'pbr_8_soi' , 'cv_ibr' , 'mean_ibr' , 'std_ibr' , # pre-inter-block rate : a criterion for stationarity.
'kde' ,
'res_mag_8_soi' , 'res_abs_8_soi' , 'base_evt_8_soi' , 'latency_8_soi' , 'window_8_soi' , 'x_lr_ms' , 'hh' ,
'l_f_8_soi' , 'l_f_8_soi_ms' , 'l_8_100' ,
'trs' , 'base_rate_8_soi' , 'res_tdm_8_soi' , # statistical comparison.
# errors : fit-6 : 6_sois , sig : incorporating only the significant ones. 2p , 3p : 2 parameter or 3 parameter fit.
# 0 : pre-set (no error). 1 : error.
'err_kde' , 'err_fit_all_2p' , 'err_fit_all_3p' , 'err_fit_6_2p' , 'err_fit_6_3p' , 'err_fit_sig_2p' , 'err_fit_sig_3p' ,
]
# initialize data
init_data = np.zeros( ( nva , len(clm) ) )
# initializing the dataframe.
# note : the index will be channel numbers
df = pd.DataFrame( data=init_data , index=np.arange(nva) , columns=clm )
###########################
# %%
# Means of all vectors (384 elements).
# vec_means = np.array([])
# vec_sds = np.array([])
# vector peaks.
#vec_p_all = []
length_v = ap[:,0].size
# you get them from the combined recording because you want to use the drift-corrected recording, which is combined one.
trace_combined = rd_pps_d.get_traces() # from the combined recording.
trace_section = trace_combined[ sample_index_beginning : sample_index_end , : ] # extracting 1 measurement's trace.
# %%
# %%
# %%
# %%
# v is needed as the input of the function, because it is an argument of the 'Parallel' function ( joblib ) in the main file :
# for v in range ...
# to make things simpler I embedded the function in this file.
def Parallelize (v) :
####################
# default error values.
err_fit_all_2p = 0
err_fit_all_3p = 0
err_fit_6_2p = 0
err_fit_6_3p = 0
err_fit_sig_2p = 0
err_fit_sig_3p = 0
#####################
# mean of sd extracted from the silence periods ( => sd_mean.py in this folder )
v_mean = mean_sd_total.loc[ v , 'mean']
v_sd = mean_sd_total.loc[ v , 'sd']
# vec_means = np.append(vec_means , v_mean )
# vec_sds = np.append(vec_sds , v_sd )
# vector peaks : positive & negative.
vec_p_pos , di_pos = ss.find_peaks( trace_section[ : , v ] , height = (v_mean + (3* v_sd) ) )
vec_p_neg , di_neg = ss.find_peaks( -trace_section[ : , v ] , height = (-v_mean + (3* v_sd) ) )
vec_p = np.concatenate(( vec_p_pos , vec_p_neg ))
# vector peaks.
#vec_p_all.append( vec_p )
# vector peacks _ continuous.
vec_p_c = np.zeros(length_v)
vec_p_c[vec_p] = 1
################
##################
# t_8_100 : the new trigger (for randomize sois): shape : (8,100).
# previous trigger (non-randomied sois) : trig_8_soi seemed to be a list of numpy arrays. since the number of triggers were not equal initially.
#################
#################
#### for the fit plot.
#### defining the baseline for all 8 sois.
#### base_8 : list of n arrays. each array is the peaks (spikes in the raw trace) of 1 soi during 10 second brfore the start of the train.
# : timestamp (or sample number) of each spike.
# here, 0 need not to be changed to nte. Since the baseline, not the intra-train period is needed.
base_8=[]
for i in range(8) :
base = vec_p[ (vec_p > ( t_8_100[i , 0] - 300000 ) ) & ( vec_p < t_8_100[i , 0] ) ] # here, 0 need not to be changed to nte.
base_8.append(base)
# these are possibly not needed.
#### base_neb_mean_8 : base of each of 8 sois / bins / mean / summed into 1 array.
#### here, there’s no need for concatenation of different segments, hence the continuous vector (vec_p_c) is not needed.
# here, 0 (in [i,0]) need not to be changed to nte. Since the baseline, not the intra-train period is needed.
base_neb_mean_8 = np.array([]) # this is for plotting the trend of baselines at the bottom of the multi_plot. This is the base in the silent inter-train interval.
base_tdm_8_soi = [1,1,1,1,1,1,1,1] # a list of 8 np arrays. each np array corresponds to 1 soi baseline. This is used to make a distribution to compare with response to test the significance.
for i in range(8) :
base_neb , edges = np.histogram(base_8[i] , bins=400 , range=( t_8_100[i , 0] - 300000 , t_8_100[i , 0] )) # creating a histogram from a non-continuous data (base_8).
base_neb_tdm , edges = np.histogram(base_8[i] , bins=100 , range=( t_8_100[i , 0] - 300000 , t_8_100[i , 0] )) # this is for testing the significance.
base_neb_mean_8 = np.append(base_neb_mean_8 , np.mean(base_neb) )
base_tdm_8_soi[i] = base_neb_tdm
# this snippet is modified to contain each bin containing 100ms : of course for pre-block part.
# the bin width (for example 25ms) should be constant between the baseline & response distributions.
# the total number of bins need not to be equal : for example 400 bins and 800 bins. It's like comparing 2 samples with different sample sizes.
# for the new stats based on half-heights, it's not needed.
# this is for testing the response significance : response part.
# res_tdm_8_soi : a list of 8 np arrays.
# each np array corresponds to 1 soi (response, not baseline).
# one_stimulus_segment : time stamps of peaks.
# 100ms = big bin.
res_tdm_8_soi = [1,1,1,1,1,1,1,1]
for i in range (8):
res_tdm_1_soi = np.array([]) # response tandem , tandem = not overlapped.
for j in range (nte , 100) :
one_stimulus_segment = vec_p[ (vec_p > ( t_8_100[i , j] + 300 ) ) & ( vec_p < (t_8_100[i , j] + 3300 )) ]
res_tdm_1_soi = np.append ( res_tdm_1_soi , one_stimulus_segment.shape ) # here, all (not sum of) spike counts in each big bin (100ms) are added together.
res_tdm_8_soi[i] = res_tdm_1_soi
# not_needed : a junk variable. not needed.
# pv = p value
not_needed , pv = mannwhitneyu(base_tdm_8_soi[7] , res_tdm_8_soi[7])
# if pv < 0.05 :
# significance_soi_8 = pv
# else :
# significance_soi_8 = 'not significant'
#### Must be multiplied to 100 to be compatible with 100 repeatitions.
base_neb_mean_8_100 = base_neb_mean_8 * 100
#### rint : rounded to the nearest integer.
base_neb_mean_8_100_rint = np.rint(base_neb_mean_8_100)
# %%
#################
###################
# l_f_8_soi is the common & nuclear step for both psth & fit plots.
l_f_8_soi = [1,1,1,1,1,1,1,1]
# this is used for creating raster plots.
# this is for 8 sois * 100 repeatitions / soi : it's dimension is actually 8 * 101
# this could have also been defined as an empty list : l_8_100 = [] instead, but then adding each soi to it should have been done differntly (see below).
l_8_100 = [1,1,1,1,1,1,1,1]
for h in range(8):
smp = np.zeros(18000)
for i in t_8_100[h , nte:] :
smp = np.vstack( (smp , vec_p_c[ int(i-3000) : int(i+15000) ] ) )
l = []
for j in range(nrt+1): # +1 : probably because smp has one row of 0s.
l.append( np.asarray(np.where(smp[j,:]==1)).flatten().tolist() ) # hence converting a continuous to a discrete array. This discrete array will be used to make a histogram.
l_f = [j for i in l for j in i]
l_f_8_soi[h] = l_f
l_8_100[h] = l
###################
c = 600/18000
d = 100
l_f_8_soi_ms = []
for k in range(8) :
l_f_8_soi_ms.append( (np.array(l_f_8_soi[k]))*c - d )
for i in range(8):
for j in range(nrt+1):
l_8_100[i][j] = ((np.array(l_8_100[i][j]))*c)-d
#################
# %% parameters + plots.
# figure : for both psth & fit.
fig = plt.figure(figsize=(17,14) , constrained_layout=True)
subfigs = fig.subfigures(3,1 , wspace=0.1 , height_ratios=[2,1,1])
ax_top = subfigs[0].subplots(2,4 , sharex=True , sharey=True)
ax_r =ax_top.ravel()
ax_bottom = subfigs[1].subplots(1,3)
ax_3 = subfigs[2].subplots(1,3)
#############
##############
## response magnitude
res_mag_8_soi = np.zeros(8)
## absolute response.
res_abs_8_soi = np.zeros(8)
## baseline activity , pre-event . evt : event.
base_evt_8_soi = np.zeros(8)
# pbr : pre-block rate [1 minute silence period].
pbr_8_soi = np.zeros(8)
####
# xl_ms & xr_ms : left & right x values of the half-height window :
# each row is 1 soi : 2 columns corresponding to xl_ms & sr_ms respectively.
x_lr_ms = np.zeros((8,2))
hh = np.zeros(8) # half-height : not the abolute value : the value of y on the curve at which the half-height intercepts it.
####
# not used : intra-period rate normalization.
# ultimate goal was : hh_rate_8_soi / period_rate_8_soi _ as a normalization tool.
hh_spk_8_soi = [0,0,0,0,0,0,0,0] # spk : spikes : the number of spikes for each trial inside the hh-window is saved here.
# tns : total number of spikes : inside the window of the half-hight : equals the sum of the above array for each soi.
hh_tns_8_soi = np.zeros(8)
period_tns_8_soi = np.zeros(8) # period ; the 110 ms period starting from the stimulus onset : to be equal in all sois.
hh_rate_8_soi = np.zeros(8) # hh_tns_8_soi / window period for each particular soi.
period_rate_8_soi = np.zeros(8) # period_tns_8_soi / 110 ms
####
# the width of the response kde.
window_8_soi = np.zeros(8)
# latency for all 8 sois.
latency_8_soi = np.zeros(8)
# the kde curves.
# since the values for each soi is an array with a different shape, I gather them as a list here.
kde_8_soi = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
# these 3 are for the statistical testing.
# the reason the first 2 are 'lists' is that each entry contains an array of shape '100'.
res_tdm_8_soi = [1,1,1,1,1,1,1,1]
base_rate_8_soi = [1,1,1,1,1,1,1,1]
trs = np.zeros(8)
for i in range(8):
# necf : normalized_to_estimated rate , conversion factor. this is used below for the kernel density estimation.
# * 1000 : to convert from (/ms) to Hz.
# x_vk , y_vk : vk : variable kernel.
necf = ( ( l_f_8_soi_ms[i].size ) / nrt ) * 1000
# variable kernel.
try :
y_vk , x_vk , o3 , o4 , o5 , o6 , o7 = opt.ssvkernel( x = l_f_8_soi_ms[i] )
except ValueError : # culprit array : an array of size 0 [an empty array].
x_vk , y_vk = np.array([]) , np.array([])
except UnboundLocalError : # while loop not satisfied (not 'True'). culprit array was : an array of size 3.
x_vk , y_vk = np.array([]) , np.array([])
except IndexError : # culprit : an array of size 1 (only 1 spike)
x_vk , y_vk = np.array([]) , np.array([])
# c : converted.
y_vk_c = y_vk * necf # deriving the estimated firing rate.
# xy : the coupled (for vectorized operations below) x & y.
xy_vk = np.vstack(( x_vk , y_vk_c ))
kde_8_soi[i] = xy_vk
# res : response
res_vk = xy_vk[: , (xy_vk[0 , :] > 0 ) & (xy_vk[0 , :] < 100 ) ]
# be : baseline relative to event (pre-event).
be_vk = xy_vk[: , (xy_vk[0 , :] < 0 ) ]
# checking if it is an empty array : this may result 'nan' values or a value error by further operations.
if res_vk.size==0 : # you can write : ... res_gk[1,:].size==0 : this makes no difference.
max_vk = 0
idx = 0
latency = 0
else :
# maximum of the response period.
max_vk = np.max(res_vk[1 , :])
# idx : index of the maximum.
idx = np.argmax( res_vk[1, :] )
# latency (ms) of the max_gk defined above, relative to 0 (stimulus onset).
latency = res_vk[0,idx]
latency_8_soi[i] = latency
# m : mean of baseline.
if be_vk.size == 0 :
be_vk_m = 0
else :
be_vk_m = np.mean(be_vk[1,:])
res_mag = max_vk - be_vk_m
res_mag_8_soi[i] = res_mag
res_abs_8_soi[i] = max_vk
base_evt_8_soi[i] = be_vk_m
########
# half-heights : x values
# input of the next step should be an array (not an integer).
idx_a = np.array([idx])
# xl , xr : interpolated indices , in ms.
# rel_height = 1 : calculates the width at the base of the peak.
if res_vk[1, :].size == 0 :
width , height , xl , xr = np.array([0]) , np.array([0]) , np.array([0]) , np.array([0])
else :
width , height , xl , xr = ss.peak_widths( res_vk[1, :] , idx_a , rel_height=0.5 )
# i : integer : it's still the index of the kde curve.
xl_i = int(xl[0])
xr_i = int(xr[0])
# converting the index to ms.
if res_vk[1, :].size == 0 :
xl_ms , xr_ms = 0 , 0
else :
xl_ms = res_vk[0 , xl_i]
xr_ms = res_vk[0 , xr_i]
x_lr_ms[i , 0] = xl_ms
x_lr_ms[i , 1] = xr_ms
hh[i] = height # half-height.
# window : the period in x axis between the half-heights.
# this can also be derived from the 'width' variable above.
window = xr_ms - xl_ms
window_8_soi[i] = window
# converting ms to samples.
# note : these x distances start from time 0 (stimulus onset).
xl_sa = int(xl_ms * 30)
xr_sa = int(xr_ms * 30)
###########
#onb , o2,o3,o4,o5 = opt.sshist(x=l_f_8_soi_ms[i]) # this is for optimal bin size.
ax_r[i].plot( x_vk , y_vk_c , linewidth=3 )
ax_r[i].hlines( height , xl_ms , xr_ms , color='m' )
#############
ax_ep = ax_r[i].twinx() # ep : event plot (raster).
ax_ep.eventplot(l_8_100[i] , linewidths=0.75 , linelengths=0.75 , colors='k')
ax_r[i].axvline(x=0 , color='k' )
#############
# %%
###############
###############
# this is for testing the response significance : response part.
# res_tdm_8_soi : a list of 8 np arrays.
# each np array corresponds to 1 soi (response, not baseline).
# one_stimulus_segment : time stamps of peaks.
# 100ms = big bin.
res_tdm_1_soi = np.array([]) # response tandem , tandem = not overlapped. rate based (per trial) : for the statistical comparison.
hh_spk_1_soi = np.zeros(nrt) # used for measuring adaptation trend per soi.
hh_tns_1_soi = np.array([]) # not used : for intra-period rate normalization.
for j in range (nte,100) :
one_trial_response_spikes = vec_p[ (vec_p >= ( t_8_100[i , j] + xl_sa ) ) & ( vec_p <= (t_8_100[i , j] + xr_sa )) ]
hh_spk_1_soi[j-nte] = one_trial_response_spikes.size
# .size : number of spikes in that period.
if window == 0 :
one_trial_response_rate = 0
else :
one_trial_response_rate = one_trial_response_spikes.size / window
# here, all (not sum of) spike counts in each big bin (100ms) are added together.
res_tdm_1_soi = np.append ( res_tdm_1_soi , one_trial_response_rate )
res_tdm_8_soi[i] = res_tdm_1_soi
hh_spk_8_soi[i] = hh_spk_1_soi
######
base_rate_1_soi = np.array([]) # response tandem , tandem = not overlapped.
for j in range (nte,100) :
# here, all (not sum of) spike counts in each big bin (100ms) are added together.
one_trial_base_spikes = vec_p[ (vec_p > ( t_8_100[i , j] - 3000 ) ) & ( vec_p < t_8_100[i , j] ) ]
# .size : number of spikes in that period.
one_trial_base_rate = one_trial_base_spikes.size / 100 # 100 : 100ms : the unit (response rate ? (window)) is ms since in reponse it's also ms.
base_rate_1_soi = np.append ( base_rate_1_soi , one_trial_base_rate )
base_rate_8_soi[i] = base_rate_1_soi
# %%
######
# her the pre-block period is the preferred terminolgy ; because of the 1st soi. Later the other periods will be named inter-block.
pre_block_spikes = vec_p[ (vec_p > ( t_8_100[i , 0] - 1800000 ) ) & ( vec_p < t_8_100[i , 0] ) ]
pre_block_rate = pre_block_spikes.size / 60 # 60 s : the 1 minute silence interval.
pbr_8_soi[i] = pre_block_rate
######
# 'not_needed' : a junk variable. not needed.
# pv = p value
# trs : test for response significance
# the 'if' statement is needed if there is no single spike in all 100 repeatitions of 1 soi, in the corresponding time periods.
if res_tdm_8_soi[i].size == 0 :
pv = 1
elif base_rate_8_soi[i].size == 0 : # due to stepping from the top, res.size here is not 0.
pv = 0
else :
try :
not_needed , pv = wilcoxon( base_rate_8_soi[i] , res_tdm_8_soi[i] , alternative='less')
except ValueError : # => DELL / analysis / stat / stat.docx for the details.
pv = 1
trs[i] = pv
##########
# %%
# ibr : inter-block rate.
std_ibr = np.std(pbr_8_soi[1:])
mean_ibr = np.mean(pbr_8_soi[1:])
cv_ibr = std_ibr / mean_ibr # cv : coefficient of variation.
# %%
# fit for normalized response.
# 6 : 6 sois : from soi_3 onwards : soi_1 & soi_2 are omitted due to the overlapping of response on baseline.
# respecting pre-event baseline.
# nan values were converted above.
try :
popt_6, pcov_6 = curve_fit(fit_func, sois[2:] , res_mag_8_soi[2:] )
except RuntimeError :
popt_6 = np.array([ 0.5 , 0 ])
pcov_6 = 0
err_fit_6_2p = 1
# t0 as a free parameter (instead of being sd (stimulus duration) as before).
try :
popt_6_3p, pcov_6_3p = curve_fit(fit_func_3p, sois[2:] , res_mag_8_soi[2:] )
except RuntimeError :
popt_6_3p = np.array([ 0.5 , 0 , 0.05 ])
pcov_6_3p = 0
err_fit_6_3p = 1
# y of the fitted curve based on the formerly derived parmeters (popt)
y_fit_6 = fit_func(sois[2:] , *popt_6)
y_fit_6_3p = fit_func_3p(sois[2:] , *popt_6_3p)
# r2_score : goodness of fit.
r2s_6 = r2_score( res_mag_8_soi[2:] , y_fit_6 )
r2s_6_3p = r2_score( res_mag_8_soi[2:] , y_fit_6_3p )
##################
# all : for all sois.
# fit for absolute responses.
try :
popt_all , pcov_all = curve_fit(fit_func, sois , res_abs_8_soi )
except RuntimeError :
popt_all= np.array([ 0.5 , 0 ])
pcov_all = 0
err_fit_all_2p = 1
try :
popt_all_3p , pcov_all_3p = curve_fit(fit_func_3p, sois , res_abs_8_soi , method='lm' )
except RuntimeError :
popt_all_3p = np.array([ 0.5 , 0 , 0.05 ])
pcov_all_3p = 0
r2s_all_3p = 0
err_fit_all_3p = 1
# the other fitting method
try :
popt_all_3p_dogbox , pcov_all_3p_dogbox = curve_fit(fit_func_3p, sois , res_abs_8_soi , method='dogbox' )
except RuntimeError :
popt_all_3p_dogbox = np.array([ 0.5 , 0 , 0.05 ])
pcov_all_3p_dogbox = 0
r2s_all_3p_dogbox = 0
# df.loc[ v , 'err_fit_all_3p' ] = 1
# y of the fitted curve based on the formerly derived parmeters (popt)
y_fit_all = fit_func(sois , *popt_all)
y_fit_all_3p = fit_func_3p(sois , *popt_all_3p)
y_fit_all_3p_dogbox = fit_func_3p(sois , *popt_all_3p_dogbox)
# r2_score : goodness of fit.
r2s_all = r2_score( res_abs_8_soi , y_fit_all )
r2s_all_3p = r2_score( res_abs_8_soi , y_fit_all_3p )
r2s_all_3p_dogbox = r2_score( res_abs_8_soi , y_fit_all_3p_dogbox )
# here, it compares the results of the 2 fitting methods (lm & dogbox) & selects the one with a better fit.
if r2s_all_3p_dogbox > r2s_all_3p :
popt_all_3p = popt_all_3p_dogbox
pcov_all_3p = pcov_all_3p_dogbox
y_fit_all_3p = y_fit_all_3p_dogbox
r2s_all_3p = r2s_all_3p_dogbox
err_fit_all_3p = 0
else :
pass # this means : don't do anything !
#########
# here, only sois with statistically significant responses are fitted to the function.
# index of sois with significant responses.
# [0] @ the end of it : because the output is a tuple. The 1st element is a numpy array.
idx_sig = np.where(trs<0.05)[0]
if idx_sig.size < 2 :
popt_sig = np.array([ 0.5 , 0 ])
popt_sig_3p = np.array([ 0.5 , 0 , 0.05 ]) # t0 is put 0.05 so that it would be similar to the original function : no additional error.
pcov_sig = 0
pcov_sig_3p = 0
err_fit_sig_2p = 1
err_fit_sig_3p = 1
elif idx_sig.size < 3 :
try :
popt_sig , pcov_sig = curve_fit( fit_func, sois[idx_sig] , res_mag_8_soi[idx_sig] ) # a-b according to Michale's suggestion.
except RuntimeError :
popt_sig = np.array([ 0.5 , 0 ])
pcov_sig = 0
err_fit_sig_2p = 1
popt_sig_3p = np.array([ 0.5 , 0 , 0.05 ])
pcov_sig_3p = 0
err_fit_sig_3p = 1
else :
try :
popt_sig , pcov_sig = curve_fit( fit_func, sois[idx_sig] , res_mag_8_soi[idx_sig] )
except RuntimeError :
popt_sig = np.array([ 0.5 , 0 ])
pcov_sig = 0
err_fit_sig_2p = 1
try :
popt_sig_3p , pcov_sig_3p = curve_fit( fit_func_3p , sois[idx_sig] , res_mag_8_soi[idx_sig] )
except RuntimeError :
popt_sig_3p = np.array([ 0.5 , 0 , 0.05 ])
pcov_sig_3p = 0
err_fit_sig_3p = 1
# y of the fitted curve based on the formerly derived parmeters (popt)
y_fit_sig = fit_func(sois , *popt_sig)
y_fit_sig_3p = fit_func_3p(sois , *popt_sig_3p)
# r2_score : goodness of fit.
if idx_sig.size < 2 : # r2_score needs a minimum amount of input data to function.
r2s_sig = 0
r2s_sig_3p = 0
else :
r2s_sig = r2_score( res_mag_8_soi[idx_sig] , y_fit_sig[idx_sig] )
r2s_sig_3p = r2_score( res_mag_8_soi[idx_sig] , y_fit_sig_3p[idx_sig] )
# %%
# %%
ax_r[0].axvline(x=-110 , color='k')
ax_r[0].axvline(x=110 , color='k')
ax_r[0].axvline(x=220 , color='k')
ax_r[0].axvline(x=330 , color='k')
ax_r[0].axvline(x=440 , color='k')
ax_r[0].axvline(x=550 , color='k')
ax_r[1].axvline(x=195 , color='k')
ax_r[1].axvline(x= 390 , color='k')
ax_r[2].axvline(x=345 , color='k')
for i in range(8):
ax_r[i].set_xlim( -100 , 500 )
# this highlights the frame of the plots with significant responses.
for i in idx_sig :
ax_r[i].spines[:].set_color('blue')
ax_r[i].spines[:].set_linewidth(4)
##################
ax_r[7].set_xlabel('time in ms')
ax_r[0].set_ylabel('KDE' , fontsize=9)
ax_r[0].set_title('soi_1 = 110 ms')
ax_r[1].set_title('soi_2 = 195 ms')
ax_r[2].set_title('soi_3 = 345 ms')
ax_r[3].set_title('soi_4 = 611 ms')
ax_r[4].set_title('soi_5 = 1.081 s')
ax_r[5].set_title('soi_6 = 1.914 s')
ax_r[6].set_title('soi_7 = 3.388 s')
ax_r[7].set_title('soi_8 = 6 s')
########################
################
# fit plot.
# here, the main solid plot incoroporates the baseline before events, not the baseline during the 1min silence interval.
# fit plot.
# here, the main solid plot incoroporates the baseline before events, not the baseline during the 1min silence interval.
########################
################
# fit plot for all sois.
# here, the main solid plot incoroporates the baseline before events, not the baseline during the 1min silence interval.
# fit_2p & _3p : 2 parameters (A & Tau) or 3 parameters (A , Tau , t0).
ax_bottom[0].plot(sois , res_abs_8_soi , linestyle='solid' , color='k' , label='actual response')
ax_bottom[0].plot(sois , y_fit_all , linestyle='dotted' , color='k' ,
label='fit_2p: A_all=' + str(np.around( popt_all[0], decimals=3)) +
' , τ_all=' + str(np.around( popt_all[1], decimals=3)) )
ax_bottom[0].plot(sois , y_fit_all_3p , linestyle='dashdot' , color='k' ,
label='fit_3p: A_all_3p=' + str(np.around( popt_all_3p[0], decimals=3)) +
' , τ_all_3p=' + str(np.around( popt_all_3p[1], decimals=3)) +
' , t0_all_3p=' + str(np.around( popt_all_3p[2], decimals=3)) )
ax_bottom[0].set_xticks(ticks=sois)
ax_bottom[0].tick_params(axis='x' , labelrotation=90 , labelsize=6)
ax_bottom[0].set_xlabel('soi(ms)' , loc='right')
ax_bottom[0].set_title('all sois : actual response _ fit \n r2_score all_sois_2p : ' +
str(np.around(r2s_all , decimals=2)) +
' __ r2_score all_sois_3p : ' + str(np.around(r2s_all_3p , decimals=2))
, fontsize=9)
ax_bottom[0].legend( fontsize=8 )
################
# fit plot _ 6 sois.
# here, the main solid plot incoroporates the baseline before events, not the baseline during the 1min silence interval.
ax_bottom[1].plot(sois[2:] , res_mag_8_soi[2:] , linestyle='solid' , color='k' , label='normalized response')
ax_bottom[1].plot(sois[2:] , y_fit_6 , linestyle='dotted' , color='k' ,
label='fit_2p : A_6=' + str(np.around( popt_6[0], decimals=3)) +
' , τ_6=' + str(np.around( popt_6[1], decimals=3)) )
ax_bottom[1].plot(sois[2:] , y_fit_6_3p , linestyle='dashdot' , color='k' ,
label='fit_3p : A_6_3p=' + str(np.around( popt_6_3p[0], decimals=3)) +
' , τ_6_3p=' + str(np.around( popt_6_3p[1], decimals=3)) +
' , t0_6_3p=' + str(np.around( popt_6_3p[2], decimals=3)) )
ax_bottom[1].set_xticks(ticks=sois[2:])
ax_bottom[1].tick_params(axis='x' , labelrotation=90 , labelsize=6)
ax_bottom[1].set_xlabel('soi(ms)' , loc='right')
ax_bottom[1].set_title('6 sois : normalized response respecting pre_event _ fit \n r2_score 6_soi_2p: ' +
str(np.around(r2s_6 , decimals=2)) +
' __ r2_score 6_soi_3p: ' + str(np.around(r2s_6_3p , decimals=2))
, fontsize=9)
ax_bottom[1].legend( fontsize=8 )
################
# fit plot for significant sois.
# here, the main solid plot incoroporates the baseline before events, not the baseline during the 1min silence interval.
# TypeError : The number of func parameters must not exceed the number of data points.
empty = np.array([]) # this is only to automatically add a text as 'label' in the plot.
ax_bottom[2].plot(sois[idx_sig] , res_abs_8_soi[idx_sig] , linestyle='solid' , color='k' , label='actual significant responses')
if idx_sig.size < 2 :
ax_bottom[2].plot( empty , color='w' , label='number of significant responses < 2' ) # I made the line legend color invisible : color='w'.
elif idx_sig.size < 3 :
ax_bottom[2].plot(sois , y_fit_sig , linestyle='dotted' , color='k' ,
label='fit_2p: A_sig=' + str(np.around( popt_sig[0], decimals=3)) +
' , τ_sig=' + str(np.around( popt_sig[1], decimals=3)) )
ax_bottom[2].plot( empty , color='w' , label='number of significant responses = 2 : no 3-parameter calculation.' )
else :
ax_bottom[2].plot(sois , y_fit_sig , linestyle='dotted' , color='k' ,
label='fit_2p: A_sig=' + str(np.around( popt_sig[0], decimals=3)) +
' , τ_sig=' + str(np.around( popt_sig[1], decimals=3)) )
ax_bottom[2].plot(sois , y_fit_sig_3p , linestyle='dashdot' , color='k' ,
label='fit_3p: A_sig_3p=' + str(np.around( popt_sig_3p[0], decimals=3)) +
' , τ_sig_3p=' + str(np.around( popt_sig_3p[1], decimals=3)) +
' , t0_sig_3p=' + str(np.around( popt_sig_3p[2], decimals=3)) )
ax_bottom[2].set_xticks(ticks=sois)
ax_bottom[2].tick_params(axis='x' , labelrotation=90 , labelsize=6)