Download hmc/reference/scripts/compare_runs.py from neurodeskorg/webapps: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/compare_runs.py
- Command line
-
hf download hf://datasets/neurodeskorg/webapps/hmc/reference/scripts/compare_runs.py
-
curl -L -o compare_runs.py https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/compare_runs.py
3.87 kB
| """Compare two NiFreeze runs (M0): motion parameters, matrices and intermediates. | |
| Usage: python -I compare_runs.py RUN_A RUN_B OUT.json | |
| Gate: |dt| <= 0.2 mm and |drot| <= 0.2 deg on each axis, for each volume. | |
| """ | |
| import hashlib | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import h5py | |
| import nibabel as nb | |
| import numpy as np | |
| import pandas as pd | |
| GATE_MM = 0.2 | |
| GATE_DEG = 0.2 | |
| def h5_affines(run: Path) -> np.ndarray: | |
| (p,) = sorted((run / "out").glob("*.h5")) | |
| with h5py.File(p, "r") as f: | |
| root = f if "motion_affines" in f else f[next(iter(f.keys()))] | |
| return np.asarray(root["motion_affines"]) | |
| def sha(path: Path) -> str: | |
| return hashlib.sha256(path.read_bytes()).hexdigest() | |
| def image_equal(a: Path, b: Path) -> bool: | |
| da = np.asarray(nb.load(a).dataobj) | |
| db = np.asarray(nb.load(b).dataobj) | |
| return da.dtype == db.dtype and np.array_equal(da, db) | |
| def main(a: Path, b: Path, out: Path) -> None: | |
| ma = pd.read_csv(a / "motion.tsv", sep="\t") | |
| mb = pd.read_csv(b / "motion.tsv", sep="\t") | |
| res = {"run_a": str(a), "run_b": str(b)} | |
| aff_a, aff_b = h5_affines(a), h5_affines(b) | |
| res["motion_affines_identical"] = bool(np.array_equal(aff_a, aff_b)) | |
| res["motion_affines_max_abs_diff"] = float(np.abs(aff_a - aff_b).max()) | |
| res["order_identical"] = (a / "work/order.txt").read_text() == ( | |
| b / "work/order.txt" | |
| ).read_text() | |
| per_axis = {} | |
| over = np.zeros(len(ma), dtype=bool) | |
| for ax in "xyz": | |
| dt = np.abs(ma[f"trans_{ax}"] - mb[f"trans_{ax}"]).to_numpy() | |
| dr = np.degrees(np.abs(ma[f"rot_{ax}"] - mb[f"rot_{ax}"]).to_numpy()) | |
| per_axis[f"trans_{ax}_max_abs_mm"] = float(dt.max()) | |
| per_axis[f"rot_{ax}_max_abs_deg"] = float(dr.max()) | |
| per_axis[f"trans_{ax}_n_over_gate"] = int((dt > GATE_MM).sum()) | |
| per_axis[f"rot_{ax}_n_over_gate"] = int((dr > GATE_DEG).sum()) | |
| over |= (dt > GATE_MM) | (dr > GATE_DEG) | |
| res["per_axis"] = per_axis | |
| bvals = np.loadtxt("/var/tmp/dwi-data/ds005577/sub-107_dir-AP_dwi.bval") | |
| shell = np.round(bvals[ma["original_index"].to_numpy()], -2) | |
| by_shell = {} | |
| for bv in sorted(set(shell.tolist())): | |
| sel = shell == bv | |
| entry = {"volumes": int(sel.sum()), "over_gate": int((over & sel).sum())} | |
| for ax in "xyz": | |
| dt = np.abs(ma[f"trans_{ax}"] - mb[f"trans_{ax}"]).to_numpy()[sel] | |
| dr = np.degrees(np.abs(ma[f"rot_{ax}"] - mb[f"rot_{ax}"]).to_numpy()[sel]) | |
| entry[f"trans_{ax}_max_abs_mm"] = float(dt.max()) | |
| entry[f"rot_{ax}_max_abs_deg"] = float(dr.max()) | |
| by_shell[str(int(bv))] = entry | |
| res["volumes"] = len(ma) | |
| res["by_shell"] = by_shell | |
| res["volumes_over_gate"] = int(over.sum()) | |
| res["volumes_over_gate_original_index"] = ma["original_index"][over].tolist() | |
| res["within_gate"] = bool(not over.any()) | |
| # Intermediates: matrices, command lines, kept images (decoded arrays). | |
| wa, wb = a / "work", b / "work" | |
| mats = sorted(p.name for p in wa.glob("ants-*0GenericAffine.mat")) | |
| res["mat_files"] = len(mats) | |
| res["mat_files_identical"] = sum(sha(wa / n) == sha(wb / n) for n in mats) | |
| cmds = sorted(p.name for p in wa.glob("cmd-*.sh")) | |
| res["cmd_files_identical"] = sum( | |
| (wa / n).read_text().replace(str(a), "<RUN>") | |
| == (wb / n).read_text().replace(str(b), "<RUN>") | |
| for n in cmds | |
| ) | |
| res["cmd_files"] = len(cmds) | |
| for kind in ("sample", "predicted"): | |
| names = sorted(p.name for p in wa.glob(f"{kind}_*.nii.gz")) | |
| res[f"{kind}_files"] = len(names) | |
| res[f"{kind}_arrays_identical"] = sum( | |
| image_equal(wa / n, wb / n) for n in names | |
| ) | |
| out.write_text(json.dumps(res, indent=2)) | |
| print(json.dumps(res, indent=1)) | |
| if __name__ == "__main__": | |
| main(Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3])) | |