0
|
1 #!/usr/bin/env python
|
|
2 import argparse
|
|
3 import os
|
|
4 import nibabel
|
|
5 import shutil
|
|
6
|
14
|
7 from dipy.core.gradients import gradient_table
|
0
|
8 from dipy.data import fetch_sherbrooke_3shell
|
14
|
9 from dipy.io import read_bvals_bvecs
|
11
|
10 from matplotlib import pyplot
|
0
|
11
|
|
12 parser = argparse.ArgumentParser()
|
|
13 parser.add_argument('--input', dest='input', help='Input dataset')
|
18
|
14 parser.add_argument('--output_nifti1', dest='output_nifti1', help='Output Nifti1 dataset')
|
14
|
15 parser.add_argument('--output_png', dest='output_png', help='Output dataset')
|
0
|
16
|
|
17 args = parser.parse_args()
|
|
18
|
10
|
19 input_dir = 'sherbrooke_3shell'
|
9
|
20 # Get input data.
|
|
21 fetch_sherbrooke_3shell()
|
|
22 fdwi = os.path.join(input_dir, 'HARDI193.nii.gz')
|
|
23 fbval = os.path.join(input_dir, 'HARDI193.bval')
|
|
24 fbvec = os.path.join(input_dir, 'HARDI193.bvec')
|
0
|
25 # Load the dMRI datasets.
|
|
26 img = nibabel.load(fdwi)
|
|
27 data = img.get_data()
|
|
28 # data is a 4D array where the first 3 dimensions are the i, j,
|
|
29 # k voxel coordinates and the last dimension is the number of
|
|
30 # non-weighted (S0s) and diffusion-weighted volumes.
|
|
31 # Visualize the results using matplotlib.
|
|
32 axial_middle = data.shape[2] // 2
|
11
|
33 pyplot.figure('Showing the datasets')
|
|
34 pyplot.subplot(1, 2, 1).set_axis_off()
|
|
35 pyplot.imshow(data[:, :, axial_middle, 0].T, cmap='gray', origin='lower')
|
|
36 pyplot.subplot(1, 2, 2).set_axis_off()
|
|
37 pyplot.imshow(data[:, :, axial_middle, 10].T, cmap='gray', origin='lower')
|
12
|
38 pyplot.savefig('data.png', bbox_inches='tight')
|
14
|
39 shutil.move('data.png', args.output_png)
|
|
40 # Load the b-values and b-vectors.
|
|
41 bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
|
|
42 gtab = gradient_table(bvals, bvecs)
|
18
|
43 # gtab can be used to tell what part of the data is the S0
|
|
44 # volumes (volumes which correspond to b-values of 0).
|
|
45 S0s = data[:, :, :, gtab.b0s_mask]
|
|
46 # Save this in a new Nifti file.
|
19
|
47 nibabel.save(nibabel.Nifti1Image(S0s, img.affine), 'output.nii')
|
|
48 shutil.move('output.nii', args.output_nifti1)
|