-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpatch_utils.py
1212 lines (1003 loc) · 34.5 KB
/
patch_utils.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
from scipy.signal import convolve2d
import numpy as np
import warnings
#import nibabel
import nrrd
import pdb
import csv
import os
import NN
class PatchBinaryData(object):
"""Class for creating patch-based data set
from a series of masked images
The class has methods for patchifying the
images, partitiong the patches, dividing
them into batches, loading the patches
from a set of indices for training a
model, etc.
"""
def __init__(self, img_addrs, mask_addrs):
self.img_addrs = img_addrs
self.mask_addrs = mask_addrs
def generate_samples(self,
img_inds,
N,
ratio_thr,
view):
"""Generating samples from somes of
the images whose indices are given
in terms of `self.img_addrs`
"""
inds_dict = {self.img_addrs[i]:[]
for i in img_inds}
labels_dict = {self.img_addrs[i]:[]
for i in img_inds}
types_dict = {self.img_addrs[i]:[]
for i in img_inds}
# corresponding integer to the view
# sagittal : 0
# coronal : 1
# axial : 2
view_id = np.where(np.array(
['sagittal',
'coronal',
'axial'])==view)[0][0]
# sampling from the volumes
for i in img_inds:
img,_ = nrrd.read(
self.img_addrs[i])
mask,_ = nrrd.read(
self.mask_addrs[i])
# determining the slices for which
# the masked volume is larger than
# a specified threshold
ratios = np.zeros(img.shape[view_id])
for j in range(len(ratios)):
if view_id==0:
mask_vol = np.sum(mask[j,:,:])
total_vol = np.prod(img.shape[1:])
elif view_id==1:
mask_vol = np.sum(mask[:,j,:])
total_vol = img.shape[0]*img.shape[2]
elif view_id==2:
mask_vol = np.sum(mask[:,:,j])
total_vol = np.prod(img.shape[:-1])
ratios[j] = float(mask_vol) / \
float(total_vol)
slices = np.where(
ratios>ratio_thr)[0]
if len(slices)==0:
raise warnings.warn(
"Image %d" % i +
" does not have any slice "+
"satsifying the ratio check.")
continue
print('Sampling %d slices from image %d'
% (len(slices), i))
sel_inds,sel_labels,sel_types=sample_masked_volume(
img, mask, slices, N, view)
inds_dict[self.img_addrs[i]] = sel_inds
labels_dict[self.img_addrs[i]]=sel_labels
types_dict[self.img_addrs[i]]=sel_types
return inds_dict, labels_dict, types_dict
def generate_grid_samples(img_addr, mask_addr,
grid_spacing, offset):
"""Taking samples from a grid from an image
and its mask
NTOE: this is working only for 'axial' view
at this time.
"""
img,_ = nrrd.read(img_addr)
mask,_ = nrrd.read(mask_addr)
s = img.shape
# x,y coordinates of the grid
Y, X = np.meshgrid(np.arange(s[1]),
np.arange(s[0]))
X = np.ravel(X)
Y = np.ravel(Y)
# grid for even slices
ind_locs_even = np.logical_and(
X%grid_spacing==0,
Y%grid_spacing==0)
# grid for odd slices
ind_locs_odd = np.logical_and(
X%grid_spacing==offset,
Y%grid_spacing==offset)
even_sel_X = X[ind_locs_even]
even_sel_Y = Y[ind_locs_even]
odd_sel_X = X[ind_locs_odd]
odd_sel_Y = Y[ind_locs_odd]
# getting 3D indices of the
# grid points
inds_3D = []
labels = []
types = []
for i in range(s[2]):
# get the appropriate grid
if i%2==0:
sel_X = even_sel_X
sel_Y = even_sel_Y
else:
sel_X = odd_sel_X
sel_Y = odd_sel_Y
sel_Z = np.ones(
len(sel_X), dtype=int)*i
grid_inds_3D = np.ravel_multi_index(
(np.array(sel_X),
np.array(sel_Y),
sel_Z), img.shape)
inds_3D += list(grid_inds_3D)
# saving the mask
grid_labels = mask[
np.array(sel_X),
np.array(sel_Y),
sel_Z]
labels += list(grid_labels)
# determining the type
# 0 : masked
# 1 : s-masked
# 2 : ns-masked
grid_types = np.ones(
len(grid_labels),
dtype=int)
grid_types[grid_labels==1]=0
_,Hvar,Lvar = partition_2d_indices(
img[:,:,i],
mask[:,:,i])
# those that are in Lvar
# should be changed to 2
slice_inds_2D = np.ravel_multi_index(
(sel_X,sel_Y), img.shape[:2])
mask_2D = mask[:,:,i]
mask_2D = mask_2D[sel_X,sel_Y]
nmask_inds_2D = slice_inds_2D[
mask_2D==0]
for ind in set(nmask_inds_2D)-set(Hvar):
grid_types[slice_inds_2D==ind]=2
types += list(grid_types)
return inds_3D, labels, types
def get_batches(inds_dict,
batch_size):
"""Divide a given set of image indices and
their labels into batches of specified
size
"""
# get the total size of the generated
# samples
imgs = list(inds_dict.keys())
n = np.sum([len(inds_dict[img])
for img in imgs])
batches = NN.gen_batch_inds(
n, batch_size)
return batches
def get_batch_vars(inds_dict,
labels_dict,
batch_inds,
patch_shape):
"""Creating tensors of data and labels
for a model with batch-learning (such
as CNN object) given a batch of
indices
CAUTIOUS: the input `patch_shape` should
have odd elements so that the radious
along each direction will be an integer
and no shape mismatch will happen.
"""
# initializing variables
b = len(batch_inds)
batch_tensors = np.zeros(
(b,)+patch_shape)
batch_labels = np.zeros((2,b))
# locating batch indices inside
# the data dictionaries
sub_dict = locate_in_dict(inds_dict,
batch_inds)
# calculating radii of the patch
rads = np.zeros(3,dtype=int)
for i in range(3):
rads[i] = int((patch_shape[i]-1)/2.)
# extracting patches from the image
cnt = 0
for img_path in list(sub_dict.keys()):
img,_ = nrrd.read(img_path)
# padding with the patch radius
# so that all patch indices
# fall in the limits of the image
# (skip the z-direction, for now)
padded_img = np.pad(
img,
((rads[0],rads[0]),
(rads[1],rads[1]),
(rads[2],rads[2])),
'constant')
# indices in the batch that belong
# to the `img_path` (sub-batch inds)
subbatch_inds = sub_dict[img_path]
b_i = len(subbatch_inds)
# adding one-hot labels
sub_labels = np.array(labels_dict[
img_path])[subbatch_inds]
subhot = np.zeros((2,b_i))
subhot[0,sub_labels==0]=1
subhot[1,sub_labels==1]=1
batch_labels[
:,cnt:cnt+b_i] = subhot
# converting to multiple-3D indicse
imgbatch_inds = np.array(inds_dict[
img_path])[subbatch_inds]
multi_inds3D = np.unravel_index(
imgbatch_inds, img.shape)
# extracting tensors
for i in range(b_i):
# multiple-indices of the
# centers change in the padded
# image;
# an adjustment is needed
center_vox = [
multi_inds3D[0][i]+rads[0],
multi_inds3D[1][i]+rads[1],
multi_inds3D[2][i]+rads[2]]
patch = padded_img[
center_vox[0]-rads[0]:
center_vox[0]+rads[0]+1,
center_vox[1]-rads[1]:
center_vox[1]+rads[1]+1,
center_vox[2]-rads[2]:
center_vox[2]+rads[2]+1]
batch_tensors[cnt,:,:,:] = patch
cnt += 1
return batch_tensors, batch_labels
def get_patches_MultiModal(tr_data,
batch_inds,
patch_shape):
"""Loading patches of some indices of
a given data with structure explained in
the function `PW_NN.PW_train_epoch_MultiModal`
The function loads patches of each modality
separately, and then merge them all together
"""
# number of modalities
m = len(tr_data[0])-2
s = len(tr_data)
b = len(batch_inds)
MM_batches = np.zeros(
(b,) + patch_shape[:2] + (m,))
# load the data from the correponding
# modalities.. using the old
# functions for single-modality
# images that were using dictionaries
for j in range(m):
trinds_dict = {}
trlabels_dict = {}
for k in range(s):
trinds_dict.update(
{tr_data[k][j]:
tr_data[k][-2]})
trlabels_dict.update(
{tr_data[k][j]:
tr_data[k][-1]})
# get the batches
# we indeed, get the label multiple times
# because they don't change when we
# read batches from different modality
(batch_tensors,
batch_labels) = get_batch_vars(
trinds_dict,
trlabels_dict,
batch_inds,
patch_shape)
# normalizing the batches
MM_batches[:,:,:,j] = np.squeeze(
batch_tensors, axis=3)
return MM_batches, batch_labels
def ravel_binary_mask(mask):
"""Reading and raveling masked indices
of a given 3D mask
The maksed indices are assumed to have
intensity values more than zero (say, 1.)
"""
multi_inds = np.where(mask>0.)
inds = np.ravel_multi_index(multi_inds,
mask.shape)
return inds
def extract_Hakims_data_path():
"""Preparing addresses pointing to the
raw images and masks of brain data
that Hakim has labeled
"""
ids = ['00%d'% i for i in
np.arange(1,10)] + [
'0%d'% i for i in
np.arange(10,67)]
root_dir = '/fileserver/commondataraw/Hakim/For_Christine/Mrakotsky_IBD_Brain/Processed'
mask_rest_of_path = 'scan01/common-processed/anatomical/03-ICC/'
T1_rest_of_path = 'scan01/common-processed/anatomical/01-t1w-ref/'
T2_rest_of_path = 'scan01/common-processed/anatomical/02-coregistration/'
mask_addrs =[]
T1_addrs = []
T2_addrs = []
Orig_addrs = []
for idx in ids:
name = os.listdir(
os.path.join(
root_dir,'Case%s'% idx))[0]
mask_addrs += [
os.path.join(
root_dir,'Case%s'% idx,
name,mask_rest_of_path,
'c%s_s01_ICC.nrrd'% idx)]
T1_addrs += [
os.path.join(
root_dir,'Case%s'% idx,
name,T1_rest_of_path,
'c%s_s01_t1w_ref.nrrd'% idx)]
T2_addrs += [
os.path.join(
root_dir,'Case%s'% idx,
name,T2_rest_of_path,
'c%s_s01_t2w_r.nrrd'% idx)]
Orig_addrs += [
os.path.join(
root_dir,'Case%s'% idx,
name,mask_rest_of_path,
'c%s_s01_BrainMask.nrrd'% idx)]
return T1_addrs, T2_addrs, \
mask_addrs, Orig_addrs
def extract_newborn_data_path():
"""Preparing addresses pointing to
the raw images and masks of
T1-weighted MRI of brains on newborn
subjects. The masks are manually
modified by Hakim.
"""
# common root directory
root_dir = '/fileserver/collections/dHCP/'+\
'dHCP_DCI_spatiotemporal_atlas/'+\
'Processed/'
# common sub-directories
# (except the data files which include
# the subject and session codes in their
# names)
T1_rest_of_path = 'common-processed' +\
'/anatomical/01-t1w-ref'
T2_rest_of_path = 'common-processed' +\
'/anatomical/02-coregistration'
mask_rest_of_path = 'common-processed' +\
'/anatomical/03-ICC'
# subject-specific sub-directories
dirs = get_subdirs(root_dir)
T1_addrs = []
T2_addrs = []
mask_addrs = []
sub_codes = []
for i, d in enumerate(dirs):
if not('sub-CC00' in d):
continue
# there are two levels of subject-
# specific sub-directories
subdir = get_subdirs(os.path.join(
root_dir, d))[0]
subsubdir = get_subdirs(os.path.join(
root_dir, d, subdir))[0]
# we need the codes for accessing
# to names of the data
sub_code = d[4:]
sub_codes += [sub_code]
sess_code = subsubdir[4:]
# subject-specific sub-directories
subdirs = os.path.join(
root_dir,
d,
subdir,
subsubdir)
"""Putting everything together"""
T1_addrs += [
os.path.join(
root_dir,
subdirs,
T1_rest_of_path,
'c%s_s%s_t1w_ref.nrrd'%
(sub_code, sess_code))]
T2_addrs += [
os.path.join(
root_dir,
subdirs,
T2_rest_of_path,
'c%s_s%s_t2w_r.nrrd'%
(sub_code, sess_code))]
mask_addrs += [
os.path.join(
root_dir,
subdirs,
mask_rest_of_path,
'c%s_s%s_ICC.nrrd'%
(sub_code, sess_code))]
return T1_addrs, T2_addrs, mask_addrs, sub_codes
def extract_lesion_data_path(group='ACE',scans=[]):
# common directory
if group=='ACE':
root_dir = '/fileserver/segmentation/Xavi/ICC-Datasets/ACE/'
elif group=='TSCR':
root_dir = '/fileserver/segmentation/Xavi/ICC-Datasets/TSCR01/'
# common sub-directories
# there are four scans, specify which
# one to use
scan_idx = 1
if len(scans)==0:
T1_rest_of_path = 'scan0%d/t1w.nrrd'% scan_idx
T2_rest_of_path = 'scan0%d/t2w.nrrd'% scan_idx
mask_rest_of_path='scan0%d/Manual-ICC.nrrd'% scan_idx
# subject-specific sub-directories
dirs = get_subdirs(root_dir)
dirs = list(np.sort(np.array(dirs)))
T1_addrs = []
T2_addrs = []
mask_addrs = []
sub_codes = []
for i,dir in enumerate(dirs):
if len(scans)==0:
T1_rest_of_path = 'scan0%d/t1w.nrrd'% scan_idx
T2_rest_of_path = 'scan0%d/t2w.nrrd'% scan_idx
mask_rest_of_path='scan0%d/Manual-ICC.nrrd'% scan_idx
else:
T1_rest_of_path = 'scan0%d/t1w.nrrd'% (scans[i])
T2_rest_of_path = 'scan0%d/t2w.nrrd'% (scans[i])
mask_rest_of_path='scan0%d/Manual-ICC.nrrd'% (scans[i])
sub_codes += [dir]
T1_path = os.path.join(
root_dir,dir,T1_rest_of_path)
T1_addrs += [T1_path]
T2_path = os.path.join(
root_dir,dir,T2_rest_of_path)
T2_addrs += [T2_path]
mask_path = os.path.join(
root_dir,dir,mask_rest_of_path)
mask_addrs += [mask_path]
return T1_addrs, T2_addrs, mask_addrs, sub_codes
def extract_NVM_data_path():
root_dir = '/fileserver/external/rawabd/Jamshid/' + \
'PWNNAL_results/unimodal_NVM/preprop_data/'
# get data codes
files = os.listdir(root_dir)
sub_codes = np.array([f[:4] for f in files])
sub_codes = np.sort(np.unique(sub_codes))
# construct full paths
T1_rest_of_path = '-t1w.nrrd'
parc_rest_of_path = '-parcellation.nrrd'
mask_rest_of_path = '-mask-wnan.nrrd'
T1_addrs = []
parc_addrs = []
mask_addrs = []
for code in sub_codes:
T1_addrs += [os.path.join(
root_dir,code+T1_rest_of_path)]
parc_addrs += [os.path.join(
root_dir,code+parc_rest_of_path)]
mask_addrs += [os.path.join(
root_dir,code+mask_rest_of_path)]
return T1_addrs, parc_addrs, mask_addrs, list(sub_codes)
def extract_Crohns_data_path(csv_path=[]):
root_dir = '/fileserver/external/rawabd/Jamshid/' + \
'PWNNAL_results/unimodal_Crohns/dat/'
# get data codes
sub_codes = np.sort(os.listdir(root_dir))
#if len(csv_path)>0:
# with open(csv_path) as f:
# A = csv.reader(f)
# next(A)
# sub_paths = [r[1] for r in A]
T1_addrs = []
mask_addrs = []
for sub in sub_codes:
T1_addrs += [os.path.join(root_dir,sub,'img.nrrd')]
mask_addrs += [os.path.join(root_dir,sub,'wall_label.nrrd')]
return T1_addrs, mask_addrs, list(sub_codes)
def preprop_NVM_data(inds, labels, parc_path):
"""Pre-processing NVM data by removing voxels
that have zero labels in the parcellation map
"""
parc,_ = nrrd.read(parc_path)
# pacellation labels for the indices
multinds = np.unravel_index(inds, parc.shape)
parc_labels = parc[multinds]
# choose only those with nnz parcellation
inds = np.array(inds)[parc_labels>0]
labels = np.array(labels)[parc_labels>0]
return list(inds), list(labels)
def get_subdirs(path):
"""returning all sub-directories of a
given path
"""
subdirs = [d for d in os.listdir(path)
if os.path.isdir(os.path.join(
path,d))]
return subdirs
def sample_masked_volume(img,
mask,
slices,
N,
view):
"""Sampling from a masked 3D image in way
that a balanced number of samples are
drawn from the masked class, the structured
background, and non-structured background
:Parameters:
**img** : 3D array
the raw image
**mask** : 3D binary array
mask of the image (with labels
0 and 1
**slices** : array-like of integers
index of slices from which data
samples will be drawn
**N** : list of three integers
* N[0]: # masked samples
* N[1]: # structured non
masked samples
* N[2]: # non-structured
non-masked samples
"""
sel_inds = []
sel_labels = []
sel_types = []
for s in slices:
if view=='axial':
img_slice = img[:,:,s]
mask_slice = mask[:,:,s]
slice_view=2
elif view=='coronal':
img_slice = img[:,s,:]
mask_slice = mask[:,s,:]
slice_view=1
elif view=='sagittal':
img_slice = img[s,:,:]
mask_slice = mask[s,:,:]
slice_view=0
# partitioning the 2D indices into
# three groups:
# (masked, structured non-masked,
# non-structured non-masked)
(masked,Hvar,Lvar)=partition_2d_indices(
img_slice, mask_slice)
gmasked = expand_raveled_inds(
masked, s, slice_view, img.shape)
gHvar = expand_raveled_inds(
Hvar, s, slice_view, img.shape)
gLvar = expand_raveled_inds(
Lvar, s, slice_view, img.shape)
# randomly draw samples from each
# partition
# -------- masked
if N[0] > len(gmasked):
sel_inds += list(gmasked)
sel_labels += list(
np.ones(len(gmasked),
dtype=int))
sel_types += [0]*len(gmasked)
else:
r_inds = np.random.permutation(
len(gmasked))[:N[0]]
sel_inds += list(gmasked[r_inds])
sel_labels += list(
np.ones(N[0],dtype=int))
sel_types += [0]*N[0]
# ------ non-masked structured
if N[1] > len(gHvar):
sel_inds += list(gHvar)
sel_labels += list(np.zeros(
len(gHvar),dtype=int))
sel_types += [1]*len(gHvar)
else:
r_inds = np.random.permutation(
len(gHvar))[:N[1]]
sel_inds += list(gHvar[r_inds])
sel_labels += list(
np.zeros(N[1],dtype=int))
sel_types += [1]*N[1]
# ------ non-masked non-structured
if N[2] > len(gLvar):
sel_inds += list(gLvar)
sel_labels += list(np.zeros(
len(gLvar),dtype=int))
sel_types += [2]*len(gLvar)
else:
r_inds = np.random.permutation(
len(gLvar))[:N[2]]
sel_inds += list(gLvar[r_inds])
sel_labels += list(
np.zeros(N[2],dtype=int))
sel_types += [2]*N[2]
return sel_inds, sel_labels, sel_types
def partition_2d_indices(img,mask):
"""Partitioning an image into three
different groups, based on the masked
indices and variance of the patches around
the pixels of the given 2D image
:Returns:
**masked_inds** : array of ints
array of indices which have
non-zero values in the mask
**Hvar_inds** : array of ints
array of indices with zero
values in the mask, and with
"high" variance patch around
them
**Lvar_inds** : array of ints
array of indices with zero
values in the mask, and with
"low" variance patch around
them
"""
masked_inds = np.where(mask>0)
masked_inds = set(np.ravel_multi_index(
masked_inds, mask.shape))
# computing the patch variance
d = 5
var_map = get_vars_2d(img, d)
var_map[var_map==0] += 1e-1
var_map = np.log(var_map)
var_thr = 2.
# create the non-masked high-variance
# (structured) and low-variance (non-
# structured) indices
# -------------------------------
# taking the High variance indices
# and removing the masked ones
Hvar_inds = np.where(
var_map > var_thr)
Hvar_inds = set(np.ravel_multi_index(
Hvar_inds, mask.shape))
Hvar_inds = Hvar_inds - masked_inds
# do the same thing for Low variance
Lvar_inds = np.where(
var_map < var_thr)
Lvar_inds = set(np.ravel_multi_index(
Lvar_inds, mask.shape))
Lvar_inds = Lvar_inds - masked_inds
# return the partitioning
return (np.array(list(masked_inds)),
np.array(list(Hvar_inds)),
np.array(list(Lvar_inds)))
def get_vars_2d(img,d):
"""Get the variance of a given 2D image
by means of a convolution
The trick is using the fact that
Var[x] = E[x^2] - E[x]^2.
Thus we can find the variance of patches
centered at different pixels, by computing
E[x] and E[x^2] using 2D convolution of
the image and a dxd all-one matrix (kernel)
where d here denotes the size of the patch
around each pixel.
"""
# making the image uint64 so that no
# precision problem happens when squaring
# up the intensities
img = np.uint64(img)
# forming the kernel
kernel = np.ones((d,d))
# E[x] -- mean of the patches
Ex = convolve2d(
img,kernel,'same') / float(d**2)
# E[x^2] -- mean of patch-squared
ExP2 = convolve2d(
img**2 ,kernel,'same') / float(d**2)
# the variance
varx = ExP2 - Ex**2
return varx
def global2local_inds(batch_inds,
set_sizes):
"""Having a finite set of sets with
ordered elements and aiming to extract
a subset of them, this function takes
global indices of the elements in this
subset and output which elements in
each set belongs to this subset; the
given subset can be one of the batches
after batch-ifying voxels of a set of
images
By "global index", we mean an indexing
system that we can refer to a specific
element of one of the sets uniquely.
Here, assuming that the sets and their
elements are ordered, our global indexing
system refers to the i-th element of the
j-th set by an index calculated by
`len(S1) + len(S2) + .. len(Si-1) + j-1`
where `S1` to `Si-1` are the sets that
are located before the target i-th set
"""
cumvols = np.append(
-1, np.cumsum(set_sizes)-1)
# finding the set indices
set_inds = cumvols.searchsorted(
batch_inds) - 1
# local index for each set
local_inds = [np.array(batch_inds)[
set_inds==i]-cumvols[i]-1 for i in
range(len(set_sizes))]
return local_inds
def locate_in_dict(inds_dict,
inds):
"""Locating a set of indices inside
a given data dictionary
Note that data dictionaries
(either index- or labels-
dictionaries) have several indices
assigned to each image. Hence
locating a global index (which refers
to a specific index inside the
dictionary is not straightforward.
Here, we assume that the indices or
labels are located in the dictionaries
in the same ordered as they are
saved. For example, only writing the
indices of the corresponding data in
the dictionary, we would get
inds_dict = {'img-1': [0, 1,...,N1-1],
'img-2': [N1, N1+1,
..., N1+N2-1],
.
.
'img-10': [N1+...+N9,
N1+...+N9+1,
...,
N1+N2+...+N10-1]}
In this function, for each given index,
we locate it in the dictionary, and
return a sub-dictionary containing
indices WITH RESPECT TO the contents of
the same keys in the input dictionary.
"""
# `imgs` are the keys
imgs = list(inds_dict.keys())
sub_dict = {img:[] for img in imgs}
key_vols = [len(inds_dict[img])
for img in imgs]
key_cumvols = np.append(
-1, np.cumsum(key_vols)-1)
for ind in inds:
# finding the corresponding key
ind_key = key_cumvols.searchsorted(
ind) - 1
# updating the sub-dictionary
sub_dict[imgs[ind_key]] += [
ind-key_cumvols[ind_key]-1]
# removing those keys who did not
# have any corresponding indices
subkey_vols = [len(sub_dict[img])
for img in imgs]
keys_to_remove = np.where(
np.array(subkey_vols)==0)[0]
for key in keys_to_remove:
del sub_dict[imgs[key]]
return sub_dict
def expand_raveled_inds(inds_2D,
slice_idx,
slice_view,
shape_3D):
"""Covnerting a set of raveled single
indices that are built in terms of
one of 2D slices of a 3D volume, into
another set of single raveled indices
that are in terms of the whole 3D shape
:Parameters:
**inds_2D** : array of integers
array of indices based on the
2D slice; if it is a single
integer error will be raised
**slice_idx** : positive integer
index of the slice based on
which the 2D indices are given
**slice_view** : int from {0,1,2}
this integer shows in what
view the slice has bee taken;
e.g., if it is 0, the slice
has been taken from the first
component of the 3D volume
(hence `slice_idx<shape[0]`,
if it is 1, it means that
the slice is taken from the
second component, so on.
**shape** : tuple of integers
shape of the whole volume
"""
# create the shape_2D from shape_3D
shape_2D = tuple(np.delete(
shape_3D, slice_view))
# get the multi-index inside the slice
multi_inds = np.unravel_index(
inds_2D, shape_2D)
# add the ID of the slice in the proper
# location of the expanded multi-indices
slice_idx_list = slice_idx*np.ones(
len(inds_2D))
slice_idx_list = np.int64(slice_idx_list)
if slice_view==0:
multi_inds = (
slice_idx_list,) + multi_inds
elif slice_view==1:
multi_inds = (
multi_inds[0],) + (
slice_idx_list,) + (
multi_inds[1],)
elif slice_view==2:
multi_inds += (slice_idx_list,)
# ravel the expanded multi-indices
# using the given 3D volume shape