aboutsummaryrefslogtreecommitdiffstats
path: root/parse/dir_map.py
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-02-07 19:07:33 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-02-07 19:07:33 -0500
commit2b416f696a50f4ae264d5aec8c78fa7686cc7927 (patch)
tree531271ef1ef37d60e1d53e4169cf7ef7623993c6 /parse/dir_map.py
parent063f5bc4b78a8368054c7e11c5fe177aafc4e28d (diff)
Added support to load DirMap's from directories.
Diffstat (limited to 'parse/dir_map.py')
-rw-r--r--parse/dir_map.py36
1 files changed, 27 insertions, 9 deletions
diff --git a/parse/dir_map.py b/parse/dir_map.py
index 781607c..b864318 100644
--- a/parse/dir_map.py
+++ b/parse/dir_map.py
@@ -1,4 +1,5 @@
1import os 1import os
2import numpy as np
2 3
3from collections import defaultdict 4from collections import defaultdict
4 5
@@ -9,21 +10,17 @@ class TreeNode(object):
9 self.values = [] 10 self.values = []
10 11
11class DirMap(object): 12class DirMap(object):
12 def to_csv(self, vals): 13 def __init__(self, in_dir = None):
13 val_strs = []
14 for key in sorted(vals.keys()):
15 val_strs += ["%s=%s" % (key, vals[key])]
16 return "%s.csv" % ("_".join(val_strs))
17
18 def __init__(self):
19 self.root = TreeNode(None) 14 self.root = TreeNode(None)
20 self.values = [] 15 self.values = []
16 if in_dir:
17 self.__read(in_dir)
21 18
22 def add_value(self, path, value): 19 def add_values(self, path, values):
23 node = self.root 20 node = self.root
24 for p in path: 21 for p in path:
25 node = node.children[p] 22 node = node.children[p]
26 node.values += [value] 23 node.values += values
27 24
28 def reduce(self): 25 def reduce(self):
29 def reduce2(node): 26 def reduce2(node):
@@ -56,6 +53,27 @@ class DirMap(object):
56 53
57 write2([out_dir], self.root) 54 write2([out_dir], self.root)
58 55
56 def __read(self, in_dir):
57 if not os.path.exists(in_dir):
58 raise ValueError("Can't load from nonexistent path : %s" % in_dir)
59
60 def read2(path):
61 if os.path.isdir(path):
62 map(lambda x : read2(path+"/"+x), os.listdir(path))
63 else:
64 with open(path, 'rb') as f:
65 data = np.loadtxt(f, delimiter=",")
66
67 # Convert to tuples of ints if possible, else floats
68 values = [map(lambda a:a if a%1 else int(a), t) for t in data]
69 values = map(tuple, values)
70
71 stripped = path if path.find(in_dir) else path[len(in_dir):]
72 path_arr = stripped.split("/")
73
74 self.add_values(path_arr, values)
75
76 read2(in_dir)
59 77
60 def __str__(self): 78 def __str__(self):
61 def str2(node, level): 79 def str2(node, level):