File size: 1,812 Bytes
a4ba047 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | """Is the reference GP prediction close to a voxel-mean predictor?
Inside the mask, correlate predicted_00007 of the GP run with the mean of the
other 89 sample_* volumes and with the mean of its own shell (excluding itself).
Usage: python -I gp_mean_check.py RUN_DIR [VOL]
"""
import json
import sys
from pathlib import Path
import nibabel as nb
import numpy as np
BVAL = Path("/var/tmp/dwi-data/ds005577/sub-107_dir-AP_dwi.bval")
FACTS = Path("/var/tmp/dwi-hmc/inputs/input-facts.json")
run = Path(sys.argv[1])
vol = int(sys.argv[2]) if len(sys.argv) > 2 else 7
work = run / "work"
mask = np.asanyarray(nb.load(work / "brainmask.nii.gz").dataobj) > 0
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]
shell = bvals[original[vol]]
all_sum = np.zeros(mask.sum())
shell_sum = np.zeros(mask.sum())
n_shell = 0
for i in range(len(original)):
if i == vol:
continue
s = np.asanyarray(nb.load(work / f"sample_{i:05d}.nii.gz").dataobj)[mask].astype(
float
)
all_sum += s
if bvals[original[i]] == shell:
shell_sum += s
n_shell += 1
pred = np.asanyarray(nb.load(work / f"predicted_{vol:05d}.nii.gz").dataobj)[
mask
].astype(float)
own = np.asanyarray(nb.load(work / f"sample_{vol:05d}.nii.gz").dataobj)[mask].astype(
float
)
out = {
"vol": vol,
"original_index": original[vol],
"bval": int(shell),
"r_pred_vs_mean_all_others": float(np.corrcoef(pred, all_sum)[0, 1]),
"r_pred_vs_mean_own_shell": float(np.corrcoef(pred, shell_sum / n_shell)[0, 1]),
"r_pred_vs_own_sample": float(np.corrcoef(pred, own)[0, 1]),
"r_own_shell_mean_vs_own_sample": float(np.corrcoef(shell_sum, own)[0, 1]),
}
print(json.dumps(out))
|