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

Function test_differentiate

xarray/tests/test_dataset.py:7913–7957  ·  view source on GitHub ↗
(dask, edge_order)

Source from the content-addressed store, hash-verified

7911@pytest.mark.parametrize("dask", [True, False])
7912@pytest.mark.parametrize("edge_order", [1, 2])
7913def test_differentiate(dask, edge_order) -> None:
7914 rs = np.random.default_rng(42)
7915 coord = [0.2, 0.35, 0.4, 0.6, 0.7, 0.75, 0.76, 0.8]
7916
7917 da = xr.DataArray(
7918 rs.random((8, 6)),
7919 dims=["x", "y"],
7920 coords={"x": coord, "z": 3, "x2d": (("x", "y"), rs.random((8, 6)))},
7921 )
7922 if dask and has_dask:
7923 da = da.chunk({"x": 4})
7924
7925 ds = xr.Dataset({"var": da})
7926
7927 # along x
7928 actual = da.differentiate("x", edge_order)
7929 expected_x = xr.DataArray(
7930 np.gradient(da, da["x"], axis=0, edge_order=edge_order),
7931 dims=da.dims,
7932 coords=da.coords,
7933 )
7934 assert_equal(expected_x, actual)
7935 assert_equal(
7936 ds["var"].differentiate("x", edge_order=edge_order),
7937 ds.differentiate("x", edge_order=edge_order)["var"],
7938 )
7939 # coordinate should not change
7940 assert_equal(da["x"], actual["x"])
7941
7942 # along y
7943 actual = da.differentiate("y", edge_order)
7944 expected_y = xr.DataArray(
7945 np.gradient(da, da["y"], axis=1, edge_order=edge_order),
7946 dims=da.dims,
7947 coords=da.coords,
7948 )
7949 assert_equal(expected_y, actual)
7950 assert_equal(actual, ds.differentiate("y", edge_order=edge_order)["var"])
7951 assert_equal(
7952 ds["var"].differentiate("y", edge_order=edge_order),
7953 ds.differentiate("y", edge_order=edge_order)["var"],
7954 )
7955
7956 with pytest.raises(ValueError):
7957 da.differentiate("x2d")
7958
7959
7960@pytest.mark.parametrize("dask", [True, False])

Callers

nothing calls this directly

Calls 4

chunkMethod · 0.95
differentiateMethod · 0.95
differentiateMethod · 0.95
assert_equalFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…