Download hmc/reference/scripts/mean_b0.py from neurodeskorg/webapps: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/mean_b0.py
- Command line
-
hf download hf://datasets/neurodeskorg/webapps/hmc/reference/scripts/mean_b0.py
-
curl -L -o mean_b0.py https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/mean_b0.py
1.02 kB
| """Mean of the b < 50 volumes as float32 NIfTI on the DWI grid (as noddi's meanVolumes). | |
| Usage: python -I mean_b0.py DWI.nii.gz BVAL OUT.nii | |
| """ | |
| import sys | |
| import nibabel as nb | |
| import numpy as np | |
| dwi_path, bval_path, out_path = sys.argv[1:4] | |
| img = nb.load(dwi_path) | |
| bvals = np.loadtxt(bval_path) | |
| b0 = np.flatnonzero(bvals < 50) | |
| data = np.asarray(img.dataobj) | |
| # float32 accumulation, volume order, then divide: the order of noddi's meanVolumes. | |
| mean = np.zeros(img.shape[:3], dtype=np.float32) | |
| for v in b0: | |
| mean += data[..., v].astype(np.float32) | |
| mean /= np.float32(len(b0)) | |
| hdr = img.header.copy() | |
| hdr.set_data_dtype(np.float32) | |
| hdr.set_data_shape(mean.shape) | |
| out = nb.Nifti1Image(mean, img.affine, hdr) | |
| out.header.set_slope_inter(1.0, 0.0) | |
| out.header.set_qform(img.header.get_qform(), code=int(img.header["qform_code"])) | |
| out.header.set_sform(img.header.get_sform(), code=int(img.header["sform_code"])) | |
| out.to_filename(out_path) | |
| print(f"b0 volumes: {b0.tolist()} mean range {mean.min():.1f}..{mean.max():.1f}") | |