Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #236 fix indexing in mode method regridding #237

Merged
merged 6 commits into from
Apr 29, 2024
Merged
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
12 changes: 12 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ All notable changes to this project will be documented in this file.
The format is based on `Keep a Changelog`_, and this project adheres to
`Semantic Versioning`_.

[Unreleased]
------------

Fixed
~~~~~

- Fixed indexing bug in the ``"mode"`` method in
:class:`xugrid.CentroidLocatorRegridder`, :class:`xugrid.OverlapRegridder`,
:class:`xugrid.RelativeOverlapRegridder`, which gave the method the tendency
to repeat the first value in the source grid across the target grid.


[0.9.0] 2024-02-15
------------------

Expand Down
15 changes: 15 additions & 0 deletions tests/test_regrid/test_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ def test_mode(args):
# The weights shouldn't be mutated!
assert np.allclose(weights, 0.5)

values = np.array([-1, 1, 1, 3, 4])
indices = np.array([1, 2, 3])
weights = np.array([1.0, 1.0, 1.0])
args = (values, indices, weights)
actual = reduce.mode(*args)
assert np.allclose(actual, 1.0)

values = np.array([99, 1, 2, 3, 4, 5, 6, 7, 8])
indices = np.array([4, 5, 6])
weights = np.array([0.5, 0.5, 0.5])
args = (values, indices, weights)
actual = reduce.mode(*args)
assert np.allclose(actual, 4)
assert np.allclose(weights, 0.5)


def test_median(args):
actual = reduce.median(*args)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_regrid/test_regridder.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ def test_overlap_regridder_structured(
assert broadcasted.dims == ("layer", "y", "x")
assert (broadcasted.isel(layer=0) == expected_results_overlap).any()

# Test if "mode" method doesn't repeat first values again
# https://github.com/Deltares/xugrid/issues/236
grid_data_adapted = grid_data_a.copy()
grid_data_adapted[0, 0] = 99
regridder = OverlapRegridder(
source=grid_data_adapted, target=grid_data_a, method="mode"
)
result = regridder.regrid(grid_data_adapted)
assert not np.all(result == 99.0)


def test_overlap_regridder(disk, quads_1):
square = quads_1
Expand Down
3 changes: 1 addition & 2 deletions xugrid/regrid/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ def mode(values, indices, weights):
return np.nan
else: # Find value with highest frequency
w_max = 0
for i in range(accum.size):
w_accum = accum[i]
for w_accum, i in zip(accum, indices):
if w_accum > w_max:
w_max = w_accum
v = values[i]
Expand Down
Loading