diff --git a/benchmarks/benchmarks/import_iris.py b/benchmarks/benchmarks/import_iris.py index ad54c23122..fc32ac289b 100644 --- a/benchmarks/benchmarks/import_iris.py +++ b/benchmarks/benchmarks/import_iris.py @@ -5,10 +5,30 @@ # licensing details. from importlib import import_module, reload +################ +# Prepare info for reset_colormaps: + +# Import and capture colormaps. +from matplotlib import colormaps # isort:skip + +_COLORMAPS_ORIG = set(colormaps) + +# Import iris.palette, which modifies colormaps. +import iris.palette + +# Derive which colormaps have been added by iris.palette. +_COLORMAPS_MOD = set(colormaps) +COLORMAPS_EXTRA = _COLORMAPS_MOD - _COLORMAPS_ORIG + +# Touch iris.palette to prevent linters complaining. +_ = iris.palette + +################ + class Iris: @staticmethod - def _import(module_name): + def _import(module_name, reset_colormaps=False): """ Have experimented with adding sleep() commands into the imported modules. The results reveal: @@ -25,6 +45,13 @@ def _import(module_name): and the repetitions are therefore no faster than the first run. """ mod = import_module(module_name) + + if reset_colormaps: + # Needed because reload() will attempt to register new colormaps a + # second time, which errors by default. + for cm_name in COLORMAPS_EXTRA: + colormaps.unregister(cm_name) + reload(mod) def time_iris(self): @@ -205,7 +232,7 @@ def time_iterate(self): self._import("iris.iterate") def time_palette(self): - self._import("iris.palette") + self._import("iris.palette", reset_colormaps=True) def time_plot(self): self._import("iris.plot") diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index d2eadb17d6..e68080e747 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -83,14 +83,20 @@ This document explains the changes made to Iris for this release #. `@rcomer`_ introduced the ``dask >=2.26`` minimum pin, so that Iris can benefit from Dask's support for `NEP13`_ and `NEP18`_. (:pull:`4905`) + #. `@trexfeathers`_ advanced the Cartopy pin to ``>=0.21``, as Cartopy's change to default Transverse Mercator projection affects an Iris test. See `SciTools/cartopy@fcb784d`_ and `SciTools/cartopy@8860a81`_ for more details. (:pull:`4968`) + #. `@trexfeathers`_ introduced the ``netcdf4!=1.6.1`` pin to avoid a problem with segfaults. (:pull:`4968`) +#. `@trexfeathers`_ updated the Matplotlib colormap registration in + :mod:`iris.palette` in response to a deprecation warning. Using the new + Matplotlib API also means a ``matplotlib>=3.5`` pin. (:pull:`4998`) + 📚 Documentation ================ @@ -114,6 +120,9 @@ This document explains the changes made to Iris for this release :mod:`~iris.fileformats.netcdf.loader` and :mod:`~iris.fileformats.netcdf.saver` submodules, just to make the code easier to handle. +#. `@trexfeathers`_ adapted the benchmark for importing :mod:`iris.palette` to + cope with new colormap behaviour in Matplotlib `v3.6`. (:pull:`4998`) + .. comment Whatsnew author names (@github name) in alphabetical order. Note that, diff --git a/lib/iris/palette.py b/lib/iris/palette.py index 626ae4e341..5aa30a6b4e 100644 --- a/lib/iris/palette.py +++ b/lib/iris/palette.py @@ -15,6 +15,7 @@ import re import cf_units +from matplotlib import colormaps as mpl_colormaps import matplotlib.cm as mpl_cm import matplotlib.colors as mpl_colors import numpy as np @@ -337,7 +338,7 @@ def _load_palette(): ) # Register the color map for use. - mpl_cm.register_cmap(cmap=cmap) + mpl_colormaps.register(cmap) # Ensure to load the color map palettes. diff --git a/requirements/ci/py310.yml b/requirements/ci/py310.yml index 76ca9e4f58..ae0090881d 100644 --- a/requirements/ci/py310.yml +++ b/requirements/ci/py310.yml @@ -15,7 +15,7 @@ dependencies: - cf-units >=3.1 - cftime >=1.5 - dask-core >=2.26 - - matplotlib + - matplotlib >=3.5 - netcdf4 !=1.6.1 - numpy >=1.19 - python-xxhash diff --git a/requirements/ci/py38.yml b/requirements/ci/py38.yml index 5a8c878ee1..c0ed574c92 100644 --- a/requirements/ci/py38.yml +++ b/requirements/ci/py38.yml @@ -15,7 +15,7 @@ dependencies: - cf-units >=3.1 - cftime >=1.5 - dask-core >=2.26 - - matplotlib + - matplotlib >=3.5 - netcdf4 !=1.6.1 - numpy >=1.19 - python-xxhash diff --git a/requirements/ci/py39.yml b/requirements/ci/py39.yml index 7931e20336..c79b0ede1d 100644 --- a/requirements/ci/py39.yml +++ b/requirements/ci/py39.yml @@ -15,7 +15,7 @@ dependencies: - cf-units >=3.1 - cftime >=1.5 - dask-core >=2.26 - - matplotlib + - matplotlib >=3.5 - netcdf4 !=1.6.1 - numpy >=1.19 - python-xxhash diff --git a/setup.cfg b/setup.cfg index 92cbe4747c..ca35a8eb4e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -51,7 +51,7 @@ install_requires = cf-units>=3.1 cftime>=1.5.0 dask[array]>=2.26 - matplotlib + matplotlib>=3.5 netcdf4!=1.6.1 numpy>=1.19 scipy