-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs2niivue.py
69 lines (63 loc) · 1.81 KB
/
fs2niivue.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Convert FreeSurfer text color tables to NiiVue format
#Example
# python lut2niivue.py /path/to/text/files
import numpy as np
import codecs, json, os
import sys
import glob
def lut2niivue(fsname):
file1 = open(fsname, 'r')
lines = file1.readlines()
mxIdx = 0;
validLines = [];
for line in lines:
if len(line) < 1:
continue;
if line.startswith('#'):
continue;
#each line should have six items:
# index, name, R, G, B, A
items = line.split()
if len(items) < 6:
continue;
idx = int(items[0]);
mxIdx = max(mxIdx, idx);
validLines.append(line)
js = {'R': [], 'G': [], 'B': [], 'A': [], 'I': [], 'labels': []}
#fill all slots, as valid lines may be sparse and skip indices
for i in range(mxIdx + 1):
js['labels'].append('');
js['R'].append(0);
js['G'].append(0);
js['B'].append(0);
js['A'].append(255);
js['I'].append(i);
js['A'][0] = 0;
for line in validLines:
items = line.split()
idx = int(items[0])
js['labels'][idx] = items[1]
js['R'][idx] = int(items[2])
js['G'][idx] = int(items[3])
js['B'][idx] = int(items[4])
#js['A'][idx] = int(items[5])
fnm = os.path.splitext(fsname)[0]+'.json'
print(fnm)
with open(fnm, 'w', encoding='utf-8', newline='\r\n') as f:
f.write(json.dumps(js, indent=4, sort_keys=False, ensure_ascii=False))
if __name__ == '__main__':
"""Convert FreeSurfer colormaps to NiiVue
Parameters
----------
fname : str
path to FreeSurfer files
"""
pth = os.getcwd()
if len(sys.argv) > 1:
pth = sys.argv[1]
os.chdir(pth)
for file in glob.glob("*.txt"):
fname = os.path.join(pth, file);
lut2niivue(fname)