import numpy as np from volve_dia import ocr, ocr_hub def test_page_content_parses_like_a_service_answer(): cats = {1: "Section Header", 3: "Table", 7: "Image"} anns = [ {"id": 1, "category_id": 1, "bbox": [165.3, 233.9, 330.6, 23.39], "text": "
Operations
"}, {"id": 2, "category_id": 3, "bbox": [0, 0, 1653, 2339], "text": "| Start | End |
| 00:00 | 06:00 |
Period: 2009-01-16 00:00
Wellbore: 15/9-F-15 B, 3.5 m") == [ "period", "2009-01-16", "00:00", "wellbore", "15/9-f-15", "b", "3.5", "m"] def test_text_candidates_rank_the_right_page_first(): pages = [ocr_hub.tokens(t) for t in ("period 2009-01-16 drill 8.5 hole to 3000 m", "period 2009-01-17 drill 8.5 hole to 3100 m", "")] images = [ocr_hub.tokens(t) for t in ("period 2009-01-17 drill 8.5 hole to 3100 m", "", "period 2009-01-16 drill 8.5 hole to 3000 m")] cands = ocr_hub.text_candidates(pages, images, prefilter=3, top_k=2) assert cands[0][0] == (2, 1.0) assert cands[1][0] == (0, 1.0) assert cands[2] == [(1, 1.0)] # a page with no text layer: the images with no text def test_correlation(): rng = np.random.default_rng(0) a = rng.integers(0, 255, (40, 30), dtype=np.uint8) assert ocr_hub.correlation(a, a) == 1.0 assert ocr_hub.correlation(a, np.pad(a, ((0, 1), (0, 1)))) == 1.0 # one pixel larger: common area assert abs(ocr_hub.correlation(a, rng.integers(0, 255, (40, 30), dtype=np.uint8))) < 0.1 blank = np.full((40, 30), 255, np.uint8) assert ocr_hub.correlation(blank, blank) == 1.0 def test_select_breaks_image_ties_by_text(): # Two reports that differ only in a date: equal correlation, the text decides. assert ocr_hub.select([(0.978, 0.93, 1), (0.978, 0.99, 2), (0.95, 1.0, 3)], 0.9, 0.01) == (0.978, 0.99, 2) # Outside the tie window the image decides, whatever the text says. assert ocr_hub.select([(0.99, 0.80, 1), (0.95, 1.0, 2)], 0.9, 0.01) == (0.99, 0.80, 1) assert ocr_hub.select([(0.85, 1.0, 1)], 0.9, 0.01) is None assert ocr_hub.select([], 0.9, 0.01) is None def test_select_text_floor_rejects_same_layout_other_report(): # A first page of another day: same layout (corr 0.92) but other text (F1 0.36); with a floor it is no match. assert ocr_hub.select([(0.921, 0.364, 1)], 0.9, 0.01) == (0.921, 0.364, 1) assert ocr_hub.select([(0.921, 0.364, 1)], 0.9, 0.01, 0.7) is None assert ocr_hub.select([(0.95, 0.5, 1), (0.93, 0.95, 2)], 0.9, 0.01, 0.7) == (0.93, 0.95, 2) def test_coarse_dot_is_a_correlation(): rng = np.random.default_rng(1) a = rng.integers(0, 255, (585, 413), dtype=np.uint8) assert abs(float(ocr_hub.coarse(a) @ ocr_hub.coarse(a)) - 1) < 1e-5 assert ocr_hub.coarse(a).shape == (ocr_hub.COARSE[0] * ocr_hub.COARSE[1],) assert float(ocr_hub.coarse(np.full((585, 413), 255, np.uint8)) @ ocr_hub.coarse(a)) == 0.0 def test_iso_dates(): assert ocr_hub.iso_dates("Period: 1997-09-04 00:00 - 1997-09-05 00:00, created 2018-05-03; 12345-01-01") == { "1997-09-04", "1997-09-05", "2018-05-03"}