Delete tools/recover_reference_assets.py
Browse files
tools/recover_reference_assets.py
DELETED
|
@@ -1,139 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""Recover document assets from the immutable executed EDA, without rerunning models.
|
| 3 |
-
|
| 4 |
-
The manifest binds every image/table to its cell/output and byte hash. Figures
|
| 5 |
-
are the original embedded PNG bytes, not recreated or statistically altered.
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
from __future__ import annotations
|
| 9 |
-
|
| 10 |
-
import argparse
|
| 11 |
-
import base64
|
| 12 |
-
import hashlib
|
| 13 |
-
import json
|
| 14 |
-
import re
|
| 15 |
-
from pathlib import Path
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def recover(root: Path) -> dict:
|
| 19 |
-
notebook = root / "notebooks/reference/DRMHB_EDA_published_2_5_0.ipynb"
|
| 20 |
-
nb = json.loads(notebook.read_text(encoding="utf-8"))
|
| 21 |
-
target = root / "docs/figures/eda"
|
| 22 |
-
target.mkdir(parents=True, exist_ok=True)
|
| 23 |
-
entries = []
|
| 24 |
-
for index, cell in enumerate(nb["cells"]):
|
| 25 |
-
source = "".join(cell.get("source", []))
|
| 26 |
-
if "publish_figure(" not in source or "def publish_figure" in source:
|
| 27 |
-
continue
|
| 28 |
-
names = re.findall(r'publish_figure\(fig,\s*"([^"]+)"', source)
|
| 29 |
-
if index == 29:
|
| 30 |
-
names = [
|
| 31 |
-
f"transition_portrait_off_r{regime}_{variable}"
|
| 32 |
-
for variable in ("flow", "load")
|
| 33 |
-
for regime in (1, 2, 3)
|
| 34 |
-
]
|
| 35 |
-
elif index == 58:
|
| 36 |
-
names = [
|
| 37 |
-
f"fault_chronology_{fault}"
|
| 38 |
-
for fault in (
|
| 39 |
-
"bearing_inner",
|
| 40 |
-
"bearing_outer",
|
| 41 |
-
"imbalance",
|
| 42 |
-
"looseness",
|
| 43 |
-
"misalignment_angular",
|
| 44 |
-
"resonance",
|
| 45 |
-
)
|
| 46 |
-
]
|
| 47 |
-
outputs = [
|
| 48 |
-
(i, item)
|
| 49 |
-
for i, item in enumerate(cell.get("outputs", []))
|
| 50 |
-
if "image/png" in item.get("data", {})
|
| 51 |
-
]
|
| 52 |
-
if len(names) != len(outputs):
|
| 53 |
-
raise RuntimeError(
|
| 54 |
-
f"Cell {index}: {len(names)} names for {len(outputs)} plots"
|
| 55 |
-
)
|
| 56 |
-
for name, (output_index, output) in zip(names, outputs):
|
| 57 |
-
encoded = output["data"]["image/png"]
|
| 58 |
-
payload = base64.b64decode(
|
| 59 |
-
"".join(encoded) if isinstance(encoded, list) else encoded
|
| 60 |
-
)
|
| 61 |
-
path = target / f"{name}.png"
|
| 62 |
-
path.write_bytes(payload)
|
| 63 |
-
entries.append(
|
| 64 |
-
{
|
| 65 |
-
"slug": name,
|
| 66 |
-
"kind": "figure",
|
| 67 |
-
"path": str(path.relative_to(root)),
|
| 68 |
-
"cell_index": index,
|
| 69 |
-
"output_index": output_index,
|
| 70 |
-
"execution_count": cell.get("execution_count"),
|
| 71 |
-
"sha256": hashlib.sha256(payload).hexdigest(),
|
| 72 |
-
"format": "original embedded PNG",
|
| 73 |
-
}
|
| 74 |
-
)
|
| 75 |
-
for index, name in [(77, "baseline_ml_summary"), (78, "baseline_detection_delay")]:
|
| 76 |
-
cell = nb["cells"][index]
|
| 77 |
-
output = "".join(
|
| 78 |
-
"".join(item.get("text", [])) for item in cell.get("outputs", [])
|
| 79 |
-
)
|
| 80 |
-
match = re.search(r"\\begin\{table\}.*?\\end\{table\}", output, re.S)
|
| 81 |
-
if match is None:
|
| 82 |
-
raise RuntimeError(f"No original LaTeX table in cell {index}")
|
| 83 |
-
original = root / "docs/provenance" / f"{name}.original.tex"
|
| 84 |
-
original.parent.mkdir(parents=True, exist_ok=True)
|
| 85 |
-
original.write_text(match.group(0) + "\n", encoding="utf-8")
|
| 86 |
-
# Only presentation changes: sizes/centering/line breaks. Numeric tokens unchanged.
|
| 87 |
-
text = match.group(0).replace("[htbp]", "[H]")
|
| 88 |
-
text = text.replace(
|
| 89 |
-
r"\begin{table}[H]",
|
| 90 |
-
r"\begin{table}[H]" + "\n" + r"\centering\small\setlength{\tabcolsep}{4pt}",
|
| 91 |
-
)
|
| 92 |
-
if name == "baseline_ml_summary":
|
| 93 |
-
text = text.replace("Healthy FPR [\\%]", r"\shortstack{Healthy\\FPR [\%]}")
|
| 94 |
-
text = text.replace(
|
| 95 |
-
"Transition FPR [\\%]", r"\shortstack{Transition\\FPR [\%]}"
|
| 96 |
-
)
|
| 97 |
-
text = text.replace("OOD FPR [\\%]", r"\shortstack{OOD\\FPR [\%]}")
|
| 98 |
-
text = text.replace("Median delay [d]", r"\shortstack{Median\\delay [d]}")
|
| 99 |
-
path = root / "docs" / f"{name}.tex"
|
| 100 |
-
path.write_text(text + "\n", encoding="utf-8")
|
| 101 |
-
entries.append(
|
| 102 |
-
{
|
| 103 |
-
"slug": name,
|
| 104 |
-
"kind": "table",
|
| 105 |
-
"cell_index": index,
|
| 106 |
-
"execution_count": cell.get("execution_count"),
|
| 107 |
-
"path": str(path.relative_to(root)),
|
| 108 |
-
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
|
| 109 |
-
"original_sha256": hashlib.sha256(original.read_bytes()).hexdigest(),
|
| 110 |
-
"format": "original printed LaTeX table; layout-only formatting",
|
| 111 |
-
}
|
| 112 |
-
)
|
| 113 |
-
manifest = {
|
| 114 |
-
"repo_id": "AlejandroGZ/DRMHB",
|
| 115 |
-
"source_revision": "d218f6e1dc7505d7c92c0b23fc1d27808a26c107",
|
| 116 |
-
"dataset_generator_version": "2.5.0",
|
| 117 |
-
"executed_conversion_version": "0.2.2",
|
| 118 |
-
"notebook": str(notebook.relative_to(root)),
|
| 119 |
-
"notebook_sha256": hashlib.sha256(notebook.read_bytes()).hexdigest(),
|
| 120 |
-
"recomputed": False,
|
| 121 |
-
"assets": entries,
|
| 122 |
-
}
|
| 123 |
-
(root / "docs/provenance/eda_assets.json").write_text(
|
| 124 |
-
json.dumps(manifest, indent=2) + "\n"
|
| 125 |
-
)
|
| 126 |
-
return manifest
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
def main() -> None:
|
| 130 |
-
parser = argparse.ArgumentParser(description=__doc__)
|
| 131 |
-
parser.add_argument(
|
| 132 |
-
"--root", type=Path, default=Path(__file__).resolve().parents[1]
|
| 133 |
-
)
|
| 134 |
-
args = parser.parse_args()
|
| 135 |
-
print(json.dumps({"recovered_assets": len(recover(args.root)["assets"])}, indent=2))
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if __name__ == "__main__":
|
| 139 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|