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