max_vertices = 70 gt = args['GraphTerm'] if 'GraphTerm' in args else '' #ct = args['ClusterTerm'] if 'ClusterTerm' in args else '' mode = args['GraphMode'] if 'GraphMode' in args else '' render.render_div_search(ft=gt, gt=gt) #display form #render.render_datalist( 'terms'); if 'GraphTerm' in args and 'GraphMode' in args: graph_path = '/tmp/{}.svg'.format(''.join( choice(string.ascii_uppercase + string.digits) for _ in xrange(10))) render.render_div_search(gt=args['GraphTerm'], ft=args['GraphTerm']) render.render_datalist() if args['GraphMode'] == 'simple': pass elif args['GraphMode'] == 'complete': ids = UniqueIdGenerator() li = query.get_cooccurrences(args['GraphTerm'])[:max_vertices] edge_list = [(ids[args['GraphTerm']], ids[term]) for (term, _) in li] weight_list = [float(w) for (_, w) in li] for i in xrange(len(li)): for j in xrange(i + 1, len(li)): t1 = li[i][0] t2 = li[j][0] co = query.get_one_cooccurrence(t1, t2) if co: edge_list.append((ids[t1], ids[t2])) weight_list.append(float(co)) g = igraph.Graph(edge_list, vertex_attrs=dict(name=ids.values()), edge_attrs=dict(weight=weight_list)) L = g.es["weight"]
def construct_graph_from_formula(cls, formula=None, attr="name", simplify=True): """Graph.Formula(formula = None, attr = "name", simplify = True) Generates a graph from a graph formula A graph formula is a simple string representation of a graph. It is very handy for creating small graphs quickly. The string consists of vertex names separated by edge operators. An edge operator is a sequence of dashes (C{-}) that may or may not start with an arrowhead (C{<} at the beginning of the sequence or C{>} at the end of the sequence). The edge operators can be arbitrarily long, i.e., you may use as many dashes to draw them as you like. This makes a total of four different edge operators: - C{-----} makes an undirected edge - C{<----} makes a directed edge pointing from the vertex on the right hand side of the operator to the vertex on the left hand side - C{---->} is the opposite of C{<----} - C{<--->} creates a mutual directed edge pair between the two vertices If you only use the undirected edge operator (C{-----}), the graph will be undirected. Otherwise it will be directed. Vertex names used in the formula will be assigned to the C{name} vertex attribute of the graph. Some simple examples: >>> from igraph import Graph >>> print Graph.Formula() # empty graph IGRAPH UN-- 0 0 -- + attr: name (v) >>> g = Graph.Formula("A-B") # undirected graph >>> g.vs["name"] ['A', 'B'] >>> print g IGRAPH UN-- 2 1 -- + attr: name (v) + edges (vertex names): A--B >>> g.get_edgelist() [(0, 1)] >>> g2 = Graph.Formula("A-----------B") >>> g2.isomorphic(g) True >>> g = Graph.Formula("A ---> B") # directed graph >>> g.vs["name"] ['A', 'B'] >>> print g IGRAPH DN-- 2 1 -- + attr: name (v) + edges (vertex names): A->B If you have may disconnected componnets, you can separate them with commas. You can also specify isolated vertices: >>> g = Graph.Formula("A--B, C--D, E--F, G--H, I, J, K") >>> print ", ".join(g.vs["name"]) A, B, C, D, E, F, G, H, I, J, K >>> g.clusters().membership [0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6] The colon (C{:}) operator can be used to specify vertex sets. If an edge operator connects two vertex sets, then every vertex from the first vertex set will be connected to every vertex in the second set: >>> g = Graph.Formula("A:B:C:D --- E:F:G") >>> g.isomorphic(Graph.Full_Bipartite(4, 3)) True Note that you have to quote vertex names if they include spaces or special characters: >>> g = Graph.Formula('"this is" +- "a silly" -+ "graph here"') >>> g.vs["name"] ['this is', 'a silly', 'graph here'] @param formula: the formula itself @param attr: name of the vertex attribute where the vertex names will be stored @param simplify: whether the simplify the constructed graph @return: the constructed graph: """ # If we have no formula, return an empty graph if formula is None: return cls(0, vertex_attrs={attr: []}) vertex_ids, edges, directed = UniqueIdGenerator(), [], False # Loop over each part in the formula for part in re.compile(r"[,\n]").split(formula): # Strip leading and trailing whitespace in the part part = part.strip() # Parse the first vertex specification from the formula for start_names, end_names, arrowheads in generate_edges(part): start_ids = [vertex_ids[name] for name in start_names] end_ids = [vertex_ids[name] for name in end_names] if not arrowheads[0] and not arrowheads[1]: # This is an undirected edge. Do we have a directed graph? if not directed: # Nope, add the edge edges.extend( (id1, id2) for id1 in start_ids for id2 in end_ids) else: # This is a directed edge directed = True if arrowheads[1]: edges.extend( (id1, id2) for id1 in start_ids for id2 in end_ids) if arrowheads[0]: edges.extend( (id2, id1) for id1 in start_ids for id2 in end_ids) # Grab the vertex names into a list vertex_attrs = {} vertex_attrs[attr] = list(vertex_ids.values()) # Construct and return the graph result = cls(len(vertex_ids), edges, directed, vertex_attrs=vertex_attrs) if simplify: result.simplify() return result
def Formula(klass, formula = None, attr = "name"): """Graph.Formula(formula = None, attr = "name") Generates a graph from a graph formula A graph formula is a simple string representation of a graph. It is very handy for creating small graphs quickly. The string consists of vertex names separated by edge operators. An edge operator is a sequence of dashes (C{-}) that may or may not start with an arrowhead (C{<} at the beginning of the sequence or C{>} at the end of the sequence). The edge operators can be arbitrarily long, i.e., you may use as many dashes to draw them as you like. This makes a total of four different edge operators: - C{-----} makes an undirected edge - C{<----} makes a directed edge pointing from the vertex on the right hand side of the operator to the vertex on the left hand side - C{---->} is the opposite of C{<----} - C{<--->} creates a mutual directed edge pair between the two vertices If you only use the undirected edge operator (C{-----}), the graph will be undirected. Otherwise it will be directed. Vertex names used in the formula will be assigned to the C{name} vertex attribute of the graph. Some simple examples: >>> print Graph.Formula() # empty graph Undirected graph (|V| = 0, |E| = 0) >>> g = Graph.Formula("A-B") # undirected graph >>> g.vs["name"] ["A", "B"] >>> print g Undirected graph (|V| = 2, |E| = 1) >>> g.get_edgelist() >>> g2 = Graph.Formula("A-----------B") >>> g2.isomorphic(g) True >>> g = Graph.Formula("A ---> B") # directed graph >>> g.vs["name"] ["A", "B"] >>> print g Directed graph (|V| = 2, |E| = 1) If you have may disconnected componnets, you can separate them with commas. You can also specify isolated vertices: >>> g = Graph.Formula("A--B, C--D, E--F, G--H, I, J, K") >>> print ", ".join(g.vs["name"]) A, B, C, D, E, F, G, H, I, J, K >>> g.clusters().membership [0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6] The colon (C{:}) operator can be used to specify vertex sets. If an edge operator connects two vertex sets, then every vertex from the first vertex set will be connected to every vertex in the second set: >>> g = Graph.Formula("A:B:C:D --- E:F:G") >>> g.isomorphic(Graph.Full_Bipartite(4, 3)) True Note that you have to quote vertex names if they include spaces or special characters: >>> g = Graph.Formula('"this is" +- "a silly" -+ "graph here"') >>> g.vs["name"] ['this is', 'a silly', 'graph here'] @param formula: the formula itself @param attr: name of the vertex attribute where the vertex names will be stored @return: the constructed graph: """ # If we have no formula, return an empty graph if formula is None: return klass(0, vertex_attrs = {attr: []}) vertex_ids, edges, directed = UniqueIdGenerator(), [], False # Loop over each part in the formula for part in formula.split(","): # Drop newlines from the part part = part.strip().replace("\n", "").replace("\t", "") # Parse the first vertex specification from the formula for start_names, end_names, arrowheads in generate_edges(part): start_ids = [vertex_ids[name] for name in start_names] end_ids = [vertex_ids[name] for name in end_names] if not arrowheads[0] and not arrowheads[1]: # This is an undirected edge. Do we have a directed graph? if not directed: # Nope, add the edge edges.extend([(id1, id2) for id1 in start_ids for id2 in end_ids]) else: # This is a directed edge directed = True if arrowheads[1]: edges.extend([(id1, id2) for id1 in start_ids for id2 in end_ids]) if arrowheads[0]: edges.extend([(id2, id1) for id1 in start_ids for id2 in end_ids]) # Grab the vertex names into a list names = sorted(((v, k) for k, v in vertex_ids._ids.iteritems())) names = [k for _, k in names] # Construct and return the graph return klass(len(names), edges, directed, vertex_attrs={attr: names})
max_vertices = 70 gt = args['GraphTerm'] if 'GraphTerm' in args else '' #ct = args['ClusterTerm'] if 'ClusterTerm' in args else '' mode = args['GraphMode'] if 'GraphMode' in args else '' render.render_div_search(ft=gt, gt=gt) #display form #render.render_datalist( 'terms'); if 'GraphTerm' in args and 'GraphMode' in args: graph_path = '/tmp/{}.svg'.format(''.join(choice(string.ascii_uppercase + string.digits) for _ in xrange(10))) render.render_div_search(gt=args['GraphTerm'], ft=args['GraphTerm']) render.render_datalist() if args['GraphMode'] == 'simple': pass elif args['GraphMode'] == 'complete': ids = UniqueIdGenerator() li = query.get_cooccurrences(args['GraphTerm'])[:max_vertices] edge_list = [(ids[args['GraphTerm']], ids[term]) for (term,_) in li] weight_list = [float(w) for (_,w) in li] for i in xrange(len(li)): for j in xrange(i+1, len(li)): t1 = li[i][0] t2 = li[j][0] co = query.get_one_cooccurrence(t1, t2) if co: edge_list.append((ids[t1], ids[t2])) weight_list.append(float(co)) g = igraph.Graph(edge_list, vertex_attrs=dict(name=ids.values()), edge_attrs=dict(weight=weight_list)) L = g.es["weight"] w = L#weights mi = min(w)
def construct_graph_from_formula(cls, formula = None, attr = "name", simplify = True): """Graph.Formula(formula = None, attr = "name", simplify = True) Generates a graph from a graph formula A graph formula is a simple string representation of a graph. It is very handy for creating small graphs quickly. The string consists of vertex names separated by edge operators. An edge operator is a sequence of dashes (C{-}) that may or may not start with an arrowhead (C{<} at the beginning of the sequence or C{>} at the end of the sequence). The edge operators can be arbitrarily long, i.e., you may use as many dashes to draw them as you like. This makes a total of four different edge operators: - C{-----} makes an undirected edge - C{<----} makes a directed edge pointing from the vertex on the right hand side of the operator to the vertex on the left hand side - C{---->} is the opposite of C{<----} - C{<--->} creates a mutual directed edge pair between the two vertices If you only use the undirected edge operator (C{-----}), the graph will be undirected. Otherwise it will be directed. Vertex names used in the formula will be assigned to the C{name} vertex attribute of the graph. Some simple examples: >>> from igraph import Graph >>> print Graph.Formula() # empty graph IGRAPH UN-- 0 0 -- + attr: name (v) >>> g = Graph.Formula("A-B") # undirected graph >>> g.vs["name"] ['A', 'B'] >>> print g IGRAPH UN-- 2 1 -- + attr: name (v) + edges (vertex names): A--B >>> g.get_edgelist() [(0, 1)] >>> g2 = Graph.Formula("A-----------B") >>> g2.isomorphic(g) True >>> g = Graph.Formula("A ---> B") # directed graph >>> g.vs["name"] ['A', 'B'] >>> print g IGRAPH DN-- 2 1 -- + attr: name (v) + edges (vertex names): A->B If you have may disconnected componnets, you can separate them with commas. You can also specify isolated vertices: >>> g = Graph.Formula("A--B, C--D, E--F, G--H, I, J, K") >>> print ", ".join(g.vs["name"]) A, B, C, D, E, F, G, H, I, J, K >>> g.clusters().membership [0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6] The colon (C{:}) operator can be used to specify vertex sets. If an edge operator connects two vertex sets, then every vertex from the first vertex set will be connected to every vertex in the second set: >>> g = Graph.Formula("A:B:C:D --- E:F:G") >>> g.isomorphic(Graph.Full_Bipartite(4, 3)) True Note that you have to quote vertex names if they include spaces or special characters: >>> g = Graph.Formula('"this is" +- "a silly" -+ "graph here"') >>> g.vs["name"] ['this is', 'a silly', 'graph here'] @param formula: the formula itself @param attr: name of the vertex attribute where the vertex names will be stored @param simplify: whether the simplify the constructed graph @return: the constructed graph: """ # If we have no formula, return an empty graph if formula is None: return cls(0, vertex_attrs = {attr: []}) vertex_ids, edges, directed = UniqueIdGenerator(), [], False # Loop over each part in the formula for part in formula.split(","): # Drop newlines from the part part = part.strip().replace("\n", "").replace("\t", "") # Parse the first vertex specification from the formula for start_names, end_names, arrowheads in generate_edges(part): start_ids = [vertex_ids[name] for name in start_names] end_ids = [vertex_ids[name] for name in end_names] if not arrowheads[0] and not arrowheads[1]: # This is an undirected edge. Do we have a directed graph? if not directed: # Nope, add the edge edges.extend((id1, id2) for id1 in start_ids \ for id2 in end_ids) else: # This is a directed edge directed = True if arrowheads[1]: edges.extend((id1, id2) for id1 in start_ids \ for id2 in end_ids) if arrowheads[0]: edges.extend((id2, id1) for id1 in start_ids \ for id2 in end_ids) # Grab the vertex names into a list vertex_attrs = {} vertex_attrs[attr] = vertex_ids.values() # Construct and return the graph result = cls(len(vertex_ids), edges, directed, vertex_attrs=vertex_attrs) if simplify: result.simplify() return result