Download hmc/reference/scripts/compare_world.py from neurodeskorg/webapps: direct link, hf CLI and curl.
- Browser
- Download file 3.42 kB
-
https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/compare_world.py
- Command line
-
hf download hf://datasets/neurodeskorg/webapps/hmc/reference/scripts/compare_world.py
-
curl -L -o compare_world.py https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/compare_world.py
3.42 kB
| """Compare two runs' motion in two conventions (Q53). | |
| Per volume: |dt| per axis about the world grid centre (A @ c, origin included), | |
| |dt| per axis by QSIPrep's verbatim formula (about A[:3,:3] @ c), |drot| per axis | |
| (rotation vector, deg), and the max over mask voxels of |displacement_a - displacement_b|. | |
| Gate: 0.2 mm and 0.2 deg per axis. | |
| Usage: python -I compare_world.py RUN_A RUN_B OUT.json | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import h5py | |
| import nibabel as nb | |
| import numpy as np | |
| from scipy.spatial.transform import Rotation | |
| GATE = 0.2 | |
| BVAL = Path("/var/tmp/dwi-data/ds005577/sub-107_dir-AP_dwi.bval") | |
| FACTS = Path("/var/tmp/dwi-hmc/inputs/input-facts.json") | |
| MASK = Path("/var/tmp/dwi-hmc/inputs/mask.nii.gz") | |
| def load(run: Path): | |
| (p,) = sorted((run / "out").glob("*.h5")) | |
| with h5py.File(p, "r") as h5: | |
| f = h5.get("0", h5) | |
| return np.asarray(f["motion_affines"]), np.asarray(f["affine"]) | |
| def params(m, centre): | |
| t = m[:3, :3] @ centre + m[:3, 3] - centre | |
| r = np.degrees(Rotation.from_matrix(m[:3, :3]).as_rotvec()) | |
| return t, r | |
| def main(a: Path, b: Path, out: Path) -> None: | |
| ma, aff = load(a) | |
| mb, _ = load(b) | |
| mask = np.asanyarray(nb.load(MASK).dataobj) > 0 | |
| xyz = nb.affines.apply_affine(aff, np.argwhere(mask)) | |
| c = (np.array(mask.shape) - 1) / 2 | |
| world_c = nb.affines.apply_affine(aff, c) | |
| qsi_c = world_c - aff[:3, 3] | |
| bvals = np.rint(np.loadtxt(BVAL)).astype(int) | |
| b0 = set(json.loads(FACTS.read_text())["nifreeze_b0_indices"]) | |
| original = [k for k in range(len(bvals)) if k not in b0] | |
| rows = [] | |
| for i, (x, y) in enumerate(zip(ma, mb, strict=True)): | |
| ta, ra = params(x, world_c) | |
| tb, rb = params(y, world_c) | |
| qa, _ = params(x, qsi_c) | |
| qb, _ = params(y, qsi_c) | |
| da = xyz @ x[:3, :3].T + x[:3, 3] | |
| db = xyz @ y[:3, :3].T + y[:3, 3] | |
| rows.append( | |
| { | |
| "original_index": original[i], | |
| "bval": int(bvals[original[i]]), | |
| "dt_world": float(np.abs(ta - tb).max()), | |
| "dt_qsiprep": float(np.abs(qa - qb).max()), | |
| "drot_deg": float(np.abs(ra - rb).max()), | |
| "mask_disp_diff_max_mm": float(np.linalg.norm(da - db, axis=1).max()), | |
| } | |
| ) | |
| res = {"run_a": str(a), "run_b": str(b), "gate": GATE, "by_shell": {}} | |
| for shell in sorted({r["bval"] for r in rows}): | |
| rs = [r for r in rows if r["bval"] == shell] | |
| res["by_shell"][str(shell)] = { | |
| "volumes": len(rs), | |
| **{ | |
| f"max_{k}": max(r[k] for r in rs) | |
| for k in ("dt_world", "dt_qsiprep", "drot_deg", "mask_disp_diff_max_mm") | |
| }, | |
| "over_gate_world": sum( | |
| r["dt_world"] > GATE or r["drot_deg"] > GATE for r in rs | |
| ), | |
| "over_gate_qsiprep": sum( | |
| r["dt_qsiprep"] > GATE or r["drot_deg"] > GATE for r in rs | |
| ), | |
| } | |
| res["over_gate_world"] = sum(v["over_gate_world"] for v in res["by_shell"].values()) | |
| res["over_gate_qsiprep"] = sum( | |
| v["over_gate_qsiprep"] for v in res["by_shell"].values() | |
| ) | |
| res["rows"] = rows | |
| out.write_text(json.dumps(res, indent=2)) | |
| print(json.dumps({k: v for k, v in res.items() if k != "rows"}, indent=1)) | |
| if __name__ == "__main__": | |
| main(Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3])) | |