Example #1
0
 def crown(self, depth=2):
     """ Returns a list of leaves, nodes connected to leaves, etc.
     """
     nodes = []
     for node in self.leaves:
         nodes += node.flatten(depth - 1)
     return cluster.unique(nodes)
def enumerate_rules(node, relation, depth=1, reversed=False):
    """ Lists all nodes involving edges of the given relation.
    With reversed=False, returns relations FROM the node: tree -> tree is-a organism -> organism
    With reversed=True, returns relations TO the node: tree -> evergreen is-a tree -> evergreen
    """
    if reversed:
        f = lambda a, b: b.has_rule(relation, a)
    else:
        f = lambda a, b: a.has_rule(relation, b)
    nodes = [n for n in node.links if f(node, n)]
    if depth > 1:
        for n in nodes:
            nodes.extend(n.enumerate_rules(relation, depth - 1, reversed))
    return unique(nodes)
Example #3
0
def enumerate_rules(node, relation, depth=1, reversed=False):
    """ Lists all nodes involving edges of the given relation.
    With reversed=False, returns relations FROM the node: tree -> tree is-a organism -> organism
    With reversed=True, returns relations TO the node: tree -> evergreen is-a tree -> evergreen
    """
    if reversed:
        f = lambda a, b: b.has_rule(relation, a)
    else:
        f = lambda a, b: a.has_rule(relation, b)
    nodes = [n for n in node.links if f(node, n)]
    if depth > 1:
        for n in nodes:
            nodes.extend(n.enumerate_rules(relation, depth-1, reversed))
    return unique(nodes)
Example #4
0
def graph_enumerate_rules(graph, relation, distance=2, heuristic=None, reversed=False):
    """ Returns nodes in the graph that have given outgoing relation.
    Nodes directly connected to the root are at the beginning of the list,
    followed by nodes sorted by weight (eigenvalue).
    The distance of the shortest path between the root and the node is limited,
    and a heuristic for finding shortest paths can be passed.
    """
    nodes = []
    if graph.root:
        nodes = graph.root.enumerate_rules(relation, 1, not reversed)
    nodes  = sorted(nodes, lambda a, b: (a.betweenness < b.betweenness)*2-1) # XXX - use betweenness or eigenvalue?
    nodes += graph.nodes_by_eigenvalue(-1)
    nodes  = unique(nodes)
    nodes  = filter(lambda n: n.has_rule(relation, reversed=reversed), nodes)
    if graph.root and distance != None:
        if isinstance(heuristic, cost):
            heuristic.graph = graph
        nodes = graph.root.neighbors(nodes, distance, heuristic)
    return nodes
def graph_enumerate_rules(graph,
                          relation,
                          distance=2,
                          heuristic=None,
                          reversed=False):
    """ Returns nodes in the graph that have given outgoing relation.
    Nodes directly connected to the root are at the beginning of the list,
    followed by nodes sorted by weight (eigenvalue).
    The distance of the shortest path between the root and the node is limited,
    and a heuristic for finding shortest paths can be passed.
    """
    nodes = []
    if graph.root:
        nodes = graph.root.enumerate_rules(relation, 1, not reversed)
    nodes = sorted(nodes, lambda a, b: (a.betweenness < b.betweenness) * 2 - 1
                   )  # XXX - use betweenness or eigenvalue?
    nodes += graph.nodes_by_eigenvalue(-1)
    nodes = unique(nodes)
    nodes = filter(lambda n: n.has_rule(relation, reversed=reversed), nodes)
    if graph.root and distance != None:
        if isinstance(heuristic, cost):
            heuristic.graph = graph
        nodes = graph.root.neighbors(nodes, distance, heuristic)
    return nodes