-
Notifications
You must be signed in to change notification settings - Fork 7
/
prob_bilat2hemi.py
executable file
·95 lines (73 loc) · 2.91 KB
/
prob_bilat2hemi.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
#!/usr/bin/env python3
"""
Separate bilateral prob atlas into hemispheric atlases
- assumes original atlas is centered on the anatomic midline and uses the voxel midplane to split the volumes
- Outputs a new 4D prob atlas with twice as many label volumes
Authors
----
Mike Tyszka, Caltech Brain Imaging Center
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
----
2022 California Institute of Technology.
"""
__version__ = '0.1.0'
import os.path as op
import sys
import argparse
import nibabel as nib
import numpy as np
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Probabilistic label volumes in microliters')
parser.add_argument('-i', '--inatlas', required=True, help="Bilateral 4D probabilistic atlas")
parser.add_argument('-o', '--outatlas', help="Hemispheric 4D probabilistic atlas")
# Parse command line arguments
args = parser.parse_args()
bilat_fname = args.inatlas
if args.outatlas:
hemi_fname = op.realpath(args.outatlas)
else:
hemi_fname = args.inatlas.replace('.nii.gz', '_LR.nii.gz')
# Load the bilateral atlas
print(f'Loading bilateral atlas from {bilat_fname}')
bilat_nii = nib.load(bilat_fname)
bilat_img = bilat_nii.get_fdata()
# Grab 4D image dimensions
nx, ny, nz, nl = bilat_img.shape
# Assume data is in RAS order
# TODO: determine LR anatomic axis from qform (qform_code == 1)
hx = int(nx/2.0)
# LH and RH masks
rh_mask = np.zeros([nx, ny, nz]).astype(int)
rh_mask[0:hx, ...] = 1
lh_mask = 1 - rh_mask
# Make space for hemispheric prob labels
rh_img = np.zeros_like(bilat_img)
lh_img = np.zeros_like(bilat_img)
print(f'Splitting bilateral labels into hemispheric labels')
for lc in range(nl):
print(f' Label {lc}')
rh_img[..., lc] = bilat_img[..., lc] * rh_mask
lh_img[..., lc] = bilat_img[..., lc] * lh_mask
# Concatenate RH and LH prob atlases
hemi_img = np.concatenate([rh_img, lh_img], axis=3)
# Create and save new Nifti image
print(f'Saving hemispheric atlas to {hemi_fname}')
hemi_nii = nib.Nifti1Image(hemi_img, affine=bilat_nii.affine)
nib.save(hemi_nii, hemi_fname)
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()