0
|
1 #!/usr/bin/env python
|
|
2 import argparse
|
3
|
3 import os
|
0
|
4 import shutil
|
|
5
|
|
6 from dipy.data import fetch_sherbrooke_3shell
|
|
7 from dipy.data import fetch_stanford_hardi
|
|
8 from dipy.data import get_sphere
|
|
9 from dipy.data import read_sherbrooke_3shell
|
|
10 from dipy.data import read_stanford_hardi
|
|
11 from dipy.direction import peaks_from_model
|
|
12 from dipy.io.image import save_nifti
|
|
13 from dipy.reconst.csdeconv import (ConstrainedSphericalDeconvModel, auto_response)
|
|
14 from dipy.reconst.dti import TensorModel
|
|
15 from dipy.segment.mask import median_otsu
|
|
16 from dipy.tracking.local import LocalTracking, ThresholdTissueClassifier
|
|
17 from dipy.tracking.streamline import Streamlines
|
|
18 from dipy.tracking.utils import random_seeds_from_mask
|
|
19 from dipy.viz import actor, window
|
|
20
|
|
21 import numpy as np
|
|
22
|
|
23 parser = argparse.ArgumentParser()
|
3
|
24 parser.add_argument('--input', dest='input', help='Input dataset')
|
0
|
25 parser.add_argument('--output_csd_direction_field', dest='output_csd_direction_field', help='Output csd direction field dataset')
|
|
26 parser.add_argument('--output_det_streamlines', dest='output_det_streamlines', help='Output det streamlines dataset')
|
|
27 parser.add_argument('--output_fa_map', dest='output_fa_map', help='Output fa map dataset')
|
3
|
28 parser.add_argument('--output_fa_map_files_path', dest='output_fa_map_files_path', help='Output fa map extra files path')
|
0
|
29
|
|
30 args = parser.parse_args()
|
|
31
|
3
|
32 def move_directory_files(source_dir, destination_dir, copy=False, remove_source_dir=False):
|
|
33 source_directory = os.path.abspath(source_dir)
|
|
34 destination_directory = os.path.abspath(destination_dir)
|
|
35 if not os.path.isdir(destination_directory):
|
|
36 os.makedirs(destination_directory)
|
|
37 for dir_entry in os.listdir(source_directory):
|
|
38 source_entry = os.path.join(source_directory, dir_entry)
|
|
39 if copy:
|
|
40 shutil.copy(source_entry, destination_directory)
|
|
41 else:
|
|
42 shutil.move(source_entry, destination_directory)
|
|
43 if remove_source_dir:
|
|
44 os.rmdir(source_directory)
|
|
45
|
0
|
46 interactive = False
|
|
47
|
|
48 # Get input data.
|
3
|
49 # TODO: do not hard-code 'stanford_hardi'
|
|
50 input_dir = 'stanford_hardi'
|
|
51 os.mkdir(input_dir)
|
|
52 for f in os.listdir(args.input_extra_files_path):
|
|
53 shutil.copy(os.path.join(args.input_extra_files_path, f), input_dir)
|
|
54 img, gtab = read_stanford_hardi()
|
0
|
55
|
|
56 data = img.get_data()
|
|
57 maskdata, mask = median_otsu(data, 3, 1, False, vol_idx=range(10, 50), dilate=2)
|
|
58
|
|
59 response, ratio = auto_response(gtab, data, roi_radius=10, fa_thr=0.7)
|
|
60 csd_model = ConstrainedSphericalDeconvModel(gtab, response)
|
|
61 sphere = get_sphere('symmetric724')
|
|
62 csd_peaks = peaks_from_model(model=csd_model, data=data, sphere=sphere, mask=mask, relative_peak_threshold=.5, min_separation_angle=25, parallel=True)
|
|
63
|
|
64 tensor_model = TensorModel(gtab, fit_method='WLS')
|
|
65 tensor_fit = tensor_model.fit(data, mask)
|
|
66 fa = tensor_fit.fa
|
|
67
|
|
68 tissue_classifier = ThresholdTissueClassifier(fa, 0.1)
|
|
69 seeds = random_seeds_from_mask(fa > 0.3, seeds_count=1)
|
|
70
|
|
71 ren = window.Renderer()
|
|
72 ren.add(actor.peak_slicer(csd_peaks.peak_dirs, csd_peaks.peak_values, colors=None))
|
|
73 window.record(ren, out_path='csd_direction_field.png', size=(900, 900))
|
|
74 shutil.move('csd_direction_field.png', args.output_csd_direction_field)
|
|
75
|
|
76 streamline_generator = LocalTracking(csd_peaks, tissue_classifier, seeds, affine=np.eye(4), step_size=0.5)
|
|
77 streamlines = Streamlines(streamline_generator)
|
|
78
|
|
79 ren.clear()
|
|
80 ren.add(actor.line(streamlines))
|
|
81 window.record(ren, out_path='det_streamlines.png', size=(900, 900))
|
|
82 shutil.move('det_streamlines.png', args.output_det_streamlines)
|
|
83
|
|
84 save_nifti('fa_map.nii', fa, img.affine)
|
|
85 shutil.move('fa_map.nii', args.output_fa_map)
|
3
|
86 move_directory_files(input_dir, args.output_fa_map_files_path, copy=True)
|