-
Notifications
You must be signed in to change notification settings - Fork 0
/
stuff.py
executable file
·2163 lines (1701 loc) · 67.6 KB
/
stuff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import nibabel as nib
import numpy as np
import os
import math
from scipy.stats import spearmanr, pearsonr, kendalltau, ttest_1samp, ttest_rel
from skimage.measure import label
import sys
# get package abspath
package_root = os.path.dirname(os.path.abspath(__file__))
' a function for zeroing the value close to zero '
def limtozero(x):
"""
zero the value close to zero
Parameters
----------
x : float
Returns
-------
0
"""
if x < 1e-15:
x = 0
return x
' a function for getting the affine of the fMRI-img '
def get_affine(file_name):
"""
get the affine of the fMRI-img
Parameters
----------
file_name : string
The filename of a sample fMRI-img in your experiment
Returns
-------
affine : array
The position information of the fMRI-image array data in a reference space.
"""
if file_name == "":
return "Invalid input"
img = nib.load(file_name)
return img.affine
' a function for fMRI RSA results correction by threshold '
def correct_by_threshold(img, threshold):
"""
correct the fMRI RSA results by threshold
Parameters
----------
img : array
A 3-D array of the fMRI RSA results.
The shape of img should be [nx, ny, nz]. nx, ny, nz represent the shape of the fMRI-img.
threshold : int
The number of voxels used in correction.
If threshold=n, only the similarity clusters consisting more than n voxels will be visualized.
Returns
-------
img : array
A 3-D array of the fMRI RSA results after correction.
The shape of img should be [nx, ny, nz]. nx, ny, nz represent the shape of the fMRI-img.
"""
if len(np.shape(img)) != 3:
return "Invalid input"
sx = np.shape(img)[0]
sy = np.shape(img)[1]
sz = np.shape(img)[2]
nsmall = 1
while nsmall*nsmall*nsmall < threshold:
nsmall = nsmall + 1
nlarge = nsmall + 2
for i in range(sx-nlarge+1):
for j in range(sy-nlarge+1):
for k in range(sz-nlarge+1):
listlarge = list(np.reshape(img[i:i+nlarge, j:j+nlarge, k:k+nlarge], [nlarge*nlarge*nlarge]))
if listlarge.count(0) < nlarge*nlarge*nlarge:
index1 = 0
for l in range(nlarge):
for m in range(nlarge):
if img[i + l, j + m, k] == 0:
index1 = index1 + 1
if img[i + l, j + m, k + nlarge - 1] == 0:
index1 = index1 + 1
for l in range(nlarge-1):
for m in range(nlarge-2):
if img[i + l, j, k + m] == 0:
index1 = index1 + 1
if img[i, j + l + 1, k + m] == 0:
index1 = index1 + 1
if img[i + nlarge - 1, j + l, k + m] == 0:
index1 = index1 + 1
if img[i + l + 1, j + nlarge - 1, k + m] == 0:
index1 = index1 + 1
nex = nlarge * nlarge * nlarge - nsmall * nsmall * nsmall
if index1 == nex:
unit = img[i+1:i+1+nsmall, j+1:j+1+nsmall, k+1:k+1+nsmall]
unit = np.reshape(unit, [nsmall*nsmall*nsmall])
list_internal = list(unit)
index2 = nsmall*nsmall*nsmall-list_internal.count(0)
if index2 < threshold:
img[i+1:i+1+nsmall, j]
for l in range(nsmall):
for m in range(nsmall):
for p in range(nsmall):
img[i+1:i+1+nsmall, j+1:j+1+nsmall, k+1:k+1+nsmall] = np.zeros([nsmall, nsmall, nsmall])
print("finished correction")
return img
' a function for FWE-correction for fMRI RSA results '
def fwe_correct(p, p_threshold):
"""
FWE correction for fMRI RSA results
Parameters
----------
p : array
The p-value map (3-D).
p_threshold: string
The p threshold.
Returns
-------
fwep : array.
The FWE corrected p-value map.
"""
px = np.shape(p)[0]
py = np.shape(p)[1]
pz = np.shape(p)[2]
n = 0
for i in range(px):
for j in range(py):
for k in range(pz):
if (math.isnan(p[i, j, k]) == False):
n = n + 1
p = p*n
fwep = np.full([px, py, pz], np.nan)
for i in range(px):
for j in range(py):
for k in range(pz):
if p[i, j, k] < p_threshold:
fwep[i, j, k] = p[i, j, k]
print("finished FWE correct")
return fwep
' a function for FDR-correction for fMRI RSA results '
def fdr_correct(p, p_threshold):
"""
FDR correction for fMRI RSA results
Parameters
----------
p : array
The p-value map (3-D).
p_threshold: string
The p threshold.
Returns
-------
fdrp : array.
The FDR corrected p-value map.
"""
px = np.shape(p)[0]
py = np.shape(p)[1]
pz = np.shape(p)[2]
n = 0
for i in range(px):
for j in range(py):
for k in range(pz):
if (math.isnan(p[i, j, k]) == False):
n = n + 1
fdrp = np.full([px, py, pz], np.nan)
pcluster = np.zeros([n], dtype=np.float)
m = 0
for i in range(px):
for j in range(py):
for k in range(pz):
if (math.isnan(p[i, j, k]) == False):
pcluster[m] = p[i, j, k]
m = m + 1
index = np.argsort(pcluster)
for l in range(n):
pcluster[index[l]] = float(pcluster[index[l]]*n/(l+1))
"""for l in range(n - 1):
if pcluster[index[-l - 1]] < pcluster[index[-l - 2]]:
pcluster[index[-l - 2]] = pcluster[index[-l - 1]]"""
m = 0
for i in range(px):
for j in range(py):
for k in range(pz):
if math.isnan(p[i, j, k]) == False:
if p[i, j, k] < p_threshold:
fdrp[i, j, k] = pcluster[m]
m = m + 1
print("finished FDR correct")
return fdrp
' a function for Cluster-wise FWE-correction for fMRI RSA results '
def cluster_fwe_correct(p, p_threshold1, p_threshold2):
"""
Cluster-wise FWE correction for fMRI RSA results
Parameters
----------
p : array
The p-value map (3-D).
p_threshold1: string
The voxel-wise p threshold.
p_threshold2: string
The cluster-wise p threshold
Returns
-------
clusterfwep : array.
The Cluster-wise FWE corrected p-value map.
"""
px = np.shape(p)[0]
py = np.shape(p)[1]
pz = np.shape(p)[2]
p01 = np.zeros([px, py, pz])
for i in range(px):
for j in range(py):
for k in range(pz):
if p[i, j, k] < p_threshold1:
p01[i, j, k] = 1
print("Cluster-wise FWE correction")
permutation_voxels = np.zeros([1000])
for k in range(1000):
# show the progressbar
percent = (k+1) / 1000 * 100
show_progressbar("Correcting", percent)
pi = np.copy(p01)
pi = np.reshape(pi, [px * py * pz])
indexk = np.arange(0, px * py * pz)
np.random.shuffle(indexk)
pi = pi[indexk]
pi = np.reshape(pi, [px, py, pz])
labels = label(pi, connectivity=1)
nclusters = int(np.max(labels))
voxelsinluster = np.zeros([nclusters + 1], dtype=int)
labels = np.reshape(labels, [px * py * pz])
for i in range(px * py * pz):
voxelsinluster[labels[i]] = voxelsinluster[labels[i]] + 1
permutation_voxels[k] = max(voxelsinluster[1:])
print("\n")
permutation_voxels = np.sort(permutation_voxels)
voxels_threshold = permutation_voxels[int(1000*(1-p_threshold2))]
labels = label(p01, connectivity=1)
nclusters = int(np.max(labels))
voxelsinluster = np.zeros([nclusters + 1], dtype=int)
labels = np.reshape(labels, [px * py * pz])
for i in range(px * py * pz):
voxelsinluster[labels[i]] = voxelsinluster[labels[i]] + 1
voxelsinluster = voxelsinluster[1:]
labels = np.reshape(labels, [px, py, pz])
clusterp = np.zeros([nclusters])
for i in range(nclusters):
clusterp[i] = (1000 - np.max(np.array(np.where(np.sort(np.append(permutation_voxels, voxelsinluster[i])) == voxelsinluster[i])))) / 1000
clusterp = clusterp * nclusters
clusterfwep = np.full([px, py, pz], np.nan)
for i in range(px):
for j in range(py):
for k in range(pz):
if (math.isnan(p[i, j, k]) == False) and labels[i, j, k] != 0\
and clusterp[labels[i, j, k]-1] < p_threshold1 and voxelsinluster[labels[i, j, k]-1] >= voxels_threshold:
clusterfwep[i, j, k] = clusterp[labels[i, j, k]-1]
print("finished Cluster-wise FWE correction")
return clusterfwep
' a function for Cluster-wise FDR-correction for fMRI RSA results '
def cluster_fdr_correct(p, p_threshold1, p_threshold2):
"""
Cluster-wise FDR correction for fMRI RSA results
Parameters
----------
p : array
The p-value map (3-D).
p_threshold1: string
The voxel-wise p threshold.
p_threshold2: string
The cluster-wise p threshold
Returns
-------
clusterfdrp : array.
The Cluster-wise FDR corrected p-value map.
"""
px = np.shape(p)[0]
py = np.shape(p)[1]
pz = np.shape(p)[2]
p01 = np.zeros([px, py, pz])
for i in range(px):
for j in range(py):
for k in range(pz):
if p[i, j, k] < p_threshold1:
p01[i, j, k] = 1
print("Cluster-wise FDR correction")
permutation_voxels = np.zeros([1000])
for k in range(1000):
# show the progressbar
percent = (k+1) / 1000 * 100
show_progressbar("Correcting", percent)
pi = np.copy(p01)
pi = np.reshape(pi, [px * py * pz])
indexk = np.arange(0, px * py * pz)
np.random.shuffle(indexk)
pi = pi[indexk]
pi = np.reshape(pi, [px, py, pz])
labels = label(pi, connectivity=1)
nclusters = int(np.max(labels))
voxelsinluster = np.zeros([nclusters+1], dtype=int)
labels = np.reshape(labels, [px*py*pz])
for i in range(px*py*pz):
voxelsinluster[labels[i]] = voxelsinluster[labels[i]] + 1
permutation_voxels[k] = max(voxelsinluster[1:])
print("\n")
permutation_voxels = np.sort(permutation_voxels)
voxels_threshold = permutation_voxels[int(1000*(1-p_threshold2))]
labels = label(p01, connectivity=1)
nclusters = int(np.max(labels))
voxelsinluster = np.zeros([nclusters+1], dtype=int)
labels = np.reshape(labels, [px*py*pz])
for i in range(px*py*pz):
voxelsinluster[labels[i]] = voxelsinluster[labels[i]] + 1
voxelsinluster = voxelsinluster[1:]
labels = np.reshape(labels, [px, py, pz])
clusterp = np.zeros([nclusters])
for i in range(nclusters):
clusterp[i] = (1000 - np.max(np.array(np.where(np.sort(np.append(permutation_voxels, voxelsinluster[i])) ==
voxelsinluster[i])))) / 1000
index = np.argsort(clusterp)
for i in range(nclusters):
clusterp[index[i]] = float(clusterp[index[i]] * nclusters / (i+1))
clusterfdrp = np.full([px, py, pz], np.nan)
for i in range(px):
for j in range(py):
for k in range(pz):
if (math.isnan(p[i, j, k]) == False) and labels[i, j, k] != 0\
and clusterp[labels[i, j, k]-1] < p_threshold1 and voxelsinluster[labels[i, j, k]-1] >= \
voxels_threshold:
clusterfdrp[i, j, k] = clusterp[labels[i, j, k]-1]
print("finished Cluster-wise FDR correction")
return clusterfdrp
' a function for getting ch2.nii.gz '
def get_bg_ch2():
"""
get ch2.nii.gz
Returns
-------
path : string
The absolute file path of 'ch2.nii.gz'
"""
return os.path.join(package_root, 'template/ch2.nii.gz')
' a function for getting ch2bet.nii.gz '
def get_bg_ch2bet():
"""
get ch2bet.nii.gz
Returns
-------
path : string
The absolute file path of 'ch2bet.nii.gz'
"""
return os.path.join(package_root, 'template/ch2bet.nii.gz')
' a function for getting HarvardOxford-cort-maxprob-thr0-1mm.nii.gz '
def get_HOcort():
"""
get HarvardOxford-cort-maxprob-thr0-1mm.nii.gz
Returns
-------
path : string
The absolute file path of 'HarvardOxford-cort-maxprob-thr0-1mm.nii.gz'
"""
return os.path.join(package_root, 'template/HarvardOxford-cort-maxprob-thr0-1mm.nii.gz')
' a function for filtering the data by a ROI mask '
def datamask(fmri_data, mask_data):
"""
filter the data by a ROI mask
Parameters
----------
fmri_data : array
The fMRI data.
The shape of fmri_data is [nx, ny, nz]. nx, ny, nz represent the size of the fMRI data.
mask_data : array
The mask data.
The shape of mask_data is [nx, ny, nz]. nx, ny, nz represent the size of the fMRI data.
Returns
-------
newfmri_data : array
The new fMRI data.
The shape of newfmri_data is [nx, ny, nz]. nx, ny, nz represent the size of the fMRI data.
"""
if len(np.shape(fmri_data)) != 3 or len(np.shape(mask_data)) != 3:
return "Invalid input"
nx, ny, nz = fmri_data.shape
newfmri_data = np.full([nx, ny, nz], np.nan)
for i in range(nx):
for j in range(ny):
for k in range(nz):
if (mask_data[i, j, k] != 0) and (math.isnan(mask_data[i, j, k]) is False):
newfmri_data[i, j, k] = fmri_data[i, j, k]
return newfmri_data
' a function for projecting the position of a point in matrix coordinate system to the position in MNI coordinate system '
def position_to_mni(point, affine):
"""
project the position in matrix coordinate system to the position in MNI coordinate system
Parameters
----------
point : list or array
The position in matrix coordinate system.
affine : array or list
The position information of the fMRI-image array data in a reference space.
Returns
-------
newpoint : array
The position in MNI coordinate system.
"""
i = point[0]
j = point[1]
k = point[2]
x = -affine[0, 3] + i * affine[0, 0] - affine[0, 0]
y = affine[1, 3] + j * affine[1, 1] - affine[1, 1]
z = affine[2, 3] + k * affine[2, 2] - affine[2, 2]
newpoint = np.array([x, y, z])
return newpoint
' a function for project the position in MNI coordinate system to the position of a point in matrix coordinate system '
def mniposition_to(mnipoint, affine):
"""
project the position in MNI coordinate system to the position of a point in matrix coordinate system
Parameters
----------
point : list or array
The position in MNI coordinate system.
affine : array or list
The position information of the fMRI-image array data in a reference space.
Returns
-------
newpoint : array
The position in matrix coordinate system.
"""
mx = int(float((mnipoint[0] - affine[0, 3])/affine[0, 0]))
my = int(float((mnipoint[1] - affine[1, 3])/affine[1, 1]))
mz = int(float((mnipoint[2] - affine[2, 3])/affine[2, 2]))
return mx, my, mz
' a function for converting data of the mask template to your data template '
def mask_to(mask, size, affine, filename=None):
"""
convert mask data of certain template to your data template
Parameters
----------
mask : string
The file path+filename for the mask of certain template.
size : array or list [nx, ny, nz]
The size of the fMRI-img in your experiments.
affine : array or list
The position information of the fMRI-image array data in a reference space.
filename : string. Default is None - 'newmask.nii'.
The file path+filename for the mask for your data template .nii file.
Notes
-----
A result .nii file of new mask will be generated at the corresponding address of filename.
"""
data = nib.load(mask).get_fdata()
nx = data.shape[0]
ny = data.shape[1]
nz = data.shape[2]
maskaffine = nib.load(mask).affine
newdata = np.zeros(size, dtype=np.float)
for i in range(nx):
for j in range(ny):
for k in range(nz):
if data[i, j, k] != 0 or math.isnan(data[i, j, k]) is False:
mx = maskaffine[0, 3]+(i-1)*maskaffine[0, 0]
my = maskaffine[1, 3]+(j-1)*maskaffine[1, 1]
mz = maskaffine[2, 3]+(k-1)*maskaffine[2, 2]
x = int(float((mx-affine[0, 3])/affine[0, 0]))
y = int(float((my-affine[1, 3])/affine[1, 1]))
z = int(float((mz-affine[2, 3])/affine[2, 2]))
if x < size[0] and y < size[1] and z < size[2]:
newdata[x, y, z] = data[i, j, k]
file = nib.Nifti1Image(newdata, affine)
if filename == None:
filename = "newmask.nii"
else:
q = ".nii" in filename
if q == True:
filename = filename
else:
filename = filename+".nii"
nib.save(file, filename)
return 0
' a function for permutation test '
def permutation_test(v1, v2, iter=1000):
"""
Conduct Permutation test
Parameters
----------
v1 : array
Vector 1.
v2 : array
Vector 2.
iter : int. Default is 1000.
The times for iteration.
Returns
-------
p : float
The permutation test result, p-value.
"""
if len(v1) != len(v2):
return "Invalid input"
# permutation test
diff = abs(np.average(v1) - np.average(v2))
v = np.hstack((v1, v2))
nv = v.shape[0]
ni = 0
for i in range(iter):
vshuffle = np.random.permutation(v)
vshuffle1 = vshuffle[:int(nv/2)]
vshuffle2 = vshuffle[int(nv/2):]
diff_i = np.average(vshuffle1) - np.average(vshuffle2)
if diff_i >= diff:
ni = ni + 1
# permunitation test p-value
p = np.float64(ni/iter)
return p
' a function for permutation test for correlation coefficients '
def permutation_corr(v1, v2, method="spearman", iter=1000):
"""
Conduct Permutation test for correlation coefficients
Parameters
----------
v1 : array
Vector 1.
v2 : array
Vector 2.
method : string 'spearman' or 'pearson' or 'kendall' or 'similarity' or 'distance'. Default is 'spearman'.
The method to calculate the similarities.
If method='spearman', calculate the Spearman Correlations. If method='pearson', calculate the Pearson
Correlations. If methd='kendall', calculate the Kendall tau Correlations. If method='similarity', calculate the
Cosine Similarities. If method='distance', calculate the Euclidean Distances.
iter : int. Default is 1000.
The times for iteration.
Returns
-------
p : float
The permutation test result, p-value.
"""
if len(v1) != len(v2):
return "Invalid input"
# permutation test
if method == "spearman":
rtest = spearmanr(v1, v2)[0]
ni = 0
for i in range(iter):
v1shuffle = np.random.permutation(v1)
v2shuffle = np.random.permutation(v2)
rperm = spearmanr(v1shuffle, v2shuffle)[0]
if rperm > rtest:
ni = ni + 1
if method == "pearson":
print(iter)
rtest = pearsonr(v1, v2)[0]
ni = 0
for i in range(iter):
v1shuffle = np.random.permutation(v1)
v2shuffle = np.random.permutation(v2)
rperm = pearsonr(v1shuffle, v2shuffle)[0]
if rperm>rtest:
ni = ni + 1
if method == "kendalltau":
rtest = kendalltau(v1, v2)[0]
ni = 0
for i in range(iter):
v1shuffle = np.random.permutation(v1)
v2shuffle = np.random.permutation(v2)
rperm = kendalltau(v1shuffle, v2shuffle)[0]
if rperm>rtest:
ni = ni + 1
p = np.float64((ni+1)/(iter+1))
return p
' a function for getting the 1-D & 1-sided cluster-index information '
def get_cluster_index_1d_1sided(m):
"""
Get 1-D & 1-sided cluster-index information from a vector
Parameters
----------
m : array
A significant vector.
The values in m should be 0 or 1, which represent not significant point or significant point, respectively.
Returns
-------
index_v : array
The cluster-index vector.
index_n : int
The number of clusters.
"""
x = np.shape(m)[0]
b = np.zeros([x+2])
b[1:x+1] = m
index_v = np.zeros([x])
index_n = 0
for i in range(x):
if b[i+1] == 1 and b[i] == 0 and b[i+2] == 1:
index_n = index_n + 1
if b[i+1] == 1:
if b[i] != 0 or b[i+2] != 0:
index_v[i] = index_n
return index_v, index_n
' a function for getting the 1-D & 2-sided cluster-index information '
def get_cluster_index_1d_2sided(m):
"""
Get 1-D & 2-sided cluster-index information from a vector
Parameters
----------
m : array
A significant vector.
The values in m should be 0 or 1 or -1, which represent not significant point or significantly higher point or
significantly less point, respectively.
Returns
-------
index_v1 : array
The "greater" cluster-index vector.
index_n1 : int
The number of "greater" clusters.
index_v2 : array
The "less" cluster-index vector.
index_n2 : int
The number of "less" clusters.
"""
x = np.shape(m)[0]
b = np.zeros([x+2])
b[1:x+1] = m
index_v1 = np.zeros([x])
index_n1 = 0
for i in range(x):
if b[i+1] == 1 and b[i] != 1 and b[i+2] == 1:
index_n1 = index_n1 + 1
if b[i+1] == 1:
if b[i] == 1 or b[i+2] == 1:
index_v1[i] = index_n1
index_v2 = np.zeros([x])
index_n2 = 0
for i in range(x):
if b[i + 1] == -1 and b[i] != -1 and b[i + 2] == -1:
index_n2 = index_n2 + 1
if b[i + 1] == -1:
if b[i] == -1 or b[i + 2] == -1:
index_v2[i] = index_n2
return index_v1, index_n1, index_v2, index_n2
' a function for getting the 2-D & 1-sided cluster-index information '
def get_cluster_index_2d_1sided(m):
"""
Get 2-D & 1-sided cluster-index information from a matrix
Parameters
----------
m : array
A significant matrix.
The values in m should be 0 or 1, which represent not significant point or significant point, respectively.
Returns
-------
index_m : array
The cluster-index matrix.
index_n : int
The number of clusters.
"""
x, y = np.shape(m)
b = np.zeros([x+2, y+2])
b[1:x+1, 1:y+1] = m
index_m = np.zeros([x, y])
index_n = 0
for i in range(x):
for j in range(y):
ii = i + 1
jj = j + 1
if b[ii, jj] == 1 and (b[ii-1, jj]+b[ii+1, jj]+b[ii, jj-1]+b[ii, jj+1]) != 0:
min_index = index_n + 1
if b[ii - 1, jj] == 1:
min_index = np.min([min_index, index_m[i - 1, j]])
if b[ii, jj - 1] == 1:
min_index = np.min([min_index, index_m[i, j - 1]])
k1 = 0
while b[ii, jj - k1] == 1:
index_m[i, j - k1] = min_index
k1 = k1 + 1
k2 = 0
while b[ii - k2, jj] == 1:
index_m[i - k2, j] = min_index
k2 = k2 + 1
k = 0
while b[ii, jj + k] == 1:
index_m[i, j + k] = min_index
k = k + 1
k = 0
while b[ii + k, jj] == 1:
index_m[i + k, j] = min_index
k = k + 1
if b[ii, jj - 1] != 1:
index_n = index_n + 1
k = 0
m = 0
while b[ii, jj + k] == 1:
if b[ii - 1, jj + k] == 1:
m = 1
k = k + 1
if m == 1:
index_n = index_n - 1
return index_m, index_n
' a function for getting the 2-D & 2-sided cluster-index information '
def get_cluster_index_2d_2sided(m):
"""
Get 2-D & 2-sided cluster-index information from a matrix
Parameters
----------
m : array
A significant matrix.
The values in m should be 0 or 1 or -1, which represent not significant point or significantly higher point or
significantly less point, respectively.
Returns
-------
index_m1 : array
The "greater" cluster-index matrix.
index_n1 : int
The "greater" number of clusters.
index_m2 : array
The "less" cluster-index matrix.
index_n2 : int
The "less" number of clusters.
"""
x, y = np.shape(m)
b1 = np.zeros([x+2, y+2])
b1[1:x+1, 1:y+1] = m
b2 = np.zeros([x+2, y+2])
b2[1:x+1, 1:y+1] = m
index_m1 = np.zeros([x, y])
index_m2 = np.zeros([x, y])