Skip to content

Fix np.digitize mapping values at the upper bin edge to an out-of-range index - #346

Merged
tomaskontrimas merged 4 commits into
masterfrom
fix-digitize-upper-edge
Nov 17, 2026
Merged

tomaskontrimas merged 4 commits into
masterfrom
fix-digitize-upper-edge

Conversation

@tomaskontrimas

Copy link
Copy Markdown
Collaborator

No description provided.

Comment on lines +364 to +368
log_true_e_idxs = np.clip(
np.digitize(log_true_e, bins=sm.true_e_bin_edges) - 1,
0,
sm.n_log10_true_e_bins - 1,
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm not 100% sure if we need to clip here as that should not happen, but it's probably better to be safe.

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.

But then we also need to make sure that the correct energy is stored in the log_true_energy key of the events record array maybe, no?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

okay, I've replaced the clip with 4f1badd because sometime later there is a check for isvalid events and in case log_true_e happened to be outside the valid range it wouldn't be injected 😅

Claude is complaining a bit but it's our fault that we didn't match aeff and sm binnings....

  A separate issue I noticed: in about 5–11 declination bins for IC40, IC59 and IC79, the spline can return energies slightly below the lower end of the valid range. That's because an effective-area
  bin whose centre is inside the range can have its lower edge outside it. Those energies are still inside the smearing matrix, but in a true-energy bin with no reconstructed-energy PDF, so those
  events come out as isvalid=False. This was already happening before the PR, so I'd leave it for a separate issue.

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.

oh well...

"""
return self._aeff_decnu_log10enu

def get_decnu_idx(self, decnu: float) -> int:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This change is here so that it merges with #342 with less conflicts

Comment on lines 338 to 340

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.

I think that, as it is written now, the last bin always includes the upper edge, right? Maybe update the docstring to reflect this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done 707a089

Comment on lines +364 to +368
log_true_e_idxs = np.clip(
np.digitize(log_true_e, bins=sm.true_e_bin_edges) - 1,
0,
sm.n_log10_true_e_bins - 1,
)

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.

But then we also need to make sure that the correct energy is stored in the log_true_energy key of the events record array maybe, no?

Comment on lines 82 to 83

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.

Let's use the new get_bin_indices_from_binedges method here too!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done 58a3c5a

@tomaskontrimas
tomaskontrimas force-pushed the fix-digitize-upper-edge branch from c89fc14 to 52419b3 Compare November 16, 2026 16:25

@chiarabellenghi chiarabellenghi left a comment

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.

Yep, ready! 🌟

@tomaskontrimas
tomaskontrimas force-pushed the fix-digitize-upper-edge branch from 4f1badd to 40ae2e6 Compare November 17, 2026 15:14
@tomaskontrimas
tomaskontrimas merged commit 0929602 into master Nov 17, 2026
6 checks passed
@tomaskontrimas
tomaskontrimas deleted the fix-digitize-upper-edge branch November 17, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants