-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils_cmd.py
147 lines (118 loc) · 6.81 KB
/
utils_cmd.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
from doit_train import do_training, get_motion_transform, get_train_and_val_csv, get_cache_dir
from torchio.transforms import CropOrPad, RandomAffine, RescaleIntensity, ApplyMask, RandomBiasField, Interpolation, RandomAffineFFT
from optparse import OptionParser
from utils_file import get_parent_path
def get_comma_separated_args(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
def get_cmd_select_data_option():
usage= "usage: %prog [options] run a model on a file "
# Parse input arguments
parser=OptionParser(usage=usage)
parser.add_option("-i", "--image_in", action="callback", dest="image_in", default='', callback=get_comma_separated_args,
type='string', help="full path to the image to test, list separate path by , ")
parser.add_option("--sample_dir", action="store", dest="sample_dir", default='',
type='string', help="instead of -i specify dir of saved sample ")
parser.add_option("--batch_size", action="store", dest="batch_size", default=2, type="int",
help=" default 2")
parser.add_option("--num_workers", action="store", dest="num_workers", default=0, type="int",
help=" default 0")
parser.add_option("--add_cut_mask", action="store_true", dest="add_cut_mask", default=False,
help="if specifie it will adda cut mask (brain) transformation default False ")
parser.add_option("--add_affine_zoom", action="store", dest="add_affine_zoom", default=0, type="float",
help=">0 means we add an extra affine transform with zoom value and if define rotations default 0")
parser.add_option("--add_affine_rot", action="store", dest="add_affine_rot", default=0, type="float",
help=">0 means we add an extra affine transform with rotation values and if define rotations default 0")
parser.add_option("--add_rescal_Imax", action="store_true", dest="add_rescal_Imax", default=False,
help="if specifie it will add a rescale intensity transformation default False ")
parser.add_option("--add_mask_brain", action="store_true", dest="add_mask_brain", default=False,
help="if specifie it will add a apply_mask (name brain) transformation default False ")
parser.add_option("--add_elastic1", action="store_true", dest="add_elastic1", default=False,
help="if specifie it will add a elastic1 transformation default False ")
parser.add_option("--add_bias", action="store_true", dest="add_bias", default=False,
help="if specifie it will add a bias transformation default False ")
parser.add_option("--add_orig", action="store_true", dest="add_orig", default=False,
help="if specifie it will add original image to sample ")
parser.add_option("--target", action="store", dest="target", default='ssim',
help=" specify the objectiv target either 'ssim' or 'random_noise' (default ssim)")
return parser
def get_tranformation_list(choice=1):
if isinstance(choice,int):
choice = [choice]
transfo_list, transfo_name = [], []
if 1 in choice:
transfo_list += [
RandomAffineFFT(scales=(1, 1), degrees=(10, 10)),
RandomAffineFFT(scales=(1.2, 1.2), degrees=(0, 0)),
RandomAffineFFT(scales=(0.8, 0.8), degrees=(0, 0)),
]
transfo_name += ['tAfft_R10', 'tAfft_S1.2', 'tAfft_S08']
if 2 in choice:
transfo_list += [
RandomAffine(scales=(1, 1), degrees=(10, 10), image_interpolation='nearest'),
RandomAffine(scales=(1.2, 1.2), degrees=(0, 0), image_interpolation='nearest'),
RandomAffine(scales=(0.8, 0.8), degrees=(0, 0), image_interpolation='nearest'),
]
transfo_name += ['tAffN_R10', 'tAffN_S1.2', 'tAffN_S08']
return transfo_list, transfo_name
def get_dataset_from_option(options):
fin = options.image_in
dir_sample = options.sample_dir
add_affine_zoom, add_affine_rot = options.add_affine_zoom, options.add_affine_rot
batch_size, num_workers = options.batch_size, options.num_workers
doit = do_training('/tmp/', 'not_use', verbose=True)
# adding transformation
tc = []
name_suffix = ''
#Attention pas de _ dans le name_suffix
if options.add_cut_mask > 0:
target_shape, mask_key = (182, 218, 182), 'brain'
tc = [CropOrPad(target_shape=target_shape, mask_name=mask_key), ]
name_suffix += '_tCropBrain'
if add_affine_rot>0 or add_affine_zoom >0:
if add_affine_zoom==0: add_affine_zoom=1 #0 -> no affine so 1
tc.append( RandomAffine(scales=(add_affine_zoom, add_affine_zoom), degrees=(add_affine_rot, add_affine_rot),
image_interpolation = Interpolation.NEAREST ) )
name_suffix += '_tAffineS{}R{}'.format(add_affine_zoom, add_affine_rot)
# for hcp should be before RescaleIntensity
mask_brain = False
if options.add_mask_brain:
tc.append(ApplyMask(masking_method='brain'))
name_suffix += '_tMaskBrain'
mask_brain = True
if options.add_rescal_Imax:
tc.append(RescaleIntensity(percentiles=(0, 99)))
name_suffix += '_tRescale-0-99'
if options.add_elastic1:
tc.append(get_motion_transform(type='elastic1'))
name_suffix += '_tElastic1'
if options.add_bias:
tc.append(RandomBiasField())
name_suffix += '_tBias'
if len(name_suffix)==0:
name_suffix = '_Raw'
target = None
if len(tc)==0: tc = None
add_to_load, add_to_load_regexp = None, None
if len(dir_sample) > 0:
print('loading from {}'.format(dir_sample))
if options.add_orig:
add_to_load, add_to_load_regexp = 'original', 'notused'
data_name = get_parent_path(dir_sample)[1]
if mask_brain and 'hcp' in data_name:
add_to_load_regexp = 'brain_T'
if add_to_load is None:
add_to_load = 'brain'
else:
add_to_load += 'brain'
doit.set_data_loader(batch_size=batch_size, num_workers=num_workers, load_from_dir=dir_sample, transforms=tc,
add_to_load=add_to_load, add_to_load_regexp=add_to_load_regexp)
name_suffix = 'On_' + data_name + name_suffix
target = options.target #'ssim' #suppose that if from sample, it should be simulation so set target
else :
print('working on ')
for ff in fin:
print(ff)
doit.set_data_loader_from_file_list(fin, transforms=tc,
batch_size=batch_size, num_workers=num_workers,
mask_key=mask_key, mask_regex='^mask')
return doit, name_suffix, target