-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracts_cifti_mask.py
executable file
·122 lines (99 loc) · 4.56 KB
/
tracts_cifti_mask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
"""
Takes a JSON file of tract ends and creates a single cifti mask of them. This
script depends on the ciftify module and connectome workbench.
Usage:
tracts_cifti_mask.py [options] <masked_image> <b0_map> <hcp_data> <tracts_json> <output_loc>
Arguments:
<masked_image> The image to be masked.
<b0_map> A b0_map for the image being masked (e.g. a
*_b0_bet.nii.gz file produced by dtifit)
<hcp_data> The full path to the subject's folder of outputs
from the hcp pipeline
<tracts_json> The JSON file of tracts and their start/end
points
<output_loc> The path to the location to store results
Options:
--save_results Retain all intermediate results
--smoothing INT The Sigma value to use during smoothing.
[default: 5]
--cost_function STR Cost function for FLIRT registration
[default: corratio]
--dof INT Degrees of freedom for FLIR registration
[default: 12]
--output_name STR Name to give the output file. If not given, the
input file will be assumed to follow the datman
naming convention and be named as
<masked image>_masks.dscalar.nii
-v, --verbose Additional log statements
"""
import os
import glob
import logging
from docopt import docopt
import tract_end_masks as nii_masks
import mni_transform
import utils
logging.basicConfig(level=logging.WARN,
format="[%(name)s] %(levelname)s: %(message)s")
logger = logging.getLogger(os.path.basename(__file__))
def main(temp_results):
arguments = docopt(__doc__)
masked_image = arguments['<masked_image>']
b0_map = arguments['<b0_map>']
hcp_data = arguments['<hcp_data>']
tracts_file = arguments['<tracts_json>']
output_loc = arguments['<output_loc>']
save_results = arguments['--save_results']
smoothing = arguments['--smoothing']
cost = arguments['--cost_function']
dof = arguments['--dof']
verbose = arguments['--verbose']
if verbose:
logger.setLevel(logging.INFO)
final_output = get_output_name(output_loc, arguments['--output_name'],
masked_image)
results_dir = get_results_dir(temp_results, output_loc, save_results)
logger.info("Generating tract end point masks.")
nifti_mask_folder = make_output_folder(results_dir, 'nii_masks')
nii_masks.generate_masks(masked_image, tracts_file, nifti_mask_folder)
logger.info("Smoothing tract end point masks with Sigma "
"{}.".format(smoothing))
smoothed_mask_folder = make_output_folder(results_dir,
'nii_sm{}'.format(smoothing))
utils.prep_masks(nifti_mask_folder, smoothed_mask_folder, smoothing)
logger.info("Moving smoothed masks to MNI space.")
mni_folder = make_output_folder(results_dir, 'nii_MNI')
transform_to_MNI(smoothed_mask_folder, mni_folder, b0_map, hcp_data,
cost, dof)
logger.info("Converting MNI space masks to cifti format.")
cifti_folder = make_output_folder(results_dir, 'cifti_masks')
project_to_surfaces(mni_folder, cifti_folder, hcp_data)
logger.info("Merging individual cifti masks to one.")
utils.merge_ciftis(cifti_folder, final_output)
def get_output_name(output_loc, user_arg, masked_image):
if user_arg:
final_name = user_arg
else:
base_name = os.path.basename(masked_image)
final_name = utils.remove_extension(base_name) + '_masks.dscalar.nii'
return os.path.join(output_loc, final_name)
def get_results_dir(temp_dir, perm_dir, save):
if save:
return perm_dir
return temp_dir
def make_output_folder(output_loc, folder_name):
new_path = os.path.join(output_loc, folder_name)
if not os.path.exists(new_path):
os.makedirs(new_path)
return new_path
def transform_to_MNI(input_dir, output_dir, b0_map, hcp_data, cost, dof):
for mask in glob.glob(os.path.join(input_dir, '*.nii*')):
mni_transform.transform_to_MNI(mask, output_dir, cost, dof, hcp_data,
b0_map)
def project_to_surfaces(input_dir, output_dir, hcp_data):
for mask in glob.glob(os.path.join(input_dir, '*.nii*')):
utils.project_to_surfaces(mask, output_dir, hcp_data)
if __name__ == "__main__":
with utils.TempDir() as temp_results:
main(temp_results)