MCPcopy
hub / github.com/pydata/xarray / test_interpolate_chunk_1d

Function test_interpolate_chunk_1d

xarray/tests/test_interp.py:903–966  ·  view source on GitHub ↗

Interpolate nd array with multiple independent indexers It should do a series of 1d interpolation

(
    method: InterpOptions, data_ndim, interp_ndim, nscalar, chunked: bool
)

Source from the content-addressed store, hash-verified

901)
902@pytest.mark.filterwarnings("ignore:Increasing number of chunks")
903def test_interpolate_chunk_1d(
904 method: InterpOptions, data_ndim, interp_ndim, nscalar, chunked: bool
905) -> None:
906 """Interpolate nd array with multiple independent indexers
907
908 It should do a series of 1d interpolation
909 """
910
911 if method in ["cubic", "pchip", "quintic"] and interp_ndim == 3:
912 pytest.skip("Too slow.")
913
914 # 3d non chunked data
915 x = np.linspace(0, 1, 6)
916 y = np.linspace(2, 4, 7)
917 z = np.linspace(-0.5, 0.5, 8)
918 da = xr.DataArray(
919 data=np.sin(x[:, np.newaxis, np.newaxis])
920 * np.cos(y[:, np.newaxis])
921 * np.exp(z),
922 coords=[("x", x), ("y", y), ("z", z)],
923 )
924
925 # choose the data dimensions
926 for data_dims in permutations(da.dims, data_ndim):
927 # select only data_ndim dim
928 da = da.isel( # take the middle line
929 {dim: len(da.coords[dim]) // 2 for dim in da.dims if dim not in data_dims}
930 )
931
932 # chunk data
933 da = da.chunk(chunks={dim: i + 1 for i, dim in enumerate(da.dims)})
934
935 # choose the interpolation dimensions
936 for interp_dims in permutations(da.dims, interp_ndim):
937 # choose the scalar interpolation dimensions
938 for scalar_dims in combinations(interp_dims, nscalar):
939 dest = {}
940 for dim in interp_dims:
941 if dim in scalar_dims:
942 # take the middle point
943 dest[dim] = 0.5 * (da.coords[dim][0] + da.coords[dim][-1])
944 else:
945 # pick some points, including outside the domain
946 before = 2 * da.coords[dim][0] - da.coords[dim][1]
947 after = 2 * da.coords[dim][-1] - da.coords[dim][-2]
948
949 dest[dim] = cast(
950 xr.DataArray,
951 np.linspace(
952 before.item(), after.item(), len(da.coords[dim]) * 13
953 ),
954 )
955 if chunked:
956 dest[dim] = xr.DataArray(data=dest[dim], dims=[dim])
957 dest[dim] = dest[dim].chunk(2)
958 actual = da.interp(method=method, **dest)
959 expected = da.compute().interp(method=method, **dest)
960

Callers

nothing calls this directly

Calls 9

iselMethod · 0.95
chunkMethod · 0.95
interpMethod · 0.95
computeMethod · 0.95
assert_identicalFunction · 0.90
linspaceMethod · 0.80
sinMethod · 0.80
chunkMethod · 0.45
interpMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…