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:
92
scripts/03_render_qc.py
Normal file
92
scripts/03_render_qc.py
Normal file
@@ -0,0 +1,92 @@
|
||||
"""Render fidelity: does the publisher HTML print what the XML source holds?
|
||||
|
||||
python scripts/03_render_qc.py # after 02_parse_ddr.py
|
||||
|
||||
ref = `qc.from_xml` of each drillReport (header lines, the header fields in `qc.FIELD_COLUMNS`, the two summaries,
|
||||
the Operations table), hyp = the publisher HTML read by `rendered.parse_html`, values compared with `qc.same_value`.
|
||||
The other 13 HTML tables (Drilling Fluid, Survey Station, ...) are not checked against the XML.
|
||||
|
||||
Writes outputs/render_qc/{items.parquet, by_key.csv, summary.json, summary.md}.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import collections
|
||||
import json
|
||||
import sys
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from volve_dia import paths
|
||||
from volve_dia.ddr import qc
|
||||
from volve_dia.ddr.rendered import parse_html
|
||||
|
||||
EXAMPLES = 3
|
||||
|
||||
|
||||
def items() -> pd.DataFrame:
|
||||
ddr = paths.outputs_dir() / "ddr"
|
||||
reports = pd.read_parquet(ddr / "reports.parquet")
|
||||
acts = collections.defaultdict(list)
|
||||
for a in pd.read_parquet(ddr / "activities.parquet").to_dict("records"):
|
||||
acts[a["report_id"]].append(a)
|
||||
rows = []
|
||||
for rep in reports.to_dict("records"):
|
||||
html = (paths.ddr_html_dir() / f"{rep['report_id']}.html").read_text(encoding="utf-8")
|
||||
hyp = qc.html_for_xml(parse_html(html))
|
||||
for it in qc.compare(qc.from_xml(rep, acts[rep["report_id"]]), hyp, equal=qc.same_value, extras=False):
|
||||
rows.append({"doc": rep["report_id"], "wellbore": rep["wellbore"], **it})
|
||||
return pd.DataFrame(rows)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
out = paths.outputs_dir() / "render_qc"
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
df = items()
|
||||
df.to_parquet(out / "items.parquet", index=False)
|
||||
keys = ["part", "table", "key"]
|
||||
unequal = df[~df["exact"]]
|
||||
by_key = (df.groupby(keys, sort=False).agg(n=("exact", "size"), equal=("exact", "sum")).reset_index()
|
||||
.merge(unequal.groupby(keys)["doc"].nunique().rename("docs_unequal").reset_index(), on=keys,
|
||||
how="left")
|
||||
.fillna({"docs_unequal": 0}))
|
||||
by_key["rate"] = by_key["equal"] / by_key["n"]
|
||||
by_key.to_csv(out / "by_key.csv", index=False)
|
||||
|
||||
mismatches = []
|
||||
for (part, table, key), g in unequal.groupby(keys, sort=False):
|
||||
pairs = collections.Counter(zip(g["ref"], g["hyp"].fillna("<absent>")))
|
||||
mismatches.append({"part": part, "table": table, "key": key, "n": len(g), "docs": int(g["doc"].nunique()),
|
||||
"top_pairs": [{"xml": r, "html": h, "n": n} for (r, h), n in pairs.most_common(EXAMPLES)]})
|
||||
parts = df.groupby("part")["exact"].agg(["size", "sum"])
|
||||
summary = {
|
||||
"docs": int(df["doc"].nunique()),
|
||||
"items": len(df),
|
||||
"by_part": {p: {"n": int(r["size"]), "equal": int(r["sum"]), "rate": round(r["sum"] / r["size"], 6)}
|
||||
for p, r in parts.iterrows()},
|
||||
"docs_all_equal": int((df.groupby("doc")["exact"].all()).sum()),
|
||||
"docs_all_equal_except_flags": int(df[~df["key"].isin(["tight well", "hpht"])].groupby("doc")["exact"]
|
||||
.all().sum()),
|
||||
"mismatches": mismatches,
|
||||
}
|
||||
(out / "summary.json").write_text(json.dumps(summary, indent=1, ensure_ascii=False) + "\n", encoding="utf-8")
|
||||
|
||||
lines = ["# Render fidelity: XML source vs publisher HTML", "",
|
||||
f"{summary['docs']} reports, {summary['items']:,} items; values compared as printed values "
|
||||
"(numbers as numbers, '', 'None' and sentinels as missing).", "",
|
||||
"| part | items | equal | rate |", "|---|---:|---:|---:|"]
|
||||
lines += [f"| {p} | {v['n']:,} | {v['equal']:,} | {v['rate']:.4f} |" for p, v in summary["by_part"].items()]
|
||||
lines += ["", f"Reports equal everywhere: {summary['docs_all_equal']}; equal except Tight well / HPHT: "
|
||||
f"{summary['docs_all_equal_except_flags']}.", "", "## Items that differ", "",
|
||||
"| part | key | items | reports | most common (XML -> HTML) |", "|---|---|---:|---:|---|"]
|
||||
for m in mismatches:
|
||||
pairs = "; ".join(f"`{p['xml'][:40]}` -> `{p['html'][:40]}` ({p['n']})" for p in m["top_pairs"])
|
||||
lines.append(f"| {m['part']} | {m['table'] + ': ' if m['table'] else ''}{m['key']} | {m['n']:,} | "
|
||||
f"{m['docs']:,} | {pairs} |")
|
||||
(out / "summary.md").write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
print("\n".join(lines))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user