Skip to content

Commit 3da8b55

Browse files
Shuo HanShuo Han
authored andcommitted
refactored; added output directory in colors.py
1 parent 626e0df commit 3da8b55

File tree

3 files changed

+103
-57
lines changed

3 files changed

+103
-57
lines changed

colors_script/calc_colormap.py

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,69 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
45
import sys
56
sys.path.append('../mindboggle/mio')
67
import colors
78
import numpy as np
89
import os
9-
import pickle
10+
import argparse
11+
import shutil
12+
13+
14+
def parse_inputs():
15+
16+
des = 'calculate colormap for labeled image, store the colors in the file'\
17+
+ ' colors.npy'
18+
parser = argparse.ArgumentParser(description=des)
19+
parser.add_argument('label_filename', help='the path of the label image file')
20+
help = 'path of folder to store intermediate files and final results'
21+
parser.add_argument('output_dir', help=help)
22+
args = parser.parse_args()
23+
return args
24+
1025

11-
filename = 'label.nii.gz'
12-
matrix_filename = 'matrix.pickle'
13-
colormap_filename = 'colormap.npy'
26+
def main(args):
27+
28+
if not os.path.isdir(args.output_dir):
29+
os.makedirs(args.output_dir)
30+
31+
matrix_filename = os.path.join(args.output_dir, 'matrix.npy')
32+
colormap_filename = os.path.join(args.output_dir, 'colormap.npy')
33+
IDs_filename = os.path.join(args.output_dir, 'IDs.npy')
34+
colors_filename = os.path.join(args.output_dir, 'colors.npy')
1435

15-
if not os.path.isfile(matrix_filename):
1636
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)
37+
if not os.path.isfile(matrix_filename) and not os.path.isfile(IDs_filename):
38+
IDs, matrix, output = colors.label_adjacency_matrix(args.label_filename,
39+
out_dir=args.output_dir)
40+
matrix = matrix.as_matrix()[:, 1:]
41+
np.save(matrix_filename, matrix)
42+
np.save(IDs_filename, IDs)
43+
else:
44+
IDs = np.load(IDs_filename)
45+
matrix = np.load(matrix_filename)
46+
47+
print('finding colormap')
48+
if not os.path.isfile(colormap_filename):
49+
ncolors = len(IDs)
50+
colormap = colors.distinguishable_colors(ncolors=ncolors,
51+
plot_colormap=False,
52+
save_csv=False,
53+
out_dir=args.output_dir)
54+
np.save(colormap_filename, colormap)
55+
else:
56+
colormap = np.load(colormap_filename)
57+
58+
print('finding label colors')
59+
label_colors = colors.group_colors(colormap, args.label_filename, IDs=IDs,
60+
adjacency_matrix=matrix,
61+
out_dir=args.output_dir,
62+
plot_colors=True,
63+
plot_graphs=False)
64+
np.save(colors_filename, label_colors)
65+
66+
67+
if __name__ == "__main__":
68+
args = parse_inputs()
69+
main(args)

colors_script/convert_to_mipav_lut.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@
22
# -*- coding: utf-8 -*-
33

44
import numpy as np
5+
import argparse
56

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))
7+
def parse_inputs():
8+
des = 'convert colormap to mipav lut file.'
9+
parser = argparse.ArgumentParser(description=des)
10+
parser.add_argument('colormap_filename')
11+
parser.add_argument('ids_filename')
12+
parser.add_argument('output_filename')
13+
args = parser.parse_args()
14+
return args
15+
16+
def main(args):
17+
18+
labels = np.load(args.ids_filename)
19+
colors = np.load(args.colormap_filename) * 255
20+
21+
contents = list()
22+
contents.append('<LUT>')
23+
contents.append('256\t# Size of LUT Arrays')
24+
25+
for i in range(256):
26+
if i in labels:
27+
idx = np.where(labels == i)[0][0]
28+
c = colors[idx].tolist()
29+
else:
30+
c = [0.0, 0.0, 0.0]
31+
c_str = [str(cc) for cc in c]
32+
line = '\t'.join([str(i), '1.0', *c_str])
33+
contents.append(line)
34+
35+
with open(args.output_filename, 'w') as file:
36+
file.write('\n'.join(contents))
37+
38+
39+
if __name__ == "__main__":
40+
args = parse_inputs()
41+
main(args)

mindboggle/mio/colors.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
1010
"""
1111
import sys
12-
sys.path.append('/Users/shuo/Code/git_repo/mindboggle')
12+
sys.path.append('../../mindboggle')
13+
import os
1314

1415

1516
def distinguishable_colors(ncolors, backgrounds=[[0,0,0],[1,1,1]],
16-
save_csv=True, plot_colormap=True, verbose=True):
17+
save_csv=True, plot_colormap=True, verbose=True,
18+
out_dir='.'):
1719
"""
1820
Create a colormap of perceptually distinguishable colors.
1921
@@ -76,6 +78,7 @@ def distinguishable_colors(ncolors, backgrounds=[[0,0,0],[1,1,1]],
7678
from colormath.color_diff import delta_e_cie2000
7779

7880
filename = "colormap_of_{0}_distinguishable_colors".format(ncolors)
81+
filename = os.path.join(out_dir, filename)
7982

8083
# ------------------------------------------------------------------------
8184
# Generate a sizable number of RGB triples. This represents our space of
@@ -160,7 +163,7 @@ def distinguishable_colors(ncolors, backgrounds=[[0,0,0],[1,1,1]],
160163
if verbose:
161164
print(rgb)
162165
plt.barh(0, 50, 1, 0, color=rgb)
163-
plt.savefig(filename + ".png")
166+
plt.savefig(filename + ".png")
164167
if verbose:
165168
print("Colormap image saved to {0}".format(filename + ".png"))
166169

@@ -178,7 +181,7 @@ def distinguishable_colors(ncolors, backgrounds=[[0,0,0],[1,1,1]],
178181

179182
def label_adjacency_matrix(label_file, ignore_values=[-1, 999], add_value=0,
180183
save_table=True, output_format='csv',
181-
verbose=True):
184+
verbose=True, out_dir='.'):
182185
"""
183186
Extract surface or volume label boundaries, find unique label pairs,
184187
and write adjacency matrix (useful for constructing a colormap).
@@ -282,6 +285,8 @@ def label_adjacency_matrix(label_file, ignore_values=[-1, 999], add_value=0,
282285
else:
283286
raise IOError("Use appropriate input file type.")
284287

288+
output_table = os.path.join(out_dir, output_table)
289+
285290
# Find unique pairs (or first two of each list):
286291
pairs = []
287292
for pair in label_pairs:

0 commit comments

Comments
 (0)