Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/xarray_regrid/methods/flox_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this issue!

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)
Expand Down
23 changes: 21 additions & 2 deletions tests/test_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,30 @@ def test_most_common(dummy_lc_data, dummy_target_grid):


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to keep this test but with a correct expected outcome rather than a new test that doesn't reuse the old fixtures.

i.e.;

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",
)

This expected result will fail on main but is correct for this fixed branch.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f1592cc. test_least_common is back on dummy_lc_data and dummy_target_grid with your expected array, and the separate example is gone. It fails on main and passes here.

dummy_lc_data["lc"].regrid.least_common(
# Labels absent from a cell must not be selected.
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",
)
input_data_int = dummy_lc_data["lc"].astype("uint8")

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 regrid_result.dtype == input_data_int.dtype


def test_oversized_most_common(dummy_lc_data, oversized_dummy_target_grid):
Expand Down