Skip to content

Commit d38be93

Browse files
committed
improvements on snr computation for Denoise
1 parent 05209eb commit d38be93

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

nipype/interfaces/dipy/preprocess.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class DenoiseInputSpec(TraitedSpec):
104104
'will be computed'), exists=True)
105105
patch_radius = traits.Int(1, desc='patch radius')
106106
block_radius = traits.Int(5, desc='block_radius')
107+
snr = traits.Float(desc='manually set an SNR')
107108

108109

109110
class DenoiseOutputSpec(TraitedSpec):
@@ -239,28 +240,37 @@ def nlmeans_proxy(in_file, settings,
239240

240241
if data.ndim < 4:
241242
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+
242254
b0 = data[..., 0]
243255

244256
if smask is None:
245257
smask = np.zeros_like(b0)
246258
smask[b0 > np.percentile(b0, 85.)] = 1
247259

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)
249262

250263
if nmask is None:
251264
nmask = np.ones_like(b0, dtype=np.uint8)
252265
bmask = settings['mask']
253266
if bmask is None:
254267
bmask = np.zeros_like(b0)
255-
bmask[b0 > np.percentile(b0, 55)] = 1
268+
bmask[b0 > np.percentile(b0[b0 > 0], 10)] = 1
256269
label_im, nb_labels = ndimage.label(bmask)
257270
sizes = ndimage.sum(bmask, label_im, range(nb_labels + 1))
258271
maxidx = np.argmax(sizes)
259272
bmask = np.zeros_like(b0, dtype=np.uint8)
260273
bmask[label_im == maxidx] = 1
261-
262-
nb.Nifti1Image(bmask, aff,
263-
None).to_filename('bmask.nii.gz')
264274
nmask[bmask > 0] = 0
265275
else:
266276
nmask = np.squeeze(nmask)
@@ -270,20 +280,26 @@ def nlmeans_proxy(in_file, settings,
270280

271281
nmask = binary_erosion(nmask, iterations=1).astype(np.uint8)
272282

273-
nb.Nifti1Image(smask.astype(np.uint8), aff,
274-
None).to_filename('smask.nii.gz')
275-
276-
277283
den = np.zeros_like(data)
278284
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+
279291
for i in range(data.shape[-1]):
280292
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)
285299

286300
den = np.squeeze(den)
301+
den /= df
302+
287303
nb.Nifti1Image(den.astype(hdr.get_data_dtype()), aff,
288304
hdr).to_filename(out_file)
289305
return out_file, snr

0 commit comments

Comments
 (0)