Source

django-hierarchy / src / tree.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# -*- coding: utf-8 -*-
# 
#  tree.py
#  django-hierarchy
#  
#  Created by Lars Yencken on 2009-04-09.
#  Copyright 2009 Lars Yencken. All rights reserved.
# 

"""An abstract tree datatype."""

from random import shuffle

class TreeNode(object):
    """
    A node in a hierarchical labelled tree. Each node contains links both
    downwards to children and upwards to a unique parent.
    """
    __slots__ = ('label', 'children', 'attrib', 'parent')

    def __init__(self, label=None, children=None, attrib=None, parent=None):
        self.label = label
        self.children = children or {}
        self.attrib = attrib or {}
        self.parent = parent

    def _get_ancestors(self):
        result = [self]
        node = self
        while node.parent is not None:
            result.append(node.parent)
            node = node.parent

        result.reverse()
        return result

    ancestors = property(_get_ancestors)

    def _get_path_to_root(self):
        """
        Returns the path from the root node to this node as a list of nodes.
        """
        path = [self]

        node = path[-1]
        while node.parent is not None:
            path.append(node.parent)
            node = path[-1]

        path.reverse()
        return path
    
    path = property(_get_path_to_root)

    def walk_preorder(self):
        """Walks the tree in a pre-order manner."""
        stack = [self]
        while stack:
            node = stack.pop()
            yield node
            stack.extend(node.children.values())

        return

    def walk(self):
        return self.walk_preorder()
    
    def walk_mptt(self):
        """
        Walks the tree in a modified pre-order traversal, visiting each node
        on the way down and again on the way up.
        """
        stack = [(self, False)]
        
        while stack:
            node, visited = stack.pop()
            yield node, visited
            
            if not visited:
                stack.append((node, True))
                stack.extend((c, False) for c in node.children.values())

    def walk_postorder(self):
        """Walks the tree in a post-order manner."""
        stack = [(self, False)]

        while stack:
            node, visited = stack.pop()

            if visited or not node:
                # Either second pass, or a leaf.
                yield node
            else:
                # First pass, has children.
                stack.append((node, True))
                stack.extend([(c, False) for c in node.children.values()])

        return

    def walk_leaves(self):
        for child in self.children.itervalues():
            for node in child.walk():
                if not node.children:
                    yield node
        return

    def walk_breadth_first(self):
        """
        Returns an iterator which walks the tree in a breadth-first manner.
        """
        nextFrontier = []
        frontier = [self]

        while frontier:
            node = frontier.pop()
            yield node

            for child_node in node.children.values():
                nextFrontier.append(child_node)

            if not frontier:
                # Exhausted this depth, so go deeper if possible.
                frontier = nextFrontier
                nextFrontier = []
                shuffle(frontier)

        return

    def unlink(self):
        """Unlink this node from its parent."""
        if self.parent:
            del self.parent.children[self.label]
        self.parent = None
        return

    def copy(self):
        """
        Returns a copy of the tree structure (but not the node values).
        """
        if self.parent:
            raise Exception, "Cannot copy from a non-root node"

        newRoot = TreeNode(self.label, self.children.copy(),
                self.attrib.copy())

        stack = [newRoot]
        while stack:
            node = stack.pop()
            for child_label, child_node in node.children.items():
                copied_child = TreeNode(
                        child_label,
                        child_node.children.copy(),
                        child_node.attrib.copy(),
                        node,
                    )
                node.children[child_label] = copied_child
                stack.append(copied_child)

        return newRoot

    def prune(self, predicate):
        """
        Prunes dead subtrees recursively.

        The method passed in is used on each node to determine if it has
        viable children or not. If all nodes are pruned, returns None.
        """
        newTree = self.copy()
        for node in newTree.walk_postorder():
            if node.children or predicate(node):
                continue
            else:
                node.unlink()

        if newTree.children or predicate(newTree):
            return newTree
        else:
            return None

    def __len__(self):
        return len(self.children)

    def __str__(self):
        return '<TreeNode: %s (%d children)>' % (self.label, len(self))

    def __repr__(self):
        return str(self)

    def __cmp__(self, rhs):
        return cmp(self.label, rhs.label)

    def add_child(self, child_node):
        """Adds the child node to this tree."""
        self.children[child_node.label] = child_node
        child_node.parent = self
        return

    def del_child(self, child_node):
        """
        Unlinks the child node from this node, and removes its parent
        pointer.
        """
        del self.children[child_node.label]
        child_node.attrib['parent'] = None
        return

    def get_path(self, path):
        """Get a particular path in the node."""
        if type(path) in (str, unicode):
            path = path.lstrip('/').split('/')
        else:
            assert type(path) == list

        next_node = self

        for label in path:
            next_node = next_node.children[label]

        return next_node

    def find_node(self, label):
        """
        Searches for the given node name in a breadth first manner.
        Returns a (path, node) tuple.
        """
        for node in self.walk_breadth_first():
            if node.label == label:
                return node
        else:
            raise KeyError, label

    def __getitem__(self, key):
        """Returns the attribute matching the given key."""
        return self.attrib.__getitem__(key)
    
    def __setitem__(self, key, value):
        """Saves the attribute value for the given key."""
        return self.attrib.__setitem__(key, value)

    def __contains__(self, key):
        return self.attrib.has_key(key)

    def __delitem__(self, key):
        """Deletes the given attribute."""
        del self.attrib[key]
        return

    def __eq__(self, rhs):
        return self.label == rhs.label and \
                self.attrib == rhs.attrib and \
                self.children == rhs.children

    def build_index(self):
        """
        Builds an index mapping each label to all the nodes which match it
        (potentially more than one).
        """
        index = {}
        for path, node in self.walk():
            if node.label not in index:
                index[node.label] = [node]
            else:
                index[node.label].append(node)

        return index

    def layout(self, method=None):
        """
        Pretty print the tree to stdout. Can optionally pass in a method
        which gets called to represents each node. The default output just
        uses the node's label.
        """
        if method is None:
            method = lambda n: n.label

        print method(self)
        self._layout_children(self, '', method)
        return

    def store(self, ModelClass, label_field='label'):
        """
        Store this tree to the database using the given Model class. This
        assumes that every node attribute is usable by the model. Calling this
        method will also remove the contents of the model.
        """
        ModelClass.objects.all().delete()
        for i, (node, visited) in enumerate(self.walk_mptt()):
            if visited:
                node['right_visit'] = i + 1 # 1-indexed
            else:
                node['left_visit'] = i + 1 # 1-indexed
        
        for node in self.walk():
            record = node.attrib.copy()
            record[label_field] = node.label
            obj = ModelClass(**record)
            obj.save()

    #------------------------------------------------------------------------#
    # PRIVATE
    #------------------------------------------------------------------------#

    def _layout_children(self, node, prefix, method):
        children = node.children.values()
        if not children:
            return

        for child in children[:-1]:
            print '%s├─ %s' % (prefix, method(child))
            self._layout_children(child, prefix + '│  ', method)
        
        child = children[-1]
        print '%s└─ %s' % (prefix, method(child))
        self._layout_children(child, prefix + '   ', method)

        return

#----------------------------------------------------------------------------#

class TreeDist(object):
    """
    A tree probability distribution, initialized by passing in a constructed
    tree which needs annotation.
    """
    #------------------------------------------------------------------------#
    # PUBLIC
    #------------------------------------------------------------------------#

    def __init__(self, root, countMethod=len):
        """
        Builds a tree distribution out of the existing tree structure.
        """
        self.root = root.copy()

        if countMethod is None:
            return

        # First pass, accumulate counts.
        for node in self.root.walk_postorder():
            node['count'] = countMethod(node)
            node['cumCount'] = node['count'] + \
                    sum([c['count'] for c in node.children.values()])

        # Second pass, convert to MLE probabilities, with the assumption that
        # counts propagate upwards.
        total = float(self.root['cumCount'])
        for node in self.root.walk():
            node['freq'] = node['cumCount'] / total

        return

    #------------------------------------------------------------------------#

    def copy(self):
        """Returns a shallow copy of the tree."""
        return TreeDist(self.root.copy(), countMethod=None)

    #------------------------------------------------------------------------#

    def layout(self):
        """Prints a graphical representation of the tree to stdout."""
        return self.root.layout(
                method=lambda n: '%s %.04f' % (n.label, n['freq'])
            )

    #------------------------------------------------------------------------#

    def combine(self, rhs, f=lambda x, y: (x + y)/2.0, label='union'):
        """Returns a tree which is topologically the union of the two trees."""
        assert self.tree.label == rhs.tree.label
        new_label = '%s(%s, %s)' % (label, self.label, rhs.label)
        newTreeDist = TreeDist(new_label)

        newTree = TreeNode(self.tree.label)

        stack = []
        for key in set(self.tree.keys()).union(rhs.tree.keys()):
            stack.append(
                    (self.tree.get(key), rhs.tree.get(key), newTree),
                )

        while stack:
            lhs_node, rhs_node, parent = stack.pop()
            if lhs_node is None:
                new_child = rhs_node.copy()
                for old_node, new_node in zip(rhs_node.walk(), new_child.walk()):
                    new_node.freq = f(0.0, old_node.freq)

            elif rhs_node is None:
                new_child = lhs_node.copy()
                for old_node, new_node in zip(lhs_node.walk(), new_child.walk()):
                    new_node.freq = f(old_node.freq, 0.0)

            else:
                new_child = TreeNode(lhs_node.label)
                new_child.freq = f(lhs_node.freq, rhs_node.freq)
                for key in set(lhs_node.keys()).union(rhs_node.keys()):
                    stack.append(
                            (lhs_node.get(key), rhs_node.get(key), new_child),
                        )

            parent.appendChild(new_child)

        newTreeDist.tree = newTree

        return newTreeDist

    def union(self, rhs):
        return self.union(rhs, f=lambda x, y: (x + y)/2.0, label='union')

    def diff(self, rhs):
        return self.union(rhs, f=lambda x, y: x - y, label='diff')

#----------------------------------------------------------------------------#