Skip to content
Snippets Groups Projects
Commit 35aa3ba7 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF,ENH: fileOrThing uses file basename as names in results dictionary for

prefixed-outputs. Bet wrapper uses new outprefix option.
parent 547512ad
No related branches found
No related tags found
No related merge requests found
...@@ -14,7 +14,7 @@ import fsl.utils.assertions as asrt ...@@ -14,7 +14,7 @@ import fsl.utils.assertions as asrt
from . import wrapperutils as wutils from . import wrapperutils as wutils
@wutils.fileOrImage('input', 'output') @wutils.fileOrImage('input', 'output', outprefix='output')
@wutils.fslwrapper @wutils.fslwrapper
def bet(input, output, **kwargs): def bet(input, output, **kwargs):
"""Wrapper for the ``bet`` command. """Wrapper for the ``bet`` command.
......
...@@ -102,8 +102,9 @@ import six ...@@ -102,8 +102,9 @@ import six
import nibabel as nib import nibabel as nib
import numpy as np import numpy as np
import fsl.utils.tempdir as tempdir
import fsl.utils.run as run import fsl.utils.run as run
import fsl.utils.path as fslpath
import fsl.utils.tempdir as tempdir
import fsl.data.image as fslimage import fsl.data.image as fslimage
...@@ -504,7 +505,14 @@ class _FileOrThing(object): ...@@ -504,7 +505,14 @@ class _FileOrThing(object):
return self.__output return self.__output
def __init__(self, func, prepIn, prepOut, load, *things, outprefix=None): def __init__(self,
func,
prepIn,
prepOut,
load,
removeExt,
*things,
outprefix=None):
"""Initialise a ``_FileOrThing`` decorator. """Initialise a ``_FileOrThing`` decorator.
:arg func: The function to be decorated. :arg func: The function to be decorated.
...@@ -519,6 +527,9 @@ class _FileOrThing(object): ...@@ -519,6 +527,9 @@ class _FileOrThing(object):
that were set to :data:`LOAD`. Must accept a file path that were set to :data:`LOAD`. Must accept a file path
as its sole argument. as its sole argument.
:arg removeExt: Function which can remove a file extension from a file
path.
:arg things: Names of all arguments which will be handled by :arg things: Names of all arguments which will be handled by
this ``_FileOrThing`` decorator. If not provided, this ``_FileOrThing`` decorator. If not provided,
*all* arguments passed to the function will be *all* arguments passed to the function will be
...@@ -526,9 +537,8 @@ class _FileOrThing(object): ...@@ -526,9 +537,8 @@ class _FileOrThing(object):
:arg outprefix: The name of a positional or keyword argument to the :arg outprefix: The name of a positional or keyword argument to the
function, which specifies an output file name prefix. function, which specifies an output file name prefix.
All other arguments which begin with this prefix ( All other arguments with names that begin with this
more specifically, which begin with ``[prefix]_``) prefix may be interpreted as things to ``LOAD``.
may be interpreted as things to load.
The ``prepIn`` and ``prepOut`` functions must accept the following The ``prepIn`` and ``prepOut`` functions must accept the following
positional arguments: positional arguments:
...@@ -544,6 +554,7 @@ class _FileOrThing(object): ...@@ -544,6 +554,7 @@ class _FileOrThing(object):
self.__prepIn = prepIn self.__prepIn = prepIn
self.__prepOut = prepOut self.__prepOut = prepOut
self.__load = load self.__load = load
self.__removeExt = removeExt
self.__things = things self.__things = things
self.__outprefix = outprefix self.__outprefix = outprefix
...@@ -570,7 +581,7 @@ class _FileOrThing(object): ...@@ -570,7 +581,7 @@ class _FileOrThing(object):
# Replace any things with file names. # Replace any things with file names.
# Also get a list of LOAD outputs # Also get a list of LOAD outputs
args = self.__prepareArgs(td, argnames, args, kwargs) args = self.__prepareArgs(td, argnames, args, kwargs)
args, kwargs, prefix, outfiles, prefixedFiles = args args, kwargs, basePrefix, outfiles, prefixes = args
# Call the function # Call the function
result = func(*args, **kwargs) result = func(*args, **kwargs)
...@@ -594,29 +605,31 @@ class _FileOrThing(object): ...@@ -594,29 +605,31 @@ class _FileOrThing(object):
result[oname] = oval result[oname] = oval
# Load or move output-prefixed files # Load or move output-prefixed files
if prefix is not None: if basePrefix is not None:
prefixDir = op.abspath(op.dirname(prefix)) prefixDir = op.abspath(op.dirname(basePrefix))
prefix = op.basename(prefix) basePrefix = op.basename(basePrefix)
allPrefixed = glob.glob(op.join(td, '{}_*'.format(prefix))) allPrefixed = glob.glob(op.join(td, '{}*'.format(basePrefix)))
for filename in allPrefixed: for filename in allPrefixed:
basename = op.basename(filename) basename = op.basename(filename)
for argname in prefixedFiles: for prefix in prefixes:
if fnmatch.fnmatch(basename, '{}*'.format(argname)): if fnmatch.fnmatch(basename, '{}*'.format(prefix)):
log.debug('Loading prefixed output %s: %s', log.debug('Loading prefixed output %s: %s',
argname, filename) prefix, filename)
fval = self.__load(filename)
if argname in result: result[argname].append(fval) fval = self.__load(filename)
else: result[argname] = [fval] basename = self.__removeExt(basename)
result[basename] = fval
break break
# if file did not match any pattern, # if file did not match any pattern,
# move it into real prefix # move it into the real prefix, where
# the function would have saved it
# if we had not modified the prefix
# (see __prepareArgs)
else: else:
log.debug('Moving prefixed output %s into %s', log.debug('Moving prefixed output %s into %s',
filename, prefixDir) filename, prefixDir)
...@@ -691,7 +704,7 @@ class _FileOrThing(object): ...@@ -691,7 +704,7 @@ class _FileOrThing(object):
for name, val in list(allargs.items()): for name, val in list(allargs.items()):
# is this argument referring # is this argument referring
# to a prefixd output? # to a prefixed output?
isprefixed = (prefix is not None and isprefixed = (prefix is not None and
name.startswith(prefix)) name.startswith(prefix))
...@@ -722,6 +735,7 @@ class _FileOrThing(object): ...@@ -722,6 +735,7 @@ class _FileOrThing(object):
outfiles[name] = outfile outfiles[name] = outfile
allargs[ name] = outfile allargs[ name] = outfile
# Assumed to be an input file
else: else:
infile = self.__prepIn(workdir, name, val) infile = self.__prepIn(workdir, name, val)
...@@ -791,7 +805,13 @@ def fileOrImage(*args, **kwargs): ...@@ -791,7 +805,13 @@ def fileOrImage(*args, **kwargs):
raise RuntimeError('Cannot handle type: {}'.format(intypes)) raise RuntimeError('Cannot handle type: {}'.format(intypes))
def decorator(func): def decorator(func):
fot = _FileOrThing(func, prepIn, prepOut, load, *args, **kwargs) fot = _FileOrThing(func,
prepIn,
prepOut,
load,
fslimage.removeExt,
*args,
**kwargs)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
result = fot(*args, **kwargs) result = fot(*args, **kwargs)
...@@ -825,7 +845,13 @@ def fileOrArray(*args, **kwargs): ...@@ -825,7 +845,13 @@ def fileOrArray(*args, **kwargs):
load = np.loadtxt load = np.loadtxt
def decorator(func): def decorator(func):
fot = _FileOrThing(func, prepIn, prepOut, load, *args, **kwargs) fot = _FileOrThing(func,
prepIn,
prepOut,
load,
fslpath.removeExt,
*args,
**kwargs)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
return fot(*args, **kwargs) return fot(*args, **kwargs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment