Skip to content

Commit 35c27d0

Browse files
committed
Add optional dependencies to ReconAll._steps
1 parent a88d264 commit 35c27d0

File tree

2 files changed

+60
-48
lines changed

2 files changed

+60
-48
lines changed

nipype/interfaces/freesurfer/preprocess.py

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from nibabel import load
2323

2424
from ... import logging
25-
from ...utils.filemanip import fname_presuffix
25+
from ...utils.filemanip import fname_presuffix, check_depends
2626
from ..io import FreeSurferSource
2727
from ..base import (TraitedSpec, File, traits,
2828
Directory, InputMultiPath,
@@ -658,86 +658,85 @@ class ReconAll(CommandLine):
658658

659659
_steps = [
660660
# autorecon1
661-
('motioncor', ['mri/rawavg.mgz', 'mri/orig.mgz']),
661+
('motioncor', ['mri/rawavg.mgz', 'mri/orig.mgz'], []),
662662
('talairach', ['mri/orig_nu.mgz',
663663
'mri/transforms/talairach.auto.xfm',
664664
'mri/transforms/talairach.xfm',
665665
# 'mri/transforms/talairach_avi.log',
666-
]),
667-
('nuintensitycor', ['mri/nu.mgz']),
668-
('normalization', ['mri/T1.mgz']),
666+
], []),
667+
('nuintensitycor', ['mri/nu.mgz'], []),
668+
('normalization', ['mri/T1.mgz'], []),
669669
('skullstrip', ['mri/talairach_with_skull.lta',
670670
'mri/brainmask.auto.mgz',
671-
'mri/brainmask.mgz']),
671+
'mri/brainmask.mgz'], []),
672672
# autorecon2
673-
('gcareg', ['mri/transforms/talairach.lta']),
674-
('canorm', ['mri/norm.mgz']),
675-
('careg', ['mri/transforms/talairach.m3z']),
673+
('gcareg', ['mri/transforms/talairach.lta'], []),
674+
('canorm', ['mri/norm.mgz'], []),
675+
('careg', ['mri/transforms/talairach.m3z'], []),
676676
('calabel', ['mri/aseg.auto_noCCseg.mgz',
677677
'mri/aseg.auto.mgz',
678-
'mri/aseg.mgz']),
679-
('normalization2', ['mri/brain.mgz']),
680-
('maskbfs', ['mri/brain.finalsurfs.mgz']),
678+
'mri/aseg.mgz'], []),
679+
('normalization2', ['mri/brain.mgz'], []),
680+
('maskbfs', ['mri/brain.finalsurfs.mgz'], []),
681681
('segmentation', ['mri/wm.seg.mgz',
682682
'mri/wm.asegedit.mgz',
683-
'mri/wm.mgz']),
683+
'mri/wm.mgz'], []),
684684
('fill', ['mri/filled.mgz',
685685
# 'scripts/ponscc.cut.log',
686-
]),
687-
('tessellate', ['surf/lh.orig.nofix', 'surf/rh.orig.nofix']),
688-
('smooth1', ['surf/lh.smoothwm.nofix', 'surf/rh.smoothwm.nofix']),
689-
('inflate1', ['surf/lh.inflated.nofix', 'surf/rh.inflated.nofix']),
690-
('qsphere', ['surf/lh.qsphere.nofix', 'surf/rh.qsphere.nofix']),
691-
('fix', ['surf/lh.orig', 'surf/rh.orig']),
686+
], []),
687+
('tessellate', ['surf/lh.orig.nofix', 'surf/rh.orig.nofix'], []),
688+
('smooth1', ['surf/lh.smoothwm.nofix', 'surf/rh.smoothwm.nofix'], []),
689+
('inflate1', ['surf/lh.inflated.nofix', 'surf/rh.inflated.nofix'], []),
690+
('qsphere', ['surf/lh.qsphere.nofix', 'surf/rh.qsphere.nofix'], []),
691+
('fix', ['surf/lh.orig', 'surf/rh.orig'], []),
692692
('white', ['surf/lh.white.preaparc', 'surf/rh.white.preaparc',
693693
'surf/lh.curv', 'surf/rh.curv',
694694
'surf/lh.area', 'surf/rh.area',
695-
'label/lh.cortex.label', 'label/rh.cortex.label']),
696-
('smooth2', ['surf/lh.smoothwm', 'surf/rh.smoothwm']),
695+
'label/lh.cortex.label', 'label/rh.cortex.label'], []),
696+
('smooth2', ['surf/lh.smoothwm', 'surf/rh.smoothwm'], []),
697697
('inflate2', ['surf/lh.inflated', 'surf/rh.inflated',
698-
'surf/lh.sulc', 'surf/rh.sulc']),
698+
'surf/lh.sulc', 'surf/rh.sulc'], []),
699699
('curvHK', ['surf/lh.white.H', 'surf/rh.white.H',
700700
'surf/lh.white.K', 'surf/rh.white.K',
701701
'surf/lh.inflated.H', 'surf/rh.inflated.H',
702-
'surf/lh.inflated.K', 'surf/rh.inflated.K']),
703-
('curvstats', ['stats/lh.curv.stats', 'stats/rh.curv.stats']),
702+
'surf/lh.inflated.K', 'surf/rh.inflated.K'], []),
703+
('curvstats', ['stats/lh.curv.stats', 'stats/rh.curv.stats'], []),
704704
# autorecon3
705-
('sphere', ['surf/lh.sphere', 'surf/rh.sphere']),
706-
('surfreg', ['surf/lh.sphere.reg', 'surf/rh.sphere.reg']),
705+
('sphere', ['surf/lh.sphere', 'surf/rh.sphere'], []),
706+
('surfreg', ['surf/lh.sphere.reg', 'surf/rh.sphere.reg'], []),
707707
('jacobian_white', ['surf/lh.jacobian_white',
708-
'surf/rh.jacobian_white']),
709-
('avgcurv', ['surf/lh.avg_curv', 'surf/rh.avg_curv']),
710-
('cortparc', ['label/lh.aparc.annot', 'label/rh.aparc.annot']),
708+
'surf/rh.jacobian_white'], []),
709+
('avgcurv', ['surf/lh.avg_curv', 'surf/rh.avg_curv'], []),
710+
('cortparc', ['label/lh.aparc.annot', 'label/rh.aparc.annot'], []),
711711
('pial', ['surf/lh.pial', 'surf/rh.pial',
712712
'surf/lh.curv.pial', 'surf/rh.curv.pial',
713713
'surf/lh.area.pial', 'surf/rh.area.pial',
714-
'surf/lh.thickness', 'surf/rh.thickness']),
715-
# TODO: Optional -T2pial / -FLAIRpial
714+
'surf/lh.thickness', 'surf/rh.thickness'], []),
716715
('cortribbon', ['mri/lh.ribbon.mgz', 'mri/rh.ribbon.mgz',
717-
'mri/ribbon.mgz']),
716+
'mri/ribbon.mgz'], []),
718717
('parcstats', ['stats/lh.aparc.astats', 'stats/rh.aparc.stats',
719-
'stats/aparc.annot.ctab']),
718+
'stats/aparc.annot.ctab'], []),
720719
('cortparc2', ['label/lh.aparc.a2009s.annot',
721-
'label/rh.aparc.a2009s.annot']),
720+
'label/rh.aparc.a2009s.annot'], []),
722721
('parcstats2', ['stats/lh.aparc.a2009s.stats',
723722
'stats/rh.aparc.a2009s.stats',
724-
'stats/aparc.annot.a2009s.ctab']),
723+
'stats/aparc.annot.a2009s.ctab'], []),
725724
('cortparc3', ['label/lh.aparc.DKTatlas.annot',
726-
'label/rh.aparc.DKTatlas.annot']),
725+
'label/rh.aparc.DKTatlas.annot'], []),
727726
('parcstats3', ['stats/lh.aparc.DKTatlas.stats',
728727
'stats/rh.aparc.DKTatlas.stats',
729-
'stats/aparc.annot.DKTatlas.ctab']),
730-
('pctsurfcon', ['surf/lh.w-g.pct.mgh', 'surf/rh.w-g.pct.mgh']),
731-
('hyporelabel', ['mri/aseg.presurf.hypos.mgz']),
728+
'stats/aparc.annot.DKTatlas.ctab'], []),
729+
('pctsurfcon', ['surf/lh.w-g.pct.mgh', 'surf/rh.w-g.pct.mgh'], []),
730+
('hyporelabel', ['mri/aseg.presurf.hypos.mgz'], []),
732731
('aparc2aseg', ['mri/aparc+aseg.mgz',
733732
'mri/aparc.a2009s+aseg.mgz',
734-
'mri/aparc.DKTatlas+aseg.mgz']),
735-
('apas2aseg', ['mri/aseg.mgz']), # XXX: Will not run because of calabel
736-
('segstats', ['stats/aseg.stats']),
737-
('wmparc', ['mri/wmparc.mgz', 'stats/wmparc.stats']),
733+
'mri/aparc.DKTatlas+aseg.mgz'], []),
734+
('apas2aseg', ['mri/aseg.mgz'], ['mri/aparc+aseg.mgz']),
735+
('segstats', ['stats/aseg.stats'], []),
736+
('wmparc', ['mri/wmparc.mgz', 'stats/wmparc.stats'], []),
738737
('balabels', ['BA.ctab', 'BA.thresh.ctab',
739738
'label/lh.entorhinal_exvivo.label',
740-
'label/rh.entorhinal_exvivo.label']),
739+
'label/rh.entorhinal_exvivo.label'], []),
741740
]
742741

743742
def _gen_subjects_dir(self):
@@ -799,15 +798,15 @@ def cmdline(self):
799798
flags = []
800799
directive = 'all'
801800
for idx, step in enumerate(self._steps):
802-
step, outfiles = step
801+
step, outfiles, infiles = step
803802
flag = '-{}'.format(step)
804803
noflag = '-no{}'.format(step)
805804
if flag in cmd or noflag in cmd:
806805
continue
807806

808-
if all([os.path.exists(os.path.join(subjects_dir,
809-
self.inputs.subject_id, f)) for
810-
f in outfiles]):
807+
subj_dir = os.path.join(subjects_dir, self.inputs.subject_id)
808+
if check_depends([os.path.join(subj_dir, f) for f in outfiles],
809+
[os.path.join(subj_dir, f) for f in infiles]):
811810
flags.append(noflag)
812811
if idx > 4:
813812
directive = 'autorecon2'

nipype/utils/filemanip.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,19 @@ def list_to_filename(filelist):
447447
else:
448448
return filelist[0]
449449

450+
451+
def check_depends(targets, dependencies):
452+
"""Return true if all targets exist and are newer than all dependencies.
453+
454+
An OSError will be raised if there are missing dependencies.
455+
"""
456+
tgts = filename_to_list(tgts)
457+
deps = filename_to_list(deps)
458+
return all(map(os.path.exists, tgts)) and \
459+
min(map(os.path.getmtime, tgts)) > \
460+
max(map(os.path.getmtime, deps), default=0)
461+
462+
450463
def save_json(filename, data):
451464
"""Save data to a json file
452465

0 commit comments

Comments
 (0)