10000 Merge pull request #26819 from timhoffm/safe-first-element · matplotlib/matplotlib@f97647b · GitHub
[go: up one dir, main page]

Skip to content

Commit f97647b

Browse files
authored
Merge pull request #26819 from timhoffm/safe-first-element
Reoder safe_first_element() and _safe_first_finite() code
2 parents f71555a + 604704c commit f97647b

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

lib/matplotlib/cbook.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,10 +1679,20 @@ def safe_first_element(obj):
16791679
This is a type-independent way of obtaining the first element,
16801680
supporting both index access and the iterator protocol.
16811681
"""
1682-
return _safe_first_finite(obj, skip_nonfinite=False)
1682+
if isinstance(obj, collections.abc.Iterator):
1683+
# needed to accept `array.flat` as input.
1684+
# np.flatiter reports as an instance of collections.Iterator but can still be
1685+
# indexed via []. This has the side effect of re-setting the iterator, but
1686+
# that is acceptable.
1687+
try:
1688+
return obj[0]
1689+
except TypeError:
1690+
pass
1691+
raise RuntimeError("matplotlib does not support generators as input" 8000 )
1692+
return next(iter(obj))
16831693

16841694

1685-
def _safe_first_finite(obj, *, skip_nonfinite=True):
1695+
def _safe_first_finite(obj):
16861696
"""
16871697
Return the first finite element in *obj* if one is available and skip_nonfinite is
16881698
True. Otherwise, return the first element.
@@ -1698,33 +1708,22 @@ def safe_isfinite(val):
16981708
try:
16991709
return math.isfinite(val)
17001710
except (TypeError, ValueError):
1711+
# if the outer object is 2d, then val is a 1d array, and
1712+
# - math.isfinite(numpy.zeros(3)) raises TypeError
1713+
# - math.isfinite(torch.zeros(3)) raises ValueError
17011714
pass
17021715
try:
17031716
return np.isfinite(val) if np.isscalar(val) else True
17041717
except TypeError:
17051718
# This is something that NumPy cannot make heads or tails of,
17061719
# assume "finite"
17071720
return True
1708-
if skip_nonfinite is False:
1709-
if isinstance(obj, collections.abc.Iterator):
1710-
# needed to accept `array.flat` as input.
1711-
# np.flatiter reports as an instance of collections.Iterator
1712-
# but can still be indexed via [].
1713-
# This has the side effect of re-setting the iterator, but
1714-
# that is acceptable.
1715-
try:
1716-
return obj[0]
1717-
except TypeError:
1718-
pass
1719-
raise RuntimeError("matplotlib does not support generators "
1720-
"as input")
1721-
return next(iter(obj))
1722-
elif isinstance(obj, np.flatiter):
1721+
1722+
if isinstance(obj, np.flatiter):
17231723
# TODO do the finite filtering on this
17241724
return obj[0]
17251725
elif isinstance(obj, collections.abc.Iterator):
1726-
raise RuntimeError("matplotlib does not "
1727-
"support generators as input")
1726+
raise RuntimeError("matplotlib does not support generators as input")
17281727
else:
17291728
for val in obj:
17301729
if safe_isfinite(val):

0 commit comments

Comments
 (0)
0