Skip to content

Instantly share code, notes, and snippets.

@mpaquette
Created November 8, 2018 16:08
Show Gist options
  • Save mpaquette/6de24e6ca47c1cb9e4a42c62b98573ef to your computer and use it in GitHub Desktop.
Save mpaquette/6de24e6ca47c1cb9e4a42c62b98573ef to your computer and use it in GitHub Desktop.
Compute mean b-value from voxelwise btable (output of compute_spatial_btable.py).
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import nibabel as nib
import argparse
DESCRIPTION = """
Compute mean b-value from voxelwise btable (output of compute_spatial_btable.py).
"""
def buildArgsParser():
p = argparse.ArgumentParser(description=DESCRIPTION)
p.add_argument('btable', action='store', type=str,
help='Path of voxelwise btable file')
p.add_argument('outfile', action='store', type=str,
help='Path of the output mean b-value file')
p.add_argument('--mask', dest='mask', action='store', type=str,
help='Path of the mask file. If none given, computes on the full volume.')
return p
def main():
# parse inpout
parser = buildArgsParser()
args = parser.parse_args()
btablefile = args.btable
outfile = args.outfile
maskfile = args.mask
btable_img = nib.load(btablefile)
btable = btable_img.get_data()
if maskfile is None:
mask = np.ones(btable.shape[:3])
print('No mask used, beware of inaccurate volume boundary.')
else:
mask = nib.load(maskfile).get_data()
# such GRANDIOS
mean_b = (btable[...,3]*mask[...,None]).mean(axis=-1)
# save
output_img = nib.Nifti1Image(mean_b, btable_img.affine, btable_img.header)
nib.save(output_img, outfile)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment