Skip to content

Commit a5184e2

Browse files
author
Shuo Han
committed
refactored calc_colormap
1 parent 611ba48 commit a5184e2

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

colors_script/calc_colormap.py

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,68 @@
22
# -*- coding: utf-8 -*-
33

44

5-
from mindboggle.mio import colors
6-
import numpy as np
75
import os
86
import argparse
9-
import shutil
7+
import numpy as np
108

9+
from mindboggle.mio.colors import distinguishable_colors, label_adjacency_matrix
1110

12-
def parse_inputs():
13-
14-
des = 'calculate colormap for labeled image, store the colors in the file'\
15-
+ ' colors.npy'
16-
parser = argparse.ArgumentParser(description=des)
17-
parser.add_argument('label_filename', help='the path of the label image file')
18-
help = 'path of folder to store intermediate files and final results'
19-
parser.add_argument('output_dir', help=help)
20-
args = parser.parse_args()
21-
return args
2211

12+
if __name__ == "__main__":
2313

24-
def main(args):
14+
description = ('calculate colormap for labeled image;'
15+
'calculated result is stored in output_dirname/colors.npy')
16+
parser = argparse.ArgumentParser(description=description)
17+
parser.add_argument('label_filename', help='path to the label image')
18+
parser.add_argument('output_dirname', help='path to the folder storing '
19+
'temporary files and result')
20+
parser.add_argument('-v', '--verbose', action='store_true', default=False)
21+
args = parser.parse_args()
2522

26-
if not os.path.isdir(args.output_dir):
27-
os.makedirs(args.output_dir)
23+
if not os.path.isdir(args.output_dirname):
24+
os.makedirs(args.output_dirname)
2825

29-
matrix_filename = os.path.join(args.output_dir, 'matrix.npy')
30-
colormap_filename = os.path.join(args.output_dir, 'colormap.npy')
31-
IDs_filename = os.path.join(args.output_dir, 'IDs.npy')
32-
colors_filename = os.path.join(args.output_dir, 'colors.npy')
26+
matrix_filename = os.path.join(args.output_dirname, 'matrix.npy')
27+
colormap_filename = os.path.join(args.output_dirname, 'colormap.npy')
28+
labels_filename = os.path.join(args.output_dirname, 'labels.npy')
29+
colors_filename = os.path.join(args.output_dirname, 'colors.npy')
30+
31+
if args.verbose:
32+
print('finding adjacency maps...')
3333

34-
print('finding adjacency maps')
35-
if not os.path.isfile(matrix_filename) and not os.path.isfile(IDs_filename):
36-
IDs, matrix, output = colors.label_adjacency_matrix(args.label_filename,
37-
out_dir=args.output_dir)
34+
if not os.path.isfile(matrix_filename) or \
35+
not os.path.isfile(labels_filename):
36+
labels, matrix = label_adjacency_matrix(args.label_filename,
37+
out_dir=args.output_dirname)[:2]
3838
matrix = matrix.as_matrix()[:, 1:]
3939
np.save(matrix_filename, matrix)
40-
np.save(IDs_filename, IDs)
40+
np.save(labels_filename, labels)
4141
else:
42-
IDs = np.load(IDs_filename)
42+
labels = np.load(labels_filename)
4343
matrix = np.load(matrix_filename)
4444

45-
print('finding colormap')
45+
if args.verbose:
46+
print('finding colormap...')
47+
4648
if not os.path.isfile(colormap_filename):
47-
ncolors = len(IDs)
48-
colormap = colors.distinguishable_colors(ncolors=ncolors,
49-
plot_colormap=False,
50-
save_csv=False,
51-
out_dir=args.output_dir)
49+
num_colors = len(labels)
50+
colormap = distinguishable_colors(ncolors=num_colors,
51+
plot_colormap=False,
52+
save_csv=False,
53+
out_dir=args.output_dirname)
5254
np.save(colormap_filename, colormap)
5355
else:
5456
colormap = np.load(colormap_filename)
5557

56-
print('finding label colors')
57-
label_colors = colors.group_colors(colormap, args.label_filename, IDs=IDs,
58-
adjacency_matrix=matrix,
59-
out_dir=args.output_dir,
60-
plot_colors=True,
61-
plot_graphs=False)
62-
np.save(colors_filename, label_colors)
58+
if args.verbose:
59+
print('finding label colors')
6360

64-
65-
if __name__ == "__main__":
66-
args = parse_inputs()
67-
main(args)
61+
if not os.path.isfile(colors_filename):
62+
label_colors = colors.group_colors(colormap,
63+
args.label_filename,
64+
IDs=labels,
65+
adjacency_matrix=matrix,
66+
out_dir=args.output_dirname,
67+
plot_colors=False,
68+
plot_graphs=False)
69+
np.save(colors_filename, label_colors)

0 commit comments

Comments
 (0)