|
| 1 | +import ipywidgets |
| 2 | +import lonboard |
1 | 3 | import numpy as np |
2 | 4 | import pytest |
3 | 5 | import xarray as xr |
4 | 6 | from arro3.core import Array, Table |
| 7 | +from matplotlib import colormaps |
5 | 8 |
|
6 | 9 | from xdggs import plotting |
7 | 10 |
|
@@ -121,3 +124,95 @@ def test_normalize(var, center, expected): |
121 | 124 | actual = plotting.normalize(var, center=center) |
122 | 125 |
|
123 | 126 | np.testing.assert_allclose(actual, expected) |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize( |
| 130 | + ["var", "kwargs", "expected"], |
| 131 | + ( |
| 132 | + pytest.param( |
| 133 | + xr.Variable("cells", [0, 3]), |
| 134 | + {"center": 2, "colormap": colormaps["viridis"], "alpha": 1}, |
| 135 | + np.array([[68, 1, 84], [94, 201, 97]], dtype="uint8"), |
| 136 | + ), |
| 137 | + pytest.param( |
| 138 | + xr.Variable("cells", [-1, 1]), |
| 139 | + {"center": None, "colormap": colormaps["viridis"], "alpha": 0.8}, |
| 140 | + np.array([[68, 1, 84, 204], [253, 231, 36, 204]], dtype="uint8"), |
| 141 | + ), |
| 142 | + ), |
| 143 | +) |
| 144 | +def test_colorize(var, kwargs, expected): |
| 145 | + actual = plotting.colorize(var, **kwargs) |
| 146 | + |
| 147 | + np.testing.assert_equal(actual, expected) |
| 148 | + |
| 149 | + |
| 150 | +class TestMapContainer: |
| 151 | + def test_init(self): |
| 152 | + map_ = lonboard.Map(layers=[]) |
| 153 | + sliders = ipywidgets.VBox( |
| 154 | + [ipywidgets.IntSlider(min=0, max=10, description="time")] |
| 155 | + ) |
| 156 | + obj = xr.DataArray([[0, 1], [2, 3]], dims=["time", "cells"]) |
| 157 | + colorize_kwargs = {"a": 1, "b": 2} |
| 158 | + |
| 159 | + container = plotting.MapContainer( |
| 160 | + dimension_sliders=sliders, |
| 161 | + map=map_, |
| 162 | + obj=obj, |
| 163 | + colorize_kwargs=colorize_kwargs, |
| 164 | + ) |
| 165 | + |
| 166 | + assert container.map == map_ |
| 167 | + xr.testing.assert_equal(container.obj, obj) |
| 168 | + assert container.dimension_sliders == sliders |
| 169 | + assert container.colorize_kwargs == colorize_kwargs |
| 170 | + |
| 171 | + def test_render(self): |
| 172 | + map_ = lonboard.Map(layers=[]) |
| 173 | + sliders = ipywidgets.VBox( |
| 174 | + [ipywidgets.IntSlider(min=0, max=10, description="time")] |
| 175 | + ) |
| 176 | + obj = xr.DataArray([[0, 1], [2, 3]], dims=["time", "cells"]) |
| 177 | + colorize_kwargs = {"a": 1, "b": 2} |
| 178 | + |
| 179 | + container = plotting.MapContainer( |
| 180 | + dimension_sliders=sliders, |
| 181 | + map=map_, |
| 182 | + obj=obj, |
| 183 | + colorize_kwargs=colorize_kwargs, |
| 184 | + ) |
| 185 | + rendered = container.render() |
| 186 | + |
| 187 | + assert isinstance(rendered, ipywidgets.VBox) |
| 188 | + |
| 189 | + |
| 190 | +@pytest.mark.parametrize( |
| 191 | + ["arr", "expected_type"], |
| 192 | + ( |
| 193 | + pytest.param( |
| 194 | + xr.DataArray( |
| 195 | + [0, 1], coords={"cell_ids": ("cells", [10, 26])}, dims="cells" |
| 196 | + ).dggs.decode( |
| 197 | + {"grid_name": "healpix", "level": 1, "indexing_scheme": "nested"} |
| 198 | + ), |
| 199 | + lonboard.Map, |
| 200 | + id="1d", |
| 201 | + ), |
| 202 | + pytest.param( |
| 203 | + xr.DataArray( |
| 204 | + [[0, 1], [2, 3]], |
| 205 | + coords={"cell_ids": ("cells", [10, 26])}, |
| 206 | + dims=["time", "cells"], |
| 207 | + ).dggs.decode( |
| 208 | + {"grid_name": "healpix", "level": 1, "indexing_scheme": "nested"} |
| 209 | + ), |
| 210 | + ipywidgets.VBox, |
| 211 | + id="2d", |
| 212 | + ), |
| 213 | + ), |
| 214 | +) |
| 215 | +def test_explore(arr, expected_type): |
| 216 | + actual = arr.dggs.explore() |
| 217 | + |
| 218 | + assert isinstance(actual, expected_type) |
0 commit comments