Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions test_unstructured/partition/pdf_image/test_pdfminer_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
from test_unstructured.unit_utils import example_doc_path
from unstructured.partition.auto import partition
from unstructured.partition.pdf_image.pdfminer_processing import (
_aggregated_iou,
_deduplicate_ltchars,
_rotate_bboxes,
_validate_bbox,
aggregate_embedded_text_by_block,
bboxes1_is_almost_subregion_of_bboxes2,
boxes_self_iou,
calculate_bbox_area,
calculate_intersection_area,
clean_pdfminer_inner_elements,
get_widget_text_from_annots,
process_file_with_pdfminer,
Expand Down Expand Up @@ -238,6 +241,36 @@ def test_aggregate_not_filling_target():
assert extracted.value == "false"


def _scalar_aggregated_iou(box1s, box2):
"""Reference: the per-box loop `_aggregated_iou` used before it was vectorized."""
intersection = 0.0
sum_areas = calculate_bbox_area(box2)
for i in range(box1s.shape[0]):
intersection += calculate_intersection_area(box1s[i, :], box2)
sum_areas += calculate_bbox_area(box1s[i, :])
union = sum_areas - intersection
return 1.0 if union == 0 else intersection / union


@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_aggregated_iou_matches_scalar_loop_bit_for_bit(dtype):
# -- the result is compared against a coverage threshold, so a last-bit difference could
# -- flip `is_extracted` at the boundary; require exact equality and the same dtype --
rng = np.random.default_rng(0)
for _ in range(500):
n = int(rng.integers(1, 40))
xy = rng.uniform(0, 2000, (n, 2))
wh = rng.uniform(-5, 300, (n, 2)) # -- includes zero-size and inverted boxes --
box1s = np.hstack([xy, xy + wh]).astype(dtype)
box2 = np.array([100, 100, 900, 1500], dtype=dtype)

expected = _scalar_aggregated_iou(box1s, box2)
actual = _aggregated_iou(box1s, box2)

assert actual == expected
assert np.asarray(actual).dtype == np.asarray(expected).dtype


@pytest.mark.parametrize(
("coords1", "coords2", "expected"),
[
Expand Down
29 changes: 16 additions & 13 deletions unstructured/partition/pdf_image/pdfminer_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,12 +901,17 @@ def remove_duplicate_elements(


def _aggregated_iou(box1s, box2):
intersection = 0.0
sum_areas = calculate_bbox_area(box2)

for i in range(box1s.shape[0]):
intersection += calculate_intersection_area(box1s[i, :], box2)
sum_areas += calculate_bbox_area(box1s[i, :])
x1, y1, x2, y2 = box1s[:, 0], box1s[:, 1], box1s[:, 2], box1s[:, 3]
inter_w = np.minimum(x2, box2[2]) - np.maximum(x1, box2[0])
inter_h = np.minimum(y2, box2[3]) - np.maximum(y1, box2[1])
inter_areas = np.where((inter_w > 0) & (inter_h > 0), inter_w * inter_h, 0.0)
areas = (x2 - x1) * (y2 - y1)

# -- cumsum accumulates left to right like the scalar loop this replaced, so the float
# -- result is bit-identical (np.sum's pairwise summation would not be). The seeds keep the
# -- coordinates' dtype so float32 input still accumulates in float32, as the loop did. --
intersection = np.cumsum(np.concatenate((np.zeros(1, inter_areas.dtype), inter_areas)))[-1]
sum_areas = np.cumsum(np.concatenate((np.atleast_1d(calculate_bbox_area(box2)), areas)))[-1]

union = sum_areas - intersection

Expand Down Expand Up @@ -937,16 +942,14 @@ def aggregate_embedded_text_by_block(
.astype(bool)
)

text = " ".join([text for text in source_regions.slice(mask).texts if text])

if sum(mask):
source_bboxes = source_regions.slice(mask).element_coords
target_bboxes = target_region.element_coords
selected = source_regions.slice(mask)
text = " ".join([text for text in selected.texts if text])

iou = _aggregated_iou(source_bboxes, target_bboxes[0, :])
if mask.any():
iou = _aggregated_iou(selected.element_coords, target_region.element_coords[0, :])

fully_filled = (
all(flag == IsExtracted.TRUE for flag in source_regions.slice(mask).is_extracted_array)
all(flag == IsExtracted.TRUE for flag in selected.is_extracted_array)
and iou > text_coverage_threshold
)
is_extracted = IsExtracted.TRUE if fully_filled else IsExtracted.PARTIAL
Expand Down
Loading