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

RF: Loaded wrapper outputs are named sensibly when possible

parent 96736732
No related branches found
No related tags found
No related merge requests found
...@@ -604,8 +604,10 @@ class FileOrThing(object): ...@@ -604,8 +604,10 @@ class FileOrThing(object):
arguments that were set to :data:`LOAD`. arguments that were set to :data:`LOAD`.
:arg load: Function which is called to load items for arguments :arg load: Function which is called to load items for arguments
that were set to :data:`LOAD`. Must accept a file path that were set to :data:`LOAD`. Must accept the
as its sole argument. following arguments:
- the name of the argument
- path to the file to be loaded
:arg removeExt: Function which can remove a file extension from a file :arg removeExt: Function which can remove a file extension from a file
path. path.
...@@ -760,7 +762,7 @@ class FileOrThing(object): ...@@ -760,7 +762,7 @@ class FileOrThing(object):
- A dictionary of ``{ name : filename }`` mappings, - A dictionary of ``{ name : filename }`` mappings,
for all arguments with a value of ``LOAD``. for all arguments with a value of ``LOAD``.
- A dictionary ``{ filepat : replstr }`` paths, for - A dictionary of ``{ filepat : replstr }`` paths, for
all output-prefix arguments with a value of ``LOAD``. all output-prefix arguments with a value of ``LOAD``.
""" """
...@@ -925,7 +927,7 @@ class FileOrThing(object): ...@@ -925,7 +927,7 @@ class FileOrThing(object):
log.debug('Loading output %s: %s', oname, ofile) log.debug('Loading output %s: %s', oname, ofile)
if op.exists(ofile): oval = self.__load(ofile) if op.exists(ofile): oval = self.__load(oname, ofile)
else: oval = None else: oval = None
result[oname] = oval result[oname] = oval
...@@ -953,21 +955,23 @@ class FileOrThing(object): ...@@ -953,21 +955,23 @@ class FileOrThing(object):
log.debug('Loading prefixed output %s [%s]: %s', log.debug('Loading prefixed output %s [%s]: %s',
prefPat, prefName, prefixed) prefPat, prefName, prefixed)
noext = self.__removeExt(prefixed)
prefPat = prefPat.replace('\\', '\\\\')
noext = re.sub('^' + prefPat, prefName, noext)
withext = re.sub('^' + prefPat, prefName, prefixed)
# if the load function returns # if the load function returns
# None, this file is probably # None, this file is probably
# not of the correct type. # not of the correct type.
fval = self.__load(fullpath) fval = self.__load(noext, fullpath)
if fval is not None: if fval is not None:
noext = self.__removeExt(prefixed)
prefPat = prefPat.replace('\\', '\\\\')
noext = re.sub('^' + prefPat, prefName, noext)
# If there is already an item in result with the # If there is already an item in result with the
# name (stripped of prefix), then instead store # name (stripped of prefix), then instead store
# the result with the full prefixed name # the result with the full prefixed name
if noext not in result: if noext not in result:
result[noext] = fval result[noext] = fval
else: else:
withext = re.sub('^' + prefPat, prefName, prefixed)
result[withext] = fval result[withext] = fval
break break
...@@ -1014,7 +1018,7 @@ def fileOrImage(*args, **kwargs): ...@@ -1014,7 +1018,7 @@ def fileOrImage(*args, **kwargs):
def prepOut(workdir, name, val): def prepOut(workdir, name, val):
return op.join(workdir, '{}.nii.gz'.format(name)) return op.join(workdir, '{}.nii.gz'.format(name))
def load(path): def load(name, path):
if not fslimage.looksLikeImage(path): if not fslimage.looksLikeImage(path):
return None return None
...@@ -1027,7 +1031,8 @@ def fileOrImage(*args, **kwargs): ...@@ -1027,7 +1031,8 @@ def fileOrImage(*args, **kwargs):
# if any arguments were fsl images, # if any arguments were fsl images,
# that takes precedence. # that takes precedence.
if fslimage.Image in intypes: if fslimage.Image in intypes:
return fslimage.Image(data, header=img.header) return fslimage.Image(data, header=img.header, name=name)
# but if all inputs were file names, # but if all inputs were file names,
# nibabel takes precedence # nibabel takes precedence
elif nib.nifti1.Nifti1Image in intypes or len(intypes) == 0: elif nib.nifti1.Nifti1Image in intypes or len(intypes) == 0:
...@@ -1076,7 +1081,7 @@ def fileOrArray(*args, **kwargs): ...@@ -1076,7 +1081,7 @@ def fileOrArray(*args, **kwargs):
def prepOut(workdir, name, val): def prepOut(workdir, name, val):
return op.join(workdir, '{}.txt'.format(name)) return op.join(workdir, '{}.txt'.format(name))
def load(path): def load(_, path):
try: return np.loadtxt(path) try: return np.loadtxt(path)
except Exception: return None except Exception: return None
...@@ -1141,7 +1146,7 @@ def fileOrText(*args, **kwargs): ...@@ -1141,7 +1146,7 @@ def fileOrText(*args, **kwargs):
def prepOut(workdir, name, val): def prepOut(workdir, name, val):
return op.join(workdir, '{}.txt'.format(name)) return op.join(workdir, '{}.txt'.format(name))
def load(path): def load(_, path):
try: try:
with open(path, "r") as f: with open(path, "r") as f:
return f.read() return f.read()
......
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