|
9 | 9 |
|
10 | 10 | import spatialdata_plot |
11 | 11 | from spatialdata_plot.pl.utils import ( |
| 12 | + _apply_cmap_alpha_to_datashader_result, |
12 | 13 | _datashader_map_aggregate_to_color, |
13 | 14 | _get_subplots, |
14 | | - _mask_transparent_cmap_entries, |
15 | 15 | set_zero_in_cmap_to_transparent, |
16 | 16 | ) |
17 | 17 | from tests.conftest import DPI, PlotTester, PlotTesterMeta |
@@ -117,74 +117,77 @@ def test_is_color_like(color_result: tuple[ColorLike, bool]): |
117 | 117 | assert spatialdata_plot.pl.utils._is_color_like(color) == result |
118 | 118 |
|
119 | 119 |
|
120 | | -class TestMaskTransparentCmapEntries: |
| 120 | +class TestCmapAlphaDatashader: |
121 | 121 | """Regression tests for #376: set_zero_in_cmap_to_transparent with datashader.""" |
122 | 122 |
|
123 | | - def test_masks_zero_values_when_cmap_has_transparent_entry(self): |
| 123 | + def test_transparent_pixels_get_alpha_zero(self): |
| 124 | + """Post-processing sets alpha=0 for pixels mapping to transparent cmap entries.""" |
| 125 | + import datashader as ds |
| 126 | + |
124 | 127 | cmap = set_zero_in_cmap_to_transparent("viridis") |
125 | | - data = np.array([[0.0, 1.0, 5.0], [0.0, 2.0, 10.0]]) |
| 128 | + data = np.array([[0.0, 5.0, 10.0]], dtype=np.float64) |
126 | 129 | agg = xr.DataArray(data, dims=["y", "x"]) |
127 | 130 |
|
128 | | - masked, returned_span = _mask_transparent_cmap_entries(agg, cmap, span=[0.0, 10.0]) |
| 131 | + shaded = ds.tf.shade(agg, cmap=cmap, min_alpha=254, how="linear") |
| 132 | + result = _apply_cmap_alpha_to_datashader_result(shaded, agg, cmap, span=[0.0, 10.0]) |
| 133 | + rgba = result.to_numpy().base if hasattr(result, "to_numpy") else result |
| 134 | + |
| 135 | + assert rgba[0, 0, 3] == 0, f"Expected alpha=0 at value=0.0, got {rgba[0, 0, 3]}" |
| 136 | + assert rgba[0, 1, 3] > 0, "Expected non-zero alpha at value=5.0" |
| 137 | + assert rgba[0, 2, 3] > 0, "Expected non-zero alpha at value=10.0" |
129 | 138 |
|
130 | | - assert np.isnan(masked.values[0, 0]) |
131 | | - assert np.isnan(masked.values[1, 0]) |
132 | | - assert masked.values[0, 1] == 1.0 |
133 | | - assert masked.values[0, 2] == 5.0 |
134 | | - assert returned_span == [0.0, 10.0] |
| 139 | + def test_opaque_cmap_unchanged(self): |
| 140 | + """Post-processing is a no-op for fully opaque cmaps.""" |
| 141 | + import datashader as ds |
135 | 142 |
|
136 | | - def test_no_effect_for_opaque_cmap(self): |
137 | 143 | cmap = plt.get_cmap("viridis") |
138 | | - data = np.array([[0.0, 5.0, 10.0]]) |
| 144 | + data = np.array([[0.0, 5.0, 10.0]], dtype=np.float64) |
139 | 145 | agg = xr.DataArray(data, dims=["y", "x"]) |
140 | 146 |
|
141 | | - masked, returned_span = _mask_transparent_cmap_entries(agg, cmap, span=[0.0, 10.0]) |
142 | | - np.testing.assert_array_equal(masked.values, data) |
| 147 | + shaded = ds.tf.shade(agg, cmap=cmap, min_alpha=254, how="linear") |
| 148 | + rgba_before = shaded.to_numpy().base.copy() |
| 149 | + result = _apply_cmap_alpha_to_datashader_result(shaded, agg, cmap, span=[0.0, 10.0]) |
| 150 | + rgba_after = result.to_numpy().base if hasattr(result, "to_numpy") else result |
| 151 | + np.testing.assert_array_equal(rgba_before, rgba_after) |
143 | 152 |
|
144 | | - def test_no_effect_for_string_cmap(self): |
145 | | - data = np.array([[0.0, 5.0, 10.0]]) |
| 153 | + def test_string_cmap_passthrough(self): |
| 154 | + """Post-processing is a no-op for string cmaps (early return).""" |
| 155 | + dummy_rgba = np.zeros((2, 3, 4), dtype=np.uint8) |
| 156 | + dummy_rgba[:, :, 3] = 200 |
| 157 | + data = np.array([[0.0, 5.0, 10.0]], dtype=np.float64) |
146 | 158 | agg = xr.DataArray(data, dims=["y", "x"]) |
147 | 159 |
|
148 | | - masked, _ = _mask_transparent_cmap_entries(agg, "viridis", span=[0.0, 10.0]) |
149 | | - np.testing.assert_array_equal(masked.values, data) |
| 160 | + result = _apply_cmap_alpha_to_datashader_result(dummy_rgba, agg, "viridis", span=[0.0, 10.0]) |
| 161 | + np.testing.assert_array_equal(result, dummy_rgba) |
150 | 162 |
|
151 | | - def test_datashader_shade_respects_transparent_cmap(self): |
152 | | - """End-to-end: _datashader_map_aggregate_to_color produces alpha=0 for transparent cmap entries.""" |
| 163 | + def test_end_to_end_datashader_map(self): |
| 164 | + """_datashader_map_aggregate_to_color produces alpha=0 for transparent cmap entries.""" |
153 | 165 | cmap = set_zero_in_cmap_to_transparent("viridis") |
154 | 166 | data = np.array([[0.0, 5.0, 10.0]], dtype=np.float64) |
155 | 167 | agg = xr.DataArray(data, dims=["y", "x"]) |
156 | 168 |
|
157 | 169 | result = _datashader_map_aggregate_to_color(agg, cmap=cmap, min_alpha=254, span=[0.0, 10.0]) |
158 | | - img = result.values if hasattr(result, "values") else result |
159 | | - |
160 | | - alpha_at_zero = (int(img[0, 0]) >> 24) & 0xFF |
161 | | - alpha_at_five = (int(img[0, 1]) >> 24) & 0xFF |
| 170 | + img = result.to_numpy().base if hasattr(result, "to_numpy") else result |
162 | 171 |
|
163 | | - assert alpha_at_zero == 0, f"Expected alpha=0 at value=0.0, got {alpha_at_zero}" |
164 | | - assert alpha_at_five > 0, f"Expected non-zero alpha at value=5.0, got {alpha_at_five}" |
| 172 | + assert img[0, 0, 3] == 0, f"Expected alpha=0 at value=0.0, got {img[0, 0, 3]}" |
| 173 | + assert img[0, 1, 3] > 0, "Expected non-zero alpha at value=5.0" |
165 | 174 |
|
166 | | - def test_span_none_with_zeros(self): |
167 | | - """Masking works when span is inferred from the aggregate (span=None).""" |
| 175 | + def test_span_none_preserves_colors(self): |
| 176 | + """With span=None, non-transparent shapes keep their correct colors.""" |
168 | 177 | cmap = set_zero_in_cmap_to_transparent("viridis") |
169 | | - data = np.array([[0.0, 3.0, 10.0]]) |
| 178 | + data = np.array([[0.0, 5.0, 10.0]], dtype=np.float64) |
170 | 179 | agg = xr.DataArray(data, dims=["y", "x"]) |
171 | 180 |
|
172 | | - masked, returned_span = _mask_transparent_cmap_entries(agg, cmap, span=None) |
173 | | - |
174 | | - assert np.isnan(masked.values[0, 0]) |
175 | | - assert masked.values[0, 1] == 3.0 |
176 | | - assert masked.values[0, 2] == 10.0 |
177 | | - assert returned_span == [0.0, 10.0], "span should be frozen from pre-masking range" |
178 | | - |
179 | | - def test_all_nan_aggregate(self): |
180 | | - """All-NaN aggregate is returned unchanged.""" |
181 | | - |
182 | | - cmap = set_zero_in_cmap_to_transparent("viridis") |
183 | | - data = np.array([[np.nan, np.nan]]) |
184 | | - agg = xr.DataArray(data, dims=["y", "x"]) |
| 181 | + result = _datashader_map_aggregate_to_color(agg, cmap=cmap, min_alpha=254) |
| 182 | + img = result.to_numpy().base if hasattr(result, "to_numpy") else result |
185 | 183 |
|
186 | | - masked, _ = _mask_transparent_cmap_entries(agg, cmap, span=None) |
187 | | - np.testing.assert_array_equal(np.isnan(masked.values), np.isnan(data)) |
| 184 | + # value=0 should be transparent |
| 185 | + assert img[0, 0, 3] == 0 |
| 186 | + # value=5 and value=10 should be opaque with correct viridis colors (not white) |
| 187 | + assert img[0, 1, 3] > 0 |
| 188 | + assert img[0, 2, 3] > 0 |
| 189 | + # The non-transparent pixels should NOT be white (R=255,G=255,B=255) |
| 190 | + assert not (img[0, 1, 0] == 255 and img[0, 1, 1] == 255 and img[0, 1, 2] == 255) |
188 | 191 |
|
189 | 192 |
|
190 | 193 | def test_extract_scalar_value(): |
|
0 commit comments