예제 #1
0
def main():
    if len(sys.argv) < 2:
        sys.exit('usage: %s < input gexf' % sys.argv[0])

    # Input Graph file

    infile = sys.argv[1]

    G = nx.read_gexf(infile)

    # extract the largest weakly connected component and convert to undirected for fa2l

    G = max(nx.weakly_connected_component_subgraphs(G),
            key=len).to_undirected()

    # set parameters

    colormap = {
        'null': 'lightgray',
        'partisan_2012_conservative': 'r',
        'partisan_2012_liberal': 'b',
        'partisan_2012_libertarian': 'y'
    }
    color_field = "partisan_code"
    size_field = 'inlink_count'
    filter_field = "inlink_count"
    label_field = "label"
    num_labels = 20  # number of labels to visualize
    k = 100  # number of nodes to visualize

    # If the size of Graph > 1000 nodes, set G to the subgraph containing largest 1000 nodes to get the layout

    if len(G.nodes()) > 1000:
        G = filter_graph(G, filter_by=filter_field, top=1000).to_undirected()

    # extract the positions

    pos = force_atlas2_layout(G,
                              iterations=50,
                              pos_list=None,
                              node_masses=None,
                              outbound_attraction_distribution=True,
                              lin_log_mode=True,
                              prevent_overlapping=True,
                              edge_weight_influence=1.0,
                              jitter_tolerance=1.0,
                              barnes_hut_optimize=True,
                              barnes_hut_theta=0.5,
                              scaling_ratio=38,
                              strong_gravity_mode=False,
                              multithread=False,
                              gravity=1.0)

    print("Extracted the positions")
    print(pos)

    # Extract top 500 nodes for visualization
    top_k_subgraph = filter_graph(G, filter_by=filter_field,
                                  top=k).to_undirected()

    # Set visual attributes

    node_colors = set_node_color(top_k_subgraph,
                                 color_by=color_field,
                                 colormap=colormap)
    node_sizes = set_node_size(top_k_subgraph,
                               size_field="inlink_count",
                               min_size=0.1,
                               max_size=800)
    node_labels = set_node_label(top_k_subgraph, label=label_field)
    subgraph_pos = get_subgraph_pos(top_k_subgraph, pos)
    edge_colors = edgecolor_by_source(top_k_subgraph, node_colors)

    print("Drawing the visualization")

    # Get specific labels

    subset_label_nodes = sorted(zip(top_k_subgraph.nodes(), node_sizes),
                                key=lambda x: x[1],
                                reverse=True)[0:num_labels]
    subset_labels = {n[0]: node_labels[n[0]] for n in subset_label_nodes}

    # plot the visualization

    fig = plt.figure(figsize=(10, 10), dpi=100)
    ax = fig.add_subplot(111)
    #ax.set(xlim=[0.0, 1.0], ylim=[0.0, 1.0], title='Network Viz')

    # Draw the nodes, edges, labels separately

    nodes = nx.draw_networkx_nodes(top_k_subgraph,
                                   pos=subgraph_pos,
                                   node_size=node_sizes,
                                   node_color=node_colors,
                                   alpha=.7)
    edges = nx.draw_networkx_edges(top_k_subgraph,
                                   pos=subgraph_pos,
                                   edge_color=edge_colors,
                                   alpha=0.01)
    labels = nx.draw_networkx_labels(top_k_subgraph,
                                     pos=subgraph_pos,
                                     labels=subset_labels,
                                     font_size=8)

    # Adjust label overlapping

    x_pos = [v[0] for k, v in subgraph_pos.items()]
    y_pos = [v[1] for k, v in subgraph_pos.items()]
    adjust_text(texts=list(labels.values()),
                x=x_pos,
                y=y_pos,
                arrowprops=dict(arrowstyle='->', color='lightgray'))

    # Declutter visualization

    #ax.axis("off");

    # save the plot

    plt.savefig("1.png")

    # Show the plot
    plt.show()
예제 #2
0
파일: run.py 프로젝트: bh0085/compbio
def run2( reset = False,
         base_net = 'kn',
         comp_net = 'fn',
         demand_bdtnp = False):
    bd = nio.getBDTNP()

    ktgs,ktfs = nio.getKNet()
    tgs,tfs = nio.getNet()
    sush = nio.getSush(on_fail = 'compute')
    
    tfset = set(ktfs.keys())
    tgset = set(ktgs.keys())

    
    tg_int = set(tgs.keys()).intersection(ktgs.keys())
    tf_int = set(tfs.keys()).intersection(ktfs.keys())
    
    if demand_bdtnp:
        tg_int = tg_int.intersection(bd.keys())
        tf_int = tf_int.intersection(bd.keys())
    
    if base_net =='kn':
        b_edges = [(tf, tg, float(wt)) 
               for tg, elt in ktgs.iteritems() if tg in tg_int
               for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]

    if comp_net == 'fn':
        c_edges = [(tf, tg, float(wt)) 
                for tg, elt in tgs.iteritems() if tg in tg_int
                for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]
    elif comp_net == 'sn':
        #Sushmita network with signed edges
        c_edges =  [(tf, tg, float(wt)) 
                 for tg, elt in sush.iteritems() if tg in tg_int
                 for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]
    elif comp_net == 'sna':
        #Sushmita network with unsigned edges
        c_edges = [(tf, tg, abs(float(wt))) 
                 for tg, elt in sush.iteritems() if tg in tg_int
                 for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]
    elif comp_net == 'kn':
        c_edges = [(tf, tg, float(wt)) 
           for tg, elt in ktgs.iteritems() if tg in tg_int
           for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]

    
    ng = 4    
    nodes = array(list(tf_int.union(tg_int)))

    bg = nx.DiGraph()
    bg.add_nodes_from(nodes)
    bg.add_weighted_edges_from(b_edges)

    cg = nx.DiGraph()
    cg.add_nodes_from(nodes)
    cg.add_weighted_edges_from(c_edges)

    cgraphs =  {comp_net:cg}
    v0 = cgraphs.values()
    k0 = cgraphs.keys()
   
 
    for k,g in zip(k0,v0):
        for prc in [10]:
            thr = percentile([e[2]['weight'] 
                              for e in nx.to_edgelist(g)], prc)
            cgraphs.update([('{0}_thr{1:2.2}'.format(k,thr),
                             nfu.thr_graph(g,thr))])    
            gt =  nfu.thr_graph(g,thr)
    v0 = cgraphs.values()
    k0 = cgraphs.keys()

    for k, v in zip(k0,v0):
        tot_edges = len(nx.to_edgelist(v))
        for n_c in [1,2,4]:
            for max_edges in array([.2,.5,1.]) * tot_edges :
                if not 'thr' in k:
                    continue
                gfilt = nfu.filter_graph(v, n_c = n_c)
                gfilt = nfu.top_edges(gfilt, max_edges = max_edges)
                gthr = nfu.thr_graph(gfilt, 1e-8)
                cgraphs.update([('{0}_flt{1}_nedge{2}'.format(k,n_c,max_edges),gfilt)])
                cgraphs.update([('{0}_flt{1}_nedge{2}_thr0'.format(k,n_c,max_edges),gthr)])

    '''
When you don't have hidden variables, networks can be modelled mby information criterion.

In what settings can you incur causality from datasets.

You need a prior to limit the number of arrowsin your graph:
  
The idea: come up with an idea from computational learning theory 
and come up with a model for interventions.

Spatial, genetic, time data to penalize network edges...

Granger causality uses time varying data to 
'''

        

    #nfplots.show(kg,pos,node_color = 'none')
    
    #nfplots.show(fg,pos,node_color = 'white', alpha = .2, with_labels = False)
    
    return bg, cgraphs
예제 #3
0
파일: run.py 프로젝트: bh0085/compbio
def run_sig(genes, show_disc = False, weighted = True):
    
    genes2 = []
    for g in genes:
        genes2.append((g[0], [[gelt[0], gelt[1]] for gelt in g[1]], g[2]))
    genes = genes2

    modules = [m[0] for m in genes]

    if len(modules[0]) == 2:
        module_type = 'doubles'
    else:
        module_type = 'triples'

    counts = [m[1] for m in genes]
    tgs,tfs = nio.getNet()
    bd = nio.getBDTNP()
    
    nodes_allowed = set(bd.keys())
    cnodes = list(nodes_allowed)

    dnodes = []
    dedges = []
    cedges = []
    cnodes = []
    for m in genes:
        for tginfo in m[1]:
            tg = tginfo[0]
            tg_mcount = tginfo[1]
            dtgnode = '{0}_{1}_mod{2}'.format(tg,tg,m[0])
            ctgnode = '{0}'.format(tg)
            dnodes.append(dtgnode)
            cnodes.append(ctgnode)
            for tf in m[0]:
                dtfnode = '{0}_{1}_mod{2}'.format(tf,tg,m[0])
                ctfnode = '{0}'.format(tf)
                dnodes.append(dtfnode)
                cnodes.append(ctfnode)
                dedges.append((dtfnode, dtgnode,tg_mcount))
                cedges.append((ctfnode, ctgnode,tg_mcount))
                
    nodes_allowed = list(set(cnodes))

    if show_disc:
        dgraph, cgraph = [nx.Graph() for i in range(2)]
        dgraph.add_nodes_from(list(set(dnodes)))
        dgraph.add_weighted_edges_from(list(set(dedges)))
        f = myplots.fignum(4, (8,8))
        ax = f.add_subplot(111)
        pos=nx.graphviz_layout(dgraph,prog="neato")
        # color nodes the same in each connected subgraph
        C=nx.connected_component_subgraphs(dgraph)
        for g in C:
            c=[random.random()]*nx.number_of_nodes(g) # random color...
            nx.draw(g,
                    pos,
                    node_size=40,
                    node_color=c,
                    vmin=0.0,
                    vmax=1.0,
                    with_labels=False
                    )
        figtitle = 'mcmc_disc'
        f.savefig(figtemplate.format(figtitle))
        return


    
    cgraph = nx.DiGraph() 
    cgraph.add_nodes_from(cnodes)
    
    cedgegrps = [(k,list(g)) for k, g in it.groupby(\
            sorted(cedges, key = lambda x: (x[0],x[1])),
            key =  lambda x: (x[0],x[1]))]
    cedges = [ (k[0],k[1], sum([gelt[2] for gelt in g])) 
                 for k,g in cedgegrps] 
                 
    if weighted == False:
        for ce in cedges:
            ce[2] = 1
        
    cgraph.add_weighted_edges_from(list(set(cedges)))


    sfRN = [(tf, tg, float(wt)) 
            for tg, elt in tgs.iteritems() if tg in nodes_allowed
            for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in nodes_allowed]
    fg = nx.DiGraph()
    fg.add_nodes_from(cnodes)
    fg.add_weighted_edges_from(sfRN)


    colors = mycolors.getct(len(cnodes))

    f = myplots.fignum(5, (8,8))
    ax =f.add_subplot(111)
    pos=nx.graphviz_layout(fg,prog="neato")
    # color nodes the same in each connected subgraph
    nx.draw(cgraph,
            pos,
            node_size=100,
            node_color=colors,
            vmin=0.0,
            vmax=1.0,
            with_labels=False,
            alpha = 1.
            )
    ax.set_title('connectivity of MCMC for network {0}'.format(module_type))
    figtitle = 'mcmc_network_{0}{1}'.\
        format(module_type,'' if weighted else 'unweighted')
    f.savefig(figtemplate.format(figtitle))




    f = myplots.fignum(5, (8,8))
    ax =f.add_subplot(111)
    #pos=nx.graphviz_layout(fg,prog="neato")
    # color nodes the same in each connected subgraph
    nx.draw(fg,
            pos,
            node_size=100,
            node_color=colors,
            vmin=0.0,
            vmax=1.0,
            with_labels=False
            )


    ax.set_title('connectivity of reference for network {0}'.format(module_type))

    figtitle = 'mcmc_ref_network_{0}{1}'.\
        format(module_type,'' if weighted else 'unweighted')
    f.savefig(figtemplate.format(figtitle))

                

    graphs = {'mcmc':cgraph,'network':fg}

            
    v0 = graphs.values()
    k0 = graphs.keys()

    for k,g in zip(k0,v0):
        for prc in [1,50,95]:
            thr = percentile([e[2]['weight'] 
                              for e in nx.to_edgelist(g)], prc)
            graphs.update([('{0}_thr{1}%'.format(k,prc),
                             nfu.thr_graph(g,thr))])    
            
    v0 = graphs.values()
    k0 = graphs.keys()

    for k, v in zip(k0,v0):
        tot_edges = len(nx.to_edgelist(fg))
        for n_c in [2,4,6,8,12,20]:
            for max_edges in array([.5,1.,2.]) * tot_edges :
                gfilt = nfu.filter_graph(v, n_c = n_c)
                gfilt = nfu.top_edges(gfilt, max_edges = max_edges)
                gthr = nfu.thr_graph(gfilt, 1e-8)
                graphs.update([('{0}_flt{1}'.format(k,n_c),gfilt)])
                graphs.update([('{0}_flt{1}_thr0'.format(k,n_c),gthr)])
            
    return graphs
예제 #4
0
파일: run.py 프로젝트: bh0085/compbio
def run( reset = False,
         base_net = 'kn',
         comp_net = 'fn',
         demand_bdtnp = False):
    tgs,tfs = nio.getNet()
    ktgs,ktfs = nio.getKNet()
    bd = nio.getBDTNP()
    #btgs,btfs = nio.getBDTNP()
    sush = nio.getSush(on_fail = 'compute')
    


    tfset = set(ktfs.keys())
    tgset = set(ktgs.keys())

    
    tg_int = set(tgs.keys()).intersection(ktgs.keys())
    tf_int = set(tfs.keys()).intersection(ktfs.keys())
    
    if demand_bdtnp:
        tg_int = tg_int.intersection(bd.keys())
        tf_int = tf_int.intersection(bd.keys())



    sfRN = [(tf, tg, float(wt)) 
            for tg, elt in tgs.iteritems() if tg in tg_int
            for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]
    

    kRN = [(tf, tg, float(wt)) 
            for tg, elt in ktgs.iteritems() if tg in tg_int
            for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]

    #Sushmita network with signed edges
    suRN =  [(tf, tg, float(wt)) 
            for tg, elt in sush.iteritems() if tg in tg_int
            for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]

    #Sushmita network with unsigned edges
    suaRN = [(tf, tg, abs(float(wt))) 
             for tg, elt in sush.iteritems() if tg in tg_int
             for tf, wt  in zip(elt['tfs'], elt['weights']) if tf in tf_int]
             
    edges = [ kRN, sfRN, suRN, suaRN]


    ng = 4
    fg, kg, sug, suag = [nx.DiGraph() for i in range(4)]
    
    
    nodes = array(list(tf_int.union(tg_int)))
    graphs =  {'kg':kg,'fg':fg,'sug':sug,'suag':suag}

    for g, edges in zip(graphs.values(), edges):
        g.add_nodes_from(nodes)
        g.add_weighted_edges_from(edges)
        


    
    for gname in ['fg','suag']:
        for prc in [10,50,75,85,90,95,98,99]:
            thr = percentile([e[2]['weight'] for e in nx.to_edgelist(graphs[gname])], prc)
            graphs.update([('{0}_thr{1:2.2}'.format(gname,thr),
                            nfu.thr_graph(graphs[gname],thr))])    
            
    v0 = graphs.values()
    k0 = graphs.keys()

    tot_edges = len(nx.to_edgelist(graphs['fg']))
    for k, v in zip(k0,v0):
        for n_c in [2,4,8 ,12]:
            for max_edges in array([.5,1.,2.,5.]) * tot_edges :
                if not 'thr' in k:
                    continue
                gfilt = nfu.filter_graph(v, n_c = n_c)
                gfilt = nfu.top_edges(gfilt, max_edges = max_edges)
                gthr = nfu.thr_graph(gfilt, 1e-8)
                graphs.update([('{0}_flt{1}'.format(k,n_c),gfilt)])
                graphs.update([('{0}_flt{1}_thr0'.format(k,n_c),gthr)])
            


        

    #nfplots.show(kg,pos,node_color = 'none')
    
    #nfplots.show(fg,pos,node_color = 'white', alpha = .2, with_labels = False)
    
    return graphs