Skip to content

Commit 33f5db1

Browse files
committed
Replace eddy_openmp at runtime if not in FSLDIR
Set _use_cuda to run on trait change Update doctest to show use_cuda behavior
1 parent 84bce14 commit 33f5db1

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

nipype/interfaces/fsl/epi.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import nibabel as nb
2222
import warnings
2323

24-
from ... import logging
2524
from ...utils.filemanip import split_filename
2625
from ...utils import NUMPY_MMAP
2726

@@ -492,13 +491,20 @@ class Eddy(FSLCommand):
492491
>>> eddy.inputs.in_acqp = 'epi_acqp.txt'
493492
>>> eddy.inputs.in_bvec = 'bvecs.scheme'
494493
>>> eddy.inputs.in_bval = 'bvals.scheme'
494+
>>> eddy.inputs.use_cuda = True
495495
>>> eddy.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
496-
'eddy --acqp=epi_acqp.txt --bvals=bvals.scheme --bvecs=bvecs.scheme \
496+
'eddy_cuda --acqp=epi_acqp.txt --bvals=bvals.scheme --bvecs=bvecs.scheme \
497497
--imain=epi.nii --index=epi_index.txt --mask=epi_mask.nii \
498498
--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'
499504
>>> res = eddy.run() # doctest: +SKIP
500505
501506
"""
507+
_cmd = 'eddy_openmp'
502508
input_spec = EddyInputSpec
503509
output_spec = EddyOutputSpec
504510

@@ -511,6 +517,9 @@ def __init__(self, **inputs):
511517
self.inputs.num_threads = self._num_threads
512518
else:
513519
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()
514523

515524
def _num_threads_update(self):
516525
self._num_threads = self.inputs.num_threads
@@ -521,27 +530,22 @@ def _num_threads_update(self):
521530
self.inputs.environ['OMP_NUM_THREADS'] = str(
522531
self.inputs.num_threads)
523532

524-
@staticmethod
525-
def _eddycmd(use_cuda):
526-
logger = logging.getLogger('interface')
527-
if 'FSLDIR' not in os.environ:
528-
logger.warn("FSLDIR not set: assuming command 'eddy'")
529-
return 'eddy'
530-
531-
FSLDIR = os.environ['FSLDIR']
532-
if use_cuda and os.path.exists(os.path.join(FSLDIR, 'eddy_cuda')):
533-
return 'eddy_cuda'
534-
elif os.path.exists(os.path.join(FSLDIR, 'eddy_openmp')):
535-
return 'eddy_openmp'
536-
elif not os.path.exists(os.path.join(FSLDIR, 'eddy')):
537-
logger.warn("No eddy binary found in FSLDIR; assuming command "
538-
"'eddy'\nFSLDIR: '{}'".format(FSLDIR))
539-
return 'eddy'
540-
541-
@property
542-
def _cmd(self):
543-
return self._eddycmd(isdefined(self.inputs.use_cuda) and
544-
self.inputs.use_cuda)
533+
def _use_cuda(self):
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
545549

546550
def _format_arg(self, name, spec, value):
547551
if name == 'in_topup_fieldcoef':

0 commit comments

Comments
 (0)