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

Function test_integrate

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

Source from the content-addressed store, hash-verified

8038
8039@pytest.mark.parametrize("dask", [True, False])
8040def test_integrate(dask) -> None:
8041 rs = np.random.default_rng(42)
8042 coord = [0.2, 0.35, 0.4, 0.6, 0.7, 0.75, 0.76, 0.8]
8043
8044 da = xr.DataArray(
8045 rs.random((8, 6)),
8046 dims=["x", "y"],
8047 coords={
8048 "x": coord,
8049 "x2": (("x",), rs.random(8)),
8050 "z": 3,
8051 "x2d": (("x", "y"), rs.random((8, 6))),
8052 },
8053 )
8054 if dask and has_dask:
8055 da = da.chunk({"x": 4})
8056
8057 ds = xr.Dataset({"var": da})
8058
8059 # along x
8060 actual = da.integrate("x")
8061 # coordinate that contains x should be dropped.
8062 expected_x = xr.DataArray(
8063 trapezoid(da.compute(), da["x"], axis=0),
8064 dims=["y"],
8065 coords={k: v for k, v in da.coords.items() if "x" not in v.dims},
8066 )
8067 assert_allclose(expected_x, actual.compute())
8068 assert_equal(ds["var"].integrate("x"), ds.integrate("x")["var"])
8069
8070 # make sure result is also a dask array (if the source is dask array)
8071 assert isinstance(actual.data, type(da.data))
8072
8073 # along y
8074 actual = da.integrate("y")
8075 expected_y = xr.DataArray(
8076 trapezoid(da, da["y"], axis=1),
8077 dims=["x"],
8078 coords={k: v for k, v in da.coords.items() if "y" not in v.dims},
8079 )
8080 assert_allclose(expected_y, actual.compute())
8081 assert_equal(actual, ds.integrate("y")["var"])
8082 assert_equal(ds["var"].integrate("y"), ds.integrate("y")["var"])
8083
8084 # along x and y
8085 actual = da.integrate(("y", "x"))
8086 assert actual.ndim == 0
8087
8088 with pytest.raises(ValueError):
8089 da.integrate("x2d")
8090
8091
8092@requires_scipy

Callers

nothing calls this directly

Calls 9

chunkMethod · 0.95
integrateMethod · 0.95
computeMethod · 0.95
integrateMethod · 0.95
assert_allcloseFunction · 0.90
assert_equalFunction · 0.90
typeFunction · 0.85
itemsMethod · 0.80
computeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…