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

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

项目:PedWorks    作者:BrnCPrz    | 项目源码 | 文件源码
def find_partition(graph):
    # code and lib from http://perso.crans.org/aynaud/communities/
    # must be an undirected graph
    g = graph
    partition = community.best_partition(g)
    print "Partitions found: ", len(set(partition.values()))
    # to show members of each partition:
    for i in set(partition.values()):
        members = [nodes for nodes in partition.keys() if partition[nodes] == i]
        print i, len(members)

        # if i==0:
        #      # write out the subgraph
        #      community_graph = graph.subgraph(members)
        #      #draw_graph(community_graph)
        #      #nx.write_edgelist(community_graph, "community.edgelist", data=False)
        #      #for member in members:
        # #         print member, i

        # print "Partition for node johncoogan: ", partition[node_id]
    nx.set_node_attributes(g, 'partition', partition)
    return g, partition
项目:mlss-2016    作者:Networks-Learning    | 项目源码 | 文件源码
def run(output_path, graph_type, force,
        seed, num_nodes, edge_prob, solution_path):

    any_op_file_exists = (P.exists(output_path) or P.exists(solution_path))

    if any_op_file_exists and not force:
        print('Cannot overwrite without --force', file=sys.stderr)
        sys.exit(-1)

    g = None
    if graph_type == 'erdos':
        g = nx.erdos_renyi_graph(num_nodes, edge_prob,
                                 seed=seed, directed=True)
    else:
        print('Unknown graph type: ', graph_type, file=sys.stderr)
        sys.exit(-1)

    A = np.zeros((num_nodes, num_nodes), dtype='float')
    # All edges are given uniformly random weights.
    for u, v, d in g.edges(data=True):
        d['act_prob'] = R.random()
        A[u, v] = d['act_prob']

    nx.write_edgelist(g, output_path)
    np.savetxt(solution_path, A, delimiter=',')
项目:PedWorks    作者:BrnCPrz    | 项目源码 | 文件源码
def write_edge_attributes(graph, filepath, format='networkx', with_data=False):
    """
     Utility function to let you write an edgelist
    """
    print "Writing edgelist to file..."
    if format == 'networkx':
        nx.write_edgelist(graph, filepath, data=with_data)
    else:
        print "generate csv"
    print "Done"
项目:anomalous-vertices-detection    作者:Kagandi    | 项目源码 | 文件源码
def write_graph(self, output_path):
        """
        Parameters
        ----------
        output_path:

        Returns
        -------
        NxGraph: Graph object

        Examples
        --------
        >>>
        """
        nx.write_edgelist(self._graph, output_path, delimiter=',', data=False)
项目:gtfspy    作者:CxAalto    | 项目源码 | 文件源码
def _write_stop_to_stop_network_edges(net, file_name, data=True, fmt=None):
    """
    Write out a network

    Parameters
    ----------
    net: networkx.DiGraph
    base_name: str
        path to the filename (without extension)
    data: bool, optional
        whether or not to write out any edge data present
    fmt: str, optional
        If "csv" write out the network in csv format.
    """
    if fmt is None:
        fmt = "edg"

    if fmt == "edg":
        if data:
            networkx.write_edgelist(net, file_name, data=True)
        else:
            networkx.write_edgelist(net, file_name)
    elif fmt == "csv":
        with open(file_name, 'w') as f:
            # writing out the header
            edge_iter = net.edges_iter(data=True)
            _, _, edg_data = next(edge_iter)
            edg_data_keys = list(sorted(edg_data.keys()))
            header = ";".join(["from_stop_I", "to_stop_I"] + edg_data_keys)
            f.write(header)
            for from_node_I, to_node_I, data in net.edges_iter(data=True):
                f.write("\n")
                values = [str(from_node_I), str(to_node_I)]
                data_values = []
                for key in edg_data_keys:
                    if key == "route_I_counts":
                        route_I_counts_string = str(data[key]).replace(" ", "")[1:-1]
                        data_values.append(route_I_counts_string)
                    else:
                        data_values.append(str(data[key]))
                all_values = values + data_values
                f.write(";".join(all_values))