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
35aa3ba7
Commit
35aa3ba7
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
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
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fsl/wrappers/bet.py
+1
-1
1 addition, 1 deletion
fsl/wrappers/bet.py
fsl/wrappers/wrapperutils.py
+47
-21
47 additions, 21 deletions
fsl/wrappers/wrapperutils.py
with
48 additions
and
22 deletions
fsl/wrappers/bet.py
+
1
−
1
View file @
35aa3ba7
...
@@ -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.
...
...
This diff is collapsed.
Click to expand it.
fsl/wrappers/wrapperutils.py
+
47
−
21
View file @
35aa3ba7
...
@@ -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
,
p
refix
,
outfiles
,
prefixe
dFile
s
=
args
args
,
kwargs
,
baseP
refix
,
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
p
refix
is
not
None
:
if
baseP
refix
is
not
None
:
prefixDir
=
op
.
abspath
(
op
.
dirname
(
p
refix
))
prefixDir
=
op
.
abspath
(
op
.
dirname
(
baseP
refix
))
p
refix
=
op
.
basename
(
p
refix
)
baseP
refix
=
op
.
basename
(
baseP
refix
)
allPrefixed
=
glob
.
glob
(
op
.
join
(
td
,
'
{}
_
*
'
.
format
(
p
refix
)))
allPrefixed
=
glob
.
glob
(
op
.
join
(
td
,
'
{}*
'
.
format
(
baseP
refix
)))
for
filename
in
allPrefixed
:
for
filename
in
allPrefixed
:
basename
=
op
.
basename
(
filename
)
basename
=
op
.
basename
(
filename
)
for
argname
in
prefixe
dFile
s
:
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 prefix
e
d 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
)
...
...
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