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
5b2762cf
Commit
5b2762cf
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
imagewrapper.naninfrange moved into its own module
parent
04fd59e2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/naninfrange.py
+55
-0
55 additions, 0 deletions
fsl/utils/naninfrange.py
with
55 additions
and
0 deletions
fsl/utils/naninfrange.py
0 → 100644
+
55
−
0
View file @
5b2762cf
#!/usr/bin/env python
#
# naninfrange.py - The naninfrange function.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""
This module provides the :func:`naninfrange` function, which calculates
the range of a numpy array, ignoring infinite and nan values.
"""
import
numpy
as
np
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
.
floating
):
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
Exception
:
return
np
.
nan
,
np
.
nan
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