Skip to content

Commit b705c0e

Browse files
josephmjeoesteban
authored andcommitted
address Oscar's comments
1 parent 09e196f commit b705c0e

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

dmriprep/interfaces/images.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class ExtractB0(SimpleInterface):
2424
2525
Example
2626
-------
27-
2827
>>> os.chdir(tmpdir)
2928
>>> extract_b0 = ExtractB0()
3029
>>> extract_b0.inputs.in_file = str(data_dir / 'dwi.nii.gz')
@@ -34,7 +33,7 @@ class ExtractB0(SimpleInterface):
3433
"""
3534

3635
input_spec = _ExtractB0InputSpec
37-
output_spec = ExtractB0OutputSpec
36+
output_spec = _ExtractB0OutputSpec
3837

3938
def _run_interface(self, runtime):
4039
self._results['out_file'] = extract_b0(
@@ -71,7 +70,7 @@ class _RescaleB0InputSpec(BaseInterfaceInputSpec):
7170
mask_file = File(exists=True, mandatory=True, desc='mask file')
7271

7372

74-
class RescaleB0OutputSpec(TraitedSpec):
73+
class _RescaleB0OutputSpec(TraitedSpec):
7574
out_file = File(exists=True, desc='b0 file')
7675

7776

@@ -82,7 +81,6 @@ class RescaleB0(SimpleInterface):
8281
8382
Example
8483
-------
85-
8684
>>> os.chdir(tmpdir)
8785
>>> rescale_b0 = RescaleB0()
8886
>>> rescale_b0.inputs.in_file = str(data_dir / 'dwi.nii.gz')
@@ -91,8 +89,8 @@ class RescaleB0(SimpleInterface):
9189
9290
"""
9391

94-
input_spec = RescaleB0InputSpec
95-
output_spec = RescaleB0OutputSpec
92+
input_spec = _RescaleB0InputSpec
93+
output_spec = _RescaleB0OutputSpec
9694

9795
def _run_interface(self, runtime):
9896
self._results['out_file'] = rescale_b0(
@@ -104,7 +102,9 @@ def _run_interface(self, runtime):
104102

105103
def rescale_b0(in_file, mask_file, newpath=None):
106104
"""
107-
Rescale the *b0* volumes from a DWI dataset."""
105+
Rescale the input volumes using the median signal intensity
106+
and output a median image.
107+
"""
108108
import numpy as np
109109
import nibabel as nib
110110
from nipype.utils.filemanip import fname_presuffix
@@ -113,20 +113,25 @@ def rescale_b0(in_file, mask_file, newpath=None):
113113
in_file, suffix='_median_b0', newpath=newpath)
114114

115115
img = nib.load(in_file)
116-
data = img.get_fdata(dtype='float32')
116+
if img.dataobj.ndim == 3:
117+
return in_file
118+
if img.shape[-1] == 1:
119+
nb.squeeze_image(img).to_filename(out_file)
120+
return out_file
117121

122+
data = img.get_fdata(dtype='float32')
118123
mask_img = nib.load(mask_file)
119124
mask_data = mask_img.get_fdata(dtype='float32')
120125

121-
mean_b0_signals = data[mask_data > 0, ...].mean(axis=0)
126+
median_signal = data[mask_data > 0, ...].median(axis=0)
122127

123-
rescale_b0 = 1000 * data / mean_b0_signals
128+
rescaled_data = 1000 * data / median_signal
124129

125-
median_b0 = np.median(rescale_b0, axis=-1)
130+
median_data = np.median(rescaled_data, axis=-1)
126131

127132
hdr = img.header.copy()
128-
hdr.set_data_shape(median_b0.shape)
133+
hdr.set_data_shape(median_data.shape)
129134
hdr.set_xyzt_units('mm')
130135
hdr.set_data_dtype(np.float32)
131-
nib.Nifti1Image(median_b0, img.affine, hdr).to_filename(out_file)
136+
nib.Nifti1Image(median_data, img.affine, hdr).to_filename(out_file)
132137
return out_file

0 commit comments

Comments
 (0)