Python networkx 模块,edges() 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用networkx.edges()

项目:ndmg    作者:neurodata    | 项目源码 | 文件源码
def scan_statistic(mygs, i):
    """
    Computes scan statistic-i on a set of graphs

    Required Parameters:
        mygs:
            - Dictionary of graphs
        i:
            - which scan statistic to compute
    """
    ss = OrderedDict()
    for key in mygs.keys():
        g = mygs[key]
        tmp = np.array(())
        for n in g.nodes():
            sg = nx.ego_graph(g, n, radius=i)
            tmp = np.append(tmp, np.sum([sg.get_edge_data(e[0], e[1])['weight']
                            for e in sg.edges()]))
        ss[key] = tmp
    return ss
项目:InnerOuterRNN    作者:Chemoinformatics    | 项目源码 | 文件源码
def create_directed_graphs(self):
        '''
        :return:
        '''
        self.directed_graphs = np.empty(
            (self.no_of_atoms, self.no_of_atoms - 1, 3), dtype=int)

        # parse all the atoms one by one and get directed graph to that atom
        # as the sink node
        for idx in range(self.no_of_atoms):
            # get shortest path from the root to all the other atoms and then reverse the edges.
            path = nx.single_source_dijkstra_path(self.graph, idx)
            G = nx.DiGraph()
            for i in range(self.no_of_atoms):
                temp = path[i]
                temp.reverse()
                G.add_path(temp)

            # do a topological sort to get a order of atoms with all edges pointing to the root
            topological_order = nx.topological_sort(G)

            sorted_path = np.empty((self.no_of_atoms - 1, 3))

            no_of_incoming_edges = {}
            for i in range(self.no_of_atoms - 1):
                node = topological_order[i]
                edge = (nx.edges(G, node))[0]
                if edge[1] in no_of_incoming_edges:
                    index = no_of_incoming_edges[edge[1]]
                    no_of_incoming_edges[edge[1]] += 1
                else:
                    index = 0
                    no_of_incoming_edges[edge[1]] = 1
                sorted_path[i, :] = [node, edge[1], index]
            self.directed_graphs[idx, :, :] = sorted_path
项目:HRG    作者:nddsg    | 项目源码 | 文件源码
def get_production_rule(G, child, itx):

    rhs = nx.Graph()
    for n in G.subgraph(child).nodes():
        rhs.add_node(n)
    for e in G.subgraph(child).edges():
        rhs.add_edge(e[0], e[1])

    # remove links between external nodes (edges in itx)
    for x in itertools.combinations(itx, 2):
        if rhs.has_edge(x[0], x[1]):
            rhs.remove_edge(x[0], x[1])

    return rhs
项目:MEM_DGM    作者:thu-ml    | 项目源码 | 文件源码
def plot(cov, fig_name):
    G=nx.Graph()
    # add node
    edgewidth = []
    edgecolor = []
    for i in xrange(cov.shape[0]):
        G.add_node(i)
    # add edge
    for i in xrange(0,cov.shape[0]-1):
        for j in xrange(i+1,cov.shape[0]):
            #print i,j,"%.3f " %cov[i][j],', ',
            G.add_edge(i,j)
            edgewidth.append(cov[i][j])
            edgecolor.append(cov[i][j])
        #print

    #print len(edgewidth), len(edgecolor), len(nx.edges(G))

    max_color = 5
    mc = max(edgecolor)
    max_width = 5
    mw = max(edgewidth)
    for i in xrange(len(edgecolor)):
        edgecolor[i] = (edgecolor[i])*max_color/mc
        edgewidth[i] = (edgewidth[i])*max_width/mw

    #print 'width', min(edgewidth), max(edgewidth), 'color', min(edgecolor), max(edgecolor)
    # set
    plt.clf()
    #plt.figure(1, size=(4,4))
    pos=nx.spring_layout(G)
    # draw
    nx.draw_networkx_nodes(G, pos, node_size=500, node_color='w', labels=True)
    nx.draw_networkx_labels(G, pos, labels=None, 
                                    font_size=12, 
                                    font_color='k', 
                                    font_family='sans-serif', 
                                    font_weight='normal', 
                                    alpha=1.0)
    nx.draw_networkx_edges(G, pos, edge_cmap = plt.cm.Blues,
                                   edge_color = edgecolor,
                                   alpha = 1,
                                   width = edgewidth)
    frame1 = plt.gca()
    frame1.axes.get_xaxis().set_visible(False)
    frame1.axes.get_yaxis().set_visible(False)
    plt.savefig(fig_name+".pdf", format='pdf')
项目:MEM_DGM    作者:zhenxuan00    | 项目源码 | 文件源码
def plot(cov, fig_name):
    G=nx.Graph()
    # add node
    edgewidth = []
    edgecolor = []
    for i in xrange(cov.shape[0]):
        G.add_node(i)
    # add edge
    for i in xrange(0,cov.shape[0]-1):
        for j in xrange(i+1,cov.shape[0]):
            #print i,j,"%.3f " %cov[i][j],', ',
            G.add_edge(i,j)
            edgewidth.append(cov[i][j])
            edgecolor.append(cov[i][j])
        #print

    #print len(edgewidth), len(edgecolor), len(nx.edges(G))

    max_color = 5
    mc = max(edgecolor)
    max_width = 5
    mw = max(edgewidth)
    for i in xrange(len(edgecolor)):
        edgecolor[i] = (edgecolor[i])*max_color/mc
        edgewidth[i] = (edgewidth[i])*max_width/mw

    #print 'width', min(edgewidth), max(edgewidth), 'color', min(edgecolor), max(edgecolor)
    # set
    plt.clf()
    #plt.figure(1, size=(4,4))
    pos=nx.spring_layout(G)
    # draw
    nx.draw_networkx_nodes(G, pos, node_size=500, node_color='w', labels=True)
    nx.draw_networkx_labels(G, pos, labels=None, 
                                    font_size=12, 
                                    font_color='k', 
                                    font_family='sans-serif', 
                                    font_weight='normal', 
                                    alpha=1.0)
    nx.draw_networkx_edges(G, pos, edge_cmap = plt.cm.Blues,
                                   edge_color = edgecolor,
                                   alpha = 1,
                                   width = edgewidth)
    frame1 = plt.gca()
    frame1.axes.get_xaxis().set_visible(False)
    frame1.axes.get_yaxis().set_visible(False)
    plt.savefig(fig_name+".pdf", format='pdf')