aboutsummaryrefslogtreecommitdiffstats
path: root/parse/dir_map.py
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-02-11 18:28:31 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-02-11 18:28:31 -0500
commitb2fa65ecfe14bb9377fbd8afa5f457a07472b6fb (patch)
tree5bb1402027e1c56eccf38682166b1850c8b89aa9 /parse/dir_map.py
parent7c647198fc40e72ef6ca23c2484bf49eba2079ee (diff)
First attempt at plot_exps.py.
Diffstat (limited to 'parse/dir_map.py')
-rw-r--r--parse/dir_map.py47
1 files changed, 31 insertions, 16 deletions
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 @@
1import os
2import numpy as np 1import numpy as np
2import os
3import re
3 4
4from collections import defaultdict 5from collections import defaultdict
5 6
@@ -10,6 +11,29 @@ class DirMap(object):
10 self.children = defaultdict(lambda : DirMap.Node(self)) 11 self.children = defaultdict(lambda : DirMap.Node(self))
11 self.values = [] 12 self.values = []
12 13
14 def heir(self, generation=1):
15 def heir2(node, generation):
16 if not generation:
17 return node
18 elif not node.children:
19 return None
20 else:
21 next_heir = node.children.values()[0]
22 return next_heir.heir(generation - 1)
23 return heir2(self, generation)
24
25 def leafs(self, path=[], offset=0):
26 path = list(path)
27 check_node = self.heir(offset)
28 if check_node and check_node.children:
29 for child_name, child_node in self.children.iteritems():
30 path += [child_name]
31 for leaf in child_node.leafs(path, offset):
32 yield leaf
33 path.pop()
34 else:
35 yield (path, self)
36
13 def __init__(self): 37 def __init__(self):
14 self.root = DirMap.Node(None) 38 self.root = DirMap.Node(None)
15 self.values = [] 39 self.values = []
@@ -22,8 +46,7 @@ class DirMap(object):
22 46
23 def remove_childless(self): 47 def remove_childless(self):
24 def remove_childless2(node): 48 def remove_childless2(node):
25 for key in node.children.keys(): 49 for key, child in node:
26 child = node.children[key]
27 remove_childless2(child) 50 remove_childless2(child)
28 if not (child.children or child.values): 51 if not (child.children or child.values):
29 node.children.pop(key) 52 node.children.pop(key)
@@ -52,19 +75,8 @@ class DirMap(object):
52 75
53 write2([out_dir], self.root) 76 write2([out_dir], self.root)
54 77
55 78 def leafs(self, offset=0):
56 def leafs(self): 79 for leaf in self.root.leafs([], offset):
57 def leafs2(path, node):
58 if node.children:
59 for child_name, child_node in node.children.iteritems():
60 path += [child_name]
61 for leaf in leafs2(path, child_node):
62 yield leaf
63 path.pop()
64 elif path:
65 yield (path, node.values)
66
67 for leaf in leafs2([], self.root):
68 yield leaf 80 yield leaf
69 81
70 @staticmethod 82 @staticmethod
@@ -77,6 +89,9 @@ class DirMap(object):
77 if os.path.isdir(path): 89 if os.path.isdir(path):
78 map(lambda x : read2(path+"/"+x), os.listdir(path)) 90 map(lambda x : read2(path+"/"+x), os.listdir(path))
79 else: 91 else:
92 if not re.match(r'.*\.csv', path):
93 return
94
80 with open(path, 'rb') as f: 95 with open(path, 'rb') as f:
81 data = np.loadtxt(f, delimiter=",") 96 data = np.loadtxt(f, delimiter=",")
82 97