cangyeone commited on
Commit
15ccff5
·
verified ·
1 Parent(s): af48cef

Upload essd_scripts/check_manuscript_consistency.py

Browse files
essd_scripts/check_manuscript_consistency.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Check that ESSD manuscript numbers agree with regenerated audit outputs."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import contextlib
8
+ import io
9
+ import json
10
+ import re
11
+ from pathlib import Path
12
+ from typing import Any
13
+
14
+ from audit_manuscript_numbers import build_report, print_latex_baseline
15
+
16
+
17
+ ROOT = Path(__file__).resolve().parents[1]
18
+ DEFAULT_TEX = ROOT / "essd" / "template.tex"
19
+
20
+
21
+ def tex_int(value: int) -> str:
22
+ return f"{value:,}".replace(",", r"\,")
23
+
24
+
25
+ def norm(text: str) -> str:
26
+ text = text.replace(r"\,", "")
27
+ text = text.replace("~", "")
28
+ text = text.replace(" ", "")
29
+ return re.sub(r"\s+", "", text)
30
+
31
+
32
+ def require(label: str, manuscript: str, snippet: str, failures: list[dict[str, str]]) -> None:
33
+ if norm(snippet) not in manuscript:
34
+ failures.append({"label": label, "expected_snippet": snippet})
35
+
36
+
37
+ def baseline_rows_latex(report: dict[str, Any]) -> str:
38
+ buf = io.StringIO()
39
+ with contextlib.redirect_stdout(buf):
40
+ print_latex_baseline(report)
41
+ return buf.getvalue()
42
+
43
+
44
+ def build_checks(report: dict[str, Any]) -> list[tuple[str, str]]:
45
+ inv = report["waveform_inventory"]
46
+ ann = report["annotation_inventory"]
47
+ cov = report["label_coverage"]["overall"]
48
+ consensus = report["consensus_audit"]
49
+ manual_supported = consensus["label_vote_audit_2019_manual_covered"]
50
+
51
+ checks = [
52
+ ("daily HDF5 files", f"Daily HDF5 files & {inv['daily_hdf5_files']}"),
53
+ ("compressed waveform size", f"Compressed waveform size & {inv['compressed_waveform_size_gib_rounded']}\\,GiB"),
54
+ ("unique station keys", f"Unique network-station keys & {inv['unique_network_station_keys']}"),
55
+ ("indexed waveform segments", f"Indexed waveform segments & {tex_int(inv['indexed_waveform_segments'])}"),
56
+ ("all-channel component-days", f"All-channel component-days & {tex_int(inv['all_channel_component_days_rounded'])}"),
57
+ ("main-family waveform segments", f"HH/BH/EH/HN waveform segments & {tex_int(inv['main_family_segments'])}"),
58
+ ("main-family component-days", f"HH/BH/EH/HN component-days & {tex_int(inv['main_family_component_days_rounded'])}"),
59
+ (
60
+ "main-family station-day-family samples",
61
+ f"HH/BH/EH/HN station-day-family samples & {tex_int(inv['main_family_station_day_family_samples'])}",
62
+ ),
63
+ ("events", f"Events & {ann['summary']['event_count']}"),
64
+ ("P/S phase arrivals", f"P/S phase arrivals & {tex_int(ann['summary']['pick_count'])}"),
65
+ ("covered arrivals", f"Arrivals with released waveform coverage & {tex_int(cov['covered_labels'])}"),
66
+ (
67
+ "2019 period row",
68
+ (
69
+ f"2019 July 1--7 & {tex_int(ann['periods']['2019']['events'])} & "
70
+ f"{tex_int(ann['periods']['2019']['phase_arrivals'])} & "
71
+ f"{tex_int(ann['periods']['2019']['manual_labels'])}"
72
+ ),
73
+ ),
74
+ (
75
+ "2021 period row",
76
+ (
77
+ f"2021 November 8--14 & {tex_int(ann['periods']['2021']['events'])} & "
78
+ f"{tex_int(ann['periods']['2021']['phase_arrivals'])} & "
79
+ f"{tex_int(ann['periods']['2021']['manual_labels'])}"
80
+ ),
81
+ ),
82
+ ("automatic labels", tex_int(ann["status_counts"]["automatic"])),
83
+ ("source records", tex_int(ann["summary"]["record_count"])),
84
+ ("event-station associations", tex_int(ann["summary"]["station_event_count"])),
85
+ (
86
+ "consensus candidate count",
87
+ f"This conservative example contains {tex_int(consensus['candidate_count'])} consensus-supported candidate",
88
+ ),
89
+ ("consensus P count", tex_int(consensus["candidate_phase_counts"]["P"])),
90
+ ("consensus S count", tex_int(consensus["candidate_phase_counts"]["S"])),
91
+ ("consensus label-matched count", tex_int(consensus["candidate_with_catalog_label_within_1p5_s"])),
92
+ ("manual covered vote total", tex_int(manual_supported["total_labels"])),
93
+ ("manual covered supported count", tex_int(manual_supported["supported_labels"])),
94
+ ]
95
+ return checks
96
+
97
+
98
+ def main() -> None:
99
+ parser = argparse.ArgumentParser(description=__doc__)
100
+ parser.add_argument("--tex", type=Path, default=DEFAULT_TEX)
101
+ parser.add_argument("--format", choices=("text", "json"), default="text")
102
+ args = parser.parse_args()
103
+
104
+ report = build_report()
105
+ manuscript = norm(args.tex.read_text(encoding="utf-8"))
106
+ failures: list[dict[str, str]] = []
107
+
108
+ for label, snippet in build_checks(report):
109
+ require(label, manuscript, snippet, failures)
110
+
111
+ baseline_text = baseline_rows_latex(report)
112
+ for line in baseline_text.splitlines():
113
+ if "&" in line and re.search(r"\d", line):
114
+ require("baseline table row", manuscript, line, failures)
115
+
116
+ result = {
117
+ "tex": str(args.tex.relative_to(ROOT) if args.tex.is_relative_to(ROOT) else args.tex),
118
+ "status": "pass" if not failures else "fail",
119
+ "checked_core_items": len(build_checks(report)),
120
+ "checked_baseline_rows": sum(1 for line in baseline_text.splitlines() if "&" in line and re.search(r"\d", line)),
121
+ "failures": failures,
122
+ }
123
+
124
+ if args.format == "json":
125
+ print(json.dumps(result, indent=2, ensure_ascii=False))
126
+ else:
127
+ if failures:
128
+ print("[FAIL] Manuscript number consistency check")
129
+ for item in failures:
130
+ print(f"- {item['label']}: missing {item['expected_snippet']}")
131
+ else:
132
+ print("[OK] Manuscript numbers match regenerated ESSD audit outputs.")
133
+ print(f"Core items checked: {result['checked_core_items']}")
134
+ print(f"Baseline rows checked: {result['checked_baseline_rows']}")
135
+
136
+ raise SystemExit(1 if failures else 0)
137
+
138
+
139
+ if __name__ == "__main__":
140
+ main()