fix(tables): make calc_bbox_intersection safety_margin actually apply - #99
Open
eeshsaxena wants to merge 1 commit into
Open
fix(tables): make calc_bbox_intersection safety_margin actually apply#99eeshsaxena wants to merge 1 commit into
eeshsaxena wants to merge 1 commit into
Conversation
The 'expanded boxes intersect' guard in calc_bbox_intersection could never be true: e.g. max(right edges) + margin <= max(left edges) never holds because every box's right edge exceeds its left. So the safety_margin branch was dead and the function only returned the intersection of the un-expanded boxes; safety_margin (passed as 5 when building table cells in ml.py) had no effect, and boxes within the margin but not strictly overlapping returned None. Expand each box by safety_margin and intersect the expanded boxes, matching the tolerance used by Element.overlaps(). Two existing test cases whose expectations were 'adjusted' to None to fit the broken behavior now assert the correct margin-expanded intersections, plus a far-apart case that stays None.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
calc_bbox_intersection(bbox1, bbox2, safety_margin=5.0)is meant to treat two boxes withinsafety_marginof each other as intersecting. But the "expanded boxes intersect" guard can never be true, sosafety_marginhas no effect:max(right edges) + margin <= max(left edges)never holds, because every box's right edge exceeds its left edge (somax(rights) >= max(lefts)and the margin only widens the gap). The other three disjuncts are false for the same reason. The function then falls through and returns the intersection of the un-expanded boxes, so it only ever returns non-Nonewhen the original boxes strictly overlap — regardless ofsafety_margin.Impact:
ml.pybuilds table cells viacalc_bbox_intersection(row.bbox, col.bbox, safety_margin=5). A row/column pair that is within 5px but not strictly overlapping (detection noise at cell edges) returnsNone, dropping the cell.The existing test even documents this — two cases were "adjusted to expect
None" to fit the broken behavior:Fix
Expand each box by
safety_margin, then intersect the expanded boxes — the same tolerance pattern used byElement.overlaps()elsewhere in the codebase. This makessafety_margintake effect while leaving themargin=0and strictly-overlapping cases unchanged.The two "adjusted" test cases now assert the correct margin-expanded intersections (
(15,15,25,25)and(21,21,25,25)), and I added a far-apart case that staysNoneto pin the tolerance boundary.