Skip to content

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

Merged
henryiii merged 4 commits into
scikit-hep:developfrom
riga:fix/rebin_edges
Apr 15, 2025
Merged

fix: Account for fuzzy edge values in rebinning with edges#999
henryiii merged 4 commits into
scikit-hep:developfrom
riga:fix/rebin_edges

Conversation

@riga
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Member

Thanks for catching that @riga!

@henryiii
Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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.

@henryiii henryiii merged commit 04e2e7b into scikit-hep:develop Apr 15, 2025
18 checks passed
@henryiii
Copy link
Copy Markdown
Member

Thanks!

@riga riga deleted the fix/rebin_edges branch April 15, 2025 14:46
@henryiii henryiii removed the needs changelog Might need a changelog entry label Feb 2, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants