-
Notifications
You must be signed in to change notification settings - Fork 7
/
separate_labels.py
executable file
·101 lines (72 loc) · 2.6 KB
/
separate_labels.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
#!/usr/bin/env python3
"""
Write separate volume for each label found in the input image
Usage
----
separate_labels.py <input label image>
separate_labels.py -h
Example
----
>>> separate_labels.py atlas.nii.gz
Authors
----
Wolfgang M. Pauli, Caltech, Division of Humaninities and Social Sciences
Dates
----
2015-05-02 WMP From scratch
2015-07-29 JMT Speed up mask generation, use zero-padded output indexing
License
----
This file is part of atlaskit.
atlaskit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
atlaskit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with atlaskit. If not, see <http://www.gnu.org/licenses/>.
Copyright
----
2015 California Institute of Technology.
"""
__version__ = '0.2.0'
import os
import sys
import argparse
import nibabel as nib
import numpy as np
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Smooth one or more atlas labels')
parser.add_argument('in_file', help="source atlas labels filename")
args = parser.parse_args()
in_file = args.in_file
# Convert relative to absolute path
in_file = os.path.abspath(in_file)
# Load the source atlas image
print('Opening %s' % in_file)
in_nii = nib.load(in_file)
# Load label image
src_labels = in_nii.get_data()
# Construct list of unique label values in image
unique_labels = np.unique(src_labels)
# loop over each unique label value
for label in unique_labels:
if label > 0:
# Create mask for current label value
out_mask = (src_labels == label).astype(int)
# Construct output filename. Use zero-padded indexing
out_file = in_file.strip('.nii.gz') + '_' + '{0:04d}'.format(label) + '.nii.gz'
# Save smoothed labels image
print('Saving label %d to %s' % (label, out_file))
out_nii = nib.Nifti1Image(out_mask, in_nii.get_affine())
out_nii.to_filename(out_file)
print('Done')
# Clean exit
sys.exit(0)
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()