-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasc_funcs.py
1349 lines (1027 loc) · 47.6 KB
/
asc_funcs.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
#!/usr/bin/env python
#
# asc_funcs.py
#
# Author: Eugene Duff <eugene.duff@gmail.com>
#
#
#############################################################################
# general connectivity matrix class, with methods for calculating correlation etc.
# from numpy import *
import numpy as np
import matplotlib.cm
import matplotlib.pylab as pl
import glob,os, numbers
import scipy.stats as stats
import scipy.sparse
import spectrum
import scipy.linalg as linalg
from itertools import combinations, chain
from scipy.misc import comb
from scipy.interpolate import interp1d
from scipy import sparse
from scipy.sparse import coo_matrix
from scipy.signal import welch
from multiprocessing import Process, Queue, current_process, freeze_support # for parallel processing of seed-based connectivity
# optional modules
try:
import nibabel as nb
except:
print("nibabel not detected, no image loading available")
class FC:
""" Functional Connectivity Class.
Stores and provides FC data of a number of subjects, including raw time series (optional), covariance,
partial correlation etc.
"""
### todo doc
def __init__(self,tcs,cov_flag=False,dof=None,ROI_info={},mask=None):
""" Initialise the FC object. """
# reading covariance matrix
if cov_flag==True:
self.tcs=None
covs=tcs
if dof is None:
raise ValueError("Need to specify dof if providing cov.")
else:
self.dof=dof
else:
# reading timecourses
if tcs.ndim==2:
tcs=(np.atleast_3d(tcs).transpose(2,0,1))
self.tcs = tcs
if dof is None:
self.dof=tcs.shape[-1]-1
elif dof == 'EstEff':
AR=np.zeros((tcs.shape[0],tcs.shape[1],15))
ps=np.zeros((tcs.shape[0],tcs.shape[1],15))
for subj in np.arange(tcs.shape[0]):
for ROI in np.arange(tcs.shape[1]):
AR[subj,ROI,:]=spectrum.aryule(pl.demean(tcs[subj,ROI,:]),15)[0]
ps[subj,ROI,:]=spectrum.correlation.CORRELATION(pl.demean(tcs[subj,ROI,:]),maxlags=14,norm='coeff')
ps = np.mean(np.mean(ps,0),0)
AR2 = np.mean(np.mean(AR,0),0)
dof_nom=tcs.shape[-1]-1
self.dof = int(dof_nom / (1-np.dot(ps[:15].T,AR2))/(1 - np.dot(np.ones(len(AR2)).T,AR2)))
else:
self.dof=dof
covs = get_covs(tcs)
if ROI_info=={}:
shp=covs.shape[-1]
ROI_info['ROI_names']=np.arange(shp).astype(str)
ROI_info['ROI_RSNs']=np.ones(shp,).astype(int)
if mask:
self.mask=mask
self.ROI_info = ROI_info
self.covs = covs
##### FC functions #####
def get_stds(self,pcorrs=False):
""" calculate std_devs. """
if not( 'stds' in self.__dict__):
sz = self.get_covs(pcorrs=pcorrs).shape
if len(sz)==3:
self.stds=np.diagonal(self.get_covs(pcorrs=pcorrs),axis1=len(sz)-2,axis2=len(sz)-1)**(0.5)
else:
self.stds=self.get_covs(pcorrs=pcorrs).diagonal()**0.5
return(self.stds)
def get_stds_m(self,pcorrs=False):
""" calculate std_devs (return matrix form). """
stds_m = self.get_stds(pcorrs=pcorrs)
return(np.tile(stds_m,(1,self.covs.shape[1])).reshape(stds_m.shape[0],stds_m.shape[1],stds_m.shape[1]))
def get_stds_m_t(self,pcorrs=False):
""" calculate std_devs (return transpose matrix form). """
return transpose(self.get_stds_m(pcorrs=pcorrs),(0,2,1))
def get_corrs(self,pcorrs=False):
""" get correlation (partial). """
if pcorrs:
return(self._get_pcorrs())
else:
if not( 'corrs' in self.__dict__):
# initialise as normal/sparse matrix
els=self.get_covs().nonzero()
self.corrs=self.get_covs()*0
dims=np.arange(len(els))
dims1 = tuple(np.setdiff1d(dims,dims[-1]))
dims2 = tuple(np.setdiff1d(dims,dims[-2]))
els1=tuple(els[index] for index in dims1)
els2=tuple(els[index] for index in dims2)
self.corrs[els] = np.array(self.get_covs()[els])/(self.get_stds()[els1]*self.get_stds()[els2])
return(self.corrs)
# get partial correlation
def _get_pcorrs(self):
if not( 'pcorrs' in self.__dict__):
self.pcorrs=self.get_corrs()*0
for a in range(len(self.pcorrs)):
self.pcorrs[a,:,:]=corr2pcorr(self.corrs[a,:,:])
return self.pcorrs
def get_covs(self,pcorrs=False):
if pcorrs:
return self._get_pcovs()
else:
return self.covs
def _get_pcovs(self):
if not( 'pcorrs' in self.__dict__):
pcorrs=self._get_pcorrs()
multiplier = (self.get_stds_m()*transpose(self.get_stds_m(),(0,2,1)))
return(pcorrs*multiplier)
class FC_con:
""" Class for contrasts between two states, A and B, providing functions to calculate stats, ASC analysis limits etc. """
def __init__(self,A,B):
self.A=A
self.B=B
def get_corr_stats(self,pcorrs=False,rel=True):
""" get basic correlation statistics between two states. """
if pcorrs:
out = self.get_pcorr_stats(self,rel=rel)
else:
if not( 'corr_stats' in self.__dict__):
if rel == True:
self.corr_stats = stats.ttest_rel(rtoz(self.A.get_corrs(pcorrs=False)),rtoz(self.B.get_corrs(pcorrs=False)))
else:
self.corr_stats = stats.ttest_ind(rtoz(self.A.get_corrs(pcorrs=False)),rtoz(self.B.get_corrs(pcorrs=False)))
out = self.corr_stats
return out
def get_pcorr_stats(self,rel=True):
""" get basic partial correlation statistics between two states. """
if not( 'pcorr_stats' in self.__dict__):
if rel == True:
self.pcorr_stats = stats.ttest_rel(rtoz(self.A.get_corrs(pcorrs=True)),rtoz(self.B.get_corrs(pcorrs=True)))
else:
self.pcorr_stats = stats.ttest_ind(rtoz(self.A.get_corrs(pcorrs=True)),rtoz(self.B.get_corrs(pcorrs=True)))
out = self.pcorr_stats
return self.pcorr_stats
def get_std_stats(self,pcorrs=False,rel=True):
""" get basic std statistics between two states. """
if not( 'std_stats' in self.__dict__):
if rel == True:
self.std_stats = stats.ttest_rel(self.A.get_stds(pcorrs=pcorrs),self.B.get_stds(pcorrs=pcorrs))[0]
else:
self.std_stats = stats.ttest_ind(self.A.get_stds(pcorrs=pcorrs),self.B.get_stds(pcorrs=pcorrs))[0]
return self.std_stats
def get_ASC_lims(self,pcorrs=False,pctl=5,errdist_perms=50,refresh=False,sim_sampling=40):
""" get ASC limits. """
if not( 'lims' in self.__dict__) or refresh:
# take mean of all subjects if required
if self.A.tcs is None and np.ndim(self.A.get_covs())==3:
A=FC(np.mean(self.A.get_covs(pcorrs=pcorrs),0),cov_flag=True, dof=self.A.dof,ROI_info=self.A.ROI_info)
B=FC(np.mean(self.B.get_covs(pcorrs=pcorrs),0),cov_flag=True, dof=self.B.dof,ROI_info=self.B.ROI_info)
elif self.A.tcs is None:
A=self.A
B=self.B
else:
A=flatten_tcs(self.A)
B=flatten_tcs(self.B)
# calculate ASC limits
self.lims=ASC_lims_all(A,B,errdist_perms=errdist_perms,pctl=pctl,pcorrs=pcorrs,sim_sampling=sim_sampling)
return(self.lims)
def get_plot_inds(self,inds_cc=None,exclude_conns=True):
""" get ASC limits. """
self.lims = gen_plot_inds(self.lims,inds_cc=inds_cc,exclude_conns=exclude_conns)
def gen_plot_inds(lims,inds_cc=None,exclude_conns=True):
""" determine which connections should be plot in each ASC class. """
plots = list(lims.keys())+ ['other']
plots.remove('covs')
# check sparseness for image-based (seed region analysis)
if scipy.sparse.issparse(lims[plots[0]]['pctls']):
voxels = lims[plots[0]]['pctls'][0,1:].toarray()
if not inds_cc:
inds_cc=np.arange(voxels.shape[-1]).astype(int)
inds_plots={}
notin = inds_cc.copy()
for plot in plots[:3]:
inds_plots[plot]=np.intersect1d(inds_cc,pl.find(lims[plot]['pctls'][0,1:].toarray()))
notin = np.setdiff1d(notin,inds_plots[plot])
inds_plots['other'] = notin
# exclude from additive if in common or uncorrelated
if exclude_conns:
inds_plots['common']=np.setdiff1d(inds_plots['common'],inds_plots['uncorrelated'])
inds_plots['additive']=np.setdiff1d(inds_plots['additive'],inds_plots['common'])
inds_plots['additive']=np.setdiff1d(inds_plots['additive'],inds_plots['uncorrelated'])
else:
# check if correlation matrix is provided
if not inds_cc:
inds_cc=np.triu_indices(n_nodes,1)
indices=np.triu_indices(n_nodes,1)
notin=inds_cc
inds_plots={}
for plot in plots[:3]:
inds_plots[plot]=np.intersect1d(inds_cc,pl.find(fa(lims[plot]['pctls'])))
notin = np.setdiff1d(notin,pl.find(fa(lims[plot]['pctls'])))
inds_plots['other'] = notin
# exclude from additive if in common or uncorrelated
if exclude_conns:
inds_plots['common']=np.setdiff1d(inds_plots['common'],inds_plots['uncorrelated'])
inds_plots['additive']=np.setdiff1d(inds_plots['additive'],inds_plots['common'])
inds_plots['additive']=np.setdiff1d(inds_plots['additive'],inds_plots['uncorrelated'])
#self.inds_plots=inds_plots
lims['covs']['inds_plots']=inds_plots
return(lims)
def ASC_lims_all(A,B,pcorrs=False,errdist_perms=0,dof=None,pctl=10,ch_type='All',sim_sampling=40,mask=None,keepRaw=True):
""" core ASC limits routine.
Takes two states, A and B and convert to analyses differences. """
if not dof:
dof=A.dof
if ch_type == 'All':
ch_type = ['covs','uncorrelated','common','additive']
elif type(ch_type)==str:
ch_type=[ch_type]
# masking for image-based analysis
if mask is None:
shp = A.get_covs().shape
if scipy.sparse.issparse(A.covs):
mask = A.get_covs().nonzero()
#todo fix masking
else:
mask = np.triu_indices(shp[-1],k=1)
if len(shp) == 3:
first=np.repeat(np.arange(shp[0]),len(mask[0]))
second=np.tile(mask[0],shp[0])
third=np.tile(mask[1],shp[0])
mask=tuple((first,second,third))
# set up basic variables
# flattened covariance and corr matrices
covsA=np.array(A.get_covs(pcorrs=pcorrs)[mask])
covsB=np.array(B.get_covs(pcorrs=pcorrs)[mask])
Acorrs=np.array(A.get_corrs(pcorrs=pcorrs)[mask]).flatten()
Bcorrs=np.array(B.get_corrs(pcorrs=pcorrs)[mask]).flatten()
shp=Bcorrs.shape
# masks for stds
dims=np.arange(len(mask))
els1 = tuple(np.setdiff1d(dims,dims[-1]))
els2 = tuple(np.setdiff1d(dims,dims[-2]))
mask1=tuple(mask[index] for index in els1)
mask2=tuple(mask[index] for index in els2)
Astdm=A.get_stds()[mask1]
Astdmt=A.get_stds()[mask2]
Bstdm=B.get_stds()[mask1]
Bstdmt=B.get_stds()[mask2]
pctls=None
lims_struct={}
# variance diffs
diffX=(Bstdm**2-Astdm**2)
diffY=(Bstdmt**2-Astdmt**2)
# calculate limits for dfferent types of ASC class
for a in ch_type:
lims_struct[a]={}
if a=='uncorrelated':
# uncorrelated signal produces straight forward limits
lims_struct[a]={}
uncorrelated = covsA / (Bstdm*Bstdmt)
# large reductions in variance can be greater than is possible for uncorrelated signal, given initial correlation.
# uncorrelated[uncorrelated>1]=1
# uncorrelated[uncorrelated<-1]=-1
zz = A.get_covs()*0
lims_struct[a]['min'] = zz.copy()
lims_struct[a]['min'][mask] = uncorrelated
lims_struct[a]['max'] = zz.copy()
lims_struct[a]['max'][mask] = uncorrelated
# check whether B (exactly) equal to uncorrelated
lims_struct['uncorrelated']['pctls_noerr']=zz.copy()
lims_struct['uncorrelated']['pctls_noerr'][mask]=(Bcorrs> lims_struct['uncorrelated']['min'][mask]) != (Bcorrs> lims_struct['uncorrelated']['max'][mask])
elif a== 'common':
# to identify limits for common, we sample across possible common signals
# 3d inds = np.tile(np.arange(sim_sampling),(shp[0],shp[1],shp[1],1))
# 3d Common=np.zeros((shp[0],shp[1],shp[2],sim_sampling,sim_sampling))
# 3d Common=np.zeros((shp,sim_sampling,sim_sampling))
shp=Acorrs.shape
inds = np.tile(np.arange(sim_sampling),(shp[-1],1))
# Common stores the limits for each sampled common signal / default to nan
Common_max=np.zeros(shp)*np.nan
Common_min=np.zeros(shp)*np.nan
# sampling range (for correlation of new signal with X_A and Y_A)
smpling=(np.arange(sim_sampling)/(sim_sampling-1.0))
smpling[-1]=0.99999 # make final value slightl less than 1
# orthogonal components form (see supp materials)
a11=Astdm # std of node 1
a21=Astdmt*Acorrs # std of node 2 projected to node 1
a22=np.sqrt(Astdmt**2-a21**2) # remaining std of node 2
# set up data matrices
Common=np.zeros(shp+(sim_sampling*2,)) # final common signal correlation
# correlation max and min bounds remove?
#cc1=np.zeros(shp+(sim_sampling*2,)) # lower correlation
#cc2=np.zeros(shp+(sim_sampling*2,)) # upper correlation bound
#
kp=np.zeros(shp+(sim_sampling*2,))
km=np.zeros(shp+(sim_sampling*2,))
#
a31a=np.zeros(shp+(sim_sampling*2,)) # std of node 2 projected to node 1
a31b=np.zeros(shp+(sim_sampling*2,))
a32a=np.zeros(shp+(sim_sampling*2,))
# sign of change in variance
signX=np.sign(Bstdm-Astdm)
signY=np.sign(Bstdmt-Astdmt)
cnt=0
# loop over different correlations of X_A with N (new signal), which may be +ve or -ve
for bbb in [-1,1]:
# loop over different correlations of X_A with N, cal
for aaa in (np.arange(len(smpling))/len(smpling))[::(-bbb)]:
#match sign to that of original correlation
corrA=signX*aaa
# calculate a_31 from quadratic
aa=1
bb=2*a11*corrA**2
cc=-(corrA**2)*( diffX )
a31 = (-bb + np.sqrt(bb**2 - 4*aa*cc))/(2*aa)
#a31a[:,cnt] = (-bb + np.sqrt(bb**2 - 4*aa*cc))/(2*aa)
#a31=a31a[:,cnt]
# now calculate a_32 from quadratic
aa = ( Bstdm**2-Astdm**2 ) -2*a11*a31
a32a[:,cnt]=bbb*np.sqrt(abs(aa - a31**2 ))
a32=a32a[:,cnt]
# calculate k, positive and negative (Case 1, Common Signal, Supporting Info)
bb = 2*(a21*a31+a22*a32)
cc = -( diffY )
kp[:,cnt] = (-bb + np.sqrt(bb**2 - 4*aa*cc))/(2*aa)
km[:,cnt] = (-bb - np.sqrt(bb**2 - 4*aa*cc))/(2*aa)
ks=np.c_[kp[:,cnt],km[:,cnt]]
# exclude wrong sign
ks[(ks[:,0]*np.sign(Acorrs)*np.sign(diffX)*np.sign(diffY))<0,0]=np.inf
ks[(ks[:,1]*np.sign(Acorrs)*np.sign(diffX)*np.sign(diffY))<0,1]=np.inf
# find the minimum k
k=ks[np.arange(shp[0]),np.argmin(abs(ks),axis=1)]
# now calculate correlations remove?
#cc1[:,cnt]=(a11*a31)/((a11)*np.sqrt(a31**2+a32**2))
#cc2[:,cnt]=(a21*k*a31 + a22*k*a32)/(np.sqrt((a21**2+a22**2))*np.sqrt((k*a31)**2+(k*a32)**2))
# now calculate covariances
cov_max_comm = a11*a21 + a11*a31*k + (a21*a31) + a22*a32 + k*(a31**2 + a32**2 )
# calculate correlations
Common[:,cnt]=cov_max_comm/(( Bstdm)*( Bstdmt))
# check that changes are within observed variance limits
Common[((a31)**2 + (a32)**2) >abs(diffX),cnt]=np.nan
Common[((a31*k)**2 + (a32*k)**2) >abs(diffY),cnt]=np.nan
# if diffX negative, can't remove more than initial variance
Common[(2*(a11*a31))<(diffX-abs(diffX)),cnt]=np.nan
# exclude sign flips
Common[np.sign(Acorrs)*np.sign(k)!=(np.sign(diffY)*np.sign(diffX)),cnt]=np.nan
Common[k==np.inf,cnt]=np.nan
cnt+=1
# Common >=0
Common_min = np.fmin(Common_min,np.nanmin(Common,1))
lims_struct[a]['min'] = A.get_covs()*0
lims_struct[a]['min'][mask]=Common_min
Common_max = np.fmax(Common_max,np.nanmax(Common,1))
lims_struct[a]['max'] = A.get_covs()*0
lims_struct[a]['max'][mask]=Common_max
# common results
lims_struct['common']['pctls_noerr']=A.get_covs()*0
lims_struct['common']['pctls_noerr'][mask]=(Bcorrs> lims_struct['common']['min'][mask]) != (Bcorrs> lims_struct['common']['max'][mask])
elif a == 'additive':
# calculate bounds for Additive Signal addition
# using formulation from Supporting Info., ASC paper
# set up data
posX=(diffX>=0)
posY=(diffY>=0)
# known variables
a11=Astdm
a21=Astdmt*Acorrs
a22=np.sqrt(Astdmt**2-a21**2)
# unknown variables
a31=np.zeros(a22.shape)
a32=np.zeros(a22.shape)
a41=np.zeros(a22.shape)
a42p=np.zeros(a22.shape)
a42n=np.zeros(a22.shape)
# unknown values for max increases in signal (pos.corr) (See Supporting info)
a32[posX]=np.sqrt(abs(diffX[posX]-a31[posX]**2))
a31[posX]=0
a41[posY]=np.sqrt(abs(a22[posY]**2/(a22[posY]**2+a21[posY]**2)))*np.sqrt(abs(diffY[posY]))
a42p[posY]=-a41[posY]*(a21[posY]/a22[posY])
a42n[posY]=a41[posY]*(a21[posY]/a22[posY])
# unknown values for max decreases
a31[~posX]=diffX[~posX]/a11[~posX]
a32[~posX]=np.sqrt(-a31[~posX]**2-diffX[~posX])
a41[~posY]=np.sqrt(abs(a22[~posY]**2/(a22[~posY]**2+a21[~posY]**2)))*np.sqrt(abs(diffY[~posY]))
a42p[~posY]= (-a21[~posY]*a41[~posY] + diffY[~posY])/a22[~posY]
a42n[~posY]= (a21[~posY]*a41[~posY] + diffY[~posY])/a22[~posY]
# varous possible values for min/max lims options e.g. a32,a41 pos/neg
lims1=a11*a21+a11*a41 + a22*a32 + a32*a42p
corr1=lims1/(Bstdm*Bstdmt)
lims2=a11*a21-a11*a41 - a22*a32 - a32*a42n
corr2=lims2/(Bstdm*Bstdmt)
lims3=a11*a21+a11*a41 - a22*a32 - a32*a42p
corr3=lims3/(Bstdm*Bstdmt)
lims4=a11*a21-a11*a41 + a22*a32 + a32*a42n
corr4=lims4/(Bstdm*Bstdmt)
# find min/max across these options
corrmin=np.minimum.reduce([corr1,corr2,corr3,corr4])
corrmax=np.maximum.reduce([corr1,corr2,corr3,corr4])
# todo mask
# check correlation between x_A and x_B,y_B to determine if changes have overshot corr=1/-1. If so max_corr = +-1 (see Supp Info)
cc_xA_yB=a11*(a21+a41)/(Astdm*Bstdmt)
cc_xA_xB=(a11**2+a11*a31)/(Astdm*Bstdm)
corr1s=((cc_xA_yB)>(cc_xA_xB))&(Acorrs>0)
corrm1s=((cc_xA_yB)<-(cc_xA_xB))&(Acorrs<0)
corrmin[corrm1s]=-1
corrmax[corr1s]=1
# fill output using mask
lims_struct[a]['min'] = A.get_covs()*0
lims_struct[a]['max'] = A.get_covs()*0
lims_struct[a]['min'][mask]=corrmin
lims_struct[a]['max'][mask]=corrmax
lims_struct['additive']['pctls_noerr']=A.get_covs()*0
lims_struct['additive']['pctls_noerr'][mask]=(Bcorrs> lims_struct['additive']['min'][mask]) != (Bcorrs> lims_struct['additive']['max'][mask])
# Run above with randomised Monte Carlo inputs for modelling of uncertainty (see paper, Supp. Info 1.1.4)
if errdist_perms > 0:
# shape of permuted data distribution
shp_dist = tuple((errdist_perms,shp[0]))
# set up data
# simulated distributon of underlying covariances
A_sim_dist=np.zeros(shp_dist)
B_sim_dist=np.zeros(shp_dist)
lims_struct['covs']['corrs_raw_A'] = np.zeros(shp_dist)
lims_struct['covs']['corrs_raw_B'] = np.zeros(shp_dist)
lims_struct['covs']['covs_raw_A'] = np.zeros(shp_dist)
lims_struct['covs']['covs_raw_B'] = np.zeros(shp_dist)
# distributions of min and max across all
lims_struct['uncorrelated']['pctls_raw'] = np.zeros(shp_dist)
lims_struct['common']['min_pctls_raw'] = np.zeros(shp_dist)
lims_struct['common']['max_pctls_raw'] = np.zeros(shp_dist)
lims_struct['additive']['min_pctls_raw'] = np.zeros(shp_dist)
lims_struct['additive']['max_pctls_raw'] = np.zeros(shp_dist)
# generate prior cov matrices
sims_gen_A=wishart_gen(A)
sims_gen_B=wishart_gen(B)
# generate simulated data
A_sims=sims_gen_A.get_sims(errdist_perms)
B_sims=sims_gen_B.get_sims(errdist_perms)
for perm_i in np.arange(errdist_perms):
# print(perm_i.astype(str))
A_sim=FC(A_sims[perm_i],cov_flag=True,dof=A.dof)
B_sim=FC(B_sims[perm_i],cov_flag=True,dof=B.dof)
out = ASC_lims_all(A_sim,B_sim,errdist_perms=0,pcorrs=pcorrs,dof=dof,ch_type=ch_type,sim_sampling=sim_sampling)
# uncorrelated
if np.ndim(A.covs)==3:
mask_sim=(mask[1],mask[2])
else:
mask_sim = mask
if 'uncorrelated' in ch_type:
lims_struct['uncorrelated']['pctls_raw'][perm_i,:] = out['uncorrelated']['min'][mask_sim]
# shared
if 'covs' in ch_type:
lims_struct['covs']['corrs_raw_A'][perm_i,:]=A_sim.get_corrs(pcorrs=pcorrs)[mask_sim]
lims_struct['covs']['corrs_raw_B'][perm_i,:]=B_sim.get_corrs(pcorrs=pcorrs)[mask_sim]
lims_struct['covs']['corrs_raw_A'][perm_i,:]=A_sim.get_covs(pcorrs=pcorrs)[mask_sim]
lims_struct['covs']['corrs_raw_B'][perm_i,:]=B_sim.get_covs(pcorrs=pcorrs)[mask_sim]
if 'common' in ch_type:
lims_struct['common']['min_pctls_raw'][perm_i,:]= out['common']['min'][mask_sim]
lims_struct['common']['max_pctls_raw'][perm_i,:]= out['common']['max'][mask_sim]
# additive
if 'additive' in ch_type:
lims_struct['additive']['min_pctls_raw'][perm_i,:]= out['additive']['min'][mask_sim]
lims_struct['additive']['max_pctls_raw'][perm_i,:]= out['additive']['max'][mask_sim]
lims_struct=calc_percentiles(A,B,lims_struct,pctl,ch_type=['covs','uncorrelated','common','additive'],pcorrs=False)
if keepRaw==False:
for pp in ch_type:
if pp=='uncorrelated':
del lims_struct[pp]['pctls_raw']
elif pp=='covs':
del lims_struct['covs']['corrs_raw_B']
del lims_struct['covs']['corrs_raw_A']
del lims_struct['covs']['covs_raw_B']
del lims_struct['covs']['covs_raw_A']
else:
del lims_struct[pp]['min_pctls_raw']
del lims_struct[pp]['max_pctls_raw']
return lims_struct
def calc_percentiles(A,B,lims_struct,pctl,mask=None,ch_type=['covs','uncorrelated','common','additive'],pcorrs=False):
""" calculate percentiles for . """
if mask is None:
shp = A.get_covs().shape
if scipy.sparse.issparse(A.covs):
mask = A.get_covs().nonzero()
#todo fix masking
else:
mask = np.triu_indices(shp[-1],k=1)
if len(shp) == 3:
first=np.repeat(np.arange(shp[0]),len(mask[0]))
second=np.tile(mask[0],shp[0])
third=np.tile(mask[1],shp[0])
mask=tuple((first,second,third))
Acorrs=np.array(A.get_corrs(pcorrs=pcorrs)[mask]).flatten()
Bcorrs=np.array(B.get_corrs(pcorrs=pcorrs)[mask]).flatten()
shp=Bcorrs.shape
if 'covs' in ch_type:
raw = lims_struct['covs']['corrs_raw_A'] - lims_struct['covs']['corrs_raw_B']
lims_struct['covs']['incl_zeros'] = percentileofscore(raw,0,0)
if 'uncorrelated' in ch_type:
uncorrelated_lims_err = lims_struct['uncorrelated']['pctls_raw']
pctl_max = np.nanpercentile(uncorrelated_lims_err,100-pctl,0)
pctl_min = np.nanpercentile(uncorrelated_lims_err,pctl,0)
lims_struct['uncorrelated']['min_pctls'] = A.get_covs()*0
lims_struct['uncorrelated']['min_pctls'][mask] = pctl_min
lims_struct['uncorrelated']['max_pctls'] = A.get_covs()*0
lims_struct['uncorrelated']['max_pctls'][mask] = pctl_max
lims_struct['uncorrelated']['pctls']=A.get_covs()*0
lims_struct['uncorrelated']['pctls'][mask]=(Bcorrs > pctl_min) != (Bcorrs> pctl_max)
# common
if 'common' in ch_type:
corr_min_common_err = lims_struct['common']['min_pctls_raw']
corr_max_common_err = lims_struct['common']['max_pctls_raw']
pctl_out_max = np.nanpercentile(corr_max_common_err,100-pctl, 0)
lims_struct['common']['max_pctls'] = A.get_covs()*0
lims_struct['common']['max_pctls'][mask] = pctl_out_max
pctl_out_min = np.nanpercentile(corr_min_common_err,pctl, 0)
lims_struct['common']['min_pctls'] = A.get_covs()*0
lims_struct['common']['min_pctls'][mask] = pctl_out_min
lims_struct['common']['pctls'] = A.get_covs()*0
lims_struct['common']['pctls'][mask] = (Bcorrs> pctl_out_max) != (Bcorrs> pctl_out_min)
# additive
if 'additive' in ch_type:
corr_min_additive_err = lims_struct['additive']['min_pctls_raw']
corr_max_additive_err = lims_struct['additive']['max_pctls_raw']
pctl_out_min = np.nanpercentile(corr_min_additive_err,pctl,0)
lims_struct['additive']['min_pctls'] = A.get_covs()*0
lims_struct['additive']['min_pctls'][mask] = pctl_out_min
pctl_out_max = np.nanpercentile(corr_max_additive_err,100-pctl,0)
lims_struct['additive']['max_pctls'] = A.get_covs()*0
lims_struct['additive']['max_pctls'][mask] = pctl_out_max
lims_struct['additive']['pctls'] = A.get_covs()*0
lims_struct['additive']['pctls'][mask] = (Bcorrs> pctl_out_max) != (Bcorrs> pctl_out_min)
return(lims_struct)
def abs_sort(x):
""" sort according to absolute value. """
if len(x.shape)==1:
return x[np.argsort(abs(x),0)]
else:
out = np.array(x)[np.argsort(abs(x),0),np.arange(x.shape[1])]
return out
def corr2pcorr(cc):
""" convert correlation to partial corr. """
pinvA=linalg.pinv(cc)
iis=np.tile(np.atleast_3d(pinvA.diagonal()).T,pinvA.shape[1])
dd=np.diag(pinvA)
output=-pinvA/np.sqrt(iis*iis.T)
output[np.where(np.eye(cc.shape[1]))]=1
return(output)
class wishart_gen:
""" class to generate simulated samples for a covariance matrix. """
def __init__(self,A):
self.A=A
def get_sims(self,errdist_perms,recalc=False):
""" generate simulated wishart samples """
A=self.A
if not( 'covs_sim' in self.__dict__) or recalc==True:
covs=A.get_covs()
dof=A.dof
# if sparse cov matrix
if scipy.sparse.issparse(A.get_covs()):
corrs=A.get_corrs()
cvs=covs.diagonal()[1:]
# generate random samples from wishart
(vars1,vars2,covmat1) = wishart_2(covs[0,0],covs.diagonal()[1:],corrs[0,1:].toarray(),dof,size=errdist_perms)
covs_sim=[]
for a in np.arange(vars2.shape[0]):
covmat=A.get_covs()*0
covmat[0,0]=vars1[a,0]
diag_inds=np.diag_indices(covs.shape[-1])
covmat[diag_inds[0][1:],diag_inds[1][1:]]=vars2[a,:]
covmat[0,1:]=covmat1[a,:]
covs_sim.append(covmat)
else:
# simulate underlying "true" covariance
whA=stats.wishart(dof,np.squeeze(covs)[:,:])
covs_sim=whA.rvs(10*errdist_perms)/(dof)
covs_sim=list(covs_sim)
# create simulated observed covariances from underlying true cov.
ppA=np.zeros((1,10*errdist_perms))
whA=[]
for yb in np.arange(10*errdist_perms):
whA.append(stats.wishart(dof,covs_sim[yb]))
ppA[0,yb]=whA[-1].pdf(np.squeeze(A.get_covs())*dof)
# generate sample distribution of covariances
ppA=ppA/sum(ppA)
ppA_cul=(np.dot(ppA,np.triu(np.ones(len(ppA.T)))).T) ## memory issues
rand_els = stats.uniform(0,1).rvs(errdist_perms)
els=np.sort(np.searchsorted(ppA_cul.flatten(),rand_els))
covs_sim=[]
for xb in np.arange(errdist_perms):
covs_sim.append(whA[els[xb]].rvs()/dof)
self.covs_sim=covs_sim
return(self.covs_sim)
def wishart_2(vars1,vars2,rho,dof,size=1):
""" Generate wishart from 2 variances and correlation matrix. """
rho=np.atleast_3d(rho).T
vars1=np.atleast_3d(vars1).T
vars2=np.atleast_3d(vars2).T
chis1 = np.atleast_3d(stats.chi2.rvs(dof,size=size))
chis2 = np.atleast_3d(stats.chi2.rvs(dof-1,size=size))
norms = np.atleast_3d(stats.norm.rvs(0,1,size=size))
sqrt_rho = np.sqrt(1-rho**2)
vars1_sim = vars1*chis1/dof
covs_sim = np.sqrt(vars1*vars2)*(rho*chis1 + sqrt_rho *(chis1**0.5)*norms)/dof
vars2_sim = vars2 * ( chis2*(sqrt_rho**2) + (sqrt_rho*norms + rho*(chis1**.5))**2 )/dof
return(vars1_sim.T,vars2_sim.T,covs_sim.T)
def wishart_pdf(cov,samples,dof):
""" wishart pdf given cov and dof. """
if scipy.sparse.issparse(cov):
var1=cov[0,0]
var2=cov.diagonal()[1:]
covs1=covs[0,:]
for a in len(var2):
whA.append(stats.wishart(dof,covsA_sim[b,:,:]))
ppA[0,yb]=whA[-1].pdf(A.get_covs()[xa,:,:]*dof)
return(cov)
def var_gamma_pdf(x,std1,std2,rho,dof):
""" marginal distribution of covariance. """
ors = (1-rho**2)
gamma_d = stats.gamma(dof/2.0)
first = abs(x)**((dof-1)/2) / ( gamma_d * np.sqrt(2^(dof-1)*pi*ors*(std1*std2)**n+1) )
second = special((dof-1)/2.0,abs(x)/(std1*std2*ors))
third = e**( (rho*x) / std1*std2*ors)
return(first*second*third)
def pcorr2corr(pcorr):
""" calculate approx correlation from pcorr. """
ipcorr=linalg.pinv(pcorr)
iis=np.tile(np.atleast_3d(ipcorr.diagonal()).T,pcorr.shape[1])
dd=diag(ipcorr)
tmp=-ipcorr*np.sqrt(iis*iis.T)
tmp[where(np.eye(cc.shape[1]))]=dd
return(tmp)
def rprior(n=1,r=3,M=np.eye(2)):
""" calculate r prior: n,r,M. """
out=np.zeros((n,len(M),len(M)))
Minv=np.la.pinv(M)
for a in np.arange(n):
out[a,:,:]= scipy.linalg.cho_solve(scipy.linalg.cho_factor( stats.wishart(r,Minv).rvs()),np.eye(len(Minv)))
return(out)
def worker(input, output):
""" worker for pprocessing """
for func, args in iter(input.get, 'STOP'):
result = calculate(func, args)
output.put(result)
def calculate(func, args):
""" calculator for pprocessing """
result = func(*args)
return results
def func_star(a_b):
""" Convert `f([1,2])` to `f(1,2)` call. """
niceness=os.nice(0)
os.nice(5-niceness)
return ASC_lims_all_pool(*a_b)
def runwithvv2(c):
""" parallelise variance calculation """
vv1s=range(len(vvs_all))
args=itertools.izip(itertools.repeat(ccs),itertools.repeat(ccval),itertools.repeat(vvs_all),vv1s,itertools.repeat(vv1s),itertools.repeat(100),itertools.repeat(100))
pool=Pool()
out=pool.imap(func_star,args)
pool.close()
pool.join()
return(out,pool)
def flatten_tcs(A,dof='EstEff'):
""" Concat timecourses, demeaning, estimating dof """
tcs_dm=pl.demean(A.tcs,2)
shp=tcs_dm.shape
out=FC((np.reshape(tcs_dm.swapaxes(0,1),[shp[1],-1])),ROI_info=A.ROI_info)
# subtract dofs from demeaning
out.dof=out.dof-shp[0]
tcs = out.tcs
if dof is None:
self.dof=tcs.shape[-1]-1
elif dof == 'EstEff':
AR=np.zeros((tcs.shape[0],tcs.shape[1],15))
ps=np.zeros((tcs.shape[0],tcs.shape[1],15))
for subj in np.arange(tcs.shape[0]):
for ROI in np.arange(tcs.shape[1]):
AR[subj,ROI,:]=spectrum.aryule(pl.demean(tcs[subj,ROI,:]),15)[0]
ps[subj,ROI,:]=spectrum.correlation.CORRELATION(pl.demean(tcs[subj,ROI,:]),maxlags=14,norm='coeff')
ps = np.mean(np.mean(ps,0),0)
AR = np.mean(np.mean(AR,0),0)
dof_nom=tcs.shape[-1]-1
dof = int(dof_nom / (1-np.dot(ps[:15].T,AR))/(1 - np.dot(np.ones(len(AR)).T,AR)))
out.dof=dof
return(out)
def seed_loader(filenames,seed_mask_file,mask_file,subj_inds=None,dof=None):
""" load data for seed analysis """
files=[]
seed=nb.load(seed_mask_file)
seed_mask = seed.get_data()
seed_points = where(seed_mask)
mask=nb.load(mask_file)
mask_data = mask.get_data()
mask_points = where(mask_data)
covmat=coo_matrix((len(mask_points[0])+1,len(mask_points[0])+1)).tocsr()
if type(filenames)==str:
filenames=[filenames]
dof_cnt=0
for file in filenames:
print(file)
newfile = nb.load(file)
files.append(newfile)
# load
data=newfile.get_data()
seed_data = data[seed_points[0],seed_points[1],seed_points[2],:]
seed_mean = pl.demean(np.mean(seed_data,0))
data_mask = data[mask_points[0],mask_points[1],mask_points[2],:]
vars = var(data_mask,1)
covs = sum(seed_mean*pl.demean(data_mask,1),1)/len(seed_mean)
# corrs = sum(seed_mean*pl.demean(data_mask,1),1)/(np.std(seed_mean)*np.std(data_mask,1)*len(seed_mean))
# data: corner, top row, first colomn, diag
rows=np.r_[0,np.zeros(vars.shape),np.arange(len(vars))+1,np.arange(len(vars))+1]
cols=np.r_[0,np.arange(len(vars))+1,np.zeros(vars.shape),np.arange(len(vars))+1]
covmat_data=np.r_[var(seed_mean),covs,covs,vars]
covmat = covmat + coo_matrix((covmat_data,(rows,cols)),shape=(len(vars)+1,len(vars)+1)).tocsr()
dof_cnt = dof_cnt+seed_data.shape[-1]-1
# FC_cov = FC(covmat)
newfile.uncache()
if not dof:
dof=dof_cnt
covmat=covmat/len(filenames)
out = FC(covmat,cov_flag=True,dof=dof,mask=mask)
return out
def seed_saver(filename,AB_con,mask=None,save_minmax=False,save_corr=False):
""" Save seed analysis results """
# todo test for nb
lims=AB_con.get_ASC_lims()
plots = list(lims.keys())
plots.remove('covs')
if ( not 'mask' in AB_con.A.__dict__.keys() ):
if ( not mask ):
raise ValueError("No mask in contrast object, please provide.")
else:
mask = AB_con.A.mask
if type(mask)==str:
mask=nb.load(mask)
mask_data = mask.get_data()
mask_points = where(mask_data)
img=set_new_data(mask,mask_data*0)
for pp in plots:
data=img.get_data()*0
data[where(mask.get_data())] = AB_con.lims[pp]['pctls'][0,1:].toarray()
img = set_new_data(img,data)
nb.save(img,filename + '_' + pp + '.nii.gz')
if save_minmax:
data=img.get_data()*0
data[where(mask.get_data())] = AB_con.lims[pp]['min'][0,1:].toarray()
img = set_new_data(img,data)
nb.save(img,filename + '_min_' + pp + '.nii.gz')
if pp != 'uncorrelated':
data=img.get_data()*0
data[where(mask.get_data())] = AB_con.lims[pp]['max'][0,1:].toarray()
img = set_new_data(img,data)
nb.save(img,filename + '_max_' + pp + '.nii.gz')
if save_corr:
data=img.get_data()*0
data[where(mask.get_data())] = AB_con.A.get_corrs()[0,1:].toarray()
img = set_new_data(img,data)
nb.save(img,filename + '_Acorr' + '.nii.gz')
data=img.get_data()*0
data[where(mask.get_data())] = AB_con.B.get_corrs()[0,1:].toarray()
img = set_new_data(img,data)
nb.save(img,filename + '_Bcorr' + '.nii.gz')
return()