comparison dmri.py @ 28:6e7ed2b633dc draft

Uploaded
author greg
date Thu, 30 Nov 2017 08:10:10 -0500
parents 65a365c8ca55
children 6c1fd6d2c07e
comparison
equal deleted inserted replaced
27:c1fba91da909 28:6e7ed2b633dc
11 import nibabel 11 import nibabel
12 12
13 parser = argparse.ArgumentParser() 13 parser = argparse.ArgumentParser()
14 parser.add_argument('--drmi_dataset', dest='drmi_dataset', help='Input dataset') 14 parser.add_argument('--drmi_dataset', dest='drmi_dataset', help='Input dataset')
15 parser.add_argument('--output_nifti1', dest='output_nifti1', help='Output Nifti1 dataset') 15 parser.add_argument('--output_nifti1', dest='output_nifti1', help='Output Nifti1 dataset')
16 parser.add_argument('--output_nifti1_extra_files', dest='output_nifti1_extra_files', help='Output Nifti1 extra files')
16 parser.add_argument('--output_png', dest='output_png', help='Output dataset') 17 parser.add_argument('--output_png', dest='output_png', help='Output dataset')
17 18
18 args = parser.parse_args() 19 args = parser.parse_args()
19 20
21 def move_directory_files(source_dir, destination_dir, copy=False, remove_source_dir=False):
22 source_directory = os.path.abspath(source_dir)
23 destination_directory = os.path.abspath(destination_dir)
24 if not os.path.isdir(destination_directory):
25 os.makedirs(destination_directory)
26 for dir_entry in os.listdir(source_directory):
27 source_entry = os.path.join(source_directory, dir_entry)
28 if copy:
29 shutil.copy(source_entry, destination_directory)
30 else:
31 shutil.move(source_entry, destination_directory)
32 if remove_source_dir:
33 os.rmdir(source_directory)
34
20 # Get input data. 35 # Get input data.
21 input_dir = args.drmi_dataset 36 input_dir = args.drmi_dataset
22 if input_dir == 'sherbrooke_3shell': 37 if input_dir == 'stanford_hardi':
38 fetch_stanford_hardi()
39 fdwi = os.path.join(input_dir, 'HARDI150.nii.gz')
40 fbval = os.path.join(input_dir, 'HARDI150.bval')
41 fbvec = os.path.join(input_dir, 'HARDI150.bvec')
42 elif input_dir == 'sherbrooke_3shell':
23 fetch_sherbrooke_3shell() 43 fetch_sherbrooke_3shell()
24 fdwi = os.path.join(input_dir, 'HARDI193.nii.gz') 44 fdwi = os.path.join(input_dir, 'HARDI193.nii.gz')
25 fbval = os.path.join(input_dir, 'HARDI193.bval') 45 fbval = os.path.join(input_dir, 'HARDI193.bval')
26 fbvec = os.path.join(input_dir, 'HARDI193.bvec') 46 fbvec = os.path.join(input_dir, 'HARDI193.bvec')
27 elif input_dir == 'stanford_hardi':
28 fetch_stanford_hardi()
29 fdwi = os.path.join(input_dir, 'HARDI150.nii.gz')
30 fbval = os.path.join(input_dir, 'HARDI150.bval')
31 fbvec = os.path.join(input_dir, 'HARDI150.bvec')
32 # Load the dMRI datasets. 47 # Load the dMRI datasets.
33 img = nibabel.load(fdwi) 48 img = nibabel.load(fdwi)
34 data = img.get_data() 49 data = img.get_data()
35 # data is a 4D array where the first 3 dimensions are the i, j, 50 # data is a 4D array where the first 3 dimensions are the i, j,
36 # k voxel coordinates and the last dimension is the number of 51 # k voxel coordinates and the last dimension is the number of
50 # volumes (volumes which correspond to b-values of 0). 65 # volumes (volumes which correspond to b-values of 0).
51 S0s = data[:, :, :, gtab.b0s_mask] 66 S0s = data[:, :, :, gtab.b0s_mask]
52 # Save this in a new Nifti file. 67 # Save this in a new Nifti file.
53 nibabel.save(nibabel.Nifti1Image(S0s, img.affine), 'output.nii') 68 nibabel.save(nibabel.Nifti1Image(S0s, img.affine), 'output.nii')
54 shutil.move('output.nii', args.output_nifti1) 69 shutil.move('output.nii', args.output_nifti1)
70 # Move the entire contents of input_dir to output_nifti1_extra_files.
71 move_directory_files(input_dir, args.output_nifti1_extra_files)