(self)
| 2193 | |
| 2194 | @requires_scipy |
| 2195 | def test_upsample_interpolate(self) -> None: |
| 2196 | from scipy.interpolate import interp1d |
| 2197 | |
| 2198 | xs = np.arange(6) |
| 2199 | ys = np.arange(3) |
| 2200 | times = pd.date_range("2000-01-01", freq="6h", periods=5) |
| 2201 | |
| 2202 | z = np.arange(5) ** 2 |
| 2203 | data = np.tile(z, (6, 3, 1)) |
| 2204 | array = DataArray(data, {"time": times, "x": xs, "y": ys}, ("x", "y", "time")) |
| 2205 | |
| 2206 | expected_times = times.to_series().resample("1h").asfreq().index |
| 2207 | # Split the times into equal sub-intervals to simulate the 6 hour |
| 2208 | # to 1 hour up-sampling |
| 2209 | new_times_idx = np.linspace(0, len(times) - 1, len(times) * 5) |
| 2210 | kinds: list[InterpOptions] = [ |
| 2211 | "linear", |
| 2212 | "nearest", |
| 2213 | "zero", |
| 2214 | "slinear", |
| 2215 | "quadratic", |
| 2216 | "cubic", |
| 2217 | "polynomial", |
| 2218 | ] |
| 2219 | for kind in kinds: |
| 2220 | kwargs = {} |
| 2221 | if kind == "polynomial": |
| 2222 | kwargs["order"] = 1 |
| 2223 | actual = array.resample(time="1h").interpolate(kind, **kwargs) |
| 2224 | # using interp1d, polynomial order is to set directly in kind using int |
| 2225 | f = interp1d( |
| 2226 | np.arange(len(times)), |
| 2227 | data, |
| 2228 | kind=kwargs["order"] if kind == "polynomial" else kind, # type: ignore[arg-type,unused-ignore] |
| 2229 | axis=-1, |
| 2230 | bounds_error=True, |
| 2231 | assume_sorted=True, |
| 2232 | ) |
| 2233 | expected_data = f(new_times_idx) |
| 2234 | expected = DataArray( |
| 2235 | expected_data, |
| 2236 | {"time": expected_times, "x": xs, "y": ys}, |
| 2237 | ("x", "y", "time"), |
| 2238 | ) |
| 2239 | # Use AllClose because there are some small differences in how |
| 2240 | # we upsample timeseries versus the integer indexing as I've |
| 2241 | # done here due to floating point arithmetic |
| 2242 | assert_allclose(expected, actual, rtol=1e-16) |
| 2243 | |
| 2244 | @requires_scipy |
| 2245 | def test_upsample_interpolate_bug_2197(self) -> None: |
nothing calls this directly
no test coverage detected