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

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

项目:synchrony    作者:cknd    | 项目源码 | 文件源码
def convert_network_3Dto2D(network):
    """
    Reshape a 3D grid graph into a 2D one, preserving
    adjacency and edge&node attributes.
    """
    assert network.graph.has_key("grid3D_dimensions")
    M,N,K = network.graph["grid3D_dimensions"]

    reshape = lambda node: (node[0],node[1] + node[2]*N)

    nodes_2D = [(reshape(node_attr[0]),node_attr[1]) for node_attr in network.node.items()]

    edges_3D = nx.to_edgelist(network)
    edges_2D = [(reshape(e[0]),reshape(e[1]),e[2]) for e in edges_3D]

    g = nx.from_edgelist(edges_2D)
    g.add_nodes_from(nodes_2D)
    g.graph["grid_dimensions"] = (M,N*K)
    g.graph["original_grid3D_dimensions"] = (M,N,K)
    return g
项目:anomalous-vertices-detection    作者:Kagandi    | 项目源码 | 文件源码
def save_as_sgraph(self, graph_path):
        """  Saves the graph as sgraph

        Parameters
        ----------
        graph_path: string
            The graph path.

        Examples
        --------
        >>>  g.save_as_sgraph("graph.sgraph")
        """
        edges = nx.to_edgelist(self._graph)
        res = {'source': [], 'dest': [], "attr": []}
        for edge in edges:
            res['source'].append(edge[0])
            res['dest'].append(edge[1])
            res['attr'].append(edge[2])
        save_nx_as_sgraph(res, graph_path)
项目:cnn-graph-classification    作者:giannisnik    | 项目源码 | 文件源码
def networkx_to_igraph(G):
    mapping = dict(zip(G.nodes(),range(G.number_of_nodes())))
    reverse_mapping = dict(zip(range(G.number_of_nodes()),G.nodes()))
    G = nx.relabel_nodes(G,mapping)
    G_ig = ig.Graph(len(G), list(zip(*list(zip(*nx.to_edgelist(G)))[:2])))
    return G_ig, reverse_mapping
项目:synchrony    作者:cknd    | 项目源码 | 文件源码
def __init__(self, graph, inputc=None, inputc_inh=None, **par):

        self.inputc = inputc.copy(order='C') if inputc is not None else None
        self.inputc_inh = inputc_inh.copy(order='C') if inputc_inh is not None else None
        self.edges = [str(e) for e in nx.to_edgelist(graph)]
        self.par = par

        par_s = [str((key, self.par[key])) for key in sorted(self.par.keys())]
        self.hashable = ''.join(self.edges)
        self.hashable += ''.join(par_s)
        self.hashable += str(self.inputc)
        self.hashable += str(self.inputc_inh)
项目:pyhiro    作者:wanweiwei07    | 项目源码 | 文件源码
def export(self):
        export = nx.to_edgelist(self.transforms)
        for e in export:
            e[2]['matrix'] = np.array(e[2]['matrix']).tolist()
        return export
项目:TextAsGraphClassification    作者:NightmareNyx    | 项目源码 | 文件源码
def networkx_to_igraph(G):
    mapping = dict(zip(G.nodes(), range(G.number_of_nodes())))
    reverse_mapping = dict(zip(range(G.number_of_nodes()), G.nodes()))
    G = nx.relabel_nodes(G, mapping)
    G_ig = ig.Graph(len(G), list(zip(*list(zip(*nx.to_edgelist(G)))[:2])))
    return G_ig, reverse_mapping