Skip to content

Commit ece13e1

Browse files
committed
fix: various nodes failing
1 parent 720e9e1 commit ece13e1

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

dmriprep/interfaces/images.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Image tools interfaces."""
2-
2+
import numpy as np
3+
import nibabel as nb
4+
from nipype.utils.filemanip import fname_presuffix
35
from nipype import logging
46
from nipype.interfaces.base import (
57
traits, TraitedSpec, BaseInterfaceInputSpec, SimpleInterface, File
@@ -45,10 +47,6 @@ def _run_interface(self, runtime):
4547

4648
def extract_b0(in_file, b0_ixs, newpath=None):
4749
"""Extract the *b0* volumes from a DWI dataset."""
48-
import numpy as np
49-
import nibabel as nb
50-
from nipype.utils.filemanip import fname_presuffix
51-
5250
out_file = fname_presuffix(
5351
in_file, suffix='_b0', newpath=newpath)
5452

@@ -71,13 +69,13 @@ class _RescaleB0InputSpec(BaseInterfaceInputSpec):
7169

7270

7371
class _RescaleB0OutputSpec(TraitedSpec):
74-
out_file = File(exists=True, desc='b0 file')
72+
out_ref = File(exists=True, desc='One average b0 file')
73+
out_b0s = File(exists=True, desc='series of rescaled b0 volumes')
7574

7675

7776
class RescaleB0(SimpleInterface):
7877
"""
79-
Rescale the b0 volumes to deal with average signal decay over time
80-
and output the median image.
78+
Rescale the b0 volumes to deal with average signal decay over time.
8179
8280
Example
8381
-------
@@ -93,44 +91,54 @@ class RescaleB0(SimpleInterface):
9391
output_spec = _RescaleB0OutputSpec
9492

9593
def _run_interface(self, runtime):
96-
self._results['out_file'] = rescale_b0(
94+
self._results['out_b0s'] = rescale_b0(
9795
self.inputs.in_file,
9896
self.inputs.mask_file,
99-
newpath=runtime.cwd)
97+
newpath=runtime.cwd
98+
)
99+
self._results['out_ref'] = median(
100+
self._results['out_b0s'],
101+
newpath=runtime.cwd
102+
)
100103
return runtime
101104

102105

103106
def rescale_b0(in_file, mask_file, newpath=None):
104-
"""
105-
Rescale the input volumes using the median signal intensity
106-
and output a median image.
107-
"""
108-
import numpy as np
109-
import nibabel as nb
110-
from nipype.utils.filemanip import fname_presuffix
111-
107+
"""Rescale the input volumes using the median signal intensity."""
112108
out_file = fname_presuffix(
113-
in_file, suffix='_median_b0', newpath=newpath)
109+
in_file, suffix='_rescaled_b0', newpath=newpath)
114110

115111
img = nb.load(in_file)
116112
if img.dataobj.ndim == 3:
117113
return in_file
118-
if img.shape[-1] == 1:
119-
nb.squeeze_image(img).to_filename(out_file)
120-
return out_file
121114

122115
data = img.get_fdata(dtype='float32')
123116
mask_img = nb.load(mask_file)
124117
mask_data = mask_img.get_fdata(dtype='float32')
125118

126-
median_signal = data[mask_data > 0, ...].median(axis=0)
127-
119+
median_signal = np.median(data[mask_data > 0, ...], axis=0)
128120
rescaled_data = 1000 * data / median_signal
121+
hdr = img.header.copy()
122+
nb.Nifti1Image(rescaled_data, img.affine, hdr).to_filename(out_file)
123+
return out_file
124+
125+
126+
def median(in_file, newpath=None):
127+
"""Average a 4D dataset across the last dimension using median."""
128+
out_file = fname_presuffix(
129+
in_file, suffix='_b0ref', newpath=newpath)
130+
131+
img = nb.load(in_file)
132+
if img.dataobj.ndim == 3:
133+
return in_file
134+
if img.shape[-1] == 1:
135+
nb.squeeze_image(img).to_filename(out_file)
136+
return out_file
129137

130-
median_data = np.median(rescaled_data, axis=-1)
138+
median_data = np.median(img.get_fdata(dtype='float32'),
139+
axis=-1)
131140

132141
hdr = img.header.copy()
133-
hdr.set_data_shape(median_data.shape)
134142
hdr.set_xyzt_units('mm')
135143
hdr.set_data_dtype(np.float32)
136144
nb.Nifti1Image(median_data, img.affine, hdr).to_filename(out_file)

dmriprep/workflows/dwi/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def init_dwi_preproc_wf(
152152

153153
summary = pe.Node(
154154
DiffusionSummary(
155+
distortion_correction='Not implemented',
155156
pe_direction=metadata.get("PhaseEncodingDirection")),
156157
name='summary', mem_gb=DEFAULT_MEMORY_MIN_GB, run_without_submitting=True)
157158

@@ -190,6 +191,7 @@ def init_dwi_preproc_wf(
190191
mem_gb=DEFAULT_MEMORY_MIN_GB)
191192

192193
workflow.connect([
194+
(inputnode, ds_report_summary, [('dwi_file', 'in_file')]),
193195
(summary, ds_report_summary, [('out_report', 'in_file')]),
194196
(dwi_reference_wf, ds_report_validation, [
195197
('outputnode.validation_report', 'in_file')]),

dmriprep/workflows/dwi/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def init_dwi_reference_wf(omp_nthreads, name='dwi_reference_wf', gen_report=Fals
102102
(reg_b0, pre_mask, [('out_file', 'in_file')]),
103103
(reg_b0, rescale_b0, [('out_file', 'in_file')]),
104104
(pre_mask, rescale_b0, [('out_file', 'mask_file')]),
105-
(rescale_b0, enhance_and_skullstrip_dwi_wf, [('out_file', 'inputnode.in_file')]),
105+
(rescale_b0, enhance_and_skullstrip_dwi_wf, [('out_ref', 'inputnode.in_file')]),
106106
(pre_mask, enhance_and_skullstrip_dwi_wf, [('out_file', 'inputnode.pre_mask')]),
107107
(validate, outputnode, [('out_file', 'dwi_file'),
108108
('out_report', 'validation_report')]),

0 commit comments

Comments
 (0)