Skip to content

fix: Account for fuzzy edge values in rebinning with edges #999

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

riga
Copy link

@riga riga commented Apr 13, 2025

This PR fixes the rebinning of axes given a sequence of new edges that was recently added by #977.

Background

We recently discovered the new rebinning feature that only requires new edges values rather than bin count "groups", which is great! It requires that the requested edges coincide (that's where the problem is) with old ones so that bin content merging is logically unambiguous. However, we ran into an edge case where a single, new edge value is subject to float precision effects (e.g. 1.200000000000001).

The issue

Currently, this causes a bug in that the newly computed edges are missing an entry, resulting in axes dropping a bin. I extended the test_rebin_1d test case to use a fuzzy edge value, showing that the assertion in

assert all(
np.isclose(
axis.edges[np.abs(axis.edges - edge).argmin()],
edge,
)
for edge in newedges
), "Edges must be in the axis"
matched_ixes = np.where(
np.isin(
axis.edges,
newedges,
)
)[0]

passes due to np.isclose, but the subsequent index lookup via an exact np.isin fails: N edges were requested, but the rebinned axis only has N - 1.

Fix

I already prepared and tested a working fix,

matched_ixes = [np.abs(axis.edges - edge).argmin() for edge in newedges]
assert all(
    np.isclose(
        axis.edges[ix],
        edge,
    )
    for ix, edge in zip(matched_ixes, newedges)
), "Edges must be in the axis"

essentially storing the argmin results, doing the assertion, and then computing new edges with them.

If you agree, I can add the commit to this PR, but I thought it might be easier to run the test case and see that it indeed fails where it should/shouldn't.

@github-actions github-actions bot added the needs changelog Might need a changelog entry label Apr 13, 2025
@andrzejnovak
Copy link
Member

Thanks for catching that @riga!

@henryiii
Copy link
Member

The fix sounds good. Also, should this be an assert, or a raise AssertionError (or another Error)? Would we really want this check skipped if running without assertions?

@riga
Copy link
Author

riga commented Apr 14, 2025

Good point. Imho, all of the checks should be mandatory. I'm changing this as well.

)
if not all_close:
msg = "Edges must be in the axis"
raise ValueError(msg)
Copy link
Member

Choose a reason for hiding this comment

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

Could we emit the offending edge? It would make for easier debug by users. Something like

f"Edge(s) {missing_edges} not found in the axis. Nearest available edges are ..."

Copy link
Author

Choose a reason for hiding this comment

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

Sure, just added that.

@henryiii henryiii added the needs backport Should be backported to an earlier version label Apr 14, 2025
@henryiii
Copy link
Member

I've found another (unrelated) bug, so likely will be making a patch release in the near future; I'll backport this too.

@riga
Copy link
Author

riga commented Apr 14, 2025

I noticed that the test should actually compare against the true (exact) edges, which failed the tests. Should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs backport Should be backported to an earlier version needs changelog Might need a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants