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
7c855190
Commit
7c855190
authored
8 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ImageWrapper handles images with inf values.
parent
ac9ae8a0
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/data/imagewrapper.py
+46
-4
46 additions, 4 deletions
fsl/data/imagewrapper.py
with
46 additions
and
4 deletions
fsl/data/imagewrapper.py
+
46
−
4
View file @
7c855190
...
...
@@ -117,6 +117,7 @@ class ImageWrapper(notifier.Notifier):
.. autosummary::
:nosignatures:
naninfrange
sliceObjToSliceTuple
sliceTupleToSliceObj
sliceCovered
...
...
@@ -414,8 +415,7 @@ class ImageWrapper(notifier.Notifier):
oldvlo
,
oldvhi
=
self
.
__volRanges
[
vol
,
:]
voldata
=
data
[...,
vi
]
newvlo
=
float
(
np
.
nanmin
(
voldata
))
newvhi
=
float
(
np
.
nanmax
(
voldata
))
newvlo
,
newvhi
=
naninfrange
(
voldata
)
if
(
not
np
.
isnan
(
oldvlo
))
and
oldvlo
<
newvlo
:
newvlo
=
oldvlo
if
(
not
np
.
isnan
(
oldvhi
))
and
oldvhi
>
newvhi
:
newvhi
=
oldvhi
...
...
@@ -429,8 +429,7 @@ class ImageWrapper(notifier.Notifier):
# Calculate the new known data
# range over the entire image
# (i.e. over all volumes).
newmin
=
float
(
np
.
nanmin
(
self
.
__volRanges
[:,
0
]))
newmax
=
float
(
np
.
nanmax
(
self
.
__volRanges
[:,
1
]))
newmin
,
newmax
=
naninfrange
(
self
.
__volRanges
)
oldmin
,
oldmax
=
self
.
__range
self
.
__range
=
(
newmin
,
newmax
)
...
...
@@ -607,6 +606,49 @@ class ImageWrapper(notifier.Notifier):
self
.
__updateDataRangeOnWrite
(
slices
,
values
)
def
naninfrange
(
data
):
"""
Returns the minimum and maximum values in the given ``numpy`` array,
ignoring ``nan`` and ``inf`` values.
The ``numpy.nanmin``/``numpy.nanmax`` functions do not handle
positive/negative infinity, so if such values are in the data, we need to
use an alternate approach to calculating the minimum/maximum.
"""
if
not
np
.
issubdtype
(
data
.
dtype
,
np
.
float
):
return
data
.
min
(),
data
.
max
()
# But np.nanmin/nanmax are substantially
# faster than the alternate, so we try it
# first.
dmin
=
np
.
nanmin
(
data
)
dmax
=
np
.
nanmax
(
data
)
# If there are no nans/infs in the data,
# we can just use nanmin/nanmax
if
np
.
isfinite
(
dmin
)
and
np
.
isfinite
(
dmax
):
return
dmin
,
dmax
# The entire array contains nans
if
np
.
isnan
(
dmin
):
return
dmin
,
dmin
# Otherwise we need to calculate min/max
# only on finite values. This is the slow
# option.
# Find all finite values
finite
=
np
.
isfinite
(
data
)
# Try to calculate min/max on those values.
# An error will be raised if there are no
# finite values in the array
try
:
return
data
[
finite
].
min
(),
data
[
finite
].
max
()
except
:
return
np
.
nan
,
np
.
nan
def
canonicalShape
(
shape
):
"""
Calculates a *canonical* shape, how the given ``shape`` should
be presented. The shape is forced to be at least three dimensions,
...
...
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