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