Skip to content

fix: gridify() IndexError from numpy float // int staying float64 - #30

Merged
petercorke merged 2 commits into
mainfrom
fix/gridify-index-dtype
Jul 30, 2026
Merged

fix: gridify() IndexError from numpy float // int staying float64#30
petercorke merged 2 commits into
mainfrom
fix/gridify-index-dtype

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

  • BaseFeature2D.gridify() crashed with IndexError: arrays used as indices must be of integer (or boolean) type on every call, regardless of whether nbins was passed as a tuple or scalar.
  • Root cause: f.p[0] // binwidth / f.p[1] // binheightf.p is a numpy float array (shape (2,1)), and floor-dividing a numpy float array by a Python int still yields a float64 result, not an int. bins[iy, ix] then rejects the float-typed index.
  • Fix: int(f.p[0].item() // binwidth) — extract the scalar with .item() first, then cast.
  • gridify() had zero test coverage before this. Added two regression tests (scalar and tuple nbins). Verified genuinely: reverted just the source fix (kept the test), confirmed both tests fail with the exact reported IndexError, then restored the fix and confirmed they pass.
  • Found while verifying an unrelated bare-except cleanup on a different branch — this crash reproduced identically on unmodified main (confirmed via git stash), so it's pre-existing, not something that fix introduced.

Test plan

  • New tests fail on unfixed code with the exact reported error, pass with the fix (verified both directions)
  • Full suite: 818 passed (816 + 2 new), 15 skipped, no other regressions

BaseFeature2D.gridify() computed grid cell indices as
f.p[0] // binwidth / f.p[1] // binheight. f.p is a numpy float array
(feature point coordinates, shape (2,1)), and floor-dividing a numpy
float array by a Python int still yields a float64 result -- so
bins[iy, ix] always raised IndexError: arrays used as indices must be
of integer (or boolean) type. This affected every call to gridify()
regardless of whether nbins was passed as a tuple or scalar.

Fix: extract the scalar value with .item() before floor-dividing, then
cast to int explicitly.

Added regression tests (gridify() had zero prior coverage) that fail
with the exact reported IndexError against the unfixed code and pass
against the fix -- verified both directions directly before committing.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit b034e7e into main Jul 30, 2026
18 checks passed
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.

1 participant