@@ -104,6 +104,7 @@ class DenoiseInputSpec(TraitedSpec):
104
104
'will be computed' ), exists = True )
105
105
patch_radius = traits .Int (1 , desc = 'patch radius' )
106
106
block_radius = traits .Int (5 , desc = 'block_radius' )
107
+ snr = traits .Float (desc = 'manually set an SNR' )
107
108
108
109
109
110
class DenoiseOutputSpec (TraitedSpec ):
@@ -239,28 +240,37 @@ def nlmeans_proxy(in_file, settings,
239
240
240
241
if data .ndim < 4 :
241
242
data = data [..., np .newaxis ]
243
+
244
+ data = np .nan_to_num (data )
245
+
246
+ if data .max () < 1.0e-4 :
247
+ raise RuntimeError ('There is no signal in the image' )
248
+
249
+ df = 1.0
250
+ if data .max () < 1000.0 :
251
+ df = 1000. / data .max ()
252
+ data *= df
253
+
242
254
b0 = data [..., 0 ]
243
255
244
256
if smask is None :
245
257
smask = np .zeros_like (b0 )
246
258
smask [b0 > np .percentile (b0 , 85. )] = 1
247
259
248
- smask = binary_erosion (smask .astype (np .uint8 ), iterations = 2 ).astype (np .uint8 )
260
+ smask = binary_erosion (
261
+ smask .astype (np .uint8 ), iterations = 2 ).astype (np .uint8 )
249
262
250
263
if nmask is None :
251
264
nmask = np .ones_like (b0 , dtype = np .uint8 )
252
265
bmask = settings ['mask' ]
253
266
if bmask is None :
254
267
bmask = np .zeros_like (b0 )
255
- bmask [b0 > np .percentile (b0 , 55 )] = 1
268
+ bmask [b0 > np .percentile (b0 [ b0 > 0 ], 10 )] = 1
256
269
label_im , nb_labels = ndimage .label (bmask )
257
270
sizes = ndimage .sum (bmask , label_im , range (nb_labels + 1 ))
258
271
maxidx = np .argmax (sizes )
259
272
bmask = np .zeros_like (b0 , dtype = np .uint8 )
260
273
bmask [label_im == maxidx ] = 1
261
-
262
- nb .Nifti1Image (bmask , aff ,
263
- None ).to_filename ('bmask.nii.gz' )
264
274
nmask [bmask > 0 ] = 0
265
275
else :
266
276
nmask = np .squeeze (nmask )
@@ -270,20 +280,26 @@ def nlmeans_proxy(in_file, settings,
270
280
271
281
nmask = binary_erosion (nmask , iterations = 1 ).astype (np .uint8 )
272
282
273
- nb .Nifti1Image (smask .astype (np .uint8 ), aff ,
274
- None ).to_filename ('smask.nii.gz' )
275
-
276
-
277
283
den = np .zeros_like (data )
278
284
snr = []
285
+
286
+ est_snr = True
287
+ if isdefined (self .inputs .snr ):
288
+ snr = [self .inputs .snr ] * data .shape [- 1 ]
289
+ est_snr = False
290
+
279
291
for i in range (data .shape [- 1 ]):
280
292
d = data [..., i ]
281
- s = np .mean (d [smask > 0 ])
282
- n = np .std (d [nmask > 0 ])
283
- snr .append (s / n )
284
- den [..., i ] = nlmeans (d , s / n , ** settings )
293
+ if est_snr :
294
+ s = np .mean (d [smask > 0 ])
295
+ n = np .std (d [nmask > 0 ])
296
+ snr .append (s / n )
297
+
298
+ den [..., i ] = nlmeans (d , snr [i ], ** settings )
285
299
286
300
den = np .squeeze (den )
301
+ den /= df
302
+
287
303
nb .Nifti1Image (den .astype (hdr .get_data_dtype ()), aff ,
288
304
hdr ).to_filename (out_file )
289
305
return out_file , snr
0 commit comments