Esempio n. 1
0
def build_weighted_graph(mutual_infos, num_features, num_values):
    """
    @arg mutual_infos: The output from L{mutual_informations_from_joint_dists}.
    @arg num_features: The number of features in the data.
    @arg num_values: The number of values each feature can take.
    @return: B{g, features, weights} where
      - B{g} is a boost.graph.Graph
      - B{features} is a map from vertices to features
      - B{weights} is a map from edges to floats that can be used in a minimum spanning
      tree algorithm to find the Chow-Liu tree.
    """
    # build a graph and label each vertex with its feature
    import boost.graph as bgl
    g = bgl.Graph()
    vertices = [g.add_vertex() for f in xrange(num_features)]
    features = g.add_vertex_property(name='features', type='integer')
    for i, v in enumerate(vertices):
        features[v] = i

    # add the edges and label each edge with its mutual information
    weights = g.add_edge_property(type='float')
    mutual_info_map = g.add_edge_property(name='mutual infos', type='float')
    for (f1, f2), joint, mi in mutual_infos:
        #print f1, f2, mi
        e = g.add_edge(vertices[f1], vertices[f2])
        mutual_info_map[e] = mi
        weights[e] = max_mutual_information(num_values) - mi
        assert weights[e] >= 0.
    return g, features, weights
Esempio n. 2
0
def test_write_graphviz():
    import boost.graph as bgl
    g = bgl.Graph()
    v1 = g.add_vertex()
    v2 = g.add_vertex()
    e = g.add_edge( v1, v2 )
    v_prop = g.add_vertex_property( 'node_id', 'string' )
    v_prop[ v1 ] = 'hello'
    e_prop = g.add_edge_property( 'node_id', 'string' )
    e_prop[ e ] = 'test'
    g.clear_vertex( v2 )
    g.remove_vertex( v2 )
    g.write_graphviz( 'test.dot' )
Esempio n. 3
0
def test_dijkstra():
    import boost.graph as bgl
    graph = bgl.Graph()
    a = graph.add_vertex()
    b = graph.add_vertex()
    e = graph.add_edge(a, b)
    distances = graph.add_vertex_property('float')
    predecessors = graph.add_vertex_property('vertex')
    bgl.dijkstra_shortest_paths(
            graph,
            a,
            predecessor_map = predecessors,
            distance_map = distances
    )
Esempio n. 4
0
    def __init__(self):
        self.g = bgl.Graph()
        "A boost.graph.Graph that stores the network."

        self.interactor_props = self.g.add_vertex_property('interactors', type='object')
        "A boost.graph vertex property that maps from vertices to interactors."

        self.primary_2_node = dict()
        "A dict mapping from primary references to nodes (vertices)"

        self.ref_2_nodes = cookbook.DictOfLists()
        "A dict mapping from references to lists of nodes (vertices)"

        self.name_2_nodes = cookbook.DictOfLists() # maps names to nodes
        "A dict mapping from names to lists of nodes (vertices)"
Esempio n. 5
0
 def __init__(self):
     import boost.graph as bgl
     import os
     import biopsy
     self.protein_vertex_map = {}
     self.g = bgl.Graph()
     self.g.add_vertex_property(name='id', type='string')
     self.g.add_vertex_property(name='label', type='string')
     self.g.add_vertex_property(name='fillcolor', type='color')
     self.g.add_vertex_property(name='style', type='string')
     self.g.add_vertex_property(name='shape', type='string')
     self.g.add_vertex_property(name='URL', type='string')
     self.entrez_map = biopsy.aliases._get_dict_from_file(
         os.path.join(biopsy.get_aliases_dir(),
                      'transfac_pssm_2_entrez_proteins.txt'))
Esempio n. 6
0
def build(edges):
    import boost.graph as bgl

    # Create a graph to build
    g = bgl.Graph()

    # Construct some property maps
    node_ids = g.add_vertex_property('node_id', 'string')
    weights = g.add_edge_property('weight', 'float')

    # create the edges and vertices
    name_2_vertex = {}
    for (name1, name2) in edges:

        # have we seen name 1 before?
        if not name_2_vertex.has_key(name1):
            v1 = g.add_vertex()
            name_2_vertex[name1] = v1
            node_ids[v1] = name1
        else:
            v1 = name_2_vertex[name1]

        # have we seen name 2 before?
        if not name_2_vertex.has_key(name2):
            v2 = g.add_vertex()
            name_2_vertex[name2] = v2
            node_ids[v2] = name2
        else:
            v2 = name_2_vertex[name2]

        # is the edge in the graph already?
        e = g.edge(v1, v2)
        if None == e:
            e = g.add_edge(v1, v2)
            weights[e] = 1
        else:
            weights[e] += 1

    return g
Esempio n. 7
0
def graph_generate(ids):
    """Creates a graph from a sequence of sequences

    Expects a sequence of sequences of ids.

    Returns a graph with ids as nodes and connections between successive ids
    in each sequence

    The graph will have an id_to_vertex dictionary attribute
    """
    import boost.graph as bgl
    g = bgl.Graph()
    g.id_to_vertex = {}
    g.add_vertex_property('vertex_id', 'string')
    for seq in ids:  # join the sequence up in a line
        last_v = None
        for i in seq:
            v = graph_vertex(g, i, add_if_necessary=True)
            if None != last_v and last_v not in g.adjacent_vertices(v):
                g.add_edge(v, last_v)
            last_v = v
    return g
Esempio n. 8
0
def test_std_graph():
    import boost.graph as bgl
    g = bgl.Graph()
    #g.add_vertex()
    pickle_test(g)
Esempio n. 9
0
def test_pickle():
    import boost.graph as bgl
    g = bgl.Graph()
    p = g.add_vertex_property( 'prop', 'float' )
    pickle.dump( g, open( 'graph.pickle', 'w' ) )
    pickle.load( open( 'graph.pickle', 'r' ) )
Esempio n. 10
0
    def build_tree(self, mi_threshold=0.0):
        """
        Builds a tree from the mutual information between the base distributions
        """
        import boost.graph as bgl
        # first build an (undirected) graph of the (negative) mutual informations
        g = bgl.Graph()
        vertices = [g.add_vertex() for i in xrange(self.K)]
        weight_map = g.add_edge_property('weight', 'float')
        joint_probabilities = g.add_edge_property('joints')
        marginal_probabilities = g.add_vertex_property('marginals')

        # add the edges and marginal probs
        for i in xrange(self.K):
            marginal_probabilities[vertices[i]] = self.marginal[i]

        # add the edges and conditional probs
        for i in xrange(self.K):
            for j in xrange(i + 1, self.K):
                if self.mutual_information[i, j] > mi_threshold:
                    e = g.add_edge(vertices[i], vertices[j])
                    weight_map[e] = -self.mutual_information[i, j]
                    joint_probabilities[e] = self.joint[i, j]

        # find the minimum spanning tree and remove those edges not in it
        spanning_tree = bgl.kruskal_minimum_spanning_tree(g, weight_map)
        for e in g.edges:
            if e not in spanning_tree:
                g.remove_edge(e)

        def index_of(v):
            for i in xrange(self.K):
                if vertices[i] == v:
                    return i
            raise RuntimeError('Could not find vertex: ' + v)

        # build a directed forest from the (undirected) graph
        def build_forest(g, root_idx=None):
            "Take a forest and build an isomorphic Digraph"
            import boost.graph as bgl

            vertices_visited = set()
            diforest = bgl.Digraph()
            edge_label = diforest.add_edge_property('label', 'string')
            di_conditionals = diforest.add_edge_property('conditionals')
            di_marginals = diforest.add_vertex_property('marginals')
            di_vertices = [diforest.add_vertex() for i in xrange(self.K)]

            def visit(i):
                if i in vertices_visited: return
                vertices_visited.add(i)
                v = vertices[i]
                di_v = di_vertices[i]
                di_marginals[di_v] = marginal_probabilities[v]
                for neighbour in g.adjacent_vertices(v):
                    j = index_of(neighbour)
                    if j not in vertices_visited:
                        e = diforest.add_edge(di_v, di_vertices[j])
                        edge_label[e] = '%.3f' % (
                            -weight_map[g.edge(v, vertices[j])])
                        joint = joint_probabilities[g.edge(v, neighbour)]
                        if index_of(neighbour) < index_of(v):
                            joint = joint.T  # make sure we have correct orientation of joint
                        di_conditionals[e] = joint_to_conditional(joint)
                        visit(j)  # recurse

            # visit all the vertices - starting with root if given
            if None != root_idx: visit(root_idx)
            for i in xrange(self.K):
                visit(i)

            # check each node has max one parent
            for v in diforest.vertices:
                assert diforest.in_degree(v) < 2

            return diforest

        # set the root to have the highest MI
        root_idx = self.mutual_information.argmax(
        ) % self.mutual_information.shape[0]
        self.d = build_forest(g, root_idx=root_idx)
Esempio n. 11
0
def path_graph(binders_1, binders_2, max_length):
    """Returns a boost.graph representing the protein links between the lists of hits"""
    import boost.graph as bgl
    g = bgl.Graph()
    id_map = {}
    g.add_vertex_property(name='id', type='string')
    g.add_vertex_property(name='label', type='string')
    g.add_vertex_property(name='fillcolor', type='color')
    g.add_vertex_property(name='style', type='string')
    g.add_vertex_property(name='shape', type='string')
    g.add_vertex_property(name='URL', type='string')
    already_examined = set()
    for b1 in binders_1:
        for b2 in binders_2:

            # the pair needs to have smaller binder first
            reversed = b1 >= b2
            if not reversed: pair = (b1, b2)
            else: pair = (b2, b1)

            # only look at pairs we have not seen already
            if pair in already_examined: continue
            else: already_examined.add(pair)

            # look for path between binders
            if not paths.has_key(pair): continue
            path = paths[pair]

            # check max length
            if len(path) > max_length: continue

            # iterate over path adding vertices and edges
            last_protein = None
            for protein in path:
                if not (protein in id_map):
                    v = g.add_vertex()
                    id_map[protein] = v
                    g.vertex_properties['id'][v] = protein

                    url = biopsy.entrez.get_protein_url(protein)
                    if url: g.vertex_properties['URL'][v] = url

                    label = biopsy.entrez.protein_name(protein)
                    g.vertex_properties['label'][v] = label or 'Unknown'
                else:
                    v = id_map[protein]
                if None != last_protein:
                    last_vertex = id_map[last_protein]
                    if None == g.edge(v, last_vertex):
                        g.add_edge(v, last_vertex)
                last_protein = protein

            # set colours for first and last in path
            first_v = id_map[path[0]]
            last_v = id_map[path[-1]]
            if reversed: first_v, last_v = last_v, first_v
            g.vertex_properties['fillcolor'][first_v] = bgl.Color.gray
            g.vertex_properties['style'][first_v] = 'filled'
            g.vertex_properties['shape'][last_v] = 'diamond'
            print biopsy.transfac.TableLink(
                b2).name, g.vertex_properties['label'][first_v]
            print biopsy.transfac.TableLink(
                b1).name, g.vertex_properties['label'][last_v]
            print

    return g
Esempio n. 12
0
    def to_bgl(ygraph, node_properties=[], edge_properties=[]):
        """
Translate a graphviz graph into a boost.python graph.

The returned translation_map behaves like a dictionary of all corresponding (boost_edge, yapgvb_edge), (yapgvb_edge, boost_edge), (boost_vertex, yapgvb_node), and (yapgvb_node, boost_vertex) key/value pairs.

@param bgraph: A yapgrvb.Graph or yapgvb.Digraph instance
@param node_properties: A list of strings naming node properties to converted from yapgvb to boost.
@param edge_properties: A list of strings naming edge properties to converted from yapgvb to boost.
@return: (bgraph, translation_map)
        """
        import boost.graph as bgl
        if ygraph.directed:
            graph = bgl.Digraph()
        else:
            graph = bgl.Graph()

        map = BoostTranslationMap(ygraph, graph)

        for node in ygraph.nodes:
            v = graph.add_vertex()
            map[node] = v
            map[v] = node

        for edge in ygraph.edges:
            e = graph.add_edge(map[edge.tail], map[edge.head])
            map[edge] = e
            map[e] = edge

        for property_name in node_properties:
            if isinstance(property_name, GraphAttributeProperty):
                prop = property_name
                property_name = prop.name
            else:
                try:
                    prop = getattr(Node, property_name)
                except:
                    raise AttributeError("Node class has no property '%s'" %
                                         property_name)
            prop_map = graph.vertex_property_map(prop.bgl_type)
            graph.vertex_properties[property_name] = prop_map

            for y in ygraph.nodes:
                b = map[y]
                prop_map[b] = prop.fget(y)

        for property_name in edge_properties:
            if isinstance(property_name, GraphAttributeProperty):
                prop = property_name
                property_name = prop.name
            else:
                try:
                    prop = getattr(Edge, property_name)
                except:
                    raise AttributeError("Edge class has no property '%s'" %
                                         property_name)
            prop_map = graph.edge_property_map(prop.bgl_type)
            graph.edge_properties[property_name] = prop_map

            for y in ygraph.edges:
                b = map[y]
                prop_map[b] = prop.fget(y)

        return graph, map
Esempio n. 13
0
import boost.graph as bgl

g = bgl.Graph((('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')), 'name')

print 'When not hidden, contains', len(
    bgl.biconnected_components(
        g, g.add_edge_property(type='integer'))), 'articulation points'

hide_edges = g.add_edge_property(type='integer')
hide_edges[g.edges.next()] = 1

print 'When hidden, contains', len(
    bgl.biconnected_components(g, g.add_edge_property(type='integer'),
                               hide_edges)), 'articulation points'

g.remove_edge(g.edges.next())
print 'When removed, contains', len(
    bgl.biconnected_components(
        g, g.add_edge_property(type='integer'))), 'articulation points'
Esempio n. 14
0
# Copyright (C) 2006 The Trustees of Indiana University.

# Use, modification and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http:#www.boost.org/LICENSE_1_0.txt)

#  Authors: Douglas Gregor
#           Andrew Lumsdaine

# Test property conversions

import boost.graph as bgl
g = bgl.Graph([(1, 2), (1, 3), (4, 6), (5, 6)])

color = g.add_vertex_property('color')

bgl.connected_components(g, color)

for v in g.vertices:
    print color[v]