- OCR: match the colleague's full-set Dataset Hub export to the DDRs by content (3,059/3,066 pages) and QC it against the HTML (CER 2.40%, exact 0.9181). - WITSML parser and formation tops: 21 DDR wellbores with time logs, 1,384 reports; WITSML 15/9-F-15 S is DDR 15/9-F-15. - Deterministic labels T1-T3 (configs/labels.yaml), 1,662 T4 questions with SQL, pandas and raw-XML answers in full agreement. - Trial label validation by three blind LLM raters (not an author or expert audit) and rater agreement script; draft protocol_v1 (not locked): split by well. - Literature notes and BibTeX; docs, report and run_m1.sh synchronised. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
76 lines
3.8 KiB
Python
76 lines
3.8 KiB
Python
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": "<p><b>Operations</b></p>"},
|
|
{"id": 2, "category_id": 3, "bbox": [0, 0, 1653, 2339],
|
|
"text": "<table><tr><td>Start</td><td>End</td></tr><tr><td>00:00</td><td>06:00</td></tr></table>"},
|
|
{"id": 3, "category_id": 7, "bbox": [10, 10, 5, 5], "text": ""},
|
|
]
|
|
blocks = ocr.html_blocks(ocr_hub.page_content(anns, cats, 1653, 2339), 2)
|
|
assert [(b.raw_label, b.label) for b in blocks] == [("Section Header", "section_header"), ("Table", "table")]
|
|
assert blocks[0].bbox == [100.0, 100.0, 300.0, 110.0]
|
|
assert blocks[1].bbox == [0.0, 0.0, 1000.0, 1000.0]
|
|
assert blocks[1].text == "Start | End\n00:00 | 06:00"
|
|
assert all(b.page == 2 for b in blocks)
|
|
|
|
|
|
def test_tokens_keep_dates_depths_and_names_whole():
|
|
assert ocr_hub.tokens("<p>Period: 2009-01-16 00:00</p> 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"}
|