Skip to content

Fix NearestNeighborMatch crash when the control pool runs below ratio#934

Merged
jeongyoonlee merged 1 commit into
uber:masterfrom
arpitjain099:chore/fix-nnmatch-control-pool
Jul 8, 2026
Merged

Fix NearestNeighborMatch crash when the control pool runs below ratio#934
jeongyoonlee merged 1 commit into
uber:masterfrom
arpitjain099:chore/fix-nnmatch-control-pool

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

Summary

NearestNeighborMatch.match() with replace=False crashes on datasets where the pool of still-unmatched controls shrinks to ratio or fewer units. It calls np.argpartition(dist, self.ratio) on the remaining controls, and once that pool holds ratio or fewer elements argpartition raises ValueError: kth(=...) out of bounds. A balanced 1:1 dataset hits this on the very last treatment unit, so plain 1:1 matching without replacement fails on any balanced (or control-scarce) input.

The fix caps the partition index at the last available position and stops early once no unmatched controls remain. The abundant-control path is untouched, so existing behavior and the current tests are unchanged.

Test plan

  • Added a regression test in tests/test_match.py covering the balanced 1:1 case (previously raised kth out of bounds) and the control-scarce case.
  • pytest tests/test_match.py passes locally.

match() with replace=False called np.argpartition(dist, self.ratio)
on the pool of still-unmatched controls. As controls get matched and
removed from the pool, that pool shrinks, and once it holds ratio or
fewer units argpartition raised "kth out of bounds". A balanced 1:1
match hits this on the very last treatment unit, so replace=False
matching failed on balanced (and control-scarce) datasets.

Cap the partition index at the last available position and stop early
when no unmatched controls remain. The abundant-control path is
unchanged; a regression test covers the balanced 1:1 and
control-scarce cases.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>

@jeongyoonlee jeongyoonlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@jeongyoonlee jeongyoonlee merged commit efe385a into uber:master Jul 8, 2026
7 checks passed
@jeongyoonlee jeongyoonlee added the bug Something isn't working label Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants