Skip to content

Commit 608278f

Browse files
committed
Use copyfile to link the surface context
Mimics the FreeSurfer MRIsBuildFileName to select appropriate files If copying files, defines pial and thickness_name (as needed) to ensure copy. Adds a non-argument sphere trait that MUST NOT be changed. mris_expand will not run in a node if there is not a `?h.sphere` associated with the in_file.
1 parent 0d058ef commit 608278f

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

nipype/interfaces/freesurfer/tests/test_auto_MRIsExpand.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def test_MRIsExpand_inputs():
1919
usedefault=True,
2020
),
2121
in_file=dict(argstr='%s',
22+
copyfile=False,
2223
mandatory=True,
2324
position=-3,
2425
),
@@ -31,9 +32,13 @@ def test_MRIsExpand_inputs():
3132
usedefault=True,
3233
),
3334
pial=dict(argstr='-pial %s',
35+
copyfile=False,
3436
),
3537
smooth_averages=dict(argstr='-A %d',
3638
),
39+
sphere=dict(copyfile=False,
40+
usedefault=True,
41+
),
3742
spring=dict(argstr='-S %g',
3843
),
3944
subjects_dir=dict(),
@@ -42,6 +47,7 @@ def test_MRIsExpand_inputs():
4247
thickness=dict(argstr='-thickness',
4348
),
4449
thickness_name=dict(argstr='-thickness_name %s',
50+
copyfile=False,
4551
),
4652
write_iterations=dict(argstr='-W %d',
4753
),

nipype/interfaces/freesurfer/utils.py

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,33 +2885,37 @@ class MRIsExpandInputSpec(FSTraitedSpec):
28852885
# Input spec derived from
28862886
# https://github.com/freesurfer/freesurfer/blob/102e053/mris_expand/mris_expand.c
28872887
in_file = File(
2888-
exists=True, mandatory=True, argstr='%s', position=-3,
2888+
exists=True, mandatory=True, argstr='%s', position=-3, copyfile=False,
28892889
desc='Surface to expand')
28902890
distance = traits.Float(
28912891
mandatory=True, argstr='%g', position=-2,
28922892
desc='Distance in mm or fraction of cortical thickness')
28932893
out_name = traits.Str(
28942894
'expanded', argstr='%s', position=-1, usedefault=True,
28952895
desc=('Output surface file\n'
2896-
'If missing "lh." or "rh.", derive from `in_file`'))
2896+
'If no path, uses directory of `in_file`\n'
2897+
'If no path AND missing "lh." or "rh.", derive from `in_file`'))
28972898
thickness = traits.Bool(
28982899
argstr='-thickness',
28992900
desc='Expand by fraction of cortical thickness, not mm')
29002901
thickness_name = traits.Str(
2901-
argstr="-thickness_name %s",
2902+
argstr="-thickness_name %s", copyfile=False,
29022903
desc=('Name of thickness file (implicit: "thickness")\n'
29032904
'If no path, uses directory of `in_file`\n'
2904-
'If missing "lh." or "rh.", derive from `in_file`'))
2905+
'If no path AND missing "lh." or "rh.", derive from `in_file`'))
29052906
navgs = traits.Tuple(
29062907
traits.Int, traits.Int,
29072908
argstr='-navgs %d %d',
29082909
desc=('Tuple of (n_averages, min_averages) parameters '
29092910
'(implicit: (16, 0))'))
29102911
pial = traits.Str(
2911-
argstr='-pial %s',
2912+
argstr='-pial %s', copyfile=False,
29122913
desc=('Name of pial file (implicit: "pial")\n'
29132914
'If no path, uses directory of `in_file`\n'
2914-
'If missing "lh." or "rh.", derive from `in_file`'))
2915+
'If no path AND missing "lh." or "rh.", derive from `in_file`'))
2916+
sphere = traits.Str(
2917+
'sphere', copyfile=False, usedefault=True,
2918+
desc='WARNING: Do not change this trait')
29152919
spring = traits.Float(argstr='-S %g', desc="Spring term (implicit: 0.05)")
29162920
dt = traits.Float(argstr='-T %g', desc='dt (implicit: 0.25)')
29172921
write_iterations = traits.Int(
@@ -2945,30 +2949,52 @@ class MRIsExpand(FSCommand):
29452949
>>> from nipype.interfaces.freesurfer import MRIsExpand
29462950
>>> mris_expand = MRIsExpand(thickness=True, distance=0.5)
29472951
>>> mris_expand.inputs.in_file = 'lh.white'
2948-
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE, +ELLIPSIS
2949-
'mris_expand -thickness lh.white 0.5 .../lh.expanded'
2952+
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE
2953+
'mris_expand -thickness lh.white 0.5 expanded'
29502954
>>> mris_expand.inputs.out_name = 'graymid'
2951-
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE, +ELLIPSIS
2952-
'mris_expand -thickness lh.white 0.5 .../lh.graymid'
2955+
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE
2956+
'mris_expand -thickness lh.white 0.5 graymid'
29532957
"""
29542958
_cmd = 'mris_expand'
29552959
input_spec = MRIsExpandInputSpec
29562960
output_spec = MRIsExpandOutputSpec
29572961

2958-
def _format_arg(self, name, spec, value):
2959-
if name == 'out_name':
2960-
value = self._list_outputs()['out_file']
2961-
return super(MRIsExpand, self)._format_arg(name, spec, value)
2962-
29632962
def _list_outputs(self):
29642963
outputs = self._outputs().get()
2965-
# Mimic FreeSurfer output filename derivation, but in local directory
2966-
# if no path specified
2967-
out_file = self.inputs.out_name
2968-
path, base = os.path.split(out_file)
2969-
if path == '' and base[:3] not in ('lh.', 'rh.'):
2970-
in_file = os.path.basename(self.inputs.in_file)
2971-
if in_file[:3] in ('lh.', 'rh.'):
2972-
out_file = os.path.basename(self.inputs.in_file)[:3] + base
2973-
outputs["out_file"] = os.path.abspath(out_file)
2964+
outputs['out_file'] = self._associated_file(self.inputs.in_file,
2965+
self.inputs.out_name)
29742966
return outputs
2967+
2968+
def _get_filecopy_info(self):
2969+
in_file = self.inputs.in_file
2970+
2971+
pial = self.inputs.pial
2972+
if not isdefined(pial):
2973+
pial = 'pial'
2974+
self.inputs.pial = self._associated_file(in_file, pial)
2975+
2976+
if isdefined(self.inputs.thickness) and self.inputs.thickness:
2977+
thickness_name = self.inputs.thickness_name
2978+
if not isdefined(thickness_name):
2979+
thickness_name = 'thickness'
2980+
self.inputs.thickness_name = self._associated_file(in_file,
2981+
thickness_name)
2982+
2983+
self.inputs.sphere = self._associated_file(in_file, self.inputs.sphere)
2984+
2985+
return super(MRIsExpand, self)._get_filecopy_info()
2986+
2987+
@staticmethod
2988+
def _associated_file(in_file, out_name):
2989+
"""Based on MRIsBuildFileName in freesurfer/utils/mrisurf.c
2990+
2991+
Use file prefix to indicate hemisphere, rather than inspecting the
2992+
surface data structure
2993+
"""
2994+
path, base = os.path.split(out_name)
2995+
if path == '':
2996+
path, in_file = os.path.split(in_file)
2997+
hemis = ('lh.', 'rh.')
2998+
if in_file[:3] in hemis and base[:3] not in hemis:
2999+
base = in_file[:3] + base
3000+
return os.path.join(path, base)

0 commit comments

Comments
 (0)