diff options
Diffstat (limited to 'plot_exps.py')
| -rwxr-xr-x | plot_exps.py | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/plot_exps.py b/plot_exps.py index b17cd36..8fbef99 100755 --- a/plot_exps.py +++ b/plot_exps.py | |||
| @@ -10,6 +10,7 @@ from optparse import OptionParser | |||
| 10 | from parse.col_map import ColMap,ColMapBuilder | 10 | from parse.col_map import ColMap,ColMapBuilder |
| 11 | from parse.dir_map import DirMap | 11 | from parse.dir_map import DirMap |
| 12 | from plot.style import StyleMap | 12 | from plot.style import StyleMap |
| 13 | from multiprocessing import Pool, cpu_count | ||
| 13 | 14 | ||
| 14 | def parse_args(): | 15 | def parse_args(): |
| 15 | parser = OptionParser("usage: %prog [options] [csv_dir]...") | 16 | parser = OptionParser("usage: %prog [options] [csv_dir]...") |
| @@ -21,10 +22,11 @@ def parse_args(): | |||
| 21 | 22 | ||
| 22 | return parser.parse_args() | 23 | return parser.parse_args() |
| 23 | 24 | ||
| 24 | ExpDetails = namedtuple('ExpDetails', ['variable', 'value', 'title', 'out']) | 25 | ExpDetails = namedtuple('ExpDetails', ['variable', 'value', 'title', |
| 26 | 'out', 'node']) | ||
| 25 | OUT_FORMAT = 'pdf' | 27 | OUT_FORMAT = 'pdf' |
| 26 | 28 | ||
| 27 | def get_details(path, out_dir): | 29 | def get_details(node, path, out_dir): |
| 28 | '''Decode a @path into details about a single experiment.''' | 30 | '''Decode a @path into details about a single experiment.''' |
| 29 | out = "_".join(path) if path else "plot" | 31 | out = "_".join(path) if path else "plot" |
| 30 | out = "%s/%s.%s" % (out_dir, out, OUT_FORMAT) | 32 | out = "%s/%s.%s" % (out_dir, out, OUT_FORMAT) |
| @@ -36,9 +38,9 @@ def get_details(path, out_dir): | |||
| 36 | title += " by %s" % variable if variable else "" | 38 | title += " by %s" % variable if variable else "" |
| 37 | title += " (%s)" % (", ".join(path)) if path else "" | 39 | title += " (%s)" % (", ".join(path)) if path else "" |
| 38 | 40 | ||
| 39 | return ExpDetails(variable, value, title, out) | 41 | return ExpDetails(variable, value, title, out, node) |
| 40 | 42 | ||
| 41 | def plot_by_variable(plot_node, details): | 43 | def plot_by_variable(details): |
| 42 | '''Plot each .csv files under @plot_node as a line on a shared plot.''' | 44 | '''Plot each .csv files under @plot_node as a line on a shared plot.''' |
| 43 | 45 | ||
| 44 | builder = ColMapBuilder() | 46 | builder = ColMapBuilder() |
| @@ -46,7 +48,7 @@ def plot_by_variable(plot_node, details): | |||
| 46 | 48 | ||
| 47 | # Generate mapping of (column)=>(line property to vary) for consistently | 49 | # Generate mapping of (column)=>(line property to vary) for consistently |
| 48 | # formatted plots | 50 | # formatted plots |
| 49 | for line_path, line_node in plot_node.children.iteritems(): | 51 | for line_path, line_node in details.node.children.iteritems(): |
| 50 | encoded = line_path[:line_path.index(".csv")] | 52 | encoded = line_path[:line_path.index(".csv")] |
| 51 | line_config = ColMap.decode(encoded) | 53 | line_config = ColMap.decode(encoded) |
| 52 | 54 | ||
| @@ -85,8 +87,6 @@ def plot_dir(data_dir, out_dir, force): | |||
| 85 | sys.stderr.write("Reading data...\n") | 87 | sys.stderr.write("Reading data...\n") |
| 86 | dir_map = DirMap.read(data_dir) | 88 | dir_map = DirMap.read(data_dir) |
| 87 | 89 | ||
| 88 | sys.stderr.write("Creating column map...\n") | ||
| 89 | |||
| 90 | if not os.path.exists(out_dir): | 90 | if not os.path.exists(out_dir): |
| 91 | os.mkdir(out_dir) | 91 | os.mkdir(out_dir) |
| 92 | 92 | ||
| @@ -94,18 +94,20 @@ def plot_dir(data_dir, out_dir, force): | |||
| 94 | 94 | ||
| 95 | # Count total plots for % counter | 95 | # Count total plots for % counter |
| 96 | num_plots = len([x for x in dir_map.leafs(1)]) | 96 | num_plots = len([x for x in dir_map.leafs(1)]) |
| 97 | plot_num = 0 | ||
| 98 | 97 | ||
| 98 | plot_details = [] | ||
| 99 | for plot_path, plot_node in dir_map.leafs(1): | 99 | for plot_path, plot_node in dir_map.leafs(1): |
| 100 | details = get_details(plot_path, out_dir) | 100 | details = get_details(plot_node, plot_path, out_dir) |
| 101 | 101 | ||
| 102 | if force or not os.path.exists(details.out): | 102 | if force or not os.path.exists(details.out): |
| 103 | plot_by_variable(plot_node, details) | 103 | plot_details += [details] |
| 104 | 104 | ||
| 105 | plot_num += 1 | 105 | procs = min(len(plot_details), cpu_count()/2) |
| 106 | 106 | pool = Pool(processes=procs) | |
| 107 | sys.stderr.write('\r {0:.2%}'.format(float(plot_num)/num_plots)) | 107 | enum = pool.imap_unordered(plot_by_variable, plot_details) |
| 108 | sys.stderr.write('\n') | 108 | for i, _ in enumerate(enum): |
| 109 | sys.stderr.write('\r {0:.2%}'.format(float(i)/num_plots)) | ||
| 110 | sys.stderr.write('\n') | ||
| 109 | 111 | ||
| 110 | def main(): | 112 | def main(): |
| 111 | opts, args = parse_args() | 113 | opts, args = parse_args() |
