Skip to content

FIX: _safe_first_finite on all non-finite array #25547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,19 +2208,11 @@ def _convert_dx(dx, x0, xconv, convert):
x0 = cbook._safe_first_finite(x0)
except (TypeError, IndexError, KeyError):
pass
except StopIteration:
# this means we found no finite element, fall back to first
# element unconditionally
x0 = cbook.safe_first_element(x0)

try:
x = cbook._safe_first_finite(xconv)
except (TypeError, IndexError, KeyError):
Copy link
Member

Choose a reason for hiding this comment

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

I think there a few other places where StopIteration is try/excepted for _safe_first_element? Did you check all those?

Copy link
Member Author

Choose a reason for hiding this comment

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

I hadn't, but I have now. I only found these two:

try:
x = cbook._safe_first_finite(x)
except (TypeError, StopIteration):
pass

try: # If cache lookup fails, look up based on first element...
first = cbook._safe_first_finite(x)
except (TypeError, StopIteration):
pass

These both predate the change that led to StopIteration being thrown for all-nan arrays. The commit that introduced the first says it was for zero length objects, and these will still trigger a StopIteration when the logic arrives here. I'm not sure about the second - should units.Registry.get_converter also be able to handle zero length objects?

Copy link
Member

Choose a reason for hiding this comment

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

I don't really know - just noted that it happens a few other places, and the more logic we can move into a consistent helper function, the better in my opinion.

Copy link
Member Author

Choose a reason for hiding this comment

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

the more logic we can move into a consistent helper function, the better in my opinion.

I certainly agree with that. I wonder if further consolidation should wait for a separate PR though, given recent clarifications about what should and should not be backported

  • my current change fixes a regression from v3.6, so it would be good to get in a patch release
  • a change that tidies the code but (hopefully) makes no difference to the user should maybe wait till v3.8

x = xconv
except StopIteration:
# this means we found no finite element, fall back to first
# element unconditionally
x = cbook.safe_first_element(xconv)

delist = False
if not np.iterable(dx):
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,13 +1619,13 @@ def safe_first_element(obj):

def _safe_first_finite(obj, *, skip_nonfinite=True):
"""
Return the first non-None (and optionally finite) element in *obj*.
Return the first finite element in *obj* if one is available and skip_nonfinite is
True. Otherwise return the first element.

This is a method for internal use.

This is a type-independent way of obtaining the first non-None element,
supporting both index access and the iterator protocol.
The first non-None element will be obtained when skip_none is True.
This is a type-independent way of obtaining the first finite element, supporting
both index access and the iterator protocol.
"""
def safe_isfinite(val):
if val is None:
Expand Down Expand Up @@ -1657,7 +1657,7 @@ def safe_isfinite(val):
raise RuntimeError("matplotlib does not "
"support generators as input")
else:
return next(val for val in obj if safe_isfinite(val))
return next((val for val in obj if safe_isfinite(val)), safe_first_element(obj))


def sanitize_sequence(data):
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,18 @@ def test_flatiter():
assert 1 == next(it)


def test__safe_first_finite_all_nan():
arr = np.full(2, np.nan)
ret = cbook._safe_first_finite(arr)
assert np.isnan(ret)


def test__safe_first_finite_all_inf():
arr = np.full(2, np.inf)
ret = cbook._safe_first_finite(arr)
assert np.isinf(ret)


def test_reshape2d():

class Dummy:
Expand Down