当前位置: 首页>>代码示例>>Python>>正文


Python networkx.write_gpickle函数代码示例

本文整理汇总了Python中networkx.write_gpickle函数的典型用法代码示例。如果您正苦于以下问题:Python write_gpickle函数的具体用法?Python write_gpickle怎么用?Python write_gpickle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了write_gpickle函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load_train_test_graphs

def load_train_test_graphs(dataset, recache_input):
    raw_mat_path = 'data/{}.npz'.format(dataset)
    train_graph_path = 'data/{}/train_graph.pkl'.format(dataset)
    test_graph_path = 'data/{}/test_graph.pkl'.format(dataset)

    if recache_input:
        print('loading sparse matrix from {}'.format(raw_mat_path))
        m = load_sparse_csr(raw_mat_path)

        print('splitting train and test...')
        train_m, test_m = split_train_test(
            m,
            weights=[0.9, 0.1])

        print('converting to nx.DiGraph')
        train_g = nx.from_scipy_sparse_matrix(train_m, create_using=nx.DiGraph(), edge_attribute='sign')
        test_g = nx.from_scipy_sparse_matrix(test_m, create_using=nx.DiGraph(), edge_attribute='sign')
                
        print('saving train and test graphs...')
        nx.write_gpickle(train_g, train_graph_path)
        nx.write_gpickle(test_g, test_graph_path)
    else:
        print('loading train and test graphs...')
        train_g = nx.read_gpickle(train_graph_path)
        test_g = nx.read_gpickle(test_graph_path)
    return train_g, test_g
开发者ID:xiaohan2012,项目名称:snpp,代码行数:26,代码来源:data.py

示例2: __init__

    def __init__(self, post_fcn = None):
        self.parse_args()
        options = self.options

        input = options.input
        # Input graph
        input_path = os.path.join(input, input + '.gpk')
        self.g = nx.read_gpickle(input_path)
        if not self.g:
            raise Exception("null input file for input path %s" % input_path)
        # Output (reduced) graph.
        # Nodes: (lat, long) tuples w/ list of associated users & location strings
        # Edges: weight: number of links in this direction
        self.r = nx.DiGraph()

        conn = Connection()
        self.input_db = conn[self.options.input_db]
        self.input_coll = self.input_db[self.options.input_coll]

        print "now processing"
        self.reduce()
        print geo_stats(self.r)

        if options.write:
            geo_path = os.path.join(input, input + '.grg')
            nx.write_gpickle(self.r, geo_path)
开发者ID:RonansPrograms,项目名称:gothub,代码行数:26,代码来源:geo_import.py

示例3: mainmain

def mainmain():
    redirects = process_redirects(sys.argv[1])
    print "redirects", len(redirects)
    sys.stderr.write(sys.argv[1] + " processed\n")

    links = process_links(sys.argv[2], redirects)
    links_t = len(links)
    print "links", links_t
    sys.stderr.write(sys.argv[2] + " processed\n")

    G = networkx.Graph()

    articles_processed = 0

    for article in links:
        articles_processed = articles_processed + 1
        if (articles_processed % 100000) == 0:
            sys.stdout.write(
                "links processed="
                + str(articles_processed)
                + "/"
                + str(links_t)
                + " (%"
                + str(articles_processed * 100 / links_t)
                + ")\n"
            )
        if len(links[article]) < 6:
            continue
        G.add_node(article)
        for l in links[article]:
            if (l in links) and (article in links[l]):  # back link is also present
                G.add_node(l)
                G.add_edge(article, l)

    networkx.write_gpickle(G, sys.argv[3])
开发者ID:dennis714,项目名称:yurichev.com,代码行数:35,代码来源:WP_links_to_graph.py

示例4: reduceGraph

def reduceGraph(read_g, write_g, minEdgeWeight, minNodeDegree, Lp, Sp):
    """
    Simplify the undirected graph and then update the 3 undirected weight properties.
    :param read_g: is the graph pickle to read
    :param write_g: is the updated graph pickle to write
    :param minEdgeWeight: the original weight of each edge should be >= minEdgeWeight
    :param minNodeDegree: the degree of each node should be >= minNodeDegree. the degree here is G.degree(node), NOT G.degree(node,weight='weight)
    :return: None
    """
    G=nx.read_gpickle(read_g)
    print 'number of original nodes: ', nx.number_of_nodes(G)
    print 'number of original edges: ', nx.number_of_edges(G)

    for (u,v,w) in G.edges(data='weight'):
        if w < minEdgeWeight:
            G.remove_edge(u,v)

    for n in G.nodes():
        if G.degree(n)<minNodeDegree:
            G.remove_node(n)

    print 'number of new nodes: ', nx.number_of_nodes(G)
    print 'number of new edges: ', nx.number_of_edges(G)

    for (a, b, w) in G.edges_iter(data='weight'):
        unweight_allocation(G, a, b, w,Lp,Sp)

    print 'update weight ok'
    nx.write_gpickle(G, write_g)

    return
开发者ID:FengShi0705,项目名称:webapp,代码行数:31,代码来源:main.py

示例5: createBridge

def createBridge(numOfNodes, edgeProb, bridgeNodes):
	'''
	numOfNodes: Number of nodes in the clustered part of the Bridge Graph
	edgeProb: Probability of existance of an edge between any two vertices.
	bridgeNodes: Number of nodes in the bridge
	This function creates a Bridge Graph with 2 main clusters connected by a bridge.
	'''	
	print "Generating and Saving Bridge Network..."	
	G1 = nx.erdos_renyi_graph(2*numOfNodes + bridgeNodes, edgeProb) #Create an ER graph with number of vertices equal to twice the number of vertices in the clusters plus the number of bridge nodes.
	G = nx.Graph() #Create an empty graph so that it can be filled with the required components from G1
	G.add_edges_from(G1.subgraph(range(numOfNodes)).edges()) #Generate an induced subgraph of the nodes, ranging from 0 to numOfNodes, from G1 and add it to G
	G.add_edges_from(G1.subgraph(range(numOfNodes + bridgeNodes,2*numOfNodes + bridgeNodes)).edges()) #Generate an induced subgraph of the nodes, ranging from (numOfNodes + bridgeNodes) to (2*numOfNodes + bridgeNodes)

	A = random.randrange(numOfNodes) #Choose a random vertex from the first component
	B = random.randrange(numOfNodes + bridgeNodes,2*numOfNodes + bridgeNodes) #Choose a random vertex from the second component

	prev = A #creating a connection from A to B via the bridge nodes
	for i in range(numOfNodes, numOfNodes + bridgeNodes):
		G.add_edge(prev, i)
		prev = i
	G.add_edge(i, B)
	
	StrMap = {}
	for node in G.nodes():
		StrMap[node] = str(node)
	G = nx.convert.relabel_nodes(G,StrMap)
	filename = "BG_" + str(numOfNodes) + "_" + str(edgeProb) + "_" + str(bridgeNodes) + ".gpickle"
	nx.write_gpickle(G,filename)#generate a gpickle file of the learnt graph.
	print "Successfully written into " + filename
开发者ID:vijaym123,项目名称:Bidirectional-Search,代码行数:29,代码来源:AtoB.py

示例6: create_graph_df

def create_graph_df(vtask_paths, graphs_dir_out):
    """
    Creates a frame that maps sourcefiles to networkx digraphs in terms of DOT files
    :param source_path_list:
    :param dest_dir_path:
    :param relabel:
    :return:
    """
    if not isdir(graphs_dir_out):
        raise ValueError('Invalid destination directory.')
    data = []
    graphgen_times = []

    print('Writing graph representations of verification tasks to {}'.format(graphs_dir_out), flush=True)

    common_prefix = commonprefix(vtask_paths)
    for vtask in tqdm(vtask_paths):
        short_prefix = dirname(common_prefix)
        path = join(graphs_dir_out, vtask[len(short_prefix):][1:])

        if not os.path.exists(dirname(path)):
            os.makedirs(dirname(path))

        ret_path = path + '.pickle'

        # DEBUG
        if isfile(ret_path):
            data.append(ret_path)
            continue

        start_time = time.time()

        graph_path, node_labels_path, edge_types_path, edge_truth_path, node_depths_path \
            = _run_cpachecker(abspath(vtask))
        nx_digraph = nx.read_graphml(graph_path)

        node_labels = _read_node_labeling(node_labels_path)
        nx.set_node_attributes(nx_digraph, 'label', node_labels)

        edge_types = _read_edge_labeling(edge_types_path)
        parsed_edge_types = _parse_edge(edge_types)
        nx.set_edge_attributes(nx_digraph, 'type', parsed_edge_types)

        edge_truth = _read_edge_labeling(edge_truth_path)
        parsed_edge_truth = _parse_edge(edge_truth)
        nx.set_edge_attributes(nx_digraph, 'truth', parsed_edge_truth)

        node_depths = _read_node_labeling(node_depths_path)
        parsed_node_depths = _parse_node_depth(node_depths)
        nx.set_node_attributes(nx_digraph, 'depth', parsed_node_depths)

        assert not isfile(ret_path)
        assert node_labels and parsed_edge_types and parsed_edge_truth and parsed_node_depths
        nx.write_gpickle(nx_digraph, ret_path)
        data.append(ret_path)

        gg_time = time.time() - start_time
        graphgen_times.append(gg_time)

    return pd.DataFrame({'graph_representation': data}, index=vtask_paths), graphgen_times
开发者ID:zenscr,项目名称:PyPRSVT,代码行数:60,代码来源:graphs.py

示例7: load_data

def load_data():
    start = time.time()
    try:
        print("Loading data from /data pickles and hfd5 adj matrices")
        f = h5py.File('data/cosponsorship_data.hdf5', 'r')
        for chamber in ['house', 'senate']:
            for congress in SUPPORTED_CONGRESSES:
                adj_matrix_lookup[(chamber, congress)] = np.asarray(f[chamber + str(congress)])

                igraph_graph = igraph.load("data/" + chamber + str(congress) + "_igraph.pickle", format="pickle")
                igraph_graph_lookup[(chamber, congress, False)] = igraph_graph

                nx_graph = nx.read_gpickle("data/" + chamber + str(congress) + "_nx.pickle")
                nx_graph_lookup[(chamber, congress, False)] = nx_graph
    except IOError as e:
        print("Loading data from cosponsorship files")
        f = h5py.File("data/cosponsorship_data.hdf5", "w")
        for chamber in ['house', 'senate']:
            for congress in SUPPORTED_CONGRESSES:
                print("Starting %s %s" % (str(congress), chamber))
                adj_matrix = load_adjacency_matrices(congress, chamber)
                data = f.create_dataset(chamber + str(congress), adj_matrix.shape, dtype='f')
                data[0: len(data)] = adj_matrix

                # igraph
                get_cosponsorship_graph(congress, chamber, False).save("data/" + chamber + str(congress) + "_igraph.pickle", "pickle")
                # networkx
                nx.write_gpickle(get_cosponsorship_graph_nx(congress, chamber, False), "data/" + chamber + str(congress) + "_nx.pickle")

                print("Done with %s %s" % (str(congress), chamber))
    print("Data loaded in %d seconds" % (time.time() - start))
开发者ID:TomWerner,项目名称:congress_cosponsorship,代码行数:31,代码来源:project.py

示例8: save_celltype_graph

    def save_celltype_graph(self, filename="celltype_conn.gml", format="gml"):
        """
        Save the celltype-to-celltype connectivity information in a file.
        
        filename -- path of the file to be saved.

        format -- format to save in. Using GML as GraphML support is
        not complete in NetworkX.  

        """
        start = datetime.now()
        if format == "gml":
            nx.write_gml(self.__celltype_graph, filename)
        elif format == "yaml":
            nx.write_yaml(self.__celltype_graph, filename)
        elif format == "graphml":
            nx.write_graphml(self.__celltype_graph, filename)
        elif format == "edgelist":
            nx.write_edgelist(self.__celltype_graph, filename)
        elif format == "pickle":
            nx.write_gpickle(self.__celltype_graph, filename)
        else:
            raise Exception("Supported formats: gml, graphml, yaml. Received: %s" % (format))
        end = datetime.now()
        delta = end - start
        config.BENCHMARK_LOGGER.info(
            "Saved celltype_graph in file %s of format %s in %g s"
            % (filename, format, delta.seconds + delta.microseconds * 1e-6)
        )
        print "Saved celltype connectivity graph in", filename
开发者ID:BhallaLab,项目名称:thalamocortical,代码行数:30,代码来源:traubnet.py

示例9: graph_preprocessing_with_counts

def graph_preprocessing_with_counts(G_input=None, save_file=None):

    if not G_input:
        graph_file = os.path.join(work_dir, "adj_graph.p")
        G = nx.read_gpickle(graph_file)
    else:
        G = G_input.copy()

    print "Raw graph size:", G.size()
    print "Raw graph nodes", G.number_of_nodes()

    profile2prob = {l.split()[0]: float(l.split()[1]) for l in open(os.path.join(work_dir, 'profile_weight.txt'))}

    for edge in G.edges(data=True):
        nodes = edge[:2]
        _weight = edge[2]['weight']
        _count = edge[2]['count']
        
        if _count < 3:
            G.remove_edge(*nodes)

    print "Pre-processed graph size", G.size()
    print "Pre-processed graph nodes", G.number_of_nodes()

    G.remove_nodes_from(nx.isolates(G))

    print "Pre-processed graph size", G.size()
    print "Pre-processed graph nodes", G.number_of_nodes()
    
    if save_file:
        print "Saving to", save_file
        nx.write_gpickle(G,save_file)

    return G
开发者ID:kyrgyzbala,项目名称:NewSystems,代码行数:34,代码来源:graph_analysis.py

示例10: store_graph

def store_graph(graph,name=None):
	filename=datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')+"_Network.gpickle"
	if name != None:
		filename=name+".gpickle"
	nx.write_gpickle(graph,filename)
	print("Finish storing the graph"+" "+filename)
	return filename
开发者ID:nkfly,项目名称:sna-final,代码行数:7,代码来源:download1.py

示例11: createScaleFreeNetwork

def createScaleFreeNetwork(numOfNodes, degree):
	'''
	numOfNodes: The number of nodes that the scale free network should have
	degree: The degree of the Scale Free Network
	This function creates a Scale Free Network containing 'numOfNodes' nodes, each of degree 'degree'
	It generates the required graph and saves it in a file. It runs the Reinforcement Algorithm to create a weightMatrix and an ordering of the vertices based on their importance by Flagging.
	'''
	global reinforce_time
	G = nx.barabasi_albert_graph(numOfNodes, degree) #Create a Scale Free Network of the given number of nodes and degree
	StrMap = {}
	for node in G.nodes():
		StrMap[node] = str(node)
	G = nx.convert.relabel_nodes(G,StrMap)

	print "Undergoing Machine Learning..."

	start = time.time()
	H = reinforce(G) #Enforce Machine Learning to generate a gml file of the learnt graph.
	finish = time.time()
	reinforce_time = finish - start

	print "Machine Learning Completed..."
	filename = "SFN_" + str(numOfNodes) + "_" + str(degree) + '.gpickle' 
	nx.write_gpickle(H,filename)#generate a gpickle file of the learnt graph.
	print "Learnt graph Successfully written into " + filename
开发者ID:vijaym123,项目名称:Human-Navigation-Algorithm,代码行数:25,代码来源:AtoB.py

示例12: handle_by_file

def handle_by_file(out_dir, tweet_file, country, net_func=comprehend_network):
    try:
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)

        if net_func == comprehend_network:
            net_type = "comprehend"
        elif net_func == user2user_network:
            net_type = "user2user"
        elif net_func == content_based_network:
            net_type = "content"
        elif net_func == entity_network:
            net_type = "entity"
        elif net_func == entity_corr_network:
            net_type = "entity_corr"
        else:
            net_type = "normal"

        net = net_func(tweet_file)
        net.graph["country"] = country
        g_date = re.search(r'\d{4}-\d{2}-\d{2}', tweet_file).group()
        net.graph["date"] = g_date
        out_file = os.path.join(out_dir,
                                "graph_%s_%s" % (net_type,
                                                 tweet_file.split(os.sep)[-1]))
        nx.write_gpickle(net, out_file + ".gpickle")
        nx.write_graphml(net, out_file + ".graphml")
    except Exception, e:
        print "Error Encoutered: %s, \n %s" \
            % (tweet_file, sys.exc_info()[0]), e
开发者ID:Tskatom,项目名称:twitter_finance,代码行数:30,代码来源:tweet_net_graph.py

示例13: fit_forestFire_mod

def fit_forestFire_mod(graphSize, graphID, dkPath, original2k, resultPath):
    """
    Runs synthetic graph tests for various 'p' values (burn rate).
    """
    outfile = open(resultPath + graphID + '_ff_dkDistances.txt', 'w')
    
    p = 0.01
    while p < 1.0:
        print 'Running modified Forest Fire with parameters: n = ', graphSize, ' p = ', p
        
        newFile = graphID + '_ff_' + str(p)

        # Create synthetic graph
        syntheticGraph = sm.forestFire_mod(graphSize, p)

        # Write pickle, edge list, and 2k distro to file
        print 'Writing pickle and calculating dK-2...\n'
        nx.write_gpickle(syntheticGraph, resultPath + newFile + '.pickle')
        getdk2(syntheticGraph, newFile, dkPath, resultPath)

        # Find distance between the dK-2 distributions
        dkDistance = tk.get_2k_distance(original2k, resultPath + newFile + '_target.2k')
        outfile.write(str(dkDistance) + '\tp = ' + str(p) + '\n')
        outfile.flush()

        p += 0.01

    outfile.close()
开发者ID:PaintScratcher,项目名称:perfrunner,代码行数:28,代码来源:fitModel.py

示例14: main

def main(**kwargs):
    cells = kwargs["cells"]
    frames = kwargs["frames"]
    if isinstance(cells, str):
        cells = np.load(cells)
    if isinstance(frames, str):
        frames = np.load(frames)

    logger.info("Creating correlation graph")
    N = int(cells.max())
    for i in range(1, N):
        logger.info("\tDone %d out of %d" % (i, N))
        indices = list(zip(*np.where(cells == i)))
        if len(indices) < 2:
            continue
        pixals = []
        for y, x in indices:
            pixals.append(frames[x, y, :])
        pixals = np.mean(pixals, axis=0)
        g_.add_node(i, timeseries=pixals, indices=indices)

    g_.graph["shape"] = frames[:, :, 0].shape
    create_correlate_graph(g_)
    outfile = kwargs.get("output", False) or "correlation_graph.pickle"
    logger.info("Writing pickle of graph to %s" % outfile)
    nx.write_gpickle(g_, outfile)
    logger.info("Graph pickle is saved to %s" % outfile)
开发者ID:dilawar,项目名称:roi_locator,代码行数:27,代码来源:generate_correlation_graph.py

示例15: create_nodes

def create_nodes(paths, args):
    """ creates nodes
    Parameters
    ----------
    paths.node_file       : file
    args.fasta_file       : file
    """

    # read in fasta to dictionary.
    seqs = io.load_fasta(args.contig_file)

    # create graph.
    G = nx.MultiGraph()

    # add nodes to graph.
    for name, seq in seqs.items():

        # skip split names.
        tmp = name.split(" ")
        name = tmp[0]

        # add node.
        G.add_node(name, {'seq':seq, 'width':len(seq), 'cov':0})

    # write to disk.
    nx.write_gpickle(G, paths.node_file)
开发者ID:jim-bo,项目名称:silp2,代码行数:26,代码来源:nodes.py


注:本文中的networkx.write_gpickle函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。