Skip to content

Commit 626e0df

Browse files
Shuo HanShuo Han
authored andcommitted
fixed colors.py and added scripts directly calling colors.py and converting to mipav lut
1 parent 5ce6137 commit 626e0df

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

colors_script/calc_colormap.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
sys.path.append('../mindboggle/mio')
6+
import colors
7+
import numpy as np
8+
import os
9+
import pickle
10+
11+
filename = 'label.nii.gz'
12+
matrix_filename = 'matrix.pickle'
13+
colormap_filename = 'colormap.npy'
14+
15+
if not os.path.isfile(matrix_filename):
16+
print('finding adjacency maps')
17+
IDs, matrix, output = colors.label_adjacency_matrix(filename, ignore_values=[])
18+
pickle.dump(matrix, open(matrix_filename, 'wb'))
19+
np.save('IDs', IDs)
20+
else:
21+
IDs = np.load('IDs.npy')
22+
matrix = pickle.load(open(matrix_filename, 'rb'))
23+
24+
matrix = matrix.as_matrix()
25+
matrix = matrix[:, 1:]
26+
27+
print('finding colormap')
28+
if not os.path.isfile(colormap_filename):
29+
colormap = colors.distinguishable_colors(ncolors=len(IDs),
30+
backgrounds=[[0,0,0],[1,1,1]],
31+
save_csv=False)
32+
np.save(colormap_filename, colormap)
33+
else:
34+
colormap = np.load(colormap_filename)
35+
36+
print(colormap.shape)
37+
38+
colors = colors.group_colors(colormap, 'cerebellum', IDs=IDs,
39+
adjacency_matrix=matrix)
40+
np.save('colors', colors)

colors_script/convert_to_mipav_lut.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import numpy as np
5+
6+
colormap_filename = 'colors.npy'
7+
labels_filename = 'IDs.npy'
8+
lut_filename = 'colors.lut'
9+
10+
labels = np.load(labels_filename)
11+
colors = np.load(colormap_filename)
12+
colors = colors * 255
13+
14+
contents = list()
15+
contents.append('<LUT>')
16+
contents.append('256\t# Size of LUT Arrays')
17+
18+
for i in range(256):
19+
if i in labels:
20+
idx = np.where(labels == i)
21+
c = colors[idx].tolist()[0]
22+
else:
23+
c = [0.0, 0.0, 0.0]
24+
c_str = [str(cc) for cc in c]
25+
line = '\t'.join([str(i), '1.0', *c_str])
26+
contents.append(line)
27+
28+
with open(lut_filename, 'w') as file:
29+
file.write('\n'.join(contents))

mindboggle/mio/colors.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
Copyright 2016, Mindboggle team (http://mindboggle.info), Apache v2.0 License
99
1010
"""
11+
import sys
12+
sys.path.append('/Users/shuo/Code/git_repo/mindboggle')
1113

1214

1315
def distinguishable_colors(ncolors, backgrounds=[[0,0,0],[1,1,1]],
@@ -502,11 +504,11 @@ def group_colors(colormap, colormap_name, description='', adjacency_matrix=[],
502504
nlabels = np.shape(colors)[0]
503505
new_colors = np.copy(colors)
504506

505-
if not IDs:
507+
if len(IDs) == 0:
506508
IDs = range(nlabels)
507-
if not names:
509+
if len(names) == 0:
508510
names = [str(x) for x in range(nlabels)]
509-
if not groups:
511+
if len(groups) == 0:
510512
groups = [1 for x in range(nlabels)]
511513

512514
# ------------------------------------------------------------------------
@@ -1188,4 +1190,4 @@ def viridis_colormap():
11881190
[0.983868, 0.904867, 0.136897],
11891191
[0.993248, 0.906157, 0.143936]]
11901192

1191-
return viridis
1193+
return viridis

0 commit comments

Comments
 (0)