(
self,
x: np.ndarray,
minindex: list[int | float],
maxindex: list[int | float],
nanindex: list[int | None],
use_dask: bool,
)
| 6042 | "use_dask", [pytest.param(True, id="dask"), pytest.param(False, id="nodask")] |
| 6043 | ) |
| 6044 | def test_idxmax( |
| 6045 | self, |
| 6046 | x: np.ndarray, |
| 6047 | minindex: list[int | float], |
| 6048 | maxindex: list[int | float], |
| 6049 | nanindex: list[int | None], |
| 6050 | use_dask: bool, |
| 6051 | ) -> None: |
| 6052 | if use_dask and not has_dask: |
| 6053 | pytest.skip("requires dask") |
| 6054 | if use_dask and x.dtype.kind == "M": |
| 6055 | pytest.xfail("dask operation 'argmax' breaks when dtype is datetime64 (M)") |
| 6056 | |
| 6057 | if x.dtype.kind == "O": |
| 6058 | # TODO: nanops._nan_argminmax_object computes once to check for all-NaN slices. |
| 6059 | max_computes = 1 |
| 6060 | else: |
| 6061 | max_computes = 0 |
| 6062 | |
| 6063 | ar0_raw = xr.DataArray( |
| 6064 | x, |
| 6065 | dims=["y", "x"], |
| 6066 | coords={"x": np.arange(x.shape[1]) * 4, "y": 1 - np.arange(x.shape[0])}, |
| 6067 | attrs=self.attrs, |
| 6068 | ) |
| 6069 | |
| 6070 | if use_dask: |
| 6071 | ar0 = ar0_raw.chunk({}) |
| 6072 | else: |
| 6073 | ar0 = ar0_raw |
| 6074 | |
| 6075 | # No dimension specified |
| 6076 | with pytest.raises(ValueError): |
| 6077 | ar0.idxmax() |
| 6078 | |
| 6079 | # dim doesn't exist |
| 6080 | with pytest.raises(KeyError): |
| 6081 | ar0.idxmax(dim="Y") |
| 6082 | |
| 6083 | ar1 = ar0.copy() |
| 6084 | del ar1.coords["y"] |
| 6085 | with pytest.raises(KeyError): |
| 6086 | ar1.idxmax(dim="y") |
| 6087 | |
| 6088 | coordarr0 = xr.DataArray( |
| 6089 | np.tile(ar0.coords["x"], [x.shape[0], 1]), dims=ar0.dims, coords=ar0.coords |
| 6090 | ) |
| 6091 | |
| 6092 | hasna = [np.isnan(x) for x in maxindex] |
| 6093 | coordarr1 = coordarr0.copy() |
| 6094 | coordarr1[hasna, :] = 1 |
| 6095 | maxindex0 = [x if not np.isnan(x) else 0 for x in maxindex] |
| 6096 | |
| 6097 | nan_mult_0 = np.array([np.nan if x else 1 for x in hasna])[:, None] |
| 6098 | expected0list = [ |
| 6099 | (coordarr1 * nan_mult_0).isel(y=yi).isel(x=indi, drop=True) |
| 6100 | for yi, indi in enumerate(maxindex0) |
| 6101 | ] |
nothing calls this directly
no test coverage detected