-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron_matching.py
3201 lines (2459 loc) · 145 KB
/
neuron_matching.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
'''
function written by Alexander Schmidt, based on the paper "Sheintuch et al., ...", allowing for complete registration of neuron footprints across several sessions
TODO:
* write plotting procedure for cluster footprints (3D), to allow manual corrections
* save data-attribute / structure after model-building, not only after registration
* change save structure, such that all that is needed for further analysis is readily accessible:
- filePath of results file
- no redundancy in SNR, r_values, cnn saving
- 'remap' into 'alignment' structure in results
- cm only needed once
last updated on January 28th, 2024
'''
import os, cv2, copy, time, logging, pickle, tqdm, h5py
import numpy as np
from inspect import signature
import scipy as sp
from scipy import signal
from scipy.optimize import curve_fit, linear_sum_assignment
import scipy.stats as sstats
from matplotlib import pyplot as plt, rc, colors as mcolors, patches as mppatches, lines as mplines, cm
from matplotlib.widgets import Slider
from .utils import *
logging.basicConfig(level=logging.INFO)
class matching:
def __init__(self,mousePath,paths=None,suffix='',matlab=False,logLevel=logging.ERROR):
'''
TODO:
* put match results into more handy shape:
- list of filePaths
- SNR, etc not twice (only in results)
- remap in results, not data (shift=0,corr=1)
- p_same containing best and best_without_match value and being in results
'''
self.matlab = matlab
## make sure suffix starts with '_'
if suffix and suffix[0] != '_':
suffix = '_' + suffix
assert not (paths is None), 'You have to provide a list of paths to the CaImAn results files to be processed!'
# if not mousePath:
# mousePath = 'data/555wt'
# if not paths:
# ## create paths that should be processed
# if fileName_results[-1] == '*':
# paths = [
# os.path.join(mousePath,sessionPath,os.path.splitext(fileName)[0] + suffix + ('.mat' if self.matlab else '.hdf5') )
# for sessionPath in sorted(os.listdir(mousePath)) if sessionPath.startswith('Session')
# for fileName in os.listdir(os.path.join(mousePath,sessionPath)) if fileName.startswith(fileName_results[:-1])]
# else:
# paths = [
# os.path.join(mousePath,sessionPath,os.path.splitext(fileName_results)[0] + suffix + ('.mat' if self.matlab else '.hdf5') )
# for sessionPath in sorted(os.listdir(mousePath)) if 'Session' in sessionPath]
# # paths.sort()
# print(f'{paths=}')
self.log = logging.getLogger("matchinglogger")
self.log.setLevel(logLevel)
self.paths = {
'sessions': paths, # list of paths to CaImAn result files to be processed in order
'data': mousePath, # path to which results are stored and loaded from
'suffix': suffix,
}
# mP = matching_params
self.params = matching_params
self.params['model'] = 'correlation'
self.params['correlation_model'] = 'shifted'
self.update_bins(self.params['nbins'])
## initialize the main data dictionaries
self.data_blueprint = {
'nA': np.zeros(2,'int'),
'cm': None,
'p_same': None,
'idx_eval': None,
'SNR_comp': None,
'r_values': None,
'cnn_preds':None,
'filePath': None,
}
self.data = {}
self.data_tmp = {}
self.data_cross = {
'D_ROIs': [],
'fp_corr': [],
}
self.status = {
'counts_calculated': False,
'model_calculated': False,
'neurons_matched': False,
}
# ## defines the functions to fit to the count histograms for calculation of p_same
# self.model['fit_functions'] = {
# ## for the single model
# 'single': {
# 'distance': {
# 'NN': 'lognorm',
# 'nNN': 'linear_sigmoid',
# },
# 'correlation': {
# 'NN': 'lognorm_reverse',
# 'nNN': ['gauss','beta'],
# }
# },
# ## for the joint model
# 'joint': {
# 'distance': {
# 'NN': 'lognorm',
# 'nNN': 'gauss'
# },
# 'correlation': {
# 'NN': 'lognorm_reverse',
# 'nNN': 'gauss'
# }
# }
# }
def update_bins(self,nbins):
self.params['nbins'] = nbins
self.params['arrays'] = self.build_arrays(nbins)
def build_arrays(self,nbins):
## create value arrays for distance and footprint correlation
arrays = {}
arrays['distance_bounds'] = np.linspace(0,self.params['neighbor_distance'],nbins+1)#[:-1]
arrays['correlation_bounds'] = np.linspace(0,1,nbins+1)#[:-1]
arrays['distance_step'] = self.params['neighbor_distance']/nbins
arrays['correlation_step'] = 1./nbins
arrays['distance'] = arrays['distance_bounds'][:-1] + arrays['distance_step']/2
arrays['correlation'] = arrays['correlation_bounds'][:-1] + arrays['correlation_step']/2
## update histogram counts (if nbins_new < nbins), merge counts, otherwise split evenly
# self.log.warning('implement method to update count histograms!')
return arrays
def run_matching(self,p_thr=[0.3,0.05],save_results=True):
print('Now running matching procedure in %s'%self.paths['data'])
print('Building model for matching ...')
self.build_model(save_results=save_results)
print('Matching neurons ...')
self.register_neurons(save_results=save_results,p_thr=p_thr)
print('Done!')
def build_model(self,save_results=False):
'''
Iterate through all sessions in chronological order to build a model for neuron matching.
The model assumes that the closest neuron to a neuron in the reference session ("nearest neighbor", NN) has a high probability of being the same neuron. Neuron distance and footprint correlation are calculated and registered distinctly for NN and "non-nearest neighbours" (nNN, within the viscinity of each neuron) in a histogram, which is then used to build a 2D model of matching probability, assuming a characteristic function for NN and nNN.
This function takes several steps applied to each session:
- align data to reference session (rigid and non-rigid shift), or skip if not possible
- remove highly correlated footprints within one session
- calculate kernel density of neuron locations (if requested by "use_kde")
- update count histogram
Finally, the count histogram is used to create the model
'''
self.update_bins(128)
self.model = {
'counts': np.zeros((self.params['nbins'],self.params['nbins'],3),'int'),
'counts_unshifted': np.zeros((self.params['nbins'],self.params['nbins'],3),'int'),
'counts_same': np.zeros((self.params['nbins'],self.params['nbins']),'int'),
'counts_same_unshifted': np.zeros((self.params['nbins'],self.params['nbins']),'int'),
'kernel': {
'idxes': {},
'kde': {}
},
}
self.nS = len(self.paths['sessions'])
self.progress = tqdm.tqdm(enumerate(self.paths['sessions']),total=self.nS)
# print(self.paths['sessions'])
has_reference = False
# s_ref = 0
for (s,self.currentPath) in self.progress:
self.data[s] = copy.deepcopy(self.data_blueprint)
self.data[s]['filePath'] = self.currentPath
self.data[s]['skipped'] = False
self.A = self.load_footprints(self.currentPath,s)
if isinstance(self.A,bool):
self.data[s]['skipped'] = True
continue
self.progress.set_description(f'Aligning data from {self.currentPath}')
## prepare (and align) footprints
prepared,self.data[s]['remap'] = self.prepare_footprints(align_to_reference=has_reference)
self.calculate_footprint_data(s)
if not prepared:
self.data[s]['skipped'] = True
continue
if self.params['use_kde']:
# self.progress.set_description('Calculate kernel density for Session %d'%s0)
self.position_kde(s) # build kernel
self.update_joint_model(s,s)
if has_reference:
self.progress.set_description('Calculate cross-statistics for %s'%self.currentPath)
self.data_cross['D_ROIs'],self.data_cross['fp_corr'],_ = calculate_statistics(
self.A,A_ref=self.A_ref,idx_eval=self.data[s]['idx_eval'],idx_eval_ref=self.data[s_ref]['idx_eval'],
binary=self.params['binary'],neighbor_distance=self.params['neighbor_distance'],convert=self.params['pxtomu'],model=self.params['correlation_model'],
dims=self.params['dims']
) # calculating distances and footprint correlations
self.progress.set_description('Update model with data from %s'%self.currentPath)
self.update_joint_model(s,s_ref)
self.A_ref = self.A.copy()
self.Cn_ref = self.data_tmp['Cn']
s_ref = s
has_reference = True
self.dynamic_fit()
if save_results:
self.save_model(suffix=self.paths['suffix'])
def dynamic_fit(self):
times = 0
while times<3:
try:
self.fit_model(times=times)
success = True
except:
success = False
if success and self.model['p_same']['which']=='joint':
print(f'found proper fit @times={times}')
break
times += 1
def register_neurons(self, p_thr=[0.3,0.05], save_results=False, model='shifted'):
'''
This function iterates through sessions chronologically to create a set of all detected neurons, matched to one another based on the created model
p_thr - (list of floats)
2 entry-list, specifying probability above which matches are accepted [0]
and above which losing contenders for a match are removed from the data [1]
'''
self.params['correlation_model'] = model
self.nS = len(self.paths['sessions'])
self.data = {}
# if not self.model['f_same']:
assert self.status['model_calculated'], 'Model not yet created - please run build_model first'
## load and prepare first set of footprints
s=0
while True:
self.data[s] = copy.deepcopy(self.data_blueprint)
self.data[s]['filePath'] = self.paths['sessions'][s]
self.data[s]['skipped'] = False
self.A = self.load_footprints(self.paths['sessions'][s],s)
if isinstance(self.A,bool):
self.data[s]['skipped'] = True
s += 1
continue
else:
break
self.progress = tqdm.tqdm(enumerate(self.paths['sessions'][s+1:],start=s+1),total=self.nS-(s+1),leave=True)
self.prepare_footprints(align_to_reference=False)
self.calculate_footprint_data(s)
self.Cn_ref = self.data_tmp['Cn']
self.A_ref = self.A[:,self.data[s]['idx_eval']]
self.A0 = self.A.copy() ## store initial footprints for common reference
## initialize reference session, containing the union of all neurons
self.data['joint'] = copy.deepcopy(self.data_blueprint)
self.data['joint']['nA'][0] = self.A_ref.shape[1]
self.data['joint']['idx_eval'] = np.ones(self.data['joint']['nA'][0],'bool')
self.data['joint']['cm'] = center_of_mass(self.A_ref,self.params['dims'][0],self.params['dims'][1],convert=self.params['pxtomu'])
## prepare and initialize assignment- and p_matched-arrays for storing results
self.results = {
'assignments': np.full((self.data[s]['nA'][1],self.nS),np.NaN),
'p_matched': np.full((self.data[s]['nA'][1],self.nS,2),np.NaN),
'Cn_corr': np.full((self.nS,self.nS),np.NaN),
}
self.results['assignments'][:,s] = np.where(self.data[s]['idx_eval'])[0]
self.results['p_matched'][:,0,0] = 1
for (s,self.currentPath) in self.progress:
if not self.currentPath:
continue
session_name = os.path.dirname(self.paths['sessions'][s]).split('/')[-1]
self.data[s] = copy.deepcopy(self.data_blueprint)
self.data[s]['filePath'] = self.currentPath
self.data[s]['skipped'] = False
self.A = self.load_footprints(self.currentPath,s)
if isinstance(self.A,bool):
self.data[s]['skipped'] = True
continue
for s0 in range(s):
# self.progress.set_description('Calculate image correlations for Session %d'%s)
if (self.paths['sessions'][s0]) and ('Cn' in self.data[s0].keys()) and ('Cn' in self.data[s].keys()):
c_max,_,_ = calculate_img_correlation(self.data[s0]['Cn'],self.data[s]['Cn'],plot_bool=False)
self.results['Cn_corr'][s0,s] = self.results['Cn_corr'][s,s0] = c_max
self.progress.set_description(f"A union size: {self.data['joint']['nA'][0]}, Preparing footprints from {session_name}")
## preparing data of current session
prepared,self.data[s]['remap'] = self.prepare_footprints(A_ref=self.A0)
self.calculate_footprint_data(s)
if not prepared:
self.data[s]['skipped'] = True
continue
## calculate matching probability between each pair of neurons
self.progress.set_description(f"A union size: {self.data['joint']['nA'][0]}, Calculate statistics for {session_name}")
self.data_cross['D_ROIs'],self.data_cross['fp_corr'],_ = calculate_statistics(
self.A,A_ref=self.A_ref,idx_eval=self.data[s]['idx_eval'],idx_eval_ref=self.data['joint']['idx_eval'],
binary=self.params['binary'],neighbor_distance=self.params['neighbor_distance'],convert=self.params['pxtomu'],model=self.params['correlation_model'],
dims=self.params['dims']
)
idx_fp = 1 if self.params['correlation_model'] == 'shifted' else 0
self.data[s]['p_same'] = calculate_p(
self.data_cross['D_ROIs'],self.data_cross['fp_corr'][idx_fp,...],
self.model['f_same'],self.params['neighbor_distance']
)
## run hungarian algorithm (HA) with (1-p_same) as score
self.progress.set_description(f"A union size: {self.data['joint']['nA'][0]}, Perform Hungarian matching on {session_name}")
matches = linear_sum_assignment(1 - self.data[s]['p_same'].toarray())
p_matched = self.data[s]['p_same'].toarray()[matches]
idx_TP = np.where(p_matched > p_thr[0])[0] ## thresholding results (HA matches all pairs, but we only want matches above p_thr)
if len(idx_TP) > 0:
matched_ref = matches[0][idx_TP] # matched neurons in s_ref
matched = matches[1][idx_TP] # matched neurons in s
# print(idx_TP,matched_ref)
else:
matched_ref = np.array([],'int')
matched = np.array([],'int')
## find neurons which were not matched in current and reference session
non_matched_ref = np.setdiff1d(list(range(self.data['joint']['nA'][0])), matched_ref)
non_matched = np.setdiff1d(list(np.where(self.data[s]['idx_eval'])[0]), matches[1][idx_TP])
non_matched = non_matched[self.data[s]['idx_eval'][non_matched]]
# print(matched,non_matched)
## calculate number of matches found
TP = np.sum(p_matched > p_thr[0]).astype('float32')
self.A_ref = self.A_ref.tolil()
## update footprint shapes of matched neurons with A_ref = (1-p/2)*A_ref + p/2*A to maintain part or all of original shape, depending on p_matched
self.A_ref[:,matched_ref] = self.A_ref[:,matched_ref].multiply(1-p_matched[idx_TP]/2) + self.A[:,matched].multiply(p_matched[idx_TP]/2)
## removing footprints from the data which were competing with another one
## to be matched and lost, but have significant probability to be the same
## this step ensures, that downstream session don't confuse this one and the
## 'winner', leading to arbitrary assignments between two clusters
for nm in non_matched:
p_all = self.data[s]['p_same'][:,nm].todense()
if np.any(p_all>p_thr[1]):
# print(f'!! neuron {nm} is removed, as it is nonmatched and has high match probability:',p_all)[p_all>0])
# print(np.where(p_all>0))
non_matched = non_matched[non_matched!=nm]
## append new neuron footprints to union
self.A_ref = sp.sparse.hstack([self.A_ref, self.A[:,non_matched]]).asformat('csc')
## update union data
self.data['joint']['nA'][0] = self.A_ref.shape[1]
self.data['joint']['idx_eval'] = np.ones(self.data['joint']['nA'][0],'bool')
self.data['joint']['cm'] = center_of_mass(self.A_ref,self.params['dims'][0],self.params['dims'][1],convert=self.params['pxtomu'])
## write neuron indices of neurons from this session
self.results['assignments'][matched_ref,s] = matched # ... matched neurons are added
## ... and non-matched (new) neurons are appended
N_add = len(non_matched)
match_add = np.zeros((N_add,self.nS))*np.NaN
match_add[:,s] = non_matched
self.results['assignments'] = np.concatenate([self.results['assignments'],match_add],axis=0)
## write match probabilities to matched neurons and reshape array to new neuron number
self.results['p_matched'][matched_ref,s,0] = p_matched[idx_TP]
## write best non-matching probability
p_all = self.data[s]['p_same'].toarray()
self.results['p_matched'][matched_ref,s,1] = [max(p_all[c,np.where(p_all[c,:]!=self.results['p_matched'][c,s,0])[0]]) for c in matched_ref]
# self.results['p_matched'][non_matched,s,1] = [max(p_all[c,np.where(p_all[c,:]!=self.results['p_matched'][c,s,0])[0]]) for c in matched_ref]
p_same_add = np.full((N_add,self.nS,2),np.NaN)
p_same_add[:,s,0] = 1
self.results['p_matched'] = np.concatenate([self.results['p_matched'],p_same_add],axis=0)
# if np.any(np.all(self.results['p_matched']>0.9,axis=2)):
# print('double match!')
# return
## some post-processing to create cluster-structures values / statistics
self.store_results()
# finally, save results
if save_results:
self.save_registration(suffix=self.paths['suffix'])
self.save_data(suffix=self.paths['suffix'])
def store_results(self):
self.results['cm'] = np.zeros(self.results['assignments'].shape + (2,)) * np.NaN
for key in ['SNR_comp','r_values','cnn_preds']:
self.results[key] = np.zeros_like(self.results['assignments'])
# for s in range(self.nS):
self.results['remap'] = {
'shift': np.zeros((self.nS,2)),
'transposed': np.zeros(self.nS,'bool'),
'corr': np.zeros(self.nS),
'corr_zscored': np.zeros(self.nS),
}
self.results['filePath'] = ['']*self.nS
has_reference = False
for s in self.data:
if s=='joint': continue
if 'remap' in self.data[s].keys():
self.results['remap']['transposed'][s] = self.data[s]['remap']['transposed'] if has_reference else False
self.results['remap']['shift'][s,:] = self.data[s]['remap']['shift'] if has_reference else [0,0]
self.results['remap']['corr'][s] = self.data[s]['remap']['c_max'] if has_reference else 1
self.results['remap']['corr_zscored'][s] = self.data[s]['remap']['c_zscored'] if has_reference else 1
if self.data[s]['skipped']: continue
self.results['filePath'][s] = self.data[s]['filePath']
idx_c = np.where(~np.isnan(self.results['assignments'][:,s]))[0]
idx_n = self.results['assignments'][idx_c,s].astype('int')
for key in ['cm','SNR_comp','r_values','cnn_preds']:
try:
self.results[key][idx_c,s,...] = self.data[s][key][idx_n,...]
except:
pass
has_reference = True
def load_footprints(self,loadPath,s=None):
'''
function to load results from neuron detection (CaImAn, OnACID) and store in according dictionaries
TODO:
* implement min/max thresholding (maybe shift thresholding to other part of code?)
'''
if loadPath and os.path.exists(loadPath):
ld = load_data(loadPath)
if 'Cn' in ld.keys():
Cn = ld['Cn'].T
else:
self.log.warning('Cn not in result files. constructing own Cn from footprints!')
Cn = np.array(ld['A'].sum(axis=1).reshape(*self.params['dims']))
self.data_tmp['Cn'] = Cn
self.data_tmp['C'] = ld['C']
if not s is None:
self.data[s]['Cn'] = Cn
## load some data necessary for further processing
if np.all([key in ld.keys() for key in ['SNR_comp','r_values','cnn_preds']]):
## if evaluation parameters are present, use them to define used neurons
for key in ['SNR_comp','r_values','cnn_preds']:
self.data[s][key] = ld[key]
## threshold neurons according to evaluation parameters
self.data[s]['idx_eval'] = ((ld['SNR_comp']>self.params['SNR_lowest']) & (ld['r_values']>self.params['rval_lowest']) & (ld['cnn_preds']>self.params['cnn_lowest'])) & ((ld['SNR_comp']>self.params['min_SNR']) | (ld['r_values']>self.params['rval_thr']) | (ld['cnn_preds']>self.params['min_cnn_thr']))
else:
## else, use all neurons
self.data[s]['idx_eval'] = np.ones(ld['A'].shape[1],'bool')
return ld['A']
else:
return False
def prepare_footprints(self,A_ref=None,align_to_reference=True,use_opt_flow=True):
'''
Function to prepare footprints for calculation and matching:
- casting to / ensuring sparse type
- calculating and reverting rigid and non-rigid shift
- normalizing
'''
## ensure footprints are stored as sparse matrices to lower computational costs and RAM usage
if 'csc_matrix' not in str(type(self.A)):
self.A = sp.sparse.csc_matrix(self.A)
remap = {
'shift': np.full((2,),np.NaN),
'flow': np.zeros((2,)+self.params['dims']),
'c_max': np.NaN,
'c_zscored': np.NaN, ## z-score of correlation
'transposed':False,
}
if align_to_reference:
## align footprints A to reference set A_ref
# if no reference set of footprints is specified, use current reference set
if A_ref is None:
A_ref = self.A_ref
## cast this one to sparse as well, if needed
if 'csc_matrix' not in str(type(A_ref)):
A_ref = sp.sparse.csc_matrix(A_ref)
## test whether images might be transposed (sometimes happens...) and flip accordingly
# Cn = np.array(self.A.sum(1).reshape(512,512))
# Cn_ref = np.array(A_ref.sum(1).reshape(512,512))
Cn = self.data_tmp['Cn']
Cn_ref = self.Cn_ref
c_max,c_zscored,_ = calculate_img_correlation(Cn_ref,Cn,plot_bool=False)
c_max_T,c_zscored_T,_ = calculate_img_correlation(Cn_ref,Cn.T,plot_bool=False)
# print('z scores:',c_zscored,c_zscored_T)
remap['c_max'] = c_max
## if no good alignment is found, don't include this session in the matching procedure (e.g. if imaging window is shifted too much)
if (c_zscored < self.params['min_session_correlation_zscore']) & \
(c_zscored_T < self.params['min_session_correlation_zscore']):
# if (c_max < self.params['min_session_correlation']) & \
# (c_max_T < self.params['min_session_correlation']):
print(f'Identified correlation {c_max} too low in session {self.currentPath}, skipping alignment, as sessions appear to have no common imaging window!')
return False, remap
if (c_max>0.95) | (c_max_T>0.95):
print(f'High correlation {c_max} found in session {self.currentPath}, skipping alignment as it is likely to be the same imaging data')
return False, remap
if (c_max_T > c_max) & (c_max_T > self.params['min_session_correlation']):
print('Transposed image')
self.A = sp.sparse.hstack([sp.sparse.csc_matrix(img.reshape(self.params['dims']).transpose().reshape(-1,1)) for img in self.A.transpose()])
remap['transposed'] = True
## calculate rigid shift and optical flow from reduced (cumulative) footprint arrays
remap['shift'],flow,remap['c_max'],remap['c_zscored'] = get_shift_and_flow(A_ref,self.A,self.params['dims'],projection=1,plot_bool=False)
# remap['shift'],flow,remap['c_max'],remap['c_zscored'] = get_shift_and_flow(Cn_ref,Cn,self.params['dims'],projection=None,plot_bool=False)
remap['flow'][0,...] = flow[...,0]
remap['flow'][1,...] = flow[...,1]
total_shift = np.sum(np.sqrt(np.array([s**2 for s in remap['shift']]).sum()))
if total_shift > self.params['max_session_shift']:
print(f'Large shift {total_shift} identified in session {self.currentPath}, skipping alignment as sessions appear to have no significantly common imaging window!')
return False, remap
## use shift and flow to align footprints - define reverse mapping
x_remap,y_remap = build_remap_from_shift_and_flow(self.params['dims'],remap['shift'],remap['flow'] if use_opt_flow else None)
## use shift and flow to align footprints - apply reverse mapping
self.A = sp.sparse.hstack([
sp.sparse.csc_matrix( # cast results to sparse type
cv2.remap(
img.reshape(self.params['dims']), # reshape image to original dimensions
x_remap, y_remap, # apply reverse identified shift and flow
cv2.INTER_CUBIC
).reshape(-1,1) # reshape back to allow sparse storage
) for img in self.A.toarray().T # loop through all footprints
])
## finally, apply normalization (watch out! changed normalization to /sum instead of /max)
# self.A_ref = normalize_sparse_array(self.A_ref)
self.A = normalize_sparse_array(self.A)
return True, remap
def calculate_footprint_data(self,s):
# print(f'Session: {s}, A: {self.A.shape}')
## calculating various statistics
self.data[s]['idx_eval'] &= (np.diff(self.A.indptr) != 0) ## finding non-empty rows in sparse array (https://mike.place/2015/sparse/)
# print('\n idx evals:',self.data[s]['idx_eval'].sum())
self.data[s]['nA'][0] = self.A.shape[1] # number of neurons
self.data[s]['cm'] = center_of_mass(self.A,*self.params['dims'],convert=self.params['pxtomu'])
self.progress.set_description(f"Calculate self-referencing statistics for {self.paths['sessions'][s]}")
## find and mark potential duplicates of neuron footprints in session
self.data_cross['D_ROIs'],self.data_cross['fp_corr'],idx_remove = calculate_statistics(
self.A,idx_eval=self.data[s]['idx_eval'],
SNR_comp=self.data[s]['SNR_comp'],C=self.data_tmp['C'],
binary=self.params['binary'],neighbor_distance=self.params['neighbor_distance'],convert=self.params['pxtomu'],model=self.params['correlation_model'],
dims=self.params['dims']
)
# print('\n idx remove:',idx_remove)
self.data[s]['idx_eval'][idx_remove] = False
self.data[s]['nA'][1] = self.data[s]['idx_eval'].sum()
def update_joint_model(self,s,s_ref):
'''
Function to update counts in the joint model
inputs:
- s,s_ref: int / string
key of current (s) and reference (s_ref) session
- use_kde: bool
defines, whether kde (kernel density estimation) is used to ...
'''
## use all neurons or only those from "medium-dense regions", defined by kde
idxes = self.model['kernel']['idxes'][s_ref] if self.params['use_kde'] else np.ones(self.data[s_ref]['nA'][0],'bool')
## find all neuron pairs below a distance threshold
neighbors = self.data_cross['D_ROIs'][idxes,:] < self.params['neighbor_distance']
NN_idx = np.zeros((self.data[s_ref]['nA'][0],self.data[s]['nA'][0]),'bool')
if s!=s_ref:
## identifying next-neighbours
idx_NN = np.nanargmin(self.data_cross['D_ROIs'][self.data[s_ref]['idx_eval'],:],axis=1)
NN_idx[self.data[s_ref]['idx_eval'],idx_NN] = True
NN_idx = NN_idx[idxes,:][neighbors]
else:
np.fill_diagonal(NN_idx,True)
# NN_idx[~idxes,:] = False
NN_idx = NN_idx[idxes,:][neighbors]
## obtain distance and correlation values of close neighbors, only
D_ROIs = self.data_cross['D_ROIs'][idxes,:][neighbors]
fp_corr = self.data_cross['fp_corr'][0,idxes,:][neighbors]
fp_corr_shifted = self.data_cross['fp_corr'][1,idxes,:][neighbors]
## update count histogram with data from current session pair
for i in tqdm.tqdm(range(self.params['nbins']),desc='updating joint model',leave=False):
## find distance indices of values falling into the current bin
idx_dist = (D_ROIs >= self.params['arrays']['distance_bounds'][i]) & (D_ROIs < self.params['arrays']['distance_bounds'][i+1])
for j in range(self.params['nbins']):
## differentiate between the two models for calculating footprint-correlation
if (self.params['correlation_model']=='unshifted') | (self.params['correlation_model']=='both'):
## find correlation indices of values falling into the current bin
idx_fp = (fp_corr > self.params['arrays']['correlation_bounds'][j]) & (self.params['arrays']['correlation_bounds'][j+1] > fp_corr)
idx_vals = idx_dist & idx_fp
if s==s_ref: # for self-comparing
self.model['counts_same_unshifted'][i,j] += np.count_nonzero(idx_vals & ~NN_idx)
else: # for cross-comparing
self.model['counts_unshifted'][i,j,0] += np.count_nonzero(idx_vals)
self.model['counts_unshifted'][i,j,1] += np.count_nonzero(idx_vals & NN_idx)
self.model['counts_unshifted'][i,j,2] += np.count_nonzero(idx_vals & ~NN_idx)
if (self.params['correlation_model']=='shifted') | (self.params['correlation_model']=='both'):
idx_fp = (fp_corr_shifted > self.params['arrays']['correlation_bounds'][j]) & (self.params['arrays']['correlation_bounds'][j+1] > fp_corr_shifted)
idx_vals = idx_dist & idx_fp
if s==s_ref: # for self-comparing
# self.model['counts_same'][i,j] += np.count_nonzero(idx_vals)
self.model['counts_same'][i,j] += np.count_nonzero(idx_vals & ~NN_idx)
else: # for cross-comparing
self.model['counts'][i,j,0] += np.count_nonzero(idx_vals)
self.model['counts'][i,j,1] += np.count_nonzero(idx_vals & NN_idx)
self.model['counts'][i,j,2] += np.count_nonzero(idx_vals & ~NN_idx)
def position_kde(self,s,plot_bool=False):
'''
function to calculate kernel density estimate of neuron density in session s
this is optional, but can be used to exclude highly dense and highly sparse regions from statistics in order to not skew statistics
'''
self.log.info('calculating kernel density estimates for session %d'%s)
## calculating kde from center of masses
x_grid, y_grid = np.meshgrid(np.linspace(0,self.params['dims'][0]*self.params['pxtomu'],self.params['dims'][0]), np.linspace(0,self.params['dims'][1]*self.params['pxtomu'],self.params['dims'][1]))
positions = np.vstack([x_grid.ravel(), y_grid.ravel()])
kde = sp.stats.gaussian_kde(self.data[s]['cm'][self.data[s]['idx_eval'],:].T)
self.model['kernel']['kde'][s] = np.reshape(kde(positions),x_grid.shape)
cm_px = (self.data[s]['cm'][self.data[s]['idx_eval'],:]/self.params['pxtomu']).astype('int')
kde_at_com = np.zeros(self.data[s]['nA'][0])*np.NaN
kde_at_com[self.data[s]['idx_eval']] = self.model['kernel']['kde'][s][cm_px[:,1],cm_px[:,0]]
self.model['kernel']['idxes'][s] = (kde_at_com > np.quantile(self.model['kernel']['kde'][s],self.params['qtl'][0])) & (kde_at_com < np.quantile(self.model['kernel']['kde'][s],self.params['qtl'][1]))
if plot_bool:
plt.figure()
h_kde = plt.imshow(self.model['kernel']['kde'][s],cmap=plt.cm.gist_earth_r,origin='lower',extent=[0,self.params['dims'][0]*self.params['pxtomu'],0,self.params['dims'][1]*self.params['pxtomu']])
#if s>0:
#col = self.data_cross['D_ROIs'].min(1)
#else:
#col = 'w'
plt.scatter(self.data[s]['cm'][:,0],self.data[s]['cm'][:,1],c='w',s=5+10*self.model['kernel']['idxes'][s],clim=[0,10],cmap='YlOrRd')
plt.xlim([0,self.params['dims'][0]*self.params['pxtomu']])
plt.ylim([0,self.params['dims'][1]*self.params['pxtomu']])
# cm_px = (self.data['cm'][s]/self.params['pxtomu']).astype('int')
# kde_at_cm = self.model['kernel']['kde'][s][cm_px[:,1],cm_px[:,0]]
plt.colorbar(h_kde)
plt.show(block=False)
def fit_model(self,correlation_model='shifted',times=0):
'''
'''
self.params['correlation_model'] = correlation_model
neighbor_dictionary = {
'NN': [],
'nNN': [],
'all': []
}
self.model = self.model | {
'fit_function': {
'single': {
'distance': copy.deepcopy(neighbor_dictionary),
'correlation': copy.deepcopy(neighbor_dictionary)
},
'joint': {
'distance': copy.deepcopy(neighbor_dictionary),
'correlation': copy.deepcopy(neighbor_dictionary)
}
},
'fit_parameter': {
'single': {
'distance': copy.deepcopy(neighbor_dictionary),
'correlation': copy.deepcopy(neighbor_dictionary)
},
'joint': {}
},
'pdf': {
'single': {
'distance': [],
'correlation': []
},
'joint': []
},
'p_same': {
'single': {},
'joint': np.zeros(0)
},
'f_same': False,
}
# print(self.params['correlation_model'])
if (not (self.params['correlation_model'] == 'unshifted')) & (not (self.params['correlation_model']=='shifted')):
raise Exception('Please specify model to be either "shifted" or "unshifted"')
key_counts = 'counts' if self.params['correlation_model']=='shifted' else 'counts_unshifted'
counts = scale_down_counts(self.model[key_counts].astype('float32'),times=times)
nbins = counts.shape[0]
self.update_bins(nbins)
def build_single_model(model,times=0):
m = 0 if model=='correlation' else 1
max_val = 1. if model=='correlation' else self.params['neighbor_distance']
fit_fun, fit_bounds = self.set_functions(model,'single')
## obtain fit parameters from fit to count histograms
for p,pop in zip((1,2,0),['NN','nNN','all']):
if pop=='all':
p0 = (self.model[key_counts][...,1].sum()/self.model[key_counts][...,0].sum(),) + \
tuple(self.model['fit_parameter']['single'][model]['NN']) + \
tuple(self.model['fit_parameter']['single'][model]['nNN'])
else:
p0 = None
failed=True
nbins = self.model[key_counts].shape[0]
# while failed==True and nbins>10:
# try:
self.log.debug(model,pop)
counts = scale_down_counts(self.model[key_counts].astype('float32'),times=times)
nbins = counts.shape[0]
self.update_bins(nbins)
normed_counts = counts[...,p].sum(m)/counts[...,p].sum()/self.params['arrays'][f'{model}_step']
plot=False
if plot:
plt.figure()
plt.plot(self.params['arrays'][model],normed_counts)
if pop=='all':
plt.plot(self.params['arrays'][model],fit_fun[pop](self.params['arrays'][model],*p0),'k--')
self.model['fit_parameter']['single'][model][pop] = curve_fit(
fit_fun[pop],
self.params['arrays'][model],
normed_counts,
bounds=fit_bounds[pop],
p0=p0
)[0]
if plot:
plt.plot(self.params['arrays'][model],fit_fun[pop](self.params['arrays'][model],*self.model['fit_parameter']['single'][model][pop]))
plt.show(block=False)
failed=False
# except:
# times += 1
# print(f'Finding solution failed. Scaling down counts by factor {times} ({nbins} bins)')
## build functions for NN population and overall
## using "all model", as it doesn't overestimate wrongly assigned tails of NN distribution
fun_NN = fun_wrapper(
fit_fun['NN'],
self.params['arrays'][model],
self.model['fit_parameter']['single'][model]['all'][1:self.model['fit_function']['single'][model]['NN_Nparams']]
) * self.model['fit_parameter']['single'][model]['all'][0]
fun_nNN = fun_wrapper(
fit_fun['nNN'],
self.params['arrays'][model],
self.model['fit_parameter']['single'][model]['all'][self.model['fit_function']['single'][model]['NN_Nparams']:]
) * (1-self.model['fit_parameter']['single'][model]['all'][0])
if model=='correlation':
cdf_NN = np.cumsum(fun_NN)/np.sum(fun_NN)
cdf_nNN = np.cumsum(fun_nNN[::-1])[::-1]/np.sum(fun_nNN)
else:
cdf_NN = np.cumsum(fun_NN[::-1])[::-1]/np.sum(fun_NN)
cdf_nNN = np.cumsum(fun_nNN)/np.sum(fun_nNN)
self.model['p_same']['single'][model] = cdf_NN/(cdf_NN+cdf_nNN)
def build_joint_model(model,times=0,counts_thr = 20):
# 'model' defines the single model functions which are used to calculate the joint model,
# while the 'weight model' merely specifies the weighting between NN and nNN functions
counts = scale_down_counts(self.model[key_counts].astype('float32'),times=times)
nbins = counts.shape[0]
self.update_bins(nbins)
m = 1 if model=='correlation' else 0
fit_fun, fit_bounds = self.set_functions(model,'joint')
# normalized_histogram = counts/counts.sum(m,keepdims=True)/self.params['arrays'][f'{model}_step']# * nbins/max_val
# normalized_histogram[np.isnan(normalized_histogram)] = 0
# print(normalized_histogram.shape)
self.model['fit_parameter']['joint'][model] = {
'NN':np.full((nbins,self.model['fit_function']['single'][model]['NN_Nparams']-1),np.NaN),
'nNN':np.full((nbins,self.model['fit_function']['single'][model]['nNN_Nparams']-1),np.NaN)
#'all':np.zeros((nbins,len(self.model['fit_parameter']['single']['correlation']['all'])))*np.NaN}
}
for p,pop in enumerate(['NN','nNN'],start=1):
fitted = False
for n in range(nbins):
### to fp-correlation: NN - reverse lognormal, nNN - reverse lognormal
fit_data = np.take(counts[...,p],n,axis=abs(m-1))
# print(f'n data (@bin {n}):',fit_data.sum())
if fit_data.sum() > counts_thr:
fit_data /= fit_data.sum()
# print(fit_data)
self.log.debug(f'data for {pop} {model}-distribution: ', fit_data)
# scale_times = 0
# while nbins > 16:
# fit_histogram = np.copy(normalized_histogram)
# fit_histogram = scale_down_counts(normalized_histogram,times=scale_times)
# nbins = fit_histogram.shape[0]
# self.update_bins(nbins)
# print(scale_times,nbins)
# print(self.params['arrays'][model])
# print('vals:',np.take(fit_histogram[...,p],i,axis=abs(m-1)))
# print(fit_bounds[pop])
# try:
# if pop=='all':
# plt.plot(self.params['arrays'][model],fit_fun[pop](self.params['arrays'][model],*p0),'k--')
plot=False
if plot:
plt.figure()
plt.plot(self.params['arrays'][model],fit_data)
try:
self.model['fit_parameter']['joint'][model][pop][n,:] = curve_fit(
fit_fun[pop],
self.params['arrays'][model],
fit_data,
bounds=fit_bounds[pop],
# p0=self.model['fit_parameter']['joint'][model][pop][i-1,:] if fitted else None
)[0]
self.log.debug(f'fit results @bin {n}:',self.model['fit_parameter']['joint'][model][pop][n,:])
except:
print(f'fit failed for {model} model and population {pop} @bin {n}/{nbins}')
if plot:
plt.plot(self.params['arrays'][model],fit_fun[pop](self.params['arrays'][model],*self.model['fit_parameter']['joint'][model][pop][n,:]))
plt.title(f'{model} model, {pop} population, bin {n} ({nbins} bins)')
plt.show(block=False)
continue
# break
# except:
# scale_times += 1
# self.log.debug(f'Finding solution failed. Scaling down counts by factor {scale_times} ({nbins/2} bins)')
if plot:
plt.plot(self.params['arrays'][model],fit_fun[pop](self.params['arrays'][model],*self.model['fit_parameter']['joint'][model][pop][n,:]))
plt.title(f'{model} model, {pop} population, bin {n} ({nbins} bins)')
plt.show(block=False)