MCPcopy Index your code
hub / github.com/pydata/xarray / test_cumulative_integrate

Function test_cumulative_integrate

xarray/tests/test_dataset.py:8094–8151  ·  view source on GitHub ↗
(dask)

Source from the content-addressed store, hash-verified

8092@requires_scipy
8093@pytest.mark.parametrize("dask", [True, False])
8094def test_cumulative_integrate(dask) -> None:
8095 rs = np.random.default_rng(43)
8096 coord = [0.2, 0.35, 0.4, 0.6, 0.7, 0.75, 0.76, 0.8]
8097
8098 da = xr.DataArray(
8099 rs.random((8, 6)),
8100 dims=["x", "y"],
8101 coords={
8102 "x": coord,
8103 "x2": (("x",), rs.random(8)),
8104 "z": 3,
8105 "x2d": (("x", "y"), rs.random((8, 6))),
8106 },
8107 )
8108 if dask and has_dask:
8109 da = da.chunk({"x": 4})
8110
8111 ds = xr.Dataset({"var": da})
8112
8113 # along x
8114 actual = da.cumulative_integrate("x")
8115
8116 from scipy.integrate import cumulative_trapezoid
8117
8118 expected_x = xr.DataArray(
8119 cumulative_trapezoid(da.compute(), da["x"], axis=0, initial=0.0), # type: ignore[call-overload,unused-ignore]
8120 dims=["x", "y"],
8121 coords=da.coords,
8122 )
8123 assert_allclose(expected_x, actual.compute())
8124 assert_equal(
8125 ds["var"].cumulative_integrate("x"),
8126 ds.cumulative_integrate("x")["var"],
8127 )
8128
8129 # make sure result is also a dask array (if the source is dask array)
8130 assert isinstance(actual.data, type(da.data))
8131
8132 # along y
8133 actual = da.cumulative_integrate("y")
8134 expected_y = xr.DataArray(
8135 cumulative_trapezoid(da, da["y"], axis=1, initial=0.0), # type: ignore[call-overload,unused-ignore]
8136 dims=["x", "y"],
8137 coords=da.coords,
8138 )
8139 assert_allclose(expected_y, actual.compute())
8140 assert_equal(actual, ds.cumulative_integrate("y")["var"])
8141 assert_equal(
8142 ds["var"].cumulative_integrate("y"),
8143 ds.cumulative_integrate("y")["var"],
8144 )
8145
8146 # along x and y
8147 actual = da.cumulative_integrate(("y", "x"))
8148 assert actual.ndim == 2
8149
8150 with pytest.raises(ValueError):
8151 da.cumulative_integrate("x2d")

Callers

nothing calls this directly

Calls 9

chunkMethod · 0.95
cumulative_integrateMethod · 0.95
computeMethod · 0.95
cumulative_integrateMethod · 0.95
assert_allcloseFunction · 0.90
assert_equalFunction · 0.90
cumulative_trapezoidFunction · 0.85
typeFunction · 0.85
computeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…