Skip to content

Commit

Permalink
exp: avoid building a colormap registry and copies
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Jun 26, 2023
1 parent 439d302 commit 24ad119
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
29 changes: 7 additions & 22 deletions yt/visualization/color_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

from . import _colormap_data as _cm

yt_colormaps = {}


def add_colormap(name, cdict):
"""
Expand All @@ -25,19 +23,7 @@ def add_colormap(name, cdict):
since="4.3",
stacklevel=3,
)
# Note: this function modifies the global variable 'yt_colormaps'
yt_colormaps[name] = LinearSegmentedColormap(name, cdict, 256)
mpl.colormaps.register(yt_colormaps[name])


def _add_colormap(name, colors):
"""
Adds a colormap to the colormaps available in yt for this session
"""
# Note: this function modifies the global variable 'yt_colormaps'
cmap = ListedColormap(colors, name=name, N=256)
yt_colormaps[cmap.name] = cmap
mpl.colormaps.register(cmap)
mpl.colormaps.register(LinearSegmentedColormap(name, cdict, 256))


# YTEP-0040 backward compatibility layer
Expand Down Expand Up @@ -66,20 +52,19 @@ def register_yt_colormaps_from_cmyt():
"""

for hist_name, alias in _HISTORICAL_ALIASES.items():
cmap = mpl.colormaps[alias].copy()
cmap.name = hist_name
mpl.colormaps.register(cmap)
mpl.colormaps.register(cmap.reversed())
cmap = mpl.colormaps[alias]
cmap_r = mpl.colormaps[f"{alias}_r"]

mpl.colormaps.register(cmap, name=hist_name)
mpl.colormaps.register(cmap_r, name=f"{hist_name}_r")


register_yt_colormaps_from_cmyt()

# Add colormaps in _colormap_data.py that weren't defined here
for k, v in _cm.color_map_luts.items():
if k in yt_colormaps:
continue
try:
_add_colormap(k, v)
mpl.colormaps.register(ListedColormap(v, name=k, N=256))
except ValueError:
# expected if another map with identical name was already registered
mylog.warning("cannot register colormap '%s' (naming collision)", k)
Expand Down
11 changes: 5 additions & 6 deletions yt/visualization/tests/test_color_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ def test_make_colormap(self):


def test_cmyt_integration():
for name in ["algae", "bds_highcontrast", "kelp", "arbre", "octarine", "kamae"]:
cmap = plt.get_cmap(name)
assert cmap.name == name
name_r = name + "_r"
cmap_r = plt.get_cmap(name_r)
assert cmap_r.name == name_r
from yt.visualization.color_maps import _HISTORICAL_ALIASES

for hist_name, alias in _HISTORICAL_ALIASES.items():
assert plt.get_cmap(hist_name) == plt.get_cmap(alias)
assert plt.get_cmap(f"{hist_name}_r") == plt.get_cmap(f"{alias}_r")

for name in ["algae", "kelp", "arbre", "octarine", "pastel"]:
cmap = plt.get_cmap("cmyt." + name)
Expand Down

0 comments on commit 24ad119

Please sign in to comment.