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

Method test_idxmin

xarray/tests/test_dataarray.py:5899–6039  ·  view source on GitHub ↗
(
        self,
        x: np.ndarray,
        minindex: list[int | float],
        maxindex: list[int | float],
        nanindex: list[int | None],
        use_dask: bool,
    )

Source from the content-addressed store, hash-verified

5897 "use_dask", [pytest.param(True, id="dask"), pytest.param(False, id="nodask")]
5898 )
5899 def test_idxmin(
5900 self,
5901 x: np.ndarray,
5902 minindex: list[int | float],
5903 maxindex: list[int | float],
5904 nanindex: list[int | None],
5905 use_dask: bool,
5906 ) -> None:
5907 if use_dask and not has_dask:
5908 pytest.skip("requires dask")
5909 if use_dask and x.dtype.kind == "M":
5910 pytest.xfail("dask operation 'argmin' breaks when dtype is datetime64 (M)")
5911
5912 if x.dtype.kind == "O":
5913 # TODO: nanops._nan_argminmax_object computes once to check for all-NaN slices.
5914 max_computes = 1
5915 else:
5916 max_computes = 0
5917
5918 ar0_raw = xr.DataArray(
5919 x,
5920 dims=["y", "x"],
5921 coords={"x": np.arange(x.shape[1]) * 4, "y": 1 - np.arange(x.shape[0])},
5922 attrs=self.attrs,
5923 )
5924
5925 if use_dask:
5926 ar0 = ar0_raw.chunk({})
5927 else:
5928 ar0 = ar0_raw
5929
5930 assert_identical(ar0, ar0)
5931
5932 # No dimension specified
5933 with pytest.raises(ValueError):
5934 ar0.idxmin()
5935
5936 # dim doesn't exist
5937 with pytest.raises(KeyError):
5938 ar0.idxmin(dim="Y")
5939
5940 assert_identical(ar0, ar0)
5941
5942 coordarr0 = xr.DataArray(
5943 np.tile(ar0.coords["x"], [x.shape[0], 1]), dims=ar0.dims, coords=ar0.coords
5944 )
5945
5946 hasna = [np.isnan(x) for x in minindex]
5947 coordarr1 = coordarr0.copy()
5948 coordarr1[hasna, :] = 1
5949 minindex0 = [x if not np.isnan(x) else 0 for x in minindex]
5950
5951 nan_mult_0 = np.array([np.nan if x else 1 for x in hasna])[:, None]
5952 expected0list = [
5953 (coordarr1 * nan_mult_0).isel(y=yi).isel(x=indi, drop=True)
5954 for yi, indi in enumerate(minindex0)
5955 ]
5956 expected0 = xr.concat(expected0list, dim="y")

Callers

nothing calls this directly

Calls 10

chunkMethod · 0.95
copyMethod · 0.95
iselMethod · 0.95
assert_identicalFunction · 0.90
raise_if_dask_computesFunction · 0.90
arangeMethod · 0.80
idxminMethod · 0.45
iselMethod · 0.45
concatMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected