Commit 0258fda1 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

fsl.utils.run.dryrun() calls will now return the command line. Useful for

testing. Tweaks to various wrapper functions. fslmaths returns results dict,
rather than file name (for testing).
parent 435141a8
...@@ -45,6 +45,8 @@ class FSLNotPresent(Exception): ...@@ -45,6 +45,8 @@ class FSLNotPresent(Exception):
def dryrun(*args): def dryrun(*args):
"""Context manager which causes all calls to :func:`run` to be logged but """Context manager which causes all calls to :func:`run` to be logged but
not executed. See the :data:`DRY_RUN` flag. not executed. See the :data:`DRY_RUN` flag.
The returned standard output will be equal to ``' '.join(args)``.
""" """
global DRY_RUN global DRY_RUN
...@@ -106,7 +108,7 @@ def run(*args, **kwargs): ...@@ -106,7 +108,7 @@ def run(*args, **kwargs):
log.debug('run: {}'.format(' '.join(args))) log.debug('run: {}'.format(' '.join(args)))
if DRY_RUN: if DRY_RUN:
stdout = '<dryrun>' stdout = ' '.join(args)
stderr = '' stderr = ''
else: else:
proc = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE) proc = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE)
......
...@@ -151,7 +151,7 @@ class fslmaths(object): ...@@ -151,7 +151,7 @@ class fslmaths(object):
# will only be one entry in # will only be one entry in
# the result dict. # the result dict.
if output is None: return list(result.values())[0] if output is None: return list(result.values())[0]
else: return output else: return result
@wutils.fileOrImage() @wutils.fileOrImage()
@wutils.fslwrapper @wutils.fslwrapper
......
...@@ -17,9 +17,13 @@ from . import wrapperutils as wutils ...@@ -17,9 +17,13 @@ from . import wrapperutils as wutils
@wutils.fileOrImage('in', 'unwarp', 'warp', 'phasemap', 'savefmap', @wutils.fileOrImage('in', 'unwarp', 'warp', 'phasemap', 'savefmap',
'loadfmap', 'saveshift', 'loadshift', 'mask') 'loadfmap', 'saveshift', 'loadshift', 'mask')
@wutils.fslwrapper @wutils.fslwrapper
def fugue(**kwargs): def fugue(input=None, **kwargs):
"""Wrapper for the ``fugue`` command.""" """Wrapper for the ``fugue`` command."""
argmap = {
'input' : 'in'
}
valmap = { valmap = {
'dwelltoasym' : wutils.SHOW_IF_TRUE, 'dwelltoasym' : wutils.SHOW_IF_TRUE,
'median' : wutils.SHOW_IF_TRUE, 'median' : wutils.SHOW_IF_TRUE,
...@@ -37,7 +41,12 @@ def fugue(**kwargs): ...@@ -37,7 +41,12 @@ def fugue(**kwargs):
'verbose' : wutils.SHOW_IF_TRUE, 'verbose' : wutils.SHOW_IF_TRUE,
} }
cmd = ['fugue'] + wutils.applyArgStyle('--=', valmap=valmap, **kwargs) kwargs.update({'input' : input})
cmd = ['fugue'] + wutils.applyArgStyle('--=',
argmap=argmap,
valmap=valmap,
**kwargs)
return cmd return cmd
......
...@@ -52,7 +52,7 @@ def melodic(input, **kwargs): ...@@ -52,7 +52,7 @@ def melodic(input, **kwargs):
asrt.assertIsNifti(input) asrt.assertIsNifti(input)
cmd = ['melodic', '--in', input] cmd = ['melodic', '--in={}'.format(input)]
cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs) cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs)
return cmd return cmd
...@@ -77,6 +77,6 @@ def fsl_regfilt(input, out, design, **kwargs): ...@@ -77,6 +77,6 @@ def fsl_regfilt(input, out, design, **kwargs):
'--in={}'.format(input), '--in={}'.format(input),
'--out={}'.format(out), '--out={}'.format(out),
'--design={}'.format(design)] '--design={}'.format(design)]
cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs) cmd += wutils.applyArgStyle('--=', valsep=',', valmap=valmap, **kwargs)
return cmd return cmd
...@@ -31,49 +31,33 @@ def fslreorient2std(input, output=None): ...@@ -31,49 +31,33 @@ def fslreorient2std(input, output=None):
@wutils.fileOrImage('input', 'output') @wutils.fileOrImage('input', 'output')
@wutils.fslwrapper @wutils.fslwrapper
def fslroi(input, output, xmin=None, xsize=None, ymin=None, ysize=None, def fslroi(input, output, *args):
zmin=None, zsize=None, tmin=None, tsize=None):
"""Wrapper for the ``fslroi`` tool.""" """Wrapper for the ``fslroi`` tool."""
assert ((tmin is not None and tsize is not None) or
(xmin is not None and xsize is not None and
ymin is not None and ysize is not None and
zmin is not None and zsize is not None)), \
"either time min/size or x/y/z min/size must be provided"
cmd = ['fslroi', input, output] asrt.assertIsNifti(input)
if xmin is not None: cmd = ['fslroi', input, output] + [str(a) for a in args]
cmd += [str(v) for v in [xmin, xsize, ymin, ysize, zmin, zsize]]
if tmin is not None:
cmd += [str(tmin), str(tsize)]
return cmd return cmd
@wutils.fileOrImage('input', 'input2') @wutils.fileOrImage('input', 'input2')
@wutils.fslwrapper @wutils.fslwrapper
def slicer(input, input2=None, label=None, lut=None, intensity=None, def slicer(input, input2=None, **kwargs):
edgethreshold=None, x=None, y=None, z=None):
"""Wrapper for the ``slicer`` command. """ """Wrapper for the ``slicer`` command. """
cmd = "slicer {0}".format(input) cmd = ['slicer', input]
if input2 is not None: if input2 is not None:
cmd += " {0}".format(input2) cmd.append(input2)
if label is not None:
cmd += " -L {0}".format(label) # slicer output options must be
if lut is not None: # applied after other options
cmd += " -l {0}".format(lut) outopts = ['x', 'y', 'z', 'a', 'A', 'S']
if intensity is not None: outargs = { k : kwargs.pop(k) for k in outopts if k in kwargs}
cmd += " -i {0} {1}".format(intensity[0], intensity[1])
if edgethreshold is not None: cmd = cmd + wutils.applyArgStyle('-', **kwargs) + \
cmd += " -e {0}".format(edgethreshold) wutils.applyArgStyle('-', **outargs)
if x is not None:
cmd += " -x {0} {1}".format(x[0], x[1])
if y is not None:
cmd += " -y {0} {1}".format(y[0], y[1])
if z is not None:
cmd += " -z {0} {1}".format(z[0], z[1])
return cmd return cmd
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment