Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD 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
FSL
fslpy
Commits
c68da9a3
Commit
c68da9a3
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: fileOrImage works with fsl.data.images.
parent
523b1f6e
No related branches found
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
+30
-3
30 additions, 3 deletions
fsl/wrappers/wrapperutils.py
with
30 additions
and
3 deletions
fsl/wrappers/wrapperutils.py
+
30
−
3
View file @
c68da9a3
...
...
@@ -635,13 +635,25 @@ class _FileOrThing(object):
def
fileOrImage
(
*
imgargs
):
"""
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``
image objects.
image
objects or :class:`.Image`
objects.
"""
# keep track of the input argument
# types on each call, so we know
# whether to return a fsl.Image or
# a nibabel image
intypes
=
[]
def
prepIn
(
workdir
,
name
,
val
):
infile
=
None
if
isinstance
(
val
,
(
fslimage
.
Image
,
nib
.
nifti1
.
Nifti1Image
)):
intypes
.
append
(
type
(
val
))
if
isinstance
(
val
,
fslimage
.
Image
):
val
=
val
.
nibImage
if
isinstance
(
val
,
nib
.
nifti1
.
Nifti1Image
):
infile
=
val
.
get_filename
()
...
...
@@ -661,13 +673,28 @@ def fileOrImage(*imgargs):
# create an independent in-memory
# copy of the image file
img
=
nib
.
load
(
path
)
return
nib
.
nifti1
.
Nifti1Image
(
img
.
get_data
(),
None
,
img
.
header
)
# if any arguments were fsl images,
# that takes precedence.
if
fslimage
.
Image
in
intypes
:
return
fslimage
.
Image
(
img
.
get_data
(),
header
=
img
.
header
)
# but if all inputs were file names,
# nibabel takes precedence
elif
nib
.
nifti1
.
Nifti1Image
in
intypes
or
len
(
intypes
)
==
0
:
return
nib
.
nifti1
.
Nifti1Image
(
img
.
get_data
(),
None
,
img
.
header
)
# this function should not be called
# under any other circumstances
else
:
raise
RuntimeError
(
'
Cannot handle type: {}
'
.
format
(
intypes
))
def
decorator
(
func
):
fot
=
_FileOrThing
(
func
,
prepIn
,
prepOut
,
load
,
*
imgargs
)
def
wrapper
(
*
args
,
**
kwargs
):
return
fot
(
*
args
,
**
kwargs
)
result
=
fot
(
*
args
,
**
kwargs
)
intypes
[:]
=
[]
return
result
return
_update_wrapper
(
wrapper
,
func
)
...
...
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