Download hmc/reference/scripts/localize_manifest.py from neurodeskorg/webapps: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/localize_manifest.py
- Command line
-
hf download hf://datasets/neurodeskorg/webapps/hmc/reference/scripts/localize_manifest.py
-
curl -L -o localize_manifest.py https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/localize_manifest.py
2.7 kB
| """Rewrite an epic run's collected/manifest.json to local paths and verify it (M0). | |
| Every entry with "path" and "sha256" is re-hashed locally and compared with the epic | |
| value. Registration fixed/moving/mask paths are checked for existence. If | |
| collected/manifest.json already exists (a local regeneration, as for e1), it is | |
| compared with the localized epic manifest before it is replaced. | |
| Usage: python -I localize_manifest.py RUN_DIR EPIC_RUN_PREFIX | |
| """ | |
| import hashlib | |
| import json | |
| import sys | |
| from pathlib import Path | |
| def sha(p: Path) -> str: | |
| h = hashlib.sha256() | |
| with p.open("rb") as f: | |
| for chunk in iter(lambda: f.read(1 << 20), b""): | |
| h.update(chunk) | |
| return h.hexdigest() | |
| def walk(node, fn): | |
| if isinstance(node, dict): | |
| return {k: walk(v, fn) for k, v in node.items()} | |
| if isinstance(node, list): | |
| return [walk(v, fn) for v in node] | |
| if isinstance(node, str): | |
| return fn(node) | |
| return node | |
| def main(run: Path, prefix: str) -> None: | |
| epic = json.loads((run / "collected.epic" / "manifest.json").read_text()) | |
| local = str(run) | |
| man = walk(epic, lambda s: local + s[len(prefix) :] if s.startswith(prefix) else s) | |
| stats = {"hashed": 0, "sha_mismatch": [], "exists_checked": 0, "missing": []} | |
| def check(node, key=None): | |
| if isinstance(node, dict): | |
| if "path" in node and "sha256" in node: | |
| p = Path(node["path"]) | |
| stats["hashed"] += 1 | |
| if not p.exists(): | |
| stats["missing"].append(str(p)) | |
| elif sha(p) != node["sha256"]: | |
| stats["sha_mismatch"].append(str(p)) | |
| for k, v in node.items(): | |
| if k != "path": | |
| check(v, k) | |
| elif isinstance(node, list): | |
| for v in node: | |
| check(v, key) | |
| elif isinstance(node, str) and key in ("fixed", "moving", "fixed_masks"): | |
| stats["exists_checked"] += 1 | |
| if not Path(node).exists(): | |
| stats["missing"].append(node) | |
| check(man) | |
| stats["missing"] = sorted(set(stats["missing"])) | |
| leftover = [] | |
| walk(man, lambda s: leftover.append(s) if prefix in s else s) | |
| stats["epic_paths_left"] = len(leftover) | |
| out = run / "collected" / "manifest.json" | |
| stats["local_regenerated_equals_localized_epic"] = ( | |
| json.loads(out.read_text()) == man if out.exists() else None | |
| ) | |
| out.write_text(json.dumps(man, indent=1)) | |
| stats["missing_count"] = len(stats["missing"]) | |
| stats["missing"] = stats["missing"][:10] | |
| print(json.dumps(stats, indent=1)) | |
| if __name__ == "__main__": | |
| main(Path(sys.argv[1]), sys.argv[2]) | |