Skip to content

Commit f3ba8d2

Browse files
authored
Merge pull request #1966 from effigies/enh/recon_undef
ENH: ReconAll handle less common cases
2 parents 33131ac + 1db7b23 commit f3ba8d2

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

nipype/interfaces/freesurfer/preprocess.py

+59-3
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,26 @@ class ReconAllInputSpec(CommandLineInputSpec):
630630
desc="Enable parallel execution")
631631
hires = traits.Bool(argstr="-hires", min_ver='6.0.0',
632632
desc="Conform to minimum voxel size (for voxels < 1mm)")
633+
mprage = traits.Bool(argstr='-mprage',
634+
desc=('Assume scan parameters are MGH MP-RAGE '
635+
'protocol, which produces darker gray matter'))
636+
big_ventricles = traits.Bool(argstr='-bigventricles',
637+
desc=('For use in subjects with enlarged '
638+
'ventricles'))
639+
brainstem = traits.Bool(argstr='-brainstem-structures',
640+
desc='Segment brainstem structures')
641+
hippocampal_subfields_T1 = traits.Bool(
642+
argstr='-hippocampal-subfields-T1', min_ver='6.0.0',
643+
desc='segment hippocampal subfields using input T1 scan')
644+
hippocampal_subfields_T2 = traits.Tuple(
645+
File(exists=True), traits.Str(),
646+
argstr='-hippocampal-subfields-T2 %s %s', min_ver='6.0.0',
647+
desc=('segment hippocampal subfields using T2 scan, identified by '
648+
'ID (may be combined with hippocampal_subfields_T1)'))
633649
expert = File(exists=True, argstr='-expert %s',
634650
desc="Set parameters using expert file")
651+
xopts = traits.Enum("use", "clean", "overwrite", argstr='-xopts-%s',
652+
desc="Use, delete or overwrite existing expert options file")
635653
subjects_dir = Directory(exists=True, argstr='-sd %s', hash_files=False,
636654
desc='path to subjects directory', genfile=True)
637655
flags = traits.Str(argstr='%s', desc='additional parameters')
@@ -933,6 +951,13 @@ def _format_arg(self, name, trait_spec, value):
933951
if name == 'T1_files':
934952
if self._is_resuming():
935953
return ''
954+
if name == 'hippocampal_subfields_T1' and \
955+
isdefined(self.inputs.hippocampal_subfields_T2):
956+
return ''
957+
if all((name == 'hippocampal_subfields_T2',
958+
isdefined(self.inputs.hippocampal_subfields_T1) and
959+
self.inputs.hippocampal_subfields_T1)):
960+
trait_spec.argstr = trait_spec.argstr.replace('T2', 'T1T2')
936961
return super(ReconAll, self)._format_arg(name, trait_spec, value)
937962

938963
@property
@@ -949,10 +974,22 @@ def cmdline(self):
949974
if not isdefined(subjects_dir):
950975
subjects_dir = self._gen_subjects_dir()
951976

977+
# Check only relevant steps
978+
directive = self.inputs.directive
979+
if not isdefined(directive):
980+
steps = []
981+
elif directive == 'autorecon1':
982+
steps = self._autorecon1_steps
983+
elif directive.startswith('autorecon2'):
984+
steps = self._autorecon2_steps
985+
elif directive == 'autorecon3':
986+
steps = self._autorecon3_steps
987+
else:
988+
steps = self._steps
989+
952990
no_run = True
953991
flags = []
954-
for idx, step in enumerate(self._steps):
955-
step, outfiles, infiles = step
992+
for step, outfiles, infiles in steps:
956993
flag = '-{}'.format(step)
957994
noflag = '-no{}'.format(step)
958995
if noflag in cmd:
@@ -989,11 +1026,30 @@ def _prep_expert_file(self):
9891026
if lines == []:
9901027
return ''
9911028

1029+
contents = ''.join(lines)
1030+
if not isdefined(self.inputs.xopts) and \
1031+
self.get_expert_file() == contents:
1032+
return ' -xopts-use'
1033+
9921034
expert_fname = os.path.abspath('expert.opts')
9931035
with open(expert_fname, 'w') as fobj:
994-
fobj.write(''.join(lines))
1036+
fobj.write(contents)
9951037
return ' -expert {}'.format(expert_fname)
9961038

1039+
def _get_expert_file(self):
1040+
# Read pre-existing options file, if it exists
1041+
if isdefined(self.inputs.subjects_dir):
1042+
subjects_dir = self.inputs.subjects_dir
1043+
else:
1044+
subjects_dir = self._gen_subjects_dir()
1045+
1046+
xopts_file = os.path.join(subjects_dir, self.inputs.subject_id,
1047+
'scripts', 'expert-options')
1048+
if not os.path.exists(xopts_file):
1049+
return ''
1050+
with open(xopts_file, 'r') as fobj:
1051+
return fobj.read()
1052+
9971053

9981054
class BBRegisterInputSpec(FSTraitedSpec):
9991055
subject_id = traits.Str(argstr='--s %s',

nipype/interfaces/freesurfer/tests/test_auto_ReconAll.py

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def test_ReconAll_inputs():
1111
),
1212
args=dict(argstr='%s',
1313
),
14+
big_ventricles=dict(argstr='-bigventricles',
15+
),
16+
brainstem=dict(argstr='-brainstem-structures',
17+
),
1418
directive=dict(argstr='-%s',
1519
position=0,
1620
usedefault=True,
@@ -24,12 +28,20 @@ def test_ReconAll_inputs():
2428
),
2529
hemi=dict(argstr='-hemi %s',
2630
),
31+
hippocampal_subfields_T1=dict(argstr='-hippocampal-subfields-T1',
32+
min_ver='6.0.0',
33+
),
34+
hippocampal_subfields_T2=dict(argstr='-hippocampal-subfields-T2 %s %s',
35+
min_ver='6.0.0',
36+
),
2737
hires=dict(argstr='-hires',
2838
min_ver='6.0.0',
2939
),
3040
ignore_exception=dict(nohash=True,
3141
usedefault=True,
3242
),
43+
mprage=dict(argstr='-mprage',
44+
),
3345
mri_aparc2aseg=dict(xor=['expert'],
3446
),
3547
mri_ca_label=dict(xor=['expert'],
@@ -98,6 +110,8 @@ def test_ReconAll_inputs():
98110
use_T2=dict(argstr='-T2pial',
99111
min_ver='5.3.0',
100112
),
113+
xopts=dict(argstr='-xopts-%s',
114+
),
101115
)
102116
inputs = ReconAll.input_spec()
103117

0 commit comments

Comments
 (0)