Exemple #1
0
 def _feedforward(self, x):
     x_in = x
     for layer in self.net:
         x_out = []
         for node in layer:
             active = activation(node['func'])
             node["a"] = active(dotprod(node['weights'], x_in))
             x_out.append(node["a"])
         x_in = x_out # set output as next input
     return x_in
Exemple #2
0
def gdraw(bgraph,cgraphs, plotname = 'default_name', measure = 'cosine'):
    #pos = nx.graphviz_layout(cgraphs['kg'])

    nodelist = bgraph.nodes()

    adjs = [ array(nx.adj_matrix(g, nodelist = nodelist)) for g in cgraphs.values() ]



    badj =  array(nx.adj_matrix(bgraph, nodelist = nodelist))
    bnrm = badj / sqrt(sum(badj**2))

    if measure == 'cosine':
        nrms = []
        bnrm = badj / sqrt(sum(badj**2))
        for a in adjs:
            n = sqrt(sum(a**2))
            nrms.append(a / n)
    
        sims = array([round(nfu.cosine_adj(a1,bnrm),8) for a1 in nrms])
    elif measure =='jaccard':
        sims = array([round(nfu.dotprod(a1,badj),8)/ (sum(a1) + sum(badj)) 
                      for a1 in adjs])

    elif measure =='specificity':
        sims = array([round(nfu.dotprod(a1,badj),8)/sum(a1) for a1 in adjs])

    elif measure =='sensitivity':
        sims = array([round(nfu.dotprod(a1,badj),8)/sum(badj) for a1 in adjs])

    else:
        raise Exception()




    srto = argsort(cgraphs.keys()) 
    #XVALs give ranks of each key index.
    xvals = argsort(srto)


    cols = map(lambda x: 
               ('flt' in x and x.count('thr') > 1) and 'orange' or
               ('flt' in x) and 'red' or
               ('thr' in x) and 'yellow' or
               ('fg' in x) and 'green' or 
               ('su' in x) and 'blue' or 
               'black', cgraphs.keys())

    yvals = sims

    f = plt.gcf()
    f = myplots.fignum(3, (.25 * len(sims),10))
    f.clear()
    ax = f.add_subplot(111)
    myplots.padded_limits(ax,xvals,yvals + [0.], margin = [.02,.02])
    ax.scatter(xvals,yvals,100, color = cols)
    ax.set_ylabel('red fly similarity ({0})'.format(measure))
    ax.set_xlabel('networks')
    ax.set_xticklabels([])
    ax.set_xticks([])

    mark_ys = [0, median(sims), mean(sims), sort(sims)[::-1][1],1]
    ax.hlines(mark_ys, *ax.get_xlim(), linestyle = ':',alpha = .2)
    

    f.savefig(cfg.dataPath('figs/meAWG/filter_{0}_meth_{1}_nolabels.pdf'.\
                               format(plotname,measure)))


    ax.set_xticks(range(len(srto)))
    cols_added = []
    annotes = []
    for z in zip(cgraphs.keys(),cols):
        if not z[1] in cols_added:
            annotes.append( ' '.join(z))
            cols_added.append(z[1])
        
    ax.annotate('\n'.join(annotes), 
                [1,1],xycoords = 'axes fraction', va = 'top', ha = 'right')
    
    ax.set_xticklabels([cgraphs.keys()[i] for i in srto], 
                       rotation = 90, va = 'bottom', size = 'xx-small')

    f.savefig(cfg.dataPath('figs/meAWG/filter_{0}_meth_{1}_labels.pdf'.\
                               format(plotname,measure)))
Exemple #3
0
def show_mcmc(graphs,nc_base =12,
              weighted = True, module_type = 'doubles',
              measure = 'cosine'):

    nodelist = graphs.values()[0].nodes()

    print 'using module type: {0}'.format(module_type)
    adjs = [ array(nx.adj_matrix(g, nodelist = nodelist)) for g in graphs.values() ]
    nrms = []
    for a in adjs:
            n = sqrt(sum(a**2))
            nrms.append(a / n)
    

    idxs_allowed =[ i for i, k in  enumerate(graphs.keys()) if 'mcmc' in k]
    
    belt = graphs.keys().index('network_flt{0}'.format(nc_base))
    if measure == 'cosine':
        sims = array([round(nfu.cosine_adj(a1,nrms[belt]),8) 
                      for i, a1 in enumerate(nrms) if i in idxs_allowed])
    elif measure == 'dotprod':
        sims = array([nfu.dotprod(a1,adjs[belt]) 
                      for i, a1 in enumerate(adfs) if i in idxs_allowed])
    else:
        raise Exception()

    keys_allowed = [k for i, k in enumerate(graphs.keys()) 
                    if i in idxs_allowed]
    srto = argsort([k for i, k in enumerate(graphs.keys()) 
                    if i in idxs_allowed]) 
    #XVALs give ranks of each key index.
    xvals = argsort(srto)


    cols = map(lambda x: 
               ('flt' in x and x.count('thr') > 1) and 'orange' or
               ('flt' in x) and 'red' or
               ('thr' in x) and 'yellow' or
               ('fg' in x) and 'green' or 
               ('su' in x) and 'blue' or 
               'black', keys_allowed)

    yvals = sims

    f = plt.gcf()
    f = myplots.fignum(3, (.25 * len(sims),10))
    f.clear()
    ax = f.add_subplot(111)
    myplots.padded_limits(ax,xvals,yvals + [0.], margin = [.02,.02])
    ax.scatter(xvals,yvals,100, color = cols)
    ax.set_ylabel('red fly similarity ({0})'.format(measure))
    ax.set_xlabel('networks')
    ax.set_xticklabels([])
    ax.set_xticks([])

    mark_ys = [0, median(sims), mean(sims), sort(sims)[::-1][1],1]
    ax.hlines(mark_ys, *ax.get_xlim(), linestyle = ':',alpha = .2)
    ax.vlines(range(len(xvals))[::10], *ax.get_ylim(), linestyle = ':',alpha = .1)
    

    

    figtitle = 'mcmc_net_comparisons_{0}_{1}net_comps{2}_sim_{3}_nolabels'.\
        format(module_type,nc_base ,'' if weighted else 'unweighted',measure)
    f.savefig(figtemplate.format(figtitle))

                

    ax.set_xticks(range(len(srto)))
    #ax.annotate('\n'.join(' '.join(z) for z in zip(graphs.keys())),
    #            [0,1],xycoords = 'axes fraction', va = 'top')
    
    ax.set_xticklabels([keys_allowed[i] for i in srto], 
                       rotation = 90, size = 'xx-small',
                       )#color = cols)


    figtitle = 'mcmc_net_comparisons_{0}_{1}net_comps{2}_sim_{3}_labels'.\
        format(module_type,nc_base ,'' if weighted else 'unweighted', measure)
    f.savefig(figtemplate.format(figtitle))