Skip to content

Commit 39a1e3d

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 2ed7fa5 commit 39a1e3d

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
@@ -2912,33 +2912,37 @@ class MRIsExpandInputSpec(FSTraitedSpec):
29122912
# Input spec derived from
29132913
# https://github.com/freesurfer/freesurfer/blob/102e053/mris_expand/mris_expand.c
29142914
in_file = File(
2915-
exists=True, mandatory=True, argstr='%s', position=-3,
2915+
exists=True, mandatory=True, argstr='%s', position=-3, copyfile=False,
29162916
desc='Surface to expand')
29172917
distance = traits.Float(
29182918
mandatory=True, argstr='%g', position=-2,
29192919
desc='Distance in mm or fraction of cortical thickness')
29202920
out_name = traits.Str(
29212921
'expanded', argstr='%s', position=-1, usedefault=True,
29222922
desc=('Output surface file\n'
2923-
'If missing "lh." or "rh.", derive from `in_file`'))
2923+
'If no path, uses directory of `in_file`\n'
2924+
'If no path AND missing "lh." or "rh.", derive from `in_file`'))
29242925
thickness = traits.Bool(
29252926
argstr='-thickness',
29262927
desc='Expand by fraction of cortical thickness, not mm')
29272928
thickness_name = traits.Str(
2928-
argstr="-thickness_name %s",
2929+
argstr="-thickness_name %s", copyfile=False,
29292930
desc=('Name of thickness file (implicit: "thickness")\n'
29302931
'If no path, uses directory of `in_file`\n'
2931-
'If missing "lh." or "rh.", derive from `in_file`'))
2932+
'If no path AND missing "lh." or "rh.", derive from `in_file`'))
29322933
navgs = traits.Tuple(
29332934
traits.Int, traits.Int,
29342935
argstr='-navgs %d %d',
29352936
desc=('Tuple of (n_averages, min_averages) parameters '
29362937
'(implicit: (16, 0))'))
29372938
pial = traits.Str(
2938-
argstr='-pial %s',
2939+
argstr='-pial %s', copyfile=False,
29392940
desc=('Name of pial file (implicit: "pial")\n'
29402941
'If no path, uses directory of `in_file`\n'
2941-
'If missing "lh." or "rh.", derive from `in_file`'))
2942+
'If no path AND missing "lh." or "rh.", derive from `in_file`'))
2943+
sphere = traits.Str(
2944+
'sphere', copyfile=False, usedefault=True,
2945+
desc='WARNING: Do not change this trait')
29422946
spring = traits.Float(argstr='-S %g', desc="Spring term (implicit: 0.05)")
29432947
dt = traits.Float(argstr='-T %g', desc='dt (implicit: 0.25)')
29442948
write_iterations = traits.Int(
@@ -2972,30 +2976,52 @@ class MRIsExpand(FSCommand):
29722976
>>> from nipype.interfaces.freesurfer import MRIsExpand
29732977
>>> mris_expand = MRIsExpand(thickness=True, distance=0.5)
29742978
>>> mris_expand.inputs.in_file = 'lh.white'
2975-
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE, +ELLIPSIS
2976-
'mris_expand -thickness lh.white 0.5 .../lh.expanded'
2979+
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE
2980+
'mris_expand -thickness lh.white 0.5 expanded'
29772981
>>> mris_expand.inputs.out_name = 'graymid'
2978-
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE, +ELLIPSIS
2979-
'mris_expand -thickness lh.white 0.5 .../lh.graymid'
2982+
>>> mris_expand.cmdline # doctest: +ALLOW_UNICODE
2983+
'mris_expand -thickness lh.white 0.5 graymid'
29802984
"""
29812985
_cmd = 'mris_expand'
29822986
input_spec = MRIsExpandInputSpec
29832987
output_spec = MRIsExpandOutputSpec
29842988

2985-
def _format_arg(self, name, spec, value):
2986-
if name == 'out_name':
2987-
value = self._list_outputs()['out_file']
2988-
return super(MRIsExpand, self)._format_arg(name, spec, value)
2989-
29902989
def _list_outputs(self):
29912990
outputs = self._outputs().get()
2992-
# Mimic FreeSurfer output filename derivation, but in local directory
2993-
# if no path specified
2994-
out_file = self.inputs.out_name
2995-
path, base = os.path.split(out_file)
2996-
if path == '' and base[:3] not in ('lh.', 'rh.'):
2997-
in_file = os.path.basename(self.inputs.in_file)
2998-
if in_file[:3] in ('lh.', 'rh.'):
2999-
out_file = os.path.basename(self.inputs.in_file)[:3] + base
3000-
outputs["out_file"] = os.path.abspath(out_file)
2991+
outputs['out_file'] = self._associated_file(self.inputs.in_file,
2992+
self.inputs.out_name)
30012993
return outputs
2994+
2995+
def _get_filecopy_info(self):
2996+
in_file = self.inputs.in_file
2997+
2998+
pial = self.inputs.pial
2999+
if not isdefined(pial):
3000+
pial = 'pial'
3001+
self.inputs.pial = self._associated_file(in_file, pial)
3002+
3003+
if isdefined(self.inputs.thickness) and self.inputs.thickness:
3004+
thickness_name = self.inputs.thickness_name
3005+
if not isdefined(thickness_name):
3006+
thickness_name = 'thickness'
3007+
self.inputs.thickness_name = self._associated_file(in_file,
3008+
thickness_name)
3009+
3010+
self.inputs.sphere = self._associated_file(in_file, self.inputs.sphere)
3011+
3012+
return super(MRIsExpand, self)._get_filecopy_info()
3013+
3014+
@staticmethod
3015+
def _associated_file(in_file, out_name):
3016+
"""Based on MRIsBuildFileName in freesurfer/utils/mrisurf.c
3017+
3018+
Use file prefix to indicate hemisphere, rather than inspecting the
3019+
surface data structure
3020+
"""
3021+
path, base = os.path.split(out_name)
3022+
if path == '':
3023+
path, in_file = os.path.split(in_file)
3024+
hemis = ('lh.', 'rh.')
3025+
if in_file[:3] in hemis and base[:3] not in hemis:
3026+
base = in_file[:3] + base
3027+
return os.path.join(path, base)

0 commit comments

Comments
 (0)