Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
fslpy
Commits
26b41155
Commit
26b41155
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: _FileOrThing supports output prefix with a directory
parent
fbec4cbe
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/wrappers/wrapperutils.py
+105
-72
105 additions, 72 deletions
fsl/wrappers/wrapperutils.py
with
105 additions
and
72 deletions
fsl/wrappers/wrapperutils.py
+
105
−
72
View file @
26b41155
...
...
@@ -85,20 +85,22 @@ and returned::
"""
import
os.path
as
op
import
os
import
sys
import
glob
import
shutil
import
random
import
string
import
fnmatch
import
inspect
import
logging
import
tempfile
import
warnings
import
functools
import
collections
import
itertools
as
it
import
os.path
as
op
import
os
import
re
import
sys
import
glob
import
shutil
import
random
import
string
import
fnmatch
import
inspect
import
logging
import
tempfile
import
warnings
import
functools
import
collections
import
six
import
nibabel
as
nib
...
...
@@ -585,66 +587,13 @@ class _FileOrThing(object):
# Replace any things with file names.
# Also get a list of LOAD outputs
args
=
self
.
__prepareArgs
(
td
,
argnames
,
args
,
kwargs
)
args
,
kwargs
,
baseP
refix
,
outfiles
,
prefixes
=
args
args
,
kwargs
,
outp
refix
,
outfiles
,
prefixes
=
args
# Call the function
result
=
func
(
*
args
,
**
kwargs
)
# make a _Results object to store
# the output. If we are decorating
# another _FileOrThing, the
# results will get merged together
# into a single _Results dict.
if
not
isinstance
(
result
,
_FileOrThing
.
_Results
):
result
=
_FileOrThing
.
_Results
(
result
)
# Load the LOADed outputs
for
oname
,
ofile
in
outfiles
.
items
():
log
.
debug
(
'
Loading output %s: %s
'
,
oname
,
ofile
)
if
op
.
exists
(
ofile
):
oval
=
self
.
__load
(
ofile
)
else
:
oval
=
None
result
[
oname
]
=
oval
# Load or move output-prefixed files
if
basePrefix
is
not
None
:
prefixDir
=
op
.
abspath
(
op
.
dirname
(
basePrefix
))
basePrefix
=
op
.
basename
(
basePrefix
)
allPrefixed
=
glob
.
glob
(
op
.
join
(
td
,
'
{}*
'
.
format
(
basePrefix
)))
for
filename
in
allPrefixed
:
basename
=
op
.
basename
(
filename
)
for
prefPat
,
prefName
in
prefixes
.
items
():
if
fnmatch
.
fnmatch
(
basename
,
'
{}*
'
.
format
(
prefPat
)):
log
.
debug
(
'
Loading prefixed output %s [%s]: %s
'
,
prefPat
,
prefName
,
filename
)
# if the load function returns
# None, this file is probably
# not of the correct type.
fval
=
self
.
__load
(
filename
)
if
fval
is
not
None
:
basename
=
self
.
__removeExt
(
basename
)
basename
=
basename
.
replace
(
prefPat
,
prefName
)
result
[
basename
]
=
fval
break
# if file did not match any pattern,
# move it into the real prefix, where
# the function would have saved it
# if we had not modified the prefix
# (see __prepareArgs)
else
:
log
.
debug
(
'
Moving prefixed output %s into %s
'
,
filename
,
prefixDir
)
shutil
.
move
(
filename
,
prefixDir
)
return
result
return
self
.
__generateResult
(
td
,
result
,
outprefix
,
outfiles
,
prefixes
)
def
__prepareArgs
(
self
,
workdir
,
argnames
,
args
,
kwargs
):
...
...
@@ -718,8 +667,14 @@ class _FileOrThing(object):
prefixedFiles
[
prefix
]
=
self
.
__outprefix
realPrefix
=
prefix
prefix
=
op
.
basename
(
prefix
)
allargs
[
self
.
__outprefix
]
=
op
.
join
(
workdir
,
prefix
)
fakePrefix
=
op
.
join
(
workdir
,
prefix
)
allargs
[
self
.
__outprefix
]
=
fakePrefix
pdir
=
op
.
dirname
(
fakePrefix
)
if
pdir
!=
''
and
not
op
.
exists
(
pdir
):
os
.
makedirs
(
pdir
)
if
len
(
self
.
__things
)
>
0
:
things
=
self
.
__things
else
:
things
=
allargs
.
keys
()
...
...
@@ -771,12 +726,90 @@ class _FileOrThing(object):
if
infile
is
not
None
:
allargs
[
name
]
=
infile
if
realPrefix
is
not
None
and
len
(
prefixedFiles
)
==
0
:
allargs
[
self
.
__outprefix
]
=
realPrefix
args
=
[
allargs
.
pop
(
k
)
for
k
in
argnames
]
kwargs
=
allargs
return
args
,
kwargs
,
realPrefix
,
outfiles
,
prefixedFiles
def
__generateResult
(
self
,
workdir
,
result
,
outprefix
,
outfiles
,
prefixes
):
"""
Loads function outputs and returns a :class:`_Results` object.
Called by :meth:`__call__` after the decorated function has been
called. Figures out what files should be loaded, and loads them into
a ``_Results`` object.
:arg workdir: Directory which contains the function outputs.
:arg result: Function return value.
:arg outprefix: Original output prefix that was passed into the
function (or ``None`` if one wasn
'
t passed)
:arg outfiles: Dictionary containing output files to be loaded (see
:meth:`__prepareArgs`).
:arg prefixes: Dictionary containing output-prefix patterns to be
loaded (see :meth:`__prepareArgs`).
:returns: A ``_Results`` object containing all loaded outputs.
"""
# make a _Results object to store
# the output. If we are decorating
# another _FileOrThing, the
# results will get merged together
# into a single _Results dict.
if
not
isinstance
(
result
,
_FileOrThing
.
_Results
):
result
=
_FileOrThing
.
_Results
(
result
)
# Load the LOADed outputs
for
oname
,
ofile
in
outfiles
.
items
():
log
.
debug
(
'
Loading output %s: %s
'
,
oname
,
ofile
)
if
op
.
exists
(
ofile
):
oval
=
self
.
__load
(
ofile
)
else
:
oval
=
None
result
[
oname
]
=
oval
# No output prefix - we're done
if
outprefix
is
None
or
len
(
prefixes
)
==
0
:
return
result
# Load or move output-prefixed files.
# Find all files with a name that
# matches the prefix that was passed
# in (recursing into matching sub-
# directories too).
allPrefixed
=
glob
.
glob
(
op
.
join
(
workdir
,
'
{}*
'
.
format
(
outprefix
)))
allPrefixed
=
[
fslpath
.
allFiles
(
f
)
if
op
.
isdir
(
f
)
else
[
f
]
for
f
in
allPrefixed
]
for
prefixed
in
it
.
chain
(
*
allPrefixed
):
fullpath
=
prefixed
prefixed
=
op
.
relpath
(
prefixed
,
workdir
)
for
prefPat
,
prefName
in
prefixes
.
items
():
if
not
fnmatch
.
fnmatch
(
prefixed
,
'
{}*
'
.
format
(
prefPat
)):
continue
log
.
debug
(
'
Loading prefixed output %s [%s]: %s
'
,
prefPat
,
prefName
,
prefixed
)
# if the load function returns
# None, this file is probably
# not of the correct type.
fval
=
self
.
__load
(
fullpath
)
if
fval
is
not
None
:
prefixed
=
self
.
__removeExt
(
prefixed
)
prefixed
=
re
.
sub
(
'
^
'
+
prefPat
,
prefName
,
prefixed
)
result
[
prefixed
]
=
fval
break
return
result
def
fileOrImage
(
*
args
,
**
kwargs
):
"""
Decorator which can be used to ensure that any NIfTI images are saved
to file, and output images can be loaded and returned as ``nibabel``
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment