Exemple #1
0
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
Exemple #2
0
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