|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 | 4 |
|
5 |
| -from mindboggle.mio import colors |
6 |
| -import numpy as np |
7 | 5 | import os
|
8 | 6 | import argparse
|
9 |
| -import shutil |
| 7 | +import numpy as np |
10 | 8 |
|
| 9 | +from mindboggle.mio.colors import distinguishable_colors, label_adjacency_matrix |
11 | 10 |
|
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 |
22 | 11 |
|
| 12 | +if __name__ == "__main__": |
23 | 13 |
|
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() |
25 | 22 |
|
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) |
28 | 25 |
|
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...') |
33 | 33 |
|
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] |
38 | 38 | matrix = matrix.as_matrix()[:, 1:]
|
39 | 39 | np.save(matrix_filename, matrix)
|
40 |
| - np.save(IDs_filename, IDs) |
| 40 | + np.save(labels_filename, labels) |
41 | 41 | else:
|
42 |
| - IDs = np.load(IDs_filename) |
| 42 | + labels = np.load(labels_filename) |
43 | 43 | matrix = np.load(matrix_filename)
|
44 | 44 |
|
45 |
| - print('finding colormap') |
| 45 | + if args.verbose: |
| 46 | + print('finding colormap...') |
| 47 | + |
46 | 48 | 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) |
52 | 54 | np.save(colormap_filename, colormap)
|
53 | 55 | else:
|
54 | 56 | colormap = np.load(colormap_filename)
|
55 | 57 |
|
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') |
63 | 60 |
|
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