webapps / hmc /reference /scripts /clip_facts.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.15 kB
"""Q55 facts from kept intermediates: dtype and range of sample_* and predicted_*,
and whether predicted_* changes across the visiting order (cumulative in-place clip).
Usage: python -I clip_facts.py RUN_DIR [OUT.json]
"""
import json
import sys
from pathlib import Path
import nibabel as nb
import numpy as np
run = Path(sys.argv[1])
work = run / "work"
order = [int(x) for x in (work / "order.txt").read_text().split()]
dwi = nb.load("/var/tmp/dwi-data/ds005577/sub-107_dir-AP_dwi.nii.gz")
facts = json.loads(Path("/var/tmp/dwi-hmc/inputs/input-facts.json").read_text())
b0 = set(facts["nifreeze_b0_indices"])
original = [k for k in range(facts["shape"][3]) if k not in b0]
def load(p):
img = nb.load(p)
return img.get_data_dtype(), np.asarray(img.dataobj)
res = {"order_first_10": order[:10], "volumes_done": len(order)}
first = order[0]
dt, s = load(work / f"sample_{first:05d}.nii.gz")
raw = np.asarray(dwi.dataobj[..., original[first]])
res["sample_first"] = {
"index": first,
"dtype": str(dt),
"min": float(s.min()),
"max": float(s.max()),
"equals_raw_input_volume": bool(np.array_equal(s, raw)),
}
preds = []
for i in order:
dtp, p = load(work / f"predicted_{i:05d}.nii.gz")
preds.append(p.astype(np.float64))
res["predicted_dtype"] = str(dtp)
res["predicted_first"] = {
"index": first,
"min": float(preds[0].min()),
"max": float(preds[0].max()),
"integral_values": bool(np.all(preds[0] == np.round(preds[0]))),
}
diffs = [float(np.abs(preds[k] - preds[k - 1]).max()) for k in range(1, len(preds))]
res["predicted_max_abs_change_between_consecutive_visits"] = diffs[:10]
res["predicted_all_identical_after_first"] = bool(
all(np.array_equal(preds[1], p) for p in preds[1:])
)
res["predicted_first_vs_second_identical"] = (
bool(np.array_equal(preds[0], preds[1])) if len(preds) > 1 else None
)
res["predicted_first_vs_last_max_abs"] = float(np.abs(preds[0] - preds[-1]).max())
res["predicted_first_vs_last_n_voxels_differ"] = int((preds[0] != preds[-1]).sum())
print(json.dumps(res, indent=1))
if len(sys.argv) > 2:
Path(sys.argv[2]).write_text(json.dumps(res, indent=2))