Skip to content

Commit c505903

Browse files
authored
Merge pull request #2642 from salma1601/afni_warp_transform
ENH: Allow transform to be saved from AFNI 3dWarp
2 parents ccc15e6 + 3dc803c commit c505903

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

nipype/interfaces/afni/preprocess.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ...utils.filemanip import (load_json, save_json, split_filename,
1414
fname_presuffix)
1515
from ..base import (CommandLineInputSpec, CommandLine, TraitedSpec, traits,
16-
isdefined, File, InputMultiPath, Undefined, Str,
16+
isdefined, File, InputMultiPath, Undefined, Str,
1717
InputMultiObject)
1818

1919
from .base import (AFNICommandBase, AFNICommand, AFNICommandInputSpec,
@@ -2566,7 +2566,7 @@ class TProject(AFNICommand):
25662566
_cmd = '3dTproject'
25672567
input_spec = TProjectInputSpec
25682568
output_spec = AFNICommandOutputSpec
2569-
2569+
25702570

25712571

25722572
class TShiftInputSpec(AFNICommandInputSpec):
@@ -2862,7 +2862,8 @@ class WarpInputSpec(AFNICommandInputSpec):
28622862
name_template='%s_warp',
28632863
desc='output image file name',
28642864
argstr='-prefix %s',
2865-
name_source='in_file')
2865+
name_source='in_file',
2866+
keep_extension=True)
28662867
tta2mni = traits.Bool(
28672868
desc='transform dataset from Talairach to MNI152', argstr='-tta2mni')
28682869
mni2tta = traits.Bool(
@@ -2893,6 +2894,13 @@ class WarpInputSpec(AFNICommandInputSpec):
28932894
argstr='-zpad %d')
28942895
verbose = traits.Bool(
28952896
desc='Print out some information along the way.', argstr='-verb')
2897+
save_warp = traits.Bool(
2898+
desc='save warp as .mat file', requires=['verbose'])
2899+
2900+
2901+
class WarpOutputSpec(TraitedSpec):
2902+
out_file = File(desc='Warped file.', exists=True)
2903+
warp_file = File(desc='warp transform .mat file')
28962904

28972905

28982906
class Warp(AFNICommand):
@@ -2924,7 +2932,25 @@ class Warp(AFNICommand):
29242932
"""
29252933
_cmd = '3dWarp'
29262934
input_spec = WarpInputSpec
2927-
output_spec = AFNICommandOutputSpec
2935+
output_spec = WarpOutputSpec
2936+
2937+
def _run_interface(self, runtime):
2938+
runtime = super(Warp, self)._run_interface(runtime)
2939+
2940+
if self.inputs.save_warp:
2941+
import numpy as np
2942+
warp_file = self._list_outputs()['warp_file']
2943+
np.savetxt(warp_file, [runtime.stdout], fmt=str('%s'))
2944+
return runtime
2945+
2946+
def _list_outputs(self):
2947+
outputs = super(Warp, self)._list_outputs()
2948+
if self.inputs.save_warp:
2949+
outputs['warp_file'] = fname_presuffix(outputs['out_file'],
2950+
suffix='_transform.mat',
2951+
use_ext=False)
2952+
2953+
return outputs
29282954

29292955

29302956
class QwarpPlusMinusInputSpec(CommandLineInputSpec):

nipype/interfaces/afni/tests/test_auto_Warp.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ def test_Warp_inputs():
2929
oblique_parent=dict(argstr='-oblique_parent %s', ),
3030
out_file=dict(
3131
argstr='-prefix %s',
32+
keep_extension=True,
3233
name_source='in_file',
3334
name_template='%s_warp',
3435
),
3536
outputtype=dict(),
37+
save_warp=dict(requires=['verbose'], ),
3638
tta2mni=dict(argstr='-tta2mni', ),
3739
verbose=dict(argstr='-verb', ),
3840
zpad=dict(argstr='-zpad %d', ),
@@ -43,7 +45,10 @@ def test_Warp_inputs():
4345
for metakey, value in list(metadata.items()):
4446
assert getattr(inputs.traits()[key], metakey) == value
4547
def test_Warp_outputs():
46-
output_map = dict(out_file=dict(), )
48+
output_map = dict(
49+
out_file=dict(),
50+
warp_file=dict(),
51+
)
4752
outputs = Warp.output_spec()
4853

4954
for key, metadata in list(output_map.items()):

0 commit comments

Comments
 (0)