Add P0 infrastructure for the Volve-DIA benchmark

Set up the data, parsing and quality-control layer for a public benchmark
on the Volve daily drilling reports (DDR) and WITSML real-time data.

- Data manifest with SHA-256 hashes of the three Volve subtrees in use.
- DDR XML parser into DuckDB (1,759 reports, 26 wellbores, 23,447
  activities, 17 tables) with locked expected counts.
- Render QC of the publisher HTML against the XML (272,544 items).
- OCR client for the shared chandra vLLM service, with a page cache keyed
  on model, prompt hash, dpi, max_tokens and temperature, and OCR QC
  against the HTML on a 26-report pilot.
- Run logging (scripts/runlog.sh, logs/index.csv), the P0 runner and
  40 tests.
- Research plan, technical overview and progress page (in Vietnamese).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 12:45:34 +00:00
commit a3c175638f
172 changed files with 145856 additions and 0 deletions

112
scripts/01_inventory.py Normal file
View File

@@ -0,0 +1,112 @@
"""Inventory of the inputs: the three versions of every DDR, PDF pages, and the WITSML wellbores.
python scripts/01_inventory.py
Writes outputs/inventory/{ddr_files.csv, witsml_wellbores.csv, summary.json} and fails if the XML, HTML and PDF
versions do not hold the same reports.
"""
from __future__ import annotations
import json
import sys
from collections import Counter
import pandas as pd
import pymupdf
from volve_dia import paths
from volve_dia.names import canonical
from volve_dia.ocr import MEGABYTE, image_bytes, load_config
#: A page with fewer characters in its text layer than this is near-empty (or a scan). In the DDR PDFs all such
#: pages are the trailing page of a multi-page report: blank, or the last empty row of a table.
NEAR_EMPTY_CHARS = 50
def ddr_files(max_image_bytes: int) -> pd.DataFrame:
dirs = {"xml": (paths.ddr_xml_dir(), ".xml"), "html": (paths.ddr_html_dir(), ".html"),
"pdf": (paths.ddr_pdf_dir(), ".pdf")}
stems = {k: {p.stem: p for p in d.glob(f"*{ext}")} for k, (d, ext) in dirs.items()}
rows = []
for s in sorted(set().union(*stems.values())):
wb = s[: -len("_YYYY_MM_DD")]
row = {"report_id": s, "wellbore": canonical(wb), "file_date": "-".join(s.rsplit("_", 3)[1:])}
for k in dirs:
p = stems[k].get(s)
row[f"{k}_bytes"] = p.stat().st_size if p else None
pdf = stems["pdf"].get(s)
if pdf:
with pymupdf.open(pdf) as doc:
chars = [len(page.get_text()) for page in doc]
heavy = sum(image_bytes(doc, page) > max_image_bytes for page in doc)
near_empty = [i for i, c in enumerate(chars, 1) if c < NEAR_EMPTY_CHARS]
row.update(pdf_pages=len(chars), pdf_text_chars=sum(chars), pdf_near_empty_pages=len(near_empty),
pdf_near_empty_not_last=sum(i != len(chars) for i in near_empty), pdf_heavy_pages=heavy)
rows.append(row)
return pd.DataFrame(rows)
def witsml_wellbores(ddr_wellbores: set[str]) -> pd.DataFrame:
root = paths.witsml_dir()
meta = {}
for line in (root / "MetaFileInfo.txt").read_text(encoding="utf-8").splitlines():
parts = [p for p in line.split(" ") if p.strip()]
if len(parts) >= 3:
meta[parts[0].strip()] = (parts[1].strip(), parts[2].strip())
rows = []
for d in sorted(p for p in root.iterdir() if p.is_dir()):
files = [f for f in d.rglob("*") if f.is_file()]
objects = sorted({p.name for p in d.glob("*/*") if p.is_dir()}) # <folder>/<n>/{log,trajectory,...}
name, uid = meta.get(d.name, (None, None))
wb = canonical(d.name)
rows.append({"folder": d.name, "meta_name": name, "uid": uid, "wellbore": wb,
"meta_agrees": name is None or canonical(name) == wb, "in_ddr": wb in ddr_wellbores,
"has_log": "log" in objects, "objects": " ".join(objects),
"n_files": len(files), "bytes": sum(f.stat().st_size for f in files)})
return pd.DataFrame(rows)
def main() -> int:
out = paths.outputs_dir() / "inventory"
out.mkdir(parents=True, exist_ok=True)
ddr = ddr_files(load_config().max_page_image_mb * MEGABYTE)
ddr.to_csv(out / "ddr_files.csv", index=False)
complete = ddr[["xml_bytes", "html_bytes", "pdf_bytes"]].notna().all(axis=1)
ddr_wbs = set(ddr["wellbore"])
wits = witsml_wellbores(ddr_wbs)
wits.to_csv(out / "witsml_wellbores.csv", index=False)
log_wbs = set(wits.loc[wits["has_log"], "wellbore"])
summary = {
"ddr_reports": len(ddr),
"ddr_in_all_three_versions": int(complete.sum()),
"ddr_wellbores": len(ddr_wbs),
"ddr_reports_per_wellbore": {k: int(v) for k, v in sorted(Counter(ddr["wellbore"]).items())},
"ddr_bytes": {k: int(ddr[f"{k}_bytes"].sum()) for k in ("xml", "html", "pdf")},
"pdf_pages": int(ddr["pdf_pages"].sum()),
"pdf_pages_per_report": {str(k): int(v) for k, v in sorted(Counter(ddr["pdf_pages"]).items())},
"pdf_near_empty_pages": int(ddr["pdf_near_empty_pages"].sum()),
"pdf_near_empty_not_last_page": int(ddr["pdf_near_empty_not_last"].sum()),
"pdf_heavy_pages": int(ddr["pdf_heavy_pages"].sum()),
"witsml_folders": len(wits),
"witsml_wellbores": int(wits["wellbore"].nunique()),
"witsml_wellbores_with_ddr": int(wits.loc[wits["in_ddr"], "wellbore"].nunique()),
"witsml_wellbores_with_log": len(log_wbs),
"witsml_log_wellbores_with_ddr": len(log_wbs & ddr_wbs),
"witsml_folders_meta_disagrees": wits.loc[~wits["meta_agrees"], "folder"].tolist(),
"witsml_wellbores_without_ddr": sorted(set(wits.loc[~wits["in_ddr"], "wellbore"])),
"ddr_wellbores_without_witsml": sorted(ddr_wbs - set(wits["wellbore"])),
"ddr_reports_on_witsml_log_wellbores": int(ddr["wellbore"].isin(log_wbs).sum()),
"witsml_bytes": int(wits["bytes"].sum()),
}
(out / "summary.json").write_text(json.dumps(summary, indent=1) + "\n", encoding="utf-8")
print(json.dumps({k: v for k, v in summary.items() if k != "ddr_reports_per_wellbore"}, indent=1))
if not complete.all():
print("reports missing a version:", ddr.loc[~complete, "report_id"].tolist())
return 1
return 0
if __name__ == "__main__":
sys.exit(main())