-
Notifications
You must be signed in to change notification settings - Fork 0
/
voxel_atlas.py
68 lines (50 loc) · 1.88 KB
/
voxel_atlas.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
"""
returns voxels in an image that correspond to a certain value in an atlas
"""
import nibabel
import xml.etree.ElementTree as ET
import numpy
import os.path
from __builtin__ import True
def getAtlasVoxels(region, atlas_file, dir = '/Applications/fmri_progs/fsl/data/atlases/'):
tree = ET.parse(os.path.join(dir, atlas_file))
root = tree.getroot()
summaryimagelist = []
summaryimagefile = ''
root_header = root.find('header')
for images in root_header.findall('images'):
if '2mm' in images.find('summaryimagefile').text:
summaryimagefile = images.find('summaryimagefile').text
atlas_name = summaryimagefile + '.nii.gz'
atlas=nibabel.load(os.path.join(dir, atlas_name[1:]))
atlas_data=atlas.get_data()
name_value = 0
for i in range(len(root[1])):
name = root[1][i].text.split('(')[0].replace("'",'').rstrip(' ').lower()
print name
if name == region.lower():
name_value = i+1
voxels = numpy.where(atlas_data==name_value)
return voxels
raise ValueError('"{region}" not in "{atlas_file}"'.format(region=region, atlas_file=atlas_file))
# voxels = getAtlasVoxels('Corticospinal tract L','JHU-tracts.xml')
def voxelToRegion(X,Y,Z,atlas_file, dir = '/Applications/fmri_progs/fsl/data/atlases/'):
tree = ET.parse(os.path.join(dir, atlas_file))
root = tree.getroot()
summaryimagelist = []
summaryimagefile = ''
root_header = root.find('header')
for images in root_header.findall('images'):
if '2mm' in images.find('summaryimagefile').text:
summaryimagefile = images.find('summaryimagefile').text
atlas_name = summaryimagefile + '.nii.gz'
atlas=nibabel.load(os.path.join(dir, atlas_name[1:]))
atlas_data=atlas.get_data()
atlasRegions = [x.text.lower() for x in root[1]]
index = atlas_data[X,Y,Z] - 1
print index
if index == -1:
return 'none'
else:
return atlasRegions[index]
# print voxelToRegion(45, 16, 36,'HarvardOxford-Cortical.xml')