|
4 | 4 |
|
5 | 5 | from __future__ import annotations |
6 | 6 |
|
| 7 | +import datetime as dt |
| 8 | + |
7 | 9 | import numpy as np |
8 | 10 | import pytest |
9 | 11 |
|
@@ -74,6 +76,55 @@ def test_image_rtol_config(self): |
74 | 76 | hv.Image({"vals": vals, "xs": xs, "ys": ys}, ["xs", "ys"], "vals") |
75 | 77 | hv.config.image_rtol = image_rtol |
76 | 78 |
|
| 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 | + |
77 | 128 | def test_image_clone(self): |
78 | 129 | vals = np.random.rand(20, 20) |
79 | 130 | xs = np.linspace(0, 10, 20) |
|
0 commit comments