FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

FIX: also exclude np.nan in RGB(A) in color mapping by tacaswell · Pull Request #27303 · matplotlib/matplotlib · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion lib/matplotlib/cm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,12 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
else:
raise ValueError("Third dimension must be 3 or 4")
if xx.dtype.kind == 'f':
if norm and (xx.max() > 1 or xx.min() < 0):
if norm and (
np.any(np.isnan(xx)) or (xx.max() > 1 or xx.min() < 0)
):

anntzer Nov 10, 2023 •
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Rewrite as
if not (xx.min() >= 0 and xx.max() <= 1) (with a comment explaining that this form also excludes nans) and no extra nan checking to save a tiny bit of performance?

raise ValueError("Floating point image RGB values "
"must be in the 0..1 range.")

if bytes:
xx = (xx * 255).astype(np.uint8)
elif xx.dtype == np.uint8:
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_colors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,23 @@ def test_scalarmappable_to_rgba(bytes):
np.testing.assert_array_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)


@pytest.mark.parametrize("inp", (
[[[2, .5, 1], [.5, .5, .5]]],
[[[np.nan, .5, 1], [.5, .5, .5]]],
[[[-1, .5, 1], [.5, .5, .5]]],
[[[np.inf, .5, 1], [.5, .5, .5]]],
[[[-np.inf, .5, 1], [.5, .5, .5]]]
)
)
def test_scalarmappable_to_rgba_invalid(inp):
sm = cm.ScalarMappable()
with pytest.raises(
ValueError,
match="Floating point image RGB values must be in the 0..1 range."
):
sm.to_rgba(np.asarray(inp))


def test_failed_conversions():
with pytest.raises(ValueError):
mcolors.to_rgba('5')
Expand Down

Back | FazBrowse Home | New Git URL