Skip to content

Commit 2134030

Browse files
authored
fix: Allow image coordinate irregularity equal to the tolerance (#6915)
1 parent 1dee097 commit 2134030

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

holoviews/core/util/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2252,7 +2252,7 @@ def validate_regular_sampling(values, rtol=10e-6):
22522252
if len(diffs) < 1:
22532253
return True
22542254
min_diff = diffs.min()
2255-
return abs(min_diff - diffs.max()) < abs(min_diff * rtol)
2255+
return abs(min_diff - diffs.max()) <= abs(min_diff * rtol)
22562256

22572257

22582258
def compute_density(start, end, length, time_unit="us"):

holoviews/tests/element/test_image.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from __future__ import annotations
66

7+
import datetime as dt
8+
79
import numpy as np
810
import pytest
911

@@ -74,6 +76,55 @@ def test_image_rtol_config(self):
7476
hv.Image({"vals": vals, "xs": xs, "ys": ys}, ["xs", "ys"], "vals")
7577
hv.config.image_rtol = image_rtol
7678

79+
@pytest.mark.parametrize(
80+
"x",
81+
[
82+
# max(|step|) / min(|step|) - 1 = 0.0015
83+
[dt.datetime(2017, 1, 1, 0, 0, 0, ms) for ms in [0, 10000, 20015]],
84+
*(
85+
np.array([0, 10000, 20015]).astype(f"datetime64[{unit}]")
86+
for unit in ["D", "h", "m", "s", "ms", "us"]
87+
),
88+
],
89+
)
90+
def test_image_datetime_rtol_failure(self, x):
91+
"""
92+
Constructing an Image with a date-time coordinate warns about irregular sampling
93+
if, approximately speaking, one less than the ratio of the max and min steps
94+
exceeds the default `image_rtol`.
95+
"""
96+
y = np.arange(2)
97+
hv.Image((x, y, np.zeros((len(y), len(x)))))
98+
self.log_handler.assert_endswith(
99+
"WARNING",
100+
"set a higher tolerance on hv.config.image_rtol or the rtol parameter in "
101+
"the Image constructor.",
102+
)
103+
104+
@pytest.mark.parametrize(
105+
"x",
106+
[
107+
# |granularity / step| = 1/400 = 0.0025
108+
[dt.datetime(2017, 1, 1, 0, 0, 0, ms) for ms in [0, 400]],
109+
# |granularity / step| = 1/800 = 0.00125
110+
*(
111+
np.array([10000, 10800]).astype(f"datetime64[{unit}]")
112+
for unit in ["D", "h", "m", "s", "ms", "us"]
113+
),
114+
],
115+
)
116+
def test_image_datetime_rtol_granularity(self, x):
117+
"""
118+
Constructing an Image with an evenly spaced date-time coordinate does not warn
119+
about irregular sampling, even when the ratio of the date-time granularity to
120+
the coordinate step is greater than the default `image_rtol` (or twice
121+
`image_rtol` for `dt.datetime` types because of how `dt.timedelta` instances
122+
round when multiplied by a `float`).
123+
"""
124+
y = np.arange(2)
125+
hv.Image((x, y, np.zeros((len(y), len(x)))))
126+
assert len(self.log_handler.tail("WARNING", n=1)) == 0
127+
77128
def test_image_clone(self):
78129
vals = np.random.rand(20, 20)
79130
xs = np.linspace(0, 10, 20)

0 commit comments

Comments
 (0)