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