Skip to content

Commit 943f040

Browse files
authored
Merge pull request nipy#1871 from effigies/patch-1
FIX: Select Eddy command at runtime
2 parents 05ad4a1 + 33f5db1 commit 943f040

File tree

1 file changed

+53
-32
lines changed

1 file changed

+53
-32
lines changed

nipype/interfaces/fsl/epi.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
... '../../testing/data'))
1313
>>> os.chdir(datadir)
1414
"""
15-
from __future__ import print_function, division, unicode_literals, absolute_import
15+
from __future__ import print_function, division, unicode_literals, \
16+
absolute_import
1617
from builtins import str
1718

1819
import os
@@ -106,7 +107,7 @@ def _run_interface(self, runtime):
106107
out_file = self.inputs.out_fieldmap
107108
im = nb.load(out_file, mmap=NUMPY_MMAP)
108109
dumb_img = nb.Nifti1Image(np.zeros(im.shape), im.affine,
109-
im.header)
110+
im.header)
110111
out_nii = nb.funcs.concat_images((im, dumb_img))
111112
nb.save(out_nii, out_file)
112113

@@ -450,26 +451,25 @@ class EddyInputSpec(FSLCommandInputSpec):
450451

451452

452453
class EddyOutputSpec(TraitedSpec):
453-
out_corrected = File(exists=True,
454-
desc=('4D image file containing all the corrected '
455-
'volumes'))
456-
out_parameter = File(exists=True,
457-
desc=('text file with parameters definining the '
458-
'field and movement for each scan'))
459-
out_rotated_bvecs = File(exists=True,
460-
desc=('File containing rotated b-values for all volumes'))
461-
out_movement_rms = File(exists=True,
462-
desc=('Summary of the "total movement" in each volume'))
463-
out_restricted_movement_rms = File(exists=True,
464-
desc=('Summary of the "total movement" in each volume '
465-
'disregarding translation in the PE direction'))
466-
out_shell_alignment_parameters = File(exists=True,
467-
desc=('File containing rigid body movement parameters '
468-
'between the different shells as estimated by a '
469-
'post-hoc mutual information based registration'))
470-
out_outlier_report = File(exists=True,
471-
desc=('Text-file with a plain language report '
472-
'on what outlier slices eddy has found'))
454+
out_corrected = File(
455+
exists=True, desc='4D image file containing all the corrected volumes')
456+
out_parameter = File(
457+
exists=True, desc=('text file with parameters definining the field and'
458+
'movement for each scan'))
459+
out_rotated_bvecs = File(
460+
exists=True, desc='File containing rotated b-values for all volumes')
461+
out_movement_rms = File(
462+
exists=True, desc='Summary of the "total movement" in each volume')
463+
out_restricted_movement_rms = File(
464+
exists=True, desc=('Summary of the "total movement" in each volume '
465+
'disregarding translation in the PE direction'))
466+
out_shell_alignment_parameters = File(
467+
exists=True, desc=('File containing rigid body movement parameters '
468+
'between the different shells as estimated by a '
469+
'post-hoc mutual information based registration'))
470+
out_outlier_report = File(
471+
exists=True, desc=('Text-file with a plain language report on what '
472+
'outlier slices eddy has found'))
473473

474474

475475
class Eddy(FSLCommand):
@@ -491,10 +491,16 @@ class Eddy(FSLCommand):
491491
>>> eddy.inputs.in_acqp = 'epi_acqp.txt'
492492
>>> eddy.inputs.in_bvec = 'bvecs.scheme'
493493
>>> eddy.inputs.in_bval = 'bvals.scheme'
494+
>>> eddy.inputs.use_cuda = True
494495
>>> eddy.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
495-
'eddy_openmp --acqp=epi_acqp.txt --bvals=bvals.scheme --bvecs=bvecs.scheme \
496+
'eddy_cuda --acqp=epi_acqp.txt --bvals=bvals.scheme --bvecs=bvecs.scheme \
496497
--imain=epi.nii --index=epi_index.txt --mask=epi_mask.nii \
497498
--out=.../eddy_corrected'
499+
>>> eddy.inputs.use_cuda = False
500+
>>> eddy.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
501+
'eddy_openmp --acqp=epi_acqp.txt --bvals=bvals.scheme \
502+
--bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \
503+
--mask=epi_mask.nii --out=.../eddy_corrected'
498504
>>> res = eddy.run() # doctest: +SKIP
499505
500506
"""
@@ -507,12 +513,13 @@ class Eddy(FSLCommand):
507513
def __init__(self, **inputs):
508514
super(Eddy, self).__init__(**inputs)
509515
self.inputs.on_trait_change(self._num_threads_update, 'num_threads')
510-
if isdefined(self.inputs.use_cuda):
511-
self._use_cuda()
512516
if not isdefined(self.inputs.num_threads):
513517
self.inputs.num_threads = self._num_threads
514518
else:
515519
self._num_threads_update()
520+
self.inputs.on_trait_change(self._use_cuda, 'use_cuda')
521+
if isdefined(self.inputs.use_cuda):
522+
self._use_cuda()
516523

517524
def _num_threads_update(self):
518525
self._num_threads = self.inputs.num_threads
@@ -524,10 +531,21 @@ def _num_threads_update(self):
524531
self.inputs.num_threads)
525532

526533
def _use_cuda(self):
527-
if self.inputs.use_cuda:
528-
_cmd = 'eddy_cuda'
529-
else:
530-
_cmd = 'eddy_openmp'
534+
self._cmd = 'eddy_cuda' if self.inputs.use_cuda else 'eddy_openmp'
535+
536+
def _run_interface(self, runtime):
537+
# If 'eddy_openmp' is missing, use 'eddy'
538+
FSLDIR = os.getenv('FSLDIR', '')
539+
cmd = self._cmd
540+
if all((FSLDIR != '',
541+
cmd == 'eddy_openmp',
542+
not os.path.exists(os.path.join(FSLDIR, cmd)))):
543+
self._cmd = 'eddy'
544+
runtime = super(Eddy, self)._run_interface(runtime)
545+
546+
# Restore command to avoid side-effects
547+
self._cmd = cmd
548+
return runtime
531549

532550
def _format_arg(self, name, spec, value):
533551
if name == 'in_topup_fieldcoef':
@@ -551,7 +569,8 @@ def _list_outputs(self):
551569
out_restricted_movement_rms = os.path.abspath(
552570
'%s.eddy_restricted_movement_rms' % self.inputs.out_base)
553571
out_shell_alignment_parameters = os.path.abspath(
554-
'%s.eddy_post_eddy_shell_alignment_parameters' % self.inputs.out_base)
572+
'%s.eddy_post_eddy_shell_alignment_parameters'
573+
% self.inputs.out_base)
555574
out_outlier_report = os.path.abspath(
556575
'%s.eddy_outlier_report' % self.inputs.out_base)
557576

@@ -560,9 +579,11 @@ def _list_outputs(self):
560579
if os.path.exists(out_movement_rms):
561580
outputs['out_movement_rms'] = out_movement_rms
562581
if os.path.exists(out_restricted_movement_rms):
563-
outputs['out_restricted_movement_rms'] = out_restricted_movement_rms
582+
outputs['out_restricted_movement_rms'] = \
583+
out_restricted_movement_rms
564584
if os.path.exists(out_shell_alignment_parameters):
565-
outputs['out_shell_alignment_parameters'] = out_shell_alignment_parameters
585+
outputs['out_shell_alignment_parameters'] = \
586+
out_shell_alignment_parameters
566587
if os.path.exists(out_outlier_report):
567588
outputs['out_outlier_report'] = out_outlier_report
568589

0 commit comments

Comments
 (0)