From b2fa65ecfe14bb9377fbd8afa5f457a07472b6fb Mon Sep 17 00:00:00 2001 From: Jonathan Herman Date: Mon, 11 Feb 2013 18:28:31 -0500 Subject: First attempt at plot_exps.py. --- parse/col_map.py | 38 +++++++++++++++++++++++++++++--------- parse/dir_map.py | 47 +++++++++++++++++++++++++++++++---------------- parse/tuple_table.py | 18 ++++++++++++------ 3 files changed, 72 insertions(+), 31 deletions(-) (limited to 'parse') diff --git a/parse/col_map.py b/parse/col_map.py index 6f83ca1..8132639 100644 --- a/parse/col_map.py +++ b/parse/col_map.py @@ -6,9 +6,9 @@ class ColMapBuilder(object): def build(self): columns = sorted(self.value_map.keys(), - key=lambda c: (len(self.value_map[c]), c)) + key=lambda c: (-len(self.value_map[c]), c)) col_list = filter(lambda c : len(self.value_map[c]) > 1, columns) - return ColMap(col_list) + return ColMap(col_list, self.value_map) def try_add(self, column, value): self.value_map[column].add( value ) @@ -17,9 +17,17 @@ class ColMapBuilder(object): del(self.value_map[column]) class ColMap(object): - def __init__(self, col_list): + def __init__(self, col_list, values = None): self.col_list = col_list self.rev_map = {} + self.values = values + + self.minimums = [] + for c in col_list: + end = 1 + while c[:end] in self.minimums: + end += 1 + self.minimums += [c[:end]] for i, col in enumerate(col_list): self.rev_map[col] = i @@ -27,6 +35,9 @@ class ColMap(object): def columns(self): return self.col_list + def get_values(self): + return self.values + def get_key(self, kv): '''Convert a key-value dict into an ordered tuple of values.''' key = () @@ -46,8 +57,7 @@ class ColMap(object): kv[self.col_list[i]] = key[i] return kv - - def encode(self, kv): + def encode(self, kv, minimum=False): '''Converted a dict into a string with items sorted according to the ColMap key order.''' def escape(val): @@ -55,13 +65,23 @@ class ColMap(object): vals = [] - for key in self.col_list: + if minimum: + format = "%s:%s" + join = ", " + else: + format = "%s=%s" + join = "_" + + reverse = list(self.col_list) + reverse.reverse() + for key in reverse: if key not in kv: continue - k, v = escape(key), escape(kv[key]) - vals += ["%s=%s" % (k, v)] + display = key if not minimum else self.minimums[self.rev_map[key]] + k, v = escape(display), escape(kv[key]) + vals += [format % (k, v)] - return "_".join(vals) + return join.join(vals) @staticmethod def decode(string): diff --git a/parse/dir_map.py b/parse/dir_map.py index e4d83e6..11c872a 100644 --- a/parse/dir_map.py +++ b/parse/dir_map.py @@ -1,5 +1,6 @@ -import os import numpy as np +import os +import re from collections import defaultdict @@ -10,6 +11,29 @@ class DirMap(object): self.children = defaultdict(lambda : DirMap.Node(self)) self.values = [] + def heir(self, generation=1): + def heir2(node, generation): + if not generation: + return node + elif not node.children: + return None + else: + next_heir = node.children.values()[0] + return next_heir.heir(generation - 1) + return heir2(self, generation) + + def leafs(self, path=[], offset=0): + path = list(path) + check_node = self.heir(offset) + if check_node and check_node.children: + for child_name, child_node in self.children.iteritems(): + path += [child_name] + for leaf in child_node.leafs(path, offset): + yield leaf + path.pop() + else: + yield (path, self) + def __init__(self): self.root = DirMap.Node(None) self.values = [] @@ -22,8 +46,7 @@ class DirMap(object): def remove_childless(self): def remove_childless2(node): - for key in node.children.keys(): - child = node.children[key] + for key, child in node: remove_childless2(child) if not (child.children or child.values): node.children.pop(key) @@ -52,19 +75,8 @@ class DirMap(object): write2([out_dir], self.root) - - def leafs(self): - def leafs2(path, node): - if node.children: - for child_name, child_node in node.children.iteritems(): - path += [child_name] - for leaf in leafs2(path, child_node): - yield leaf - path.pop() - elif path: - yield (path, node.values) - - for leaf in leafs2([], self.root): + def leafs(self, offset=0): + for leaf in self.root.leafs([], offset): yield leaf @staticmethod @@ -77,6 +89,9 @@ class DirMap(object): if os.path.isdir(path): map(lambda x : read2(path+"/"+x), os.listdir(path)) else: + if not re.match(r'.*\.csv', path): + return + with open(path, 'rb') as f: data = np.loadtxt(f, delimiter=",") diff --git a/parse/tuple_table.py b/parse/tuple_table.py index 469a424..105b786 100644 --- a/parse/tuple_table.py +++ b/parse/tuple_table.py @@ -12,7 +12,7 @@ class TupleTable(object): self.col_map = col_map self.table = defaultdict(default) - def col_map(self): + def get_col_map(self): return self.col_map def __getitem__(self, kv): @@ -93,14 +93,21 @@ class ReducedTupleTable(TupleTable): Leaf = namedtuple('Leaf', ['stat', 'variable', 'base', 'summary', 'config', 'values']) def leafs(): - for path, values in dir_map.leafs(): - stat, variable, base_type, summary_type, leaf = path + for path, node in dir_map.leafs(): + # The path will be of at least size 1: the filename + leaf = path.pop() + + # Set acceptable defaults for the rest of the path + path += ['?', '?', 'Avg', 'Avg'][len(path):] + + [stat, variable, base_type, summary_type] = path config_str = leaf[:leaf.index('.csv')] config = ColMap.decode(config_str) - yield Leaf(stat, variable, base_type, - summary_type, config, values) + leaf = Leaf(stat, variable, base_type, + summary_type, config, node.values) + yield leaf builder = ColMapBuilder() @@ -119,7 +126,6 @@ class ReducedTupleTable(TupleTable): summary = table[leaf.config][leaf.stat] summary[leaf.summary][leaf.base] = y - print("read: %s" % table) return table def write_map(self, out_map): -- cgit v1.2.2