25
25
from skimage .transform import resize
26
26
from utils .utils import get_task_code , make_empty_dir
27
27
28
- from data_preprocessing .configs import ct_max , ct_mean , ct_min , ct_std , patch_size , spacings , task
28
+ from data_preprocessing .configs import (
29
+ ct_max ,
30
+ ct_mean ,
31
+ ct_min ,
32
+ ct_std ,
33
+ patch_size ,
34
+ spacings ,
35
+ task ,
36
+ )
29
37
30
38
31
39
class Preprocessor :
@@ -45,9 +53,15 @@ def __init__(self, args):
45
53
self .ct_min , self .ct_max , self .ct_mean , self .ct_std = (0 ,) * 4
46
54
if not self .training :
47
55
self .results = os .path .join (self .results , self .args .exec_mode )
48
- self .crop_foreg = transforms .CropForegroundd (keys = ["image" , "label" ], source_key = "image" )
49
- nonzero = True if self .modality != "CT" else False # normalize only non-zero region for MRI
50
- self .normalize_intensity = transforms .NormalizeIntensity (nonzero = nonzero , channel_wise = True )
56
+ self .crop_foreg = transforms .CropForegroundd (
57
+ keys = ["image" , "label" ], source_key = "image"
58
+ )
59
+ nonzero = (
60
+ True if self .modality != "CT" else False
61
+ ) # normalize only non-zero region for MRI
62
+ self .normalize_intensity = transforms .NormalizeIntensity (
63
+ nonzero = nonzero , channel_wise = True
64
+ )
51
65
if self .args .exec_mode == "val" :
52
66
dataset_json = json .load (open (metadata_path , "r" ))
53
67
dataset_json ["val" ] = dataset_json ["training" ]
@@ -76,7 +90,9 @@ def run(self):
76
90
_mean = round (self .ct_mean , 2 )
77
91
_std = round (self .ct_std , 2 )
78
92
if self .verbose :
79
- print (f"[CT] min: { self .ct_min } , max: { self .ct_max } , mean: { _mean } , std: { _std } " )
93
+ print (
94
+ f"[CT] min: { self .ct_min } , max: { self .ct_max } , mean: { _mean } , std: { _std } "
95
+ )
80
96
81
97
self .run_parallel (self .preprocess_pair , self .args .exec_mode )
82
98
@@ -114,7 +130,7 @@ def preprocess_pair(self, pair):
114
130
if self .args .ohe :
115
131
mask = np .ones (image .shape [1 :], dtype = np .float32 )
116
132
for i in range (image .shape [0 ]):
117
- zeros = np .where (image [i ] < = 0 )
133
+ zeros = np .where (image [i ] = = 0 )
118
134
mask [zeros ] *= 0.0
119
135
image = self .normalize_intensity (image ).astype (np .float32 )
120
136
mask = np .expand_dims (mask , 0 )
@@ -131,15 +147,28 @@ def standardize(self, image, label):
131
147
pad_shape = self .calculate_pad_shape (image )
132
148
image_shape = image .shape [1 :]
133
149
if pad_shape != image_shape :
134
- paddings = [(pad_sh - image_sh ) / 2 for (pad_sh , image_sh ) in zip (pad_shape , image_shape )]
150
+ paddings = [
151
+ (pad_sh - image_sh ) / 2
152
+ for (pad_sh , image_sh ) in zip (pad_shape , image_shape )
153
+ ]
135
154
image = self .pad (image , paddings )
136
155
label = self .pad (label , paddings )
137
156
if self .args .dim == 2 : # Center cropping 2D images.
138
157
_ , _ , height , weight = image .shape
139
158
start_h = (height - self .patch_size [0 ]) // 2
140
159
start_w = (weight - self .patch_size [1 ]) // 2
141
- image = image [:, :, start_h : start_h + self .patch_size [0 ], start_w : start_w + self .patch_size [1 ]]
142
- label = label [:, :, start_h : start_h + self .patch_size [0 ], start_w : start_w + self .patch_size [1 ]]
160
+ image = image [
161
+ :,
162
+ :,
163
+ start_h : start_h + self .patch_size [0 ],
164
+ start_w : start_w + self .patch_size [1 ],
165
+ ]
166
+ label = label [
167
+ :,
168
+ :,
169
+ start_h : start_h + self .patch_size [0 ],
170
+ start_w : start_w + self .patch_size [1 ],
171
+ ]
143
172
return image , label
144
173
145
174
def normalize (self , image ):
@@ -148,7 +177,9 @@ def normalize(self, image):
148
177
return self .normalize_intensity (image )
149
178
150
179
def save (self , image , label , fname , image_metadata ):
151
- mean , std = np .round (np .mean (image , (1 , 2 , 3 )), 2 ), np .round (np .std (image , (1 , 2 , 3 )), 2 )
180
+ mean , std = np .round (np .mean (image , (1 , 2 , 3 )), 2 ), np .round (
181
+ np .std (image , (1 , 2 , 3 )), 2
182
+ )
152
183
if self .verbose :
153
184
print (f"Saving { fname } shape { image .shape } mean { mean } std { std } " )
154
185
self .save_npy (image , fname , "_x.npy" )
@@ -191,7 +222,9 @@ def calculate_pad_shape(self, image):
191
222
image_shape = image .shape [1 :]
192
223
if len (min_shape ) == 2 : # In 2D case we don't want to pad depth axis.
193
224
min_shape .insert (0 , image_shape [0 ])
194
- pad_shape = [max (mshape , ishape ) for mshape , ishape in zip (min_shape , image_shape )]
225
+ pad_shape = [
226
+ max (mshape , ishape ) for mshape , ishape in zip (min_shape , image_shape )
227
+ ]
195
228
return pad_shape
196
229
197
230
def get_intensities (self , pair ):
@@ -233,10 +266,16 @@ def calculate_new_shape(self, spacing, shape):
233
266
return new_shape
234
267
235
268
def save_npy (self , image , fname , suffix ):
236
- np .save (os .path .join (self .results , fname .replace (".nii.gz" , suffix )), image , allow_pickle = False )
269
+ np .save (
270
+ os .path .join (self .results , fname .replace (".nii.gz" , suffix )),
271
+ image ,
272
+ allow_pickle = False ,
273
+ )
237
274
238
275
def run_parallel (self , func , exec_mode ):
239
- return Parallel (n_jobs = self .args .n_jobs )(delayed (func )(pair ) for pair in self .metadata [exec_mode ])
276
+ return Parallel (n_jobs = self .args .n_jobs )(
277
+ delayed (func )(pair ) for pair in self .metadata [exec_mode ]
278
+ )
240
279
241
280
def load_nifty (self , fname ):
242
281
return nibabel .load (os .path .join (self .data_path , fname ))
@@ -266,7 +305,9 @@ def standardize_layout(data):
266
305
267
306
@staticmethod
268
307
def resize_fn (image , shape , order , mode ):
269
- return resize (image , shape , order = order , mode = mode , cval = 0 , clip = True , anti_aliasing = False )
308
+ return resize (
309
+ image , shape , order = order , mode = mode , cval = 0 , clip = True , anti_aliasing = False
310
+ )
270
311
271
312
def resample_anisotrophic_image (self , image , shape ):
272
313
resized_channels = []
0 commit comments