From c18e748bc82c081e0ddd4cc378518388222d73cd Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Fri, 7 Jul 2023 13:30:03 -0400 Subject: [PATCH 1/3] Support variety of data inputs --- geoviews/element/geo.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/geoviews/element/geo.py b/geoviews/element/geo.py index f055ce49..85fd5b1e 100644 --- a/geoviews/element/geo.py +++ b/geoviews/element/geo.py @@ -353,29 +353,30 @@ class WindBarbs(_Element, Selection2DExpr, HvGeometry): @classmethod def from_uv(cls, data, kdims=None, vdims=None, **params): - if isinstance(data, tuple): - xs, ys, us, vs = data - else: - us = data[vdims[0]] - vs = data[vdims[1]] + if kdims is None: + kdims = ['x', 'y'] + if vdims is None: + vdims = ['u', 'v'] + dataset = Dataset(data, kdims=kdims, vdims=vdims, **params) + us, vs = (dataset.dimension_values(i) for i in range(2, 4)) uv_magnitudes = np.hypot(us, vs) # unscaled radians = np.pi / 2 - np.arctan2(-vs, -us) - if isinstance(data, tuple): - reorganized_data = (xs, ys, radians, uv_magnitudes) - else: - # calculations on this data could mutate the original data - # here we do not do any calculations; we only store the data - reorganized_data = {} - for kdim in kdims: - reorganized_data[kdim] = data[kdim] - reorganized_data["Angle"] = radians - reorganized_data["Magnitude"] = uv_magnitudes - for vdim in vdims[2:]: - reorganized_data[vdim] = data[vdim] - vdims = ["Angle", "Magnitude"] + vdims[2:] - return cls(reorganized_data, kdims=kdims, vdims=vdims, **params) + # calculations on this data could mutate the original data + # here we do not do any calculations; we only store the data + repackaged_dataset = {} + for kdim in kdims: + repackaged_dataset[kdim] = dataset[kdim] + repackaged_dataset["Angle"] = radians + repackaged_dataset["Magnitude"] = uv_magnitudes + for vdim in vdims[2:]: + repackaged_dataset[vdim] = dataset[vdim] + vdims = [ + Dimension('Angle', cyclic=True, range=(0, 2 * np.pi)), + Dimension('Magnitude') + ] + vdims[2:] + return cls(repackaged_dataset, kdims=kdims, vdims=vdims, **params) class Image(_Element, HvImage): From adfb5af716b3ca5f21f06be35d85a978af9d87b8 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Fri, 7 Jul 2023 22:31:09 -0400 Subject: [PATCH 2/3] Fix tests --- geoviews/tests/plotting/mpl/test_chart.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geoviews/tests/plotting/mpl/test_chart.py b/geoviews/tests/plotting/mpl/test_chart.py index ed48fc63..8921617a 100644 --- a/geoviews/tests/plotting/mpl/test_chart.py +++ b/geoviews/tests/plotting/mpl/test_chart.py @@ -54,7 +54,7 @@ def test_windbarbs_dataset(self): np.testing.assert_almost_equal(mpl_barbs.v.data, V.T.flatten()) def test_windbarbs_from_uv(self): - x = np.linspace(-1, 1, 4) + x = np.linspace(-1, 1, 2) X, Y = np.meshgrid(x, x) U, V = 10 * X, 2 * Y @@ -64,8 +64,8 @@ def test_windbarbs_from_uv(self): gv_barbs = WindBarbs((X, Y, angle, mag)) gv_barbs_uv = WindBarbs.from_uv((X, Y, U, V)) - np.testing.assert_almost_equal(gv_barbs.data["Angle"], gv_barbs_uv.data["Angle"]) - np.testing.assert_almost_equal(gv_barbs.data["Magnitude"], gv_barbs_uv.data["Magnitude"]) + np.testing.assert_almost_equal(gv_barbs.data["Angle"].T.flatten(), gv_barbs_uv.data["Angle"]) + np.testing.assert_almost_equal(gv_barbs.data["Magnitude"].T.flatten(), gv_barbs_uv.data["Magnitude"]) def test_windbarbs_dataset_from_uv_other_dim(self): x = np.linspace(-1, 1, 4) From a332141f032e1b29b96a074733d94a934fc29a66 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Mon, 10 Jul 2023 10:11:02 -0400 Subject: [PATCH 3/3] Address comments --- geoviews/element/geo.py | 3 ++- geoviews/tests/plotting/mpl/test_chart.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/geoviews/element/geo.py b/geoviews/element/geo.py index 85fd5b1e..18da9368 100644 --- a/geoviews/element/geo.py +++ b/geoviews/element/geo.py @@ -358,9 +358,10 @@ def from_uv(cls, data, kdims=None, vdims=None, **params): if vdims is None: vdims = ['u', 'v'] dataset = Dataset(data, kdims=kdims, vdims=vdims, **params) - us, vs = (dataset.dimension_values(i) for i in range(2, 4)) + us, vs = (dataset.dimension_values(i) for i in (2, 3)) uv_magnitudes = np.hypot(us, vs) # unscaled + # using meteorological convention (direction FROM which wind blows) radians = np.pi / 2 - np.arctan2(-vs, -us) # calculations on this data could mutate the original data diff --git a/geoviews/tests/plotting/mpl/test_chart.py b/geoviews/tests/plotting/mpl/test_chart.py index 8921617a..0af2cb17 100644 --- a/geoviews/tests/plotting/mpl/test_chart.py +++ b/geoviews/tests/plotting/mpl/test_chart.py @@ -54,7 +54,7 @@ def test_windbarbs_dataset(self): np.testing.assert_almost_equal(mpl_barbs.v.data, V.T.flatten()) def test_windbarbs_from_uv(self): - x = np.linspace(-1, 1, 2) + x = np.linspace(-1, 1, 4) X, Y = np.meshgrid(x, x) U, V = 10 * X, 2 * Y