Skip to content

Commit

Permalink
Raise error if attempting filled contours using datetime spatial coor…
Browse files Browse the repository at this point in the history
…dinates
  • Loading branch information
ianthomas23 committed Oct 13, 2023
1 parent b33418e commit 0d653a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions holoviews/operation/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ def _process(self, element, key=None):
# if any data is a datetime, transform to matplotlib's numerical format
data_is_datetime = tuple(isdatetime(arr) for k, arr in enumerate(data))
if any(data_is_datetime):
if any(data_is_datetime[:2]) and self.p.filled:
raise RuntimeError("Datetime spatial coordinates are not supported "
"for filled contour calculations.")
data = tuple(
date2num(d) if is_datetime else d
for d, is_datetime in zip(data, data_is_datetime)
Expand Down
29 changes: 17 additions & 12 deletions holoviews/tests/operation/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,16 @@ def test_qmesh_curvilinear_edges_contours(self):
self.assertEqual(op_contours, contour)

def test_image_contours_filled(self):
img = Image(np.array([[0, 1, 0], [3, 4, 5.], [6, 7, 8]]))
op_contours = contours(img, filled=True, levels=[2, 2.5])
data = [[(-0.333333, 0.111111, 2.25), (-0.333333, 0.055556, 2.25), (0., 0.166667, 2.25),
(0.333333, 0.166667, 2.25), (0.333333, 0.2, 2.25), (0., 0.222222, 2.25), (-0.333333, 0.111111, 2.25)]]
polys = Polygons(data, vdims=img.vdims[0].clone(range=(2, 2.5)))
img = Image(np.array([[0, 0, 0], [0, 1, 0.], [0, 0, 0]]))
# Single polygon with hole
op_contours = contours(img, filled=True, levels=[0.25, 0.75])
data = [[(-0.25, 0.0, 0.5), (0.0, -0.25, 0.5), (0.25, 0.0, 0.5), (0.0, 0.25, 0.5),
(-0.25, 0.0, 0.5)]]
polys = Polygons(data, vdims=img.vdims[0].clone(range=(0.25, 0.75)))
self.assertEqual(op_contours, polys)
expected_holes = [[[np.array([[0.0, -0.08333333], [-0.08333333, 0.0], [0.0, 0.08333333],
[0.08333333, 0.0], [0.0, -0.08333333]])]]]
np.testing.assert_array_almost_equal(op_contours.holes(), expected_holes)

def test_image_contours_filled_empty(self):
img = Image(np.array([[0, 1, 0], [3, 4, 5.], [6, 7, 8]]))
Expand All @@ -206,13 +210,14 @@ def test_image_contours_filled_empty(self):
polys = Polygons(None, vdims=img.vdims[0].clone(range=(20.0, 23.0)))
self.assertEqual(op_contours, polys)



# dates on x, y, and xy ...




def test_image_contours_filled_xy_datetime(self):
x = np.array(['2023-09-01', '2023-09-02', '2023-09-03'], dtype='datetime64')
y = np.array(['2023-10-07', '2023-10-08', '2023-10-09'], dtype='datetime64')
z = np.array([[0, 0, 0], [0, 1, 0.], [0, 0, 0]])
img = Image((x, y, z))
msg = 'Datetime spatial coordinates are not supported for filled contour calculations.'
with pytest.raises(RuntimeError, match=msg):
_ = contours(img, filled=True, levels=[0.5, 2.0])

def test_points_histogram(self):
points = Points([float(i) for i in range(10)])
Expand Down

0 comments on commit 0d653a5

Please sign in to comment.