Skip to content

Commit 5f4d402

Browse files
committed
Add FSSurfaceCommand and change MRIsCombine setup.
1 parent 9ca230f commit 5f4d402

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed

nipype/interfaces/freesurfer/base.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,45 @@ def version(self):
162162
return ver.rstrip().split('-v')[-1]
163163

164164

165+
class FSSurfaceCommand(FSCommand):
166+
"""Support for FreeSurfer surface-related functions.
167+
For some functions, if the output file is not specified starting with
168+
'lh.' or 'rh.', FreeSurfer prepends the prefix from the input file to the
169+
output filename. Output out_file must be adjusted to accommodate this.
170+
By including the full path in the filename, we can also avoid this behavior.
171+
"""
172+
def __init__(self, **inputs):
173+
super(FSSurfaceCommand, self).__init__(**inputs)
174+
175+
def _get_filecopy_info(self):
176+
"""Filename normalization routine to perform only when run in Node
177+
context
178+
"""
179+
self._normalize_filenames()
180+
return super(FSSurfaceCommand, self)._get_filecopy_info()
181+
182+
def _normalize_filenames(self):
183+
pass
184+
185+
@staticmethod
186+
def _associated_file(in_file, out_name):
187+
"""Based on MRIsBuildFileName in freesurfer/utils/mrisurf.c
188+
189+
Use in_file prefix to indicate hemisphere for out_name, rather than
190+
inspecting the surface data structure.
191+
"""
192+
path, base = os.path.split(out_name)
193+
if path == '':
194+
_, in_file = os.path.split(in_file)
195+
hemis = ('lh.', 'rh.')
196+
if in_file[:3] in hemis and base[:3] not in hemis:
197+
base = in_file[:3] + base
198+
return os.path.abspath(os.path.join(path, base))
199+
200+
165201
class FSScriptCommand(FSCommand):
166-
""" Support for Freesurfer script commands with log inputs.terminal_output """
202+
""" Support for Freesurfer script commands with log inputs.terminal_output
203+
"""
167204
_terminal_output = 'file'
168205
_always_run = False
169206

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from __future__ import unicode_literals
3+
from ..base import FSSurfaceCommand
4+
5+
6+
def test_FSSurfaceCommand_inputs():
7+
input_map = dict(args=dict(argstr='%s',
8+
),
9+
environ=dict(nohash=True,
10+
usedefault=True,
11+
),
12+
ignore_exception=dict(nohash=True,
13+
usedefault=True,
14+
),
15+
subjects_dir=dict(),
16+
terminal_output=dict(nohash=True,
17+
),
18+
)
19+
inputs = FSSurfaceCommand.input_spec()
20+
21+
for key, metadata in list(input_map.items()):
22+
for metakey, value in list(metadata.items()):
23+
assert getattr(inputs.traits()[key], metakey) == value
24+

nipype/interfaces/freesurfer/utils.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from ...utils.filemanip import fname_presuffix, split_filename
2222
from ..base import (TraitedSpec, File, traits, OutputMultiPath, isdefined,
2323
CommandLine, CommandLineInputSpec)
24-
from .base import (FSCommand, FSTraitedSpec,
24+
from .base import (FSCommand, FSTraitedSpec, FSSurfaceCommand,
2525
FSScriptCommand, FSScriptOutputSpec,
2626
FSTraitedSpecOpenMP, FSCommandOpenMP)
2727
__docformat__ = 'restructuredtext'
@@ -974,7 +974,7 @@ class MRIsCombineOutputSpec(TraitedSpec):
974974
'in_files.')
975975

976976

977-
class MRIsCombine(FSCommand):
977+
class MRIsCombine(FSSurfaceCommand):
978978
"""
979979
Uses Freesurfer's mris_convert to combine two surface files into one.
980980
@@ -997,19 +997,28 @@ class MRIsCombine(FSCommand):
997997
output_spec = MRIsCombineOutputSpec
998998

999999
def _list_outputs(self):
1000+
outputs = self._outputs().get()
1001+
outputs['out_file'] = self._associated_file(self.inputs.in_files[0],
1002+
self.inputs.out_file)
1003+
return outputs
1004+
1005+
@staticmethod
1006+
def _associated_file(in_file, out_name):
1007+
"""Unlike the standard _associated_file, which uses the prefix from
1008+
in_file, in MRIsCombine, it uses 'lh.' as the prefix for the output
1009+
file no matter what the inputs are.
10001010
"""
1001-
If the output file is not specified starting with 'lh.' or 'rh.',
1002-
FreeSurfer prepends 'lh.' to the filename. It never adds 'rh.', even if
1003-
both input files are from the right hemisphere. Output out_file must be
1004-
adjusted to accommodate this.
1005-
"""
1006-
outputs = self.output_spec().get()
1007-
if any(self.inputs.out_file.startswith(pre) for pre in ['lh.', 'rh.']):
1008-
outputs['out_file'] = self.inputs.out_file
1009-
else:
1010-
outputs['out_file'] = 'lh.' + self.inputs.out_file
1011+
path, base = os.path.split(out_name)
1012+
if path == '':
1013+
_, in_file = os.path.split(in_file)
1014+
hemis = ('lh.', 'rh.')
1015+
if base[:3] not in hemis:
1016+
base = 'lh.' + base
1017+
return os.path.abspath(os.path.join(path, base))
10111018

1012-
return outputs
1019+
def _normalize_filenames(self):
1020+
if isdefined(self.inputs.out_file):
1021+
self.inputs.out_file = os.path.abspath(self.inputs.out_file)
10131022

10141023

10151024
class MRITessellateInputSpec(FSTraitedSpec):

0 commit comments

Comments
 (0)