From 82083da262a532a3c2d8959559a60901ca98decf Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Fri, 2 Mar 2018 15:56:38 +0000
Subject: [PATCH] Cleaned up eddy and fugue modules. Add sean as author.

---
 fsl/wrappers/bet.py   |   1 +
 fsl/wrappers/eddy.py  | 168 +++++++++++++++++-------------------------
 fsl/wrappers/flirt.py |   1 +
 fsl/wrappers/fugue.py |  59 +++++++++------
 4 files changed, 104 insertions(+), 125 deletions(-)

diff --git a/fsl/wrappers/bet.py b/fsl/wrappers/bet.py
index 93f1e9ed8..1eadd23d8 100644
--- a/fsl/wrappers/bet.py
+++ b/fsl/wrappers/bet.py
@@ -2,6 +2,7 @@
 #
 # bet.py - Wrapper for the FSL bet command.
 #
+# Author: Sean Fitzgibbon <sean.fitzgibbon@ndcn.ox.ac.uk>
 # Author: Paul McCarthy <pauldmccarthy@gmail.com>
 #
 """This module provides the :func:`bet` function, a wrapper for the FSL
diff --git a/fsl/wrappers/eddy.py b/fsl/wrappers/eddy.py
index 1081edb6d..c5d272ad2 100644
--- a/fsl/wrappers/eddy.py
+++ b/fsl/wrappers/eddy.py
@@ -1,113 +1,79 @@
 #!/usr/bin/env python
 #
-# eddy.py -
+# eddy.py - Wrappers for topup and eddy.
 #
+# Author: Sean Fitzgibbon <sean.fitzgibbon@ndcn.ox.ac.uk>
 # Author: Paul McCarthy <pauldmccarthy@gmail.com>
 #
+"""This module provides wrapper functions for the FSL `TOPUP
+<https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/topup>`_ and `EDDY
+<https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/eddy>`_ tools, for field map
+estimation and eddy-current distortion correction.
 
+.. autosummary::
+   :nosignatures:
+
+   eddy_cuda
+   topup
+"""
+
+
+import fsl.utils.run        as run
+import fsl.utils.assertions as asrt
+from . import wrapperutils  as wutils
+
+
+@wutils.fileOrImage('imain', 'mask', 'field')
+@wutils.fileOrArray('index', 'acqp', 'bvecs', 'bvals', 'field_mat')
+def eddy_cuda(imain, mask, index, acqp, bvecs, bvals, out, **kwargs):
+    """Wrapper for the ``eddy_cuda`` command."""
+
+    valmap = {
+        'fep'                             : wutils.SHOW_IF_TRUE,
+        'initrand'                        : wutils.SHOW_IF_TRUE,
+        'repol'                           : wutils.SHOW_IF_TRUE,
+        'ol_pos'                          : wutils.SHOW_IF_TRUE,
+        'ol_sqr'                          : wutils.SHOW_IF_TRUE,
+        'dont_sep_offs_move'              : wutils.SHOW_IF_TRUE,
+        'dont_peas'                       : wutils.SHOW_IF_TRUE,
+        'data_is_shelled'                 : wutils.SHOW_IF_TRUE,
+        'b0_only'                         : wutils.SHOW_IF_TRUE,
+        'dont_mask_output'                : wutils.SHOW_IF_TRUE,
+        'cnr_maps'                        : wutils.SHOW_IF_TRUE,
+        'residuals'                       : wutils.SHOW_IF_TRUE,
+        'estimate_move_by_susceptibility' : wutils.SHOW_IF_TRUE,
+        'verbose'                         : wutils.SHOW_IF_TRUE,
+        'very_verbose'                    : wutils.SHOW_IF_TRUE,
+    }
 
-def eddy_cuda(imain, mask, index, acqp, bvecs, bvals, out, very_verbose=False,
-              niter=None, fwhm=None, s2v_niter=None, mporder=None, nvoxhp=None,
-              slspec=None, b0_only=False, topup=None, field=None,
-              field_mat=None, debug=None, s2v_fwhm=None, interp=None,
-              dont_mask_output=False, s2v_interp=None, ref_scan_no=None,
-              data_is_shelled=False, estimate_move_by_susceptibility=False,
-              mbs_niter=None, mbs_lambda=None, mbs_ksp=None, s2v_lambda=None,
-              cnr_maps=False, residuals=False):
-    """Correct eddy current-induced distortions and subject movements."""
     asrt.assertFileExists(imain, mask, index, acqp, bvecs, bvals)
     asrt.assertIsNifti(imain, mask)
 
-    assert not (topup and field), "topup and field arguments are incompatible"
-
-    out = img.splitext(out)[0]
-
-    opts = " --imain={0} --mask={1} --index={2} --bvals={3} " \
-           "--bvecs={4} --acqp={5} --out={6}".format(imain, mask, index, bvals,
-                                                     bvecs, acqp, out)
-
-    cmd = op.join(os.getenv('DHCP_EDDY_PATH', ''), 'eddy_cuda')
-    # cmd = 'eddy_cuda'
-    cmd = cmd + opts
-
-    if very_verbose:
-        cmd += " --very_verbose"
-    if estimate_move_by_susceptibility:
-        cmd += " --estimate_move_by_susceptibility"
-    if data_is_shelled:
-        cmd += " --data_is_shelled"
-    if mbs_niter is not None:
-        cmd += " --mbs_niter={0}".format(mbs_niter)
-    if mbs_lambda is not None:
-        cmd += " --mbs_lambda={0}".format(mbs_lambda)
-    if mbs_ksp is not None:
-        cmd += " --mbs_ksp={0}".format(mbs_ksp)
-    if niter is not None:
-        cmd += " --niter={0}".format(niter)
-    if fwhm is not None:
-        cmd += " --fwhm={0}".format(fwhm)
-    if s2v_fwhm is not None:
-        cmd += " --s2v_fwhm={0}".format(s2v_fwhm)
-    if s2v_niter is not None:
-        cmd += " --s2v_niter={0}".format(s2v_niter)
-    if s2v_interp is not None:
-        cmd += " --s2v_interp={0}".format(s2v_interp)
-    if interp is not None:
-        cmd += " --interp={0}".format(interp)
-    if mporder is not None:
-        cmd += " --mporder={0}".format(mporder)
-    if nvoxhp is not None:
-        cmd += " --nvoxhp={0}".format(nvoxhp)
-    if slspec is not None:
-        cmd += " --slspec={0}".format(slspec)
-    if topup is not None:
-        cmd += " --topup={0}".format(topup)
-    if field is not None:
-        field = img.splitext(field)[0]
-        cmd += " --field={0}".format(field)
-    if b0_only:
-        cmd += " --b0_only"
-    if field_mat is not None:
-        cmd += " --field_mat={0}".format(field_mat)
-    if debug is not None:
-        cmd += " --debug={0}".format(debug)
-    if dont_mask_output:
-        cmd += " --dont_mask_output"
-    if ref_scan_no is not None:
-        cmd += " --ref_scan_no={0}".format(ref_scan_no)
-    if s2v_lambda is not None:
-        cmd += " --s2v_lambda={0}".format(s2v_lambda)
-    if cnr_maps:
-        cmd += " --cnr_maps"
-    if residuals:
-        cmd += " --residuals"
-
-    shops.run(cmd)
-
-
-
-
-def topup(imain, datain, config=None, fout=None, iout=None, out=None,
-          verbose=False, subsamp=None, logout=None):
-    """Estimate and correct susceptibility induced distortions."""
-    asrt.assertFileExists(imain, datain)
+    kwargs.update({'imain' : imain,
+                   'mask'  : mask,
+                   'index' : index,
+                   'acqp'  : acqp,
+                   'bvecs' : bvecs,
+                   'bvals' : bvals,
+                   'out'   : out})
+
+    cmd = ['eddy_cuda'] + wutils.applyArgStyle('--=', valmap=valmap, **kwargs)
+    return run.runfsl(cmd)
+
+
+@wutils.fileOrImage('imain', 'fout', 'iout')
+@wutils.fileOrArray('datain')
+def topup(imain, datain, **kwargs):
+    """Wrapper for the ``topup`` command."""
+
+    valmap = {
+        'verbose' : wutils.SHOW_IF_TRUE
+    }
+
+    asrt.assertFileExists(datain)
     asrt.assertIsNifti(imain)
 
-    cmd = "topup --imain={0} --datain={1}".format(imain, datain)
-
-    if config is not None:
-        cmd += " --config={0}".format(config)
-    if fout is not None:
-        cmd += " --fout={0}".format(fout)
-    if iout is not None:
-        cmd += " --iout={0}".format(iout)
-    if out is not None:
-        cmd += " --out={0}".format(out)
-    if subsamp is not None:
-        cmd += " --subsamp={0}".format(subsamp)
-    if logout is not None:
-        cmd += " --logout={0}".format(logout)
-    if verbose:
-        cmd += " -v"
-
-    shops.run(cmd)
+    cmd  = ['topup', '--imain={}'.format(imain), '--datain={}'.format(datain)]
+    cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs)
+
+    return run.runfsl(cmd)
diff --git a/fsl/wrappers/flirt.py b/fsl/wrappers/flirt.py
index b571aa3d6..7d3ef4769 100644
--- a/fsl/wrappers/flirt.py
+++ b/fsl/wrappers/flirt.py
@@ -2,6 +2,7 @@
 #
 # flirt.py - Wrappers for FLIRT commands.
 #
+# Author: Sean Fitzgibbon <sean.fitzgibbon@ndcn.ox.ac.uk>
 # Author: Paul McCarthy <pauldmccarthy@gmail.com>
 #
 """This module provides wrapper functions for the FSL `FLIRT
diff --git a/fsl/wrappers/fugue.py b/fsl/wrappers/fugue.py
index 298493beb..7a4cf8e5f 100644
--- a/fsl/wrappers/fugue.py
+++ b/fsl/wrappers/fugue.py
@@ -1,42 +1,53 @@
 #!/usr/bin/env python
 #
-# fugue.py -
+# fugue.py - Wrappers for fugue/field map tools.
 #
+# Author: Sean Fitzgibbon <sean.fitzgibbon@ndcn.ox.ac.uk>
 # Author: Paul McCarthy <pauldmccarthy@gmail.com>
 #
-"""
+"""This module contains wrappers for the FSL `FUGUE
+<https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE>`_ tools, for EPI field map
+processing and distortion correction.
 """
 
 
-import fsl.utils.run as run
+import fsl.utils.run       as run
+from . import wrapperutils as wutils
 
 
+@wutils.fileOrImage('in', 'unwarp', 'warp', 'phasemap', 'savefmap',
+                    'loadfmap', 'saveshift', 'loadshift', 'mask')
 def fugue(**kwargs):
-    """FMRIB's Utility for Geometric Unwarping of EPIs."""
-
-    cmd = "fugue"
-
-    if kwargs.pop('unmaskshift', False):
-        cmd += " --unmaskshift"
-    if kwargs.pop('despike', False):
-        cmd += " --despike"
-    if kwargs.pop('unmaskfmap', False):
-        cmd += " --unmaskfmap"
-
-    cmd += ' '.join(['--{}={}'.format(k, v) for k, v in kwargs.items()])
+    """Wrapper for the ``fugue`` command."""
+
+    valmap = {
+        'dwelltoasym' : wutils.SHOW_IF_TRUE,
+        'median'      : wutils.SHOW_IF_TRUE,
+        'despike'     : wutils.SHOW_IF_TRUE,
+        'nofill'      : wutils.SHOW_IF_TRUE,
+        'noextend'    : wutils.SHOW_IF_TRUE,
+        'pava'        : wutils.SHOW_IF_TRUE,
+        'phaseconj'   : wutils.SHOW_IF_TRUE,
+        'icorr'       : wutils.SHOW_IF_TRUE,
+        'icorronly'   : wutils.SHOW_IF_TRUE,
+        'unmaskfmap'  : wutils.SHOW_IF_TRUE,
+        'unmaskshift' : wutils.SHOW_IF_TRUE,
+        'nokspace'    : wutils.SHOW_IF_TRUE,
+        'nocheck'     : wutils.SHOW_IF_TRUE,
+        'verbose'     : wutils.SHOW_IF_TRUE,
+    }
+
+    cmd = ['fugue'] + wutils.applyArgStyle('--=',  valmap=valmap, **kwargs)
 
     return run.runfsl(cmd)
 
 
-def sigloss(input, output, te=None, slicedir=None, mask=None):
-    """Estimate signal loss from a field map (in rad/s)."""
-    cmd = "sigloss -i {0} -s {1}".format(input, output)
+def sigloss(input, sigloss, **kwargs):
+    """Wrapper for the ``sigloss`` command."""
+
+    valmap = {'verbose' : wutils.SHOW_IF_TRUE}
 
-    if te is not None:
-        cmd += " --te={0}".format(te)
-    if slicedir is not None:
-        cmd += " --slicedir={0}".format(slicedir)
-    if mask is not None:
-        cmd += " --mask={0}".format(mask)
+    cmd  = ['sigloss', '--in', input, '--sigloss', sigloss]
+    cmd += wutils.applyArgStyle('--', valmap=valmap, **kwargs)
 
     return run.runfsl(cmd)
-- 
GitLab