webapps / hmc /reference /scripts /folder_volume_map.py
gdevenyi's picture
hmc: NiFreeze 3bcfb00 and QSIPrep 26.1.1 SHORELine reference runs on ds005577 sub-107 (part 9)
a4ba047 verified
Raw History Blame Contribute Delete
2.19 kB
"""Map each collected QSIPrep registration folder to the moving volume in its command.
Usage: python -I folder_volume_map.py REFERENCE_DIR
For each run qsiprep-*/collected/{b0,model}/<iteration>/NNN/command.txt, reads the
moving image of the first --metric (the second path in the brackets) and prints the
folder name and the split_dwis volume. Shows that the folder name is not always the
volume index.
"""
import json
import re
import sys
from pathlib import Path
root = Path(sys.argv[1])
out: dict = {}
for run in sorted(root.glob("qsiprep-*")):
for cmd in sorted((run / "collected").glob("*/*/*/command.txt")):
stage, it, name = cmd.parts[-4:-1]
text = cmd.read_text()
m = re.search(r"--metric\s+\w+\[\s*([^,\s]+),\s*([^,\s]+),", text)
vol = re.search(r"vol\.(\d{4})\.nii", m.group(2)).group(1)
out.setdefault(run.name, {}).setdefault(f"{stage}/{it}", {})[name] = int(vol)
# The maps stated in the README.
B0 = [10 * k for k in range(11)]
DWI = [v for v in range(101) if v % 10]
EXPECTED = {
"b0/iterative_alignment_000": {f"{k:03d}": v for k, v in enumerate(B0)},
"b0/iterative_alignment_001": {f"1{k}".zfill(3): v for k, v in enumerate(B0)},
"model/initial_model_iteration": {f"{k:03d}": v for k, v in enumerate(DWI)},
"model/shoreline_iteration001": {f"{k:03d}": v for k, v in enumerate(DWI)},
}
for run, groups in out.items():
assert groups == EXPECTED, run
man = json.loads((root / run / "collected/manifest-host-paths.json").read_text())
# The manifest index equals the folder name, and its moving path gives the volume.
for it, entries in man["b0_stage"].items():
for e in entries:
vol = int(re.search(r"vol\.(\d{4})\.nii", e["moving"][0]).group(1))
assert EXPECTED[f"b0/{it}"][f"{e['index']:03d}"] == vol, (run, it, e)
summary = {}
for run, groups in out.items():
for key, m in groups.items():
summary.setdefault(key, []).append(m)
for key, maps in summary.items():
same = all(m == maps[0] for m in maps)
first = maps[0]
print(key, "same in all runs:", same)
print(" ", json.dumps(dict(list(first.items())[:3] + list(first.items())[-2:])))