From 146c143fd37f0567e8a44b19ca8f59112fedd4ab Mon Sep 17 00:00:00 2001 From: Zackary Loevseth <307867630+ZackaryLoevseth@users.noreply.github.com> Date: Fri, 20 Nov 2026 05:26:13 +0000 Subject: [PATCH 1/2] Ignore labels absent from a cell in least_common compute_mode counts with fill_value=-1, so a label missing from a cell got a count of -1 and idxmin returned it. Absent labels are now set to the count dtype's maximum before idxmin, which keeps the integer output dtype. The run-only test_least_common is replaced with the example from #77. --- src/xarray_regrid/methods/flox_reduce.py | 7 ++++++- tests/test_reduce.py | 22 +++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/xarray_regrid/methods/flox_reduce.py b/src/xarray_regrid/methods/flox_reduce.py index 6045306..f0c37ec 100644 --- a/src/xarray_regrid/methods/flox_reduce.py +++ b/src/xarray_regrid/methods/flox_reduce.py @@ -180,7 +180,12 @@ def compute_mode( expected_groups=(pd.Index(values.astype(data)), *bounds), fill_value=-1, ) - result = result.idxmax(array_name) if not anti_mode else result.idxmin(array_name) + if anti_mode: + # Labels absent from a cell have the fill count (-1): exclude them. + absent = np.iinfo(result.dtype).max + result = result.where(result > 0, absent).idxmin(array_name) + else: + result = result.idxmax(array_name) result = restore_properties(result, data, target_ds, coords, fill_value) result = result.reindex_like(target_coords, copy=False) diff --git a/tests/test_reduce.py b/tests/test_reduce.py index fbc7a21..3c6d4f2 100644 --- a/tests/test_reduce.py +++ b/tests/test_reduce.py @@ -110,12 +110,24 @@ def test_most_common(dummy_lc_data, dummy_target_grid): assert regrid_result.dtype == input_data_int.dtype -def test_least_common(dummy_lc_data, dummy_target_grid): - # Currently just test if the method runs: code is 99% the same as most_common - dummy_lc_data["lc"].regrid.least_common( - dummy_target_grid, - values=EXP_LABELS, +def test_least_common(): + # Each 2x2 block is one target cell: {3,3,3,4}, {1,1,1,2}, {2,2,2,2}, {4,4,4,3}. + # Labels absent from a cell must not be selected. + axis = [0.5, 1.5, 2.5, 3.5] + data = xr.DataArray( + np.array( + [[3, 3, 1, 1], [3, 4, 1, 2], [2, 2, 4, 4], [2, 2, 4, 3]], dtype="uint8" + ), + dims=("y", "x"), + coords={"y": axis, "x": axis}, + name="lc", ) + target = xr.Dataset(coords={"y": [1.0, 3.0], "x": [1.0, 3.0]}) + + result = data.regrid.least_common(target, values=np.array([1, 2, 3, 4])) + + assert_array_equal(result.values, [[4, 2], [2, 3]]) + assert result.dtype == data.dtype def test_oversized_most_common(dummy_lc_data, oversized_dummy_target_grid): From f1592ccc09816713b3a5c75b877d676e7b3fd182 Mon Sep 17 00:00:00 2001 From: Zackary Loevseth <307867630+ZackaryLoevseth@users.noreply.github.com> Date: Fri, 27 Nov 2026 15:41:48 +0000 Subject: [PATCH 2/2] Test least_common on the existing fixtures Use dummy_lc_data and dummy_target_grid with the expected grid from the review instead of a separate example. The test fails on main and passes with the fix. --- tests/test_reduce.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/tests/test_reduce.py b/tests/test_reduce.py index 3c6d4f2..fa52115 100644 --- a/tests/test_reduce.py +++ b/tests/test_reduce.py @@ -110,24 +110,31 @@ def test_most_common(dummy_lc_data, dummy_target_grid): assert regrid_result.dtype == input_data_int.dtype -def test_least_common(): - # Each 2x2 block is one target cell: {3,3,3,4}, {1,1,1,2}, {2,2,2,2}, {4,4,4,3}. +def test_least_common(dummy_lc_data, dummy_target_grid): # Labels absent from a cell must not be selected. - axis = [0.5, 1.5, 2.5, 3.5] - data = xr.DataArray( - np.array( - [[3, 3, 1, 1], [3, 4, 1, 2], [2, 2, 4, 4], [2, 2, 4, 3]], dtype="uint8" - ), - dims=("y", "x"), - coords={"y": axis, "x": axis}, - name="lc", + expected_data = np.array( + [ + [2, 2, 0, 0, 0, 0], + [0, 2, 2, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + [3, 0, 0, 0, 0, 1], + ], + dtype="uint8", ) - target = xr.Dataset(coords={"y": [1.0, 3.0], "x": [1.0, 3.0]}) + input_data_int = dummy_lc_data["lc"].astype("uint8") - result = data.regrid.least_common(target, values=np.array([1, 2, 3, 4])) + regrid_result = input_data_int.regrid.least_common( + dummy_target_grid, + values=EXP_LABELS, + ) + xr.testing.assert_equal( + regrid_result, + make_expected_ds(expected_data)["lc"], + ) - assert_array_equal(result.values, [[4, 2], [2, 3]]) - assert result.dtype == data.dtype + assert regrid_result.dtype == input_data_int.dtype def test_oversized_most_common(dummy_lc_data, oversized_dummy_target_grid):