diff options
author | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-10-29 09:59:35 -0400 |
---|---|---|
committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-10-29 09:59:35 -0400 |
commit | 5b50c58ea4881dd185897dfa93860c60f551d815 (patch) | |
tree | 1eee3271b5ab9fad7774a073110287b27e4d32fd /plot_exps.py | |
parent | f1e90e1a5f7b148cf8113fe463615bd95d5bf26d (diff) |
Prettied up parse output.
Diffstat (limited to 'plot_exps.py')
-rwxr-xr-x[-rw-r--r--] | plot_exps.py | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/plot_exps.py b/plot_exps.py index 06f43b0..46784bc 100644..100755 --- a/plot_exps.py +++ b/plot_exps.py | |||
@@ -1,7 +1,77 @@ | |||
1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
2 | from __future__ import print_function | 2 | from __future__ import print_function |
3 | 3 | ||
4 | import os | ||
5 | import re | ||
6 | import plot | ||
7 | import shutil as sh | ||
8 | |||
9 | from collections import defaultdict | ||
4 | from optparse import OptionParser | 10 | from optparse import OptionParser |
11 | from gnuplot import Plot, curve | ||
12 | from random import randrange | ||
13 | |||
14 | class StyleMaker(object): | ||
15 | LINE_WIDTH = 1.5 | ||
16 | POINT_SIZE = 0.6 | ||
17 | BEST_COLORS = [ | ||
18 | '#ff0000', # red | ||
19 | '#000001', # black | ||
20 | '#0000ff', # blue | ||
21 | '#be00c4', # purple | ||
22 | '#ffd700', # yellow | ||
23 | ] | ||
24 | |||
25 | def __init__(csvs): | ||
26 | self.main_key, self.col_map = __find_columns(csvs) | ||
27 | self.cur_style = 1 | ||
28 | |||
29 | # Use this for least-common varying attribute | ||
30 | self.main_map = {} | ||
31 | # Everything else is a color | ||
32 | self.color_map = TupleTable(self.col_map) | ||
33 | |||
34 | def __find_columns(csvs): | ||
35 | vals = defaultdict(lambda:set) | ||
36 | |||
37 | for csv in csvs: | ||
38 | to_decode = os.path.splitext(csv_file)[0] | ||
39 | params = plot.decode(to_decode) | ||
40 | for k,v in params.iteritems: | ||
41 | vals[k].add(v) | ||
42 | |||
43 | try: | ||
44 | main_key = min([(k,v) for (k,v) in thing.iteritems() if len(v) > 1], | ||
45 | key=operator.itemgetter(1))[0] | ||
46 | except ValueError: | ||
47 | main_key = None | ||
48 | |||
49 | col_map = ColMap() | ||
50 | for k,v in vals.iterkeys(): | ||
51 | if k == self.main_key: continue | ||
52 | for i in v: | ||
53 | self.col_map.try_add(k, i) | ||
54 | return (main_key, col_map) | ||
55 | |||
56 | def __rand_color(): | ||
57 | return "#%s" % "".join([hex(randrange(0, 255))[2:] for i in range(3)]) | ||
58 | |||
59 | def get_style(csv): | ||
60 | to_decode = os.path.splitext(csv_file)[0] | ||
61 | params = plot.decode(to_decode) | ||
62 | |||
63 | if kv not in self.color_map: | ||
64 | color = best.pop() if BEST_COLORS else __rand_color() | ||
65 | self.color_map.add_exp(params, color) | ||
66 | |||
67 | if self.main_key in params: | ||
68 | val = params[self.main_key] | ||
69 | if val not in self.main_map: | ||
70 | self.main_map[val] = self.cur_style | ||
71 | self.cur_style += 1 | ||
72 | style = self.main_map[val] | ||
73 | else: | ||
74 | style = 1 | ||
5 | 75 | ||
6 | def parse_args(): | 76 | def parse_args(): |
7 | parser = OptionParser("usage: %prog [options] [csv_dir]...") | 77 | parser = OptionParser("usage: %prog [options] [csv_dir]...") |
@@ -13,9 +83,54 @@ def parse_args(): | |||
13 | 83 | ||
14 | return parser.parse_args() | 84 | return parser.parse_args() |
15 | 85 | ||
86 | def get_label(kv): | ||
87 | label = [] | ||
88 | for key, value in kv.iteritems(): | ||
89 | label += ["%s=%s" % (key.capitalize(), value)] | ||
90 | return ", ".join(label) | ||
91 | |||
92 | def add_line(plot, csv_file): | ||
93 | to_decode = os.path.splitext(csv_file)[0] | ||
94 | params = plot.decode(to_decode) | ||
95 | |||
96 | def get_stat(path, name): | ||
97 | full = os.path.abspath(path) | ||
98 | rstr = r"(?P<STAT>[^/]+)/((max|min|var|avg)/)*(%s/?)?$" % name | ||
99 | regex = re.compile(rstr, re.I | re.M) | ||
100 | match = regex.search(full) | ||
101 | return match.group("STAT") | ||
102 | |||
103 | def plot_exp(name, data_dir, out_dir): | ||
104 | p = Plot() | ||
105 | p.format = 'pdf' | ||
106 | p.output = "%s/%s.pdf" % (out_dir, name) | ||
107 | p.xlabel = name.replace("vary-", "") | ||
108 | p.ylabel = get_stat(data_dir, name) | ||
109 | p.font = 'Helvetica' | ||
110 | p.dashed_lines = True | ||
111 | p.enhanced_text = True | ||
112 | p.size = ('5.0cm', '5.0cm') | ||
113 | p.font_size = '6pt' | ||
114 | p.key = 'on bmargin center horizontal' | ||
115 | |||
116 | csvs = [f for f in os.listdir(data_dir) if re.match("*.csv", f)] | ||
117 | col_map = get_col_map(csvs) | ||
118 | |||
119 | |||
16 | def main(): | 120 | def main(): |
17 | opts, args = parse_args() | 121 | opts, args = parse_args() |
18 | args = args or [os.getcwd()] | 122 | args = args or [os.getcwd()] |
19 | 123 | ||
124 | # if opts.force and os.path.exists(opts.out_dir): | ||
125 | # sh.rmtree(opts.out_dir) | ||
126 | # if not os.path.exists(opts.out_dir): | ||
127 | # os.mkdir(opts.out_dir) | ||
128 | |||
129 | for exp in args: | ||
130 | name = os.path.split(exp)[1] | ||
131 | out_dir = "%s/%s" % (opts.out_dir, exp) | ||
132 | |||
133 | plot_exp(name, exp, out_dir) | ||
134 | |||
20 | if __name__ == '__main__': | 135 | if __name__ == '__main__': |
21 | main() | 136 | main() |