def networkx_cooccurrence_graph( nC, v, labels, max_edges = 10 ) : """ Returns a Networkx-like undirected graph, to eventually be used for analysis and display """ Result = nx.Graph() nv = v.astype( float32 ) / v.max() cutoff = cooccur_cutoff( nC, max_edges ); coords = zip(*(nC >= cutoff).nonzero()) # make a dict of all nodes which are mentioned in the coords nodes = {} index = 1 # explicitly cast everything so that gexf and other files can convert correctly for coord in set(chain.from_iterable(coords)) : if not nodes.has_key( coord ) : Result.add_node( str(index), label=str(index), coord=str(coord), labelval=str(labels[coord]), index = str(index), width=float(nv[coord]) ) nodes[ coord ] = str(index) index = index + 1 for coord in coords : Result.add_edge( nodes[coord[0]], nodes[coord[1]], weight = float(nC[coord]), penwidth = float(nC[coord]) ) return Result
def doi_legend( nC, v, labels, max_edges = 10 ) : cutoff = cooccur_cutoff( nC, max_edges ) coords = zip(*(nC >= cutoff).nonzero()) dois = [labels[x] for x in set(chain.from_iterable(coords))] b = Crossref_browser() result = [legend_line( dois.index(doi) + 1, doi, b ) for doi in dois] return result
def neato_cooccurrence_graph( nC, v, labels, max_edges = 10, fnam_stem = "test", label_nodes_directly = False, scale=1.0, min_node_size = 0.1 ): """ makes a neato-style undirected graph from the given cooccurrence matrix, vector of total occurrences, and labels. Assume C is normalized as desired and that all is pruned as desired! """ nv = v.astype( float32 ) / v.max() cutoff = cooccur_cutoff( nC, max_edges ); graph = pydot.Dot( graph_type = 'graph' ) graph.set_overlap("false") coords = zip(*(nC >= cutoff).nonzero()) # make a dict of all nodes which are mentioned in the coords nodes = {} index = 1 for coord in set(chain.from_iterable(coords)) : if not nodes.has_key( coord ) : node = pydot.Node( str(coord) ) if v != None : #print coord label = labels[coord] if label_nodes_directly : node.set_label( label ) else : node.set_label( str(index) ) #node.set_penwidth( nv[ coord ] ) node.set_fixedsize("true") node.set_width( max(min_node_size,scale *nv[ coord ]) ) node.set_shape("circle") nodes[ coord ] = node graph.add_node( node ) index = index + 1 for coord in coords : edge = pydot.Edge( nodes[coord[0]], nodes[coord[1]] ) edge.set_weight( nC[coord] ) edge.set_penwidth( nC[coord]*5 ) #edge.set_label( str(int(m[coord]) )) graph.add_edge(edge) if not label_nodes_directly : legend = pydot.Node( "legend" ) nodelist = nodes.items() nodelist.sort( lambda a,b : cmp(node_index(a[1].get_label()),node_index(b[1].get_label())) ) legend.set_label( "\l".join([x[1].get_label()+":"+labels[x[0]] for x in nodelist])+"\l" ) legend.set_shape("box") graph.add_node(legend) #print graph.to_string() graph.write_dot(fnam_stem+'.dot', prog='neato' ) graph.write_png(fnam_stem+'.png', prog='neato' )