-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_colors.py
More file actions
139 lines (113 loc) · 4.44 KB
/
test_colors.py
File metadata and controls
139 lines (113 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import pytest
import numpy as np
import matplotlib.colors as mcolors
from ultraplot import colors as pcolors
from ultraplot import config
@pytest.fixture(autouse=True)
def setup_teardown():
"""
Reset the colormap database before and after each test.
"""
# This ensures a clean state for each test.
# The singleton instance is replaced with a new one.
pcolors._cmap_database = pcolors._init_cmap_database()
config.register_cmaps(default=True)
config.register_cycles(default=True)
yield
def test_lazy_loading_builtin():
"""
Test that built-in colormaps are lazy-loaded.
"""
# Before access, it should be a matplotlib colormap
cmap_raw = pcolors._cmap_database._cmaps["viridis"]
assert isinstance(
cmap_raw,
(
pcolors.ContinuousColormap,
pcolors.DiscreteColormap,
mcolors.ListedColormap,
),
)
# After access, it should be an ultraplot colormap
cmap_get = pcolors._cmap_database.get_cmap("viridis")
assert isinstance(cmap_get, pcolors.ContinuousColormap)
# The internal representation should also be updated
cmap_raw_after = pcolors._cmap_database._cmaps["viridis"]
assert isinstance(cmap_raw_after, pcolors.ContinuousColormap)
def test_case_insensitivity():
"""
Test that colormap lookup is case-insensitive.
"""
cmap1 = pcolors._cmap_database.get_cmap("ViRiDiS")
cmap2 = pcolors._cmap_database.get_cmap("viridis")
assert cmap1.name.lower().startswith("_viridis")
assert cmap2.name.lower().startswith("_viridis")
def test_reversed_shifted():
"""
Test reversed and shifted colormaps.
"""
# Create a simple colormap to test the reversal logic
# This avoids dependency on the exact definition of 'viridis' in matplotlib
colors_list = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # Red, Green, Blue
test_cmap = pcolors.ContinuousColormap.from_list("test_cmap", colors_list)
pcolors._cmap_database.register(test_cmap)
cmap = pcolors._cmap_database.get_cmap("test_cmap")
cmap_r = pcolors._cmap_database.get_cmap("test_cmap_r")
# Check name
assert cmap_r.name == "_test_cmap_copy_r"
# Check colors
# Start of original should be end of reversed
assert np.allclose(cmap(0.0), cmap_r(1.0))
# End of original should be start of reversed
assert np.allclose(cmap(1.0), cmap_r(0.0))
# Middle should be the same
assert np.allclose(cmap(0.5)[:3], cmap_r(0.5)[:3][::-1])
def test_grays_translation():
"""
Test that 'Grays' is translated to 'greys'.
"""
cmap_grays = pcolors._cmap_database.get_cmap("Grays")
assert cmap_grays.name.lower().startswith("_greys")
def test_lazy_loading_file(tmp_path):
"""
Test that colormaps from files are lazy-loaded.
"""
# Create a dummy colormap file
cmap_data = "1, 0, 0\n0, 1, 0\n0, 0, 1"
cmap_file = tmp_path / "my_test_cmap.rgb"
cmap_file.write_text(cmap_data)
# Register it lazily
pcolors._cmap_database.register_lazy("my_test_cmap", str(cmap_file), "continuous")
# Before access, it should be a lazy-load dict
cmap_raw = pcolors._cmap_database._cmaps["my_test_cmap"]
assert isinstance(cmap_raw, dict)
assert cmap_raw["is_lazy"]
# After access, it should be an ultraplot colormap
cmap_get = pcolors._cmap_database.get_cmap("my_test_cmap")
assert isinstance(cmap_get, pcolors.ContinuousColormap)
assert cmap_get.name.lower().startswith("_my_test_cmap")
# The internal representation should also be updated
cmap_raw_after = pcolors._cmap_database._cmaps["my_test_cmap"]
assert isinstance(cmap_raw_after, pcolors.ContinuousColormap)
def test_register_new():
"""
Test registering a new colormap.
"""
colors_list = [(0, 0, 0), (1, 1, 1)]
new_cmap = pcolors.DiscreteColormap(colors_list, name="my_new_cmap")
pcolors._cmap_database.register(new_cmap)
# Check it was registered
cmap_get = pcolors._cmap_database.get_cmap("my_new_cmap")
assert cmap_get.name.lower().startswith(
"_my_new_cmap"
), f"Received {cmap_get.name.lower()} expected _my_new_cmap"
assert len(cmap_get.colors) == 2
def test_get_cmap_accepts_colormap_objects():
"""
Colormap lookups should accept colormap objects directly.
"""
cmap = pcolors._cmap_database.get_cmap("viridis")
cmap_from_obj = pcolors._cmap_database.get_cmap(cmap)
assert cmap_from_obj is not cmap
assert cmap_from_obj.name == cmap.name