0
|
1 #!/usr/bin/env python
|
|
2 import argparse
|
|
3 import os
|
|
4 import shutil
|
|
5
|
14
|
6 from dipy.core.gradients import gradient_table
|
22
|
7 from dipy.data import fetch_sherbrooke_3shell, fetch_stanford_hardi
|
14
|
8 from dipy.io import read_bvals_bvecs
|
11
|
9 from matplotlib import pyplot
|
0
|
10
|
26
|
11 import nibabel
|
|
12
|
0
|
13 parser = argparse.ArgumentParser()
|
21
|
14 parser.add_argument('--drmi_dataset', dest='drmi_dataset', help='Input dataset')
|
30
|
15 parser.add_argument('--drmi_dataset_type', dest='drmi_dataset_type', help='Input dataset type')
|
18
|
16 parser.add_argument('--output_nifti1', dest='output_nifti1', help='Output Nifti1 dataset')
|
28
|
17 parser.add_argument('--output_nifti1_extra_files', dest='output_nifti1_extra_files', help='Output Nifti1 extra files')
|
14
|
18 parser.add_argument('--output_png', dest='output_png', help='Output dataset')
|
0
|
19
|
|
20 args = parser.parse_args()
|
|
21
|
28
|
22 def move_directory_files(source_dir, destination_dir, copy=False, remove_source_dir=False):
|
|
23 source_directory = os.path.abspath(source_dir)
|
|
24 destination_directory = os.path.abspath(destination_dir)
|
|
25 if not os.path.isdir(destination_directory):
|
|
26 os.makedirs(destination_directory)
|
|
27 for dir_entry in os.listdir(source_directory):
|
|
28 source_entry = os.path.join(source_directory, dir_entry)
|
|
29 if copy:
|
|
30 shutil.copy(source_entry, destination_directory)
|
|
31 else:
|
|
32 shutil.move(source_entry, destination_directory)
|
|
33 if remove_source_dir:
|
|
34 os.rmdir(source_directory)
|
|
35
|
9
|
36 # Get input data.
|
21
|
37 input_dir = args.drmi_dataset
|
30
|
38 #if input_dir == 'stanford_hardi':
|
|
39 if args.drmi_dataset_type == "dataset":
|
28
|
40 fetch_stanford_hardi()
|
|
41 fdwi = os.path.join(input_dir, 'HARDI150.nii.gz')
|
|
42 fbval = os.path.join(input_dir, 'HARDI150.bval')
|
|
43 fbvec = os.path.join(input_dir, 'HARDI150.bvec')
|
30
|
44 img = nibabel.load(fdwi)
|
|
45 else:
|
|
46 img, gtab, labels = read_stanford_labels()
|
0
|
47 data = img.get_data()
|
|
48 # data is a 4D array where the first 3 dimensions are the i, j,
|
|
49 # k voxel coordinates and the last dimension is the number of
|
|
50 # non-weighted (S0s) and diffusion-weighted volumes.
|
|
51 # Visualize the results using matplotlib.
|
|
52 axial_middle = data.shape[2] // 2
|
11
|
53 pyplot.subplot(1, 2, 1).set_axis_off()
|
|
54 pyplot.imshow(data[:, :, axial_middle, 0].T, cmap='gray', origin='lower')
|
|
55 pyplot.subplot(1, 2, 2).set_axis_off()
|
|
56 pyplot.imshow(data[:, :, axial_middle, 10].T, cmap='gray', origin='lower')
|
12
|
57 pyplot.savefig('data.png', bbox_inches='tight')
|
14
|
58 shutil.move('data.png', args.output_png)
|
|
59 # Load the b-values and b-vectors.
|
|
60 bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
|
|
61 gtab = gradient_table(bvals, bvecs)
|
18
|
62 # gtab can be used to tell what part of the data is the S0
|
|
63 # volumes (volumes which correspond to b-values of 0).
|
|
64 S0s = data[:, :, :, gtab.b0s_mask]
|
|
65 # Save this in a new Nifti file.
|
19
|
66 nibabel.save(nibabel.Nifti1Image(S0s, img.affine), 'output.nii')
|
|
67 shutil.move('output.nii', args.output_nifti1)
|
28
|
68 # Move the entire contents of input_dir to output_nifti1_extra_files.
|
|
69 move_directory_files(input_dir, args.output_nifti1_extra_files)
|