arviz icon indicating copy to clipboard operation
arviz copied to clipboard

ValueError: A colormap named "cet_gray" is already registered

Open yannikschaelte opened this issue 3 years ago • 4 comments

Software version info

colorcet 3.0.1 arviz 0.12.1 python 3.10/8 matplotlib 3.6.0/1 afaik introduced with recent colorcet/matplotlib release

Description of expected behavior and the observed behavior

When multiple libraries use mpl.colormaps.register(cmap, name=cmap_name) with the same cmap_name, a ValueError: A colormap named "cet_gray" is already registered. is raised. This happens e.g. for colorcet + arviz, both of which define cet_gray (see here for arviz, here for colorcet).

Arviz appears here to emulate the colorcet color.

Is this an issue/responsibility of colorcet/arviz/...?

Complete, minimal, self-contained example code that reproduces the issue

import arviz
import colorcet

or

import colorcet
import arviz

This prohibits the use of both simultaneously.

Stacktrace

Traceback (most recent call last):
 File "<string>", line 1, in <module>
 File "/home/yannik/env/env/lib/python3.10/site-packages/arviz/__init__.py", line 320, in <module>
   _mpl_cm("gray", _linear_grey_10_95_c0)
 File "/home/yannik/env/env/lib/python3.10/site-packages/arviz/__init__.py", line 317, in _mpl_cm
   register_cmap("cet_" + name, cmap=cmap)
 File "/home/yannik/env/env/lib/python3.10/site-packages/matplotlib/_api/deprecation.py", line 200, in wrapper
   return func(*args, **kwargs)
 File "/home/yannik/env/env/lib/python3.10/site-packages/matplotlib/cm.py", line 248, in register_cmap
   _colormaps.register(cmap, name=name, force=override_builtin)
 File "/home/yannik/env/env/lib/python3.10/site-packages/matplotlib/cm.py", line 149, in register
   raise ValueError(
ValueError: A colormap named "cet_gray" is already registered.

yannikschaelte avatar Oct 12 '22 13:10 yannikschaelte

Maybe we can add a check and only register the cmap if colorcet is not installed? (so that both import orders work as opposed to checking if the cmap is registered and requiring arviz to be imported second) cc @aloctavodia

OriolAbril avatar Oct 12 '22 16:10 OriolAbril

Maybe wrapping in a try/except ValueError right here? It doesn't "commute" with import order, but I'll pop over to the colorcet side and see if maybe that should just be a best practice.

ColCarroll avatar Oct 12 '22 17:10 ColCarroll

Why do we use same name as colorcet?

ahartikainen avatar Oct 12 '22 17:10 ahartikainen

It is the same colorscale, so it makes sense from that perspective.

ColCarroll avatar Oct 12 '22 18:10 ColCarroll

Sounds like they are suggesting something similar to you, @OriolAbril ! https://github.com/holoviz/colorcet/issues/96#issuecomment-1276576516

ColCarroll avatar Oct 13 '22 00:10 ColCarroll