Skip to content

Commit 35c902c

Browse files
FIX: resampling bug
1 parent f039428 commit 35c902c

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

nirodents/workflows/brainextraction.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
from nipype.pipeline import engine as pe
1414
from nipype.interfaces import utility as niu
1515
from nipype.interfaces.ants import N4BiasFieldCorrection, Atropos
16-
from nipype.interfaces.ants.utils import ResampleImageBySpacing, AI # , ImageMath,
16+
from nipype.interfaces.ants.utils import AI # , ImageMath, ResampleImageBySpacing,
1717
from nipype.interfaces.afni import MaskTool
18-
# from nipype.interfaces.fsl import ApplyMask
1918
from nipype.interfaces.io import DataSink
2019

2120
# niworkflows
2221
from niworkflows.interfaces.ants import ImageMath
22+
from niworkflows.utils.images import resample_by_spacing
2323
from niworkflows.interfaces.nibabel import ApplyMask
2424
from niworkflows.interfaces.fixes import (
2525
FixHeaderRegistration as Registration,
@@ -70,6 +70,17 @@ def init_rodent_brain_extraction_wf(
7070
tpl_tissue_labels = get_template(in_template,resolution=debug + 1, desc='cerebrum', suffix='dseg')
7171
tpl_brain_mask = get_template(in_template, resolution=debug + 1, desc='cerebrum', suffix='mask')
7272

73+
# resample template and target
74+
res_tmpl = pe.Node(niu.Function(function=res_by_spc,
75+
input_names=['in_file'], output_names=['out_file']), name='res_tmpl')
76+
if tpl_target_path:
77+
res_tmpl.inputs.in_file = tpl_target_path
78+
79+
res_target = pe.Node(niu.Function(function=res_by_spc,
80+
input_names=['in_file'], output_names=['out_file']), name='res_target')
81+
res_target2 = pe.Node(niu.Function(function=res_by_spc,
82+
input_names=['in_file'], output_names=['out_file']), name='res_target2')
83+
7384
dil_mask = pe.Node(MaskTool(), name = 'dil_mask')
7485
dil_mask.inputs.outputtype = 'NIFTI_GZ'
7586
dil_mask.inputs.dilate_inputs = '2'
@@ -88,15 +99,6 @@ def init_rodent_brain_extraction_wf(
8899
bspline_fitting_distance=bspline_fitting_distance),
89100
n_procs=omp_nthreads, name='inu_n4', iterfield=['input_image'])
90101

91-
# resample template and target
92-
res_tmpl = pe.Node(ResampleImageBySpacing(
93-
out_spacing=(0.4, 0.4, 0.4), apply_smoothing=False), name='res_tmpl')
94-
if tpl_target_path:
95-
res_tmpl.inputs.input_image = str(tpl_target_path)
96-
res_target = pe.Node(ResampleImageBySpacing(
97-
out_spacing=(0.4, 0.4, 0.4), apply_smoothing=False), name='res_target')
98-
res_target2 = pe.Node(ResampleImageBySpacing(
99-
out_spacing=(0.4, 0.4, 0.4), apply_smoothing=False), name='res_target2')
100102

101103
lap_tmpl = pe.Node(ImageMath(operation='Laplacian', op2='0.4'), name='lap_tmpl')
102104
if tpl_target_path:
@@ -217,16 +219,16 @@ def init_rodent_brain_extraction_wf(
217219
wf.connect([
218220
# resampling, truncation, initial N4, and creation of laplacian
219221
(inputnode, trunc, [('in_files', 'op1')]),
220-
(trunc, res_target, [(('output_image', _pop), 'input_image')]),
221-
(res_target, inu_n4, [('output_image', 'input_image')]),
222+
(trunc, res_target, [(('output_image', _pop), 'in_file')]),
223+
(res_target, inu_n4, [('out_file', 'input_image')]),
222224

223225
# dilation of input mask
224226
(inputnode, dil_mask, [('in_mask', 'in_file')]),
225227

226228
# ants AI inputs
227229
(inu_n4, init_aff, [(('output_image', _pop), 'moving_image')]),
228230
(dil_mask, init_aff, [('out_file', 'fixed_image_mask')]),
229-
(res_tmpl, init_aff, [('output_image', 'fixed_image')]),
231+
(res_tmpl, init_aff, [('out_file', 'fixed_image')]),
230232

231233
# warp mask to individual space
232234
(dil_mask, warp_mask, [('out_file', 'input_image')]),
@@ -241,10 +243,10 @@ def init_rodent_brain_extraction_wf(
241243
(inu_n4_final, lap_target, [(('output_image', _pop), 'op1')]),
242244
(lap_target, norm_lap_target, [('output_image', 'op1')]),
243245
(norm_lap_target, mrg_target, [('output_image', 'in2')]),
244-
(inu_n4_final, res_target2, [(('output_image', _pop), 'input_image')]),
245-
(res_target2, mrg_target, [('output_image', 'in1')]),
246+
(inu_n4_final, res_target2, [(('output_image', _pop), 'in_file')]),
247+
(res_target2, mrg_target, [('out_file', 'in1')]),
246248

247-
(res_tmpl, mrg_tmpl, [('output_image', 'in1')]),
249+
(res_tmpl, mrg_tmpl, [('out_file', 'in1')]),
248250
(lap_tmpl, norm_lap_tmpl, [('output_image', 'op1')]),
249251
(norm_lap_tmpl, mrg_tmpl, [('output_image', 'in2')]),
250252

@@ -295,22 +297,22 @@ def init_rodent_brain_extraction_wf(
295297
elif modality == 'mp2rage':
296298
wf.connect([
297299
# resampling and creation of laplacians
298-
(inputnode, res_target, [('in_files', 'input_image')]),
300+
(inputnode, res_target, [('in_files', 'in_file')]),
299301
(inputnode, lap_target, [('in_files', 'op1')]),
300302
(lap_target, norm_lap_target, [('output_image', 'op1')]),
301303
(norm_lap_target, mrg_target, [('output_image', 'in2')]),
302-
(res_target, mrg_target, [('output_image', 'in1')]),
304+
(res_target, mrg_target, [('out_file', 'in1')]),
303305

304-
(res_tmpl, mrg_tmpl, [('output_image', 'in1')]),
306+
(res_tmpl, mrg_tmpl, [('out_file', 'in1')]),
305307
(lap_tmpl, norm_lap_tmpl, [('output_image', 'op1')]),
306308
(norm_lap_tmpl, mrg_tmpl, [('output_image', 'in2')]),
307309

308310
#dilation of input mask
309311
(inputnode, dil_mask, [('in_mask', 'in_file')]),
310312

311313
# ants AI inputs
312-
(res_tmpl, init_aff, [('output_image', 'fixed_image')]),
313-
(res_target, init_aff, [('output_image', 'moving_image')]),
314+
(res_tmpl, init_aff, [('out_file', 'fixed_image')]),
315+
(res_target, init_aff, [('out_file', 'moving_image')]),
314316
(dil_mask, init_aff, [('out_file', 'fixed_image_mask')]),
315317

316318
# warp mask to individual space
@@ -365,3 +367,19 @@ def _pop(in_files):
365367
if isinstance(in_files, (list, tuple)):
366368
return in_files[0]
367369
return in_files
370+
371+
def res_by_spc(in_file, out_file = None):
372+
import os.path as op
373+
from niworkflows.utils.images import resample_by_spacing
374+
import nibabel as nib
375+
resampled = resample_by_spacing(in_file, (0.4, 0.4, 0.4), clip = False)
376+
377+
if out_file is None:
378+
fname, ext = op.splitext(op.basename(in_file))
379+
if ext == '.gz':
380+
fname, ext2 = op.splitext(fname)
381+
ext = ext2 + ext
382+
out_file = op.abspath('{}_resampled{}'.format(fname, ext))
383+
384+
nib.save(resampled, out_file)
385+
return out_file

0 commit comments

Comments
 (0)