From b471a126e35a861ffa0f2ebf122f988a40367c72 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Fri, 31 Jan 2025 14:18:11 +0100 Subject: [PATCH 1/5] test `colorize` --- xdggs/tests/test_plotting.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/xdggs/tests/test_plotting.py b/xdggs/tests/test_plotting.py index 6070248e..b591726c 100644 --- a/xdggs/tests/test_plotting.py +++ b/xdggs/tests/test_plotting.py @@ -2,6 +2,7 @@ import pytest import xarray as xr from arro3.core import Array, Table +from matplotlib import colormaps from xdggs import plotting @@ -121,3 +122,24 @@ def test_normalize(var, center, expected): actual = plotting.normalize(var, center=center) np.testing.assert_allclose(actual, expected) + + +@pytest.mark.parametrize( + ["var", "kwargs", "expected"], + ( + pytest.param( + xr.Variable("cells", [0, 3]), + {"center": 2, "colormap": colormaps["viridis"], "alpha": 1}, + np.array([[68, 1, 84], [94, 201, 97]], dtype="uint8"), + ), + pytest.param( + xr.Variable("cells", [-1, 1]), + {"center": None, "colormap": colormaps["viridis"], "alpha": 0.8}, + np.array([[68, 1, 84, 204], [253, 231, 36, 204]], dtype="uint8"), + ), + ), +) +def test_colorize(var, kwargs, expected): + actual = plotting.colorize(var, **kwargs) + + np.testing.assert_equal(actual, expected) From 5c93a4995438dc43160ef8db4c2a691b7f321640 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Fri, 31 Jan 2025 15:24:11 +0100 Subject: [PATCH 2/5] basic test for `explore` --- xdggs/tests/test_plotting.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/xdggs/tests/test_plotting.py b/xdggs/tests/test_plotting.py index b591726c..22edeedf 100644 --- a/xdggs/tests/test_plotting.py +++ b/xdggs/tests/test_plotting.py @@ -1,3 +1,5 @@ +import ipywidgets +import lonboard import numpy as np import pytest import xarray as xr @@ -143,3 +145,34 @@ def test_colorize(var, kwargs, expected): actual = plotting.colorize(var, **kwargs) np.testing.assert_equal(actual, expected) + + +@pytest.mark.parametrize( + ["arr", "expected_type"], + ( + pytest.param( + xr.DataArray( + [0, 1], coords={"cell_ids": ("cells", [10, 26])}, dims="cells" + ).dggs.decode( + {"grid_name": "healpix", "level": 1, "indexing_scheme": "nested"} + ), + lonboard.Map, + id="1d", + ), + pytest.param( + xr.DataArray( + [[0, 1], [2, 3]], + coords={"cell_ids": ("cells", [10, 26])}, + dims=["time", "cells"], + ).dggs.decode( + {"grid_name": "healpix", "level": 1, "indexing_scheme": "nested"} + ), + ipywidgets.VBox, + id="2d", + ), + ), +) +def test_explore(arr, expected_type): + actual = arr.dggs.explore() + + assert isinstance(actual, expected_type) From a8c8a0b7ee019b90b6ca02d517be8c34c36620cc Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Fri, 31 Jan 2025 15:27:02 +0100 Subject: [PATCH 3/5] check that `explore` raises on arrays without a spatial dim --- xdggs/tests/test_plotting.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/xdggs/tests/test_plotting.py b/xdggs/tests/test_plotting.py index 22edeedf..88779ca5 100644 --- a/xdggs/tests/test_plotting.py +++ b/xdggs/tests/test_plotting.py @@ -176,3 +176,9 @@ def test_explore(arr, expected_type): actual = arr.dggs.explore() assert isinstance(actual, expected_type) + + +def test_explore_without_spatial_dim(): + arr = xr.DataArray([0, 1], dims="time") + with pytest.raises(ValueError, match="only works with a spatial dimension"): + arr.dggs.explore() From a5c953ebe8b7deb4b60bb4f36f2c0b6dffa6df69 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Fri, 31 Jan 2025 15:29:00 +0100 Subject: [PATCH 4/5] remove the duplicate check --- xdggs/plotting.py | 5 ----- xdggs/tests/test_plotting.py | 6 ------ 2 files changed, 11 deletions(-) diff --git a/xdggs/plotting.py b/xdggs/plotting.py index b4e2e2bd..1ca2225f 100644 --- a/xdggs/plotting.py +++ b/xdggs/plotting.py @@ -103,11 +103,6 @@ def explore( cell_id_coord = arr.dggs.coord [cell_dim] = cell_id_coord.dims - if cell_dim not in arr.dims: - raise ValueError( - f"exploration plotting only works with a spatial dimension ('{cell_dim}')" - ) - cell_ids = cell_id_coord.data grid_info = arr.dggs.grid_info diff --git a/xdggs/tests/test_plotting.py b/xdggs/tests/test_plotting.py index 88779ca5..22edeedf 100644 --- a/xdggs/tests/test_plotting.py +++ b/xdggs/tests/test_plotting.py @@ -176,9 +176,3 @@ def test_explore(arr, expected_type): actual = arr.dggs.explore() assert isinstance(actual, expected_type) - - -def test_explore_without_spatial_dim(): - arr = xr.DataArray([0, 1], dims="time") - with pytest.raises(ValueError, match="only works with a spatial dimension"): - arr.dggs.explore() From b8f894615c32190f001f343f87f019ae97b09df7 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Fri, 31 Jan 2025 15:55:58 +0100 Subject: [PATCH 5/5] extra tests for `MapContainer` --- xdggs/tests/test_plotting.py | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/xdggs/tests/test_plotting.py b/xdggs/tests/test_plotting.py index 22edeedf..ab72024c 100644 --- a/xdggs/tests/test_plotting.py +++ b/xdggs/tests/test_plotting.py @@ -147,6 +147,46 @@ def test_colorize(var, kwargs, expected): np.testing.assert_equal(actual, expected) +class TestMapContainer: + def test_init(self): + map_ = lonboard.Map(layers=[]) + sliders = ipywidgets.VBox( + [ipywidgets.IntSlider(min=0, max=10, description="time")] + ) + obj = xr.DataArray([[0, 1], [2, 3]], dims=["time", "cells"]) + colorize_kwargs = {"a": 1, "b": 2} + + container = plotting.MapContainer( + dimension_sliders=sliders, + map=map_, + obj=obj, + colorize_kwargs=colorize_kwargs, + ) + + assert container.map == map_ + xr.testing.assert_equal(container.obj, obj) + assert container.dimension_sliders == sliders + assert container.colorize_kwargs == colorize_kwargs + + def test_render(self): + map_ = lonboard.Map(layers=[]) + sliders = ipywidgets.VBox( + [ipywidgets.IntSlider(min=0, max=10, description="time")] + ) + obj = xr.DataArray([[0, 1], [2, 3]], dims=["time", "cells"]) + colorize_kwargs = {"a": 1, "b": 2} + + container = plotting.MapContainer( + dimension_sliders=sliders, + map=map_, + obj=obj, + colorize_kwargs=colorize_kwargs, + ) + rendered = container.render() + + assert isinstance(rendered, ipywidgets.VBox) + + @pytest.mark.parametrize( ["arr", "expected_type"], (