Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Optimize imshow #26335

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 8 commits into from
Jul 28, 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
3 changes: 1 addition & 2 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,7 @@ def safe_masked_invalid(x, copy=False):
# copy with the byte order swapped.
x = x.byteswap(inplace=copy).newbyteorder('N') # Swap to native order.
try:
xm = np.ma.masked_invalid(x, copy=False)
xm.shrink_mask()
xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False)
except TypeError:
return x
return xm
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,5 +735,6 @@ def _ensure_cmap(cmap):
cmap_name = cmap if cmap is not None else mpl.rcParams["image.cmap"]
# use check_in_list to ensure type stability of the exception raised by
# the internal usage of this (ValueError vs KeyError)
_api.check_in_list(sorted(_colormaps), cmap=cmap_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

This matters for the printed error message when the colormap is invalid. (We could instead ensure that _colormaps is always sorted by sorting it whenever a new item is added, though.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. We could ensure the ordering of _colormaps by adding c._cmaps = {k: c._cmaps[k] for k in sorted(c._cmaps)} at the end of ColormapRegistry.register. Under the assumption that number of colormaps added is reasonable, this will not have any other negative performance impact.

Another option is to replace the code with

if not cmap_name in _colormaps:
    _api.check_in_list(sorted(_colormaps), cmap=cmap_name)

Copy link
Member

Choose a reason for hiding this comment

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

Making sure that __iter__ on ColormapRegistry is always sorted is probably the best option (rather than mucking with dictionary order we can keep a self._sorted_keys = sorted(self._cmap) attribute and iterate over that in __iter__.

Copy link
Member

Choose a reason for hiding this comment

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

but the if not in... is also fine (as I see you already pushed that).

if cmap_name not in _colormaps:
_api.check_in_list(sorted(_colormaps), cmap=cmap_name)
return mpl.colormaps[cmap_name]
6 changes: 6 additions & 0 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,12 @@ def autoscale(self, A):
def autoscale_None(self, A):
"""If vmin or vmax are not set, use the min/max of *A* to set them."""
A = np.asanyarray(A)

if isinstance(A, np.ma.MaskedArray):
# we need to make the distinction between an array, False, np.bool_(False)
if A.mask is False or not A.mask.shape:
A = A.data
Comment on lines +1396 to +1397
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another way of testing:

Suggested change
if A.mask is False or not A.mask.shape:
A = A.data
try:
no_mask = not bool(A.mask)
except ValueError:
no_mask = False
if no_mask:
A = A.data

Numpy generated a ValueError when testing an array for truth value.


if self.vmin is None and A.size:
self.vmin = A.min()
if self.vmax is None and A.size:
Expand Down