-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_surface_gii.py
executable file
·40 lines (33 loc) · 1.29 KB
/
read_surface_gii.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
#!/usr/bin/env python3
import json
import argparse
from nibabel import gifti
def read_surface(filename):
# load gifti image
surface = gifti.read(filename)
# get point set
points = surface.get_arrays_from_intent('NIFTI_INTENT_POINTSET')[0]
# get triangle set
trig = surface.get_arrays_from_intent('NIFTI_INTENT_TRIANGLE')[0]
# create output dict
output_dict = {}
output_dict['vertices'] = []
output_dict['faces'] = []
# store data in dict
for item in points.data:
dataset = [float(element) for element in item]
output_dict['vertices'].append(dataset)
for item in trig.data:
dataset = [int(element) for element in item]
output_dict['faces'].append(dataset)
# return dict
return output_dict
if __name__ == '__main__':
# setup argument parser and read arguments
parser = argparse.ArgumentParser(description="I read in gifti surface files.")
parser.add_argument('filename', metavar='surface_file', help='path to surface file')
parsed = parser.parse_args()
geometry = read_surface(parsed.filename)
with open('{}.json'.format(parsed.filename),'w') as datafile:
#json.dump(geometry,datafile,sort_keys=True,indent=4,separators=(',',':'))
json.dump(geometry,datafile,separators=(',',':'))