Skip to content

Commit 4249158

Browse files
authored
plotting tests (#127)
* test `colorize` * basic test for `explore` * check that `explore` raises on arrays without a spatial dim * remove the duplicate check * extra tests for `MapContainer`
1 parent c792c3e commit 4249158

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

xdggs/plotting.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ def explore(
103103
cell_id_coord = arr.dggs.coord
104104
[cell_dim] = cell_id_coord.dims
105105

106-
if cell_dim not in arr.dims:
107-
raise ValueError(
108-
f"exploration plotting only works with a spatial dimension ('{cell_dim}')"
109-
)
110-
111106
cell_ids = cell_id_coord.data
112107
grid_info = arr.dggs.grid_info
113108

xdggs/tests/test_plotting.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import ipywidgets
2+
import lonboard
13
import numpy as np
24
import pytest
35
import xarray as xr
46
from arro3.core import Array, Table
7+
from matplotlib import colormaps
58

69
from xdggs import plotting
710

@@ -121,3 +124,95 @@ def test_normalize(var, center, expected):
121124
actual = plotting.normalize(var, center=center)
122125

123126
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

Comments
 (0)