8000 Reorder safe_first_element() and _safe_first_finite() code by timhoffm · Pull Request #26819 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Reorder safe_first_element() and _safe_first_finite() code #26819

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
Sep 19, 2023
Merged
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
37 changes: 18 additions & 19 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -1679,10 +1679,20 @@ def safe_first_element(obj):
This is a type-independent way of obtaining the first element,
supporting both index access and the iterator protocol.
"""
return _safe_first_finite(obj, skip_nonfinite=False)
if isinstance(obj, collections.abc.Iterator):
# needed to accept `array.flat` as input.
# np.flatiter reports as an instance of collecti 8000 ons.Iterator but can still be
# indexed via []. This has the side effect of re-setting the iterator, but
# that is acceptable.
try:
return obj[0]
except TypeError:
pass
raise RuntimeError("matplotlib does not support generators as input")
return next(iter(obj))


def _safe_first_finite(obj, *, skip_nonfinite=True):
def _safe_first_finite(obj):
"""
Return the first finite element in *obj* if one is available and skip_nonfinite is
True. Otherwise, return the first element.
Expand All @@ -1698,33 +1708,22 @@ def safe_isfinite(val):
try:
return math.isfinite(val)
except (TypeError, ValueError):
# if the outer object is 2d, then val is a 1d array, and
# - math.isfinite(numpy.zeros(3)) raises TypeError
# - math.isfinite(torch.zeros(3)) raises ValueError
pass
try:
return np.isfinite(val) if np.isscalar(val) else True
except TypeError:
# This is something that NumPy cannot make heads or tails of,
# assume "finite"
return True
if skip_nonfinite is False:
if isinstance(obj, collections.abc.Iterator):
# needed to accept `array.flat` as input.
# np.flatiter reports as an instance of collections.Iterator
# but can still be indexed via [].
# This has the side effect of re-setting the iterator, but
# that is acceptable.
try:
return obj[0]
except TypeError:
pass
raise RuntimeError("matplotlib does not support generators "
"as input")
return next(iter(obj))
elif isinstance(obj, np.flatiter):

if isinstance(obj, np.flatiter):
# TODO do the finite filtering on this
return obj[0]
elif isinstance(obj, collections.abc.Iterator):
raise RuntimeError("matplotlib does not "
"support generators as input")
raise RuntimeError("matplotlib does not support generators as input")
else:
for val in obj:
if safe_isfinite(val):
Expand Down
0