| 6188 | "ignore:Behaviour of argmin/argmax with neither dim nor :FutureWarning" |
| 6189 | ) |
| 6190 | def test_argmin_dim( |
| 6191 | self, |
| 6192 | x: np.ndarray, |
| 6193 | minindex: list[int | float], |
| 6194 | maxindex: list[int | float], |
| 6195 | nanindex: list[int | None], |
| 6196 | ) -> None: |
| 6197 | ar = xr.DataArray( |
| 6198 | x, |
| 6199 | dims=["y", "x"], |
| 6200 | coords={"x": np.arange(x.shape[1]) * 4, "y": 1 - np.arange(x.shape[0])}, |
| 6201 | attrs=self.attrs, |
| 6202 | ) |
| 6203 | indarrnp = np.tile(np.arange(x.shape[1], dtype=np.intp), [x.shape[0], 1]) |
| 6204 | indarr = xr.DataArray(indarrnp, dims=ar.dims, coords=ar.coords) |
| 6205 | |
| 6206 | if np.isnan(minindex).any(): |
| 6207 | with pytest.raises(ValueError): |
| 6208 | ar.argmin(dim="x") |
| 6209 | return |
| 6210 | |
| 6211 | expected0list = [ |
| 6212 | indarr.isel(y=yi).isel(x=indi, drop=True) |
| 6213 | for yi, indi in enumerate(minindex) |
| 6214 | ] |
| 6215 | expected0 = {"x": xr.concat(expected0list, dim="y")} |
| 6216 | expected0[ |
| 6217 | "x" |
| 6218 | ].attrs = self.attrs # Default keeps attrs for reduction operations |
| 6219 | |
| 6220 | result0 = ar.argmin(dim=["x"]) |
| 6221 | for key in expected0: |
| 6222 | assert_identical(result0[key], expected0[key]) |
| 6223 | |
| 6224 | result1 = ar.argmin(dim=["x"], keep_attrs=True) |
| 6225 | expected1 = deepcopy(expected0) |
| 6226 | expected1["x"].attrs = self.attrs |
| 6227 | for key in expected1: |
| 6228 | assert_identical(result1[key], expected1[key]) |
| 6229 | |
| 6230 | minindex = [ |
| 6231 | x if y is None or ar.dtype.kind == "O" else y |
| 6232 | for x, y in zip(minindex, nanindex, strict=True) |
| 6233 | ] |
| 6234 | expected2list = [ |
| 6235 | indarr.isel(y=yi).isel(x=indi, drop=True) |
| 6236 | for yi, indi in enumerate(minindex) |
| 6237 | ] |
| 6238 | expected2 = {"x": xr.concat(expected2list, dim="y")} |
| 6239 | expected2[ |
| 6240 | "x" |
| 6241 | ].attrs = self.attrs # Default keeps attrs for reduction operations |
| 6242 | |
| 6243 | result2 = ar.argmin(dim=["x"], skipna=False) |
| 6244 | |
| 6245 | for key in expected2: |
| 6246 | assert_identical(result2[key], expected2[key]) |
| 6247 | |