Python networkx.read_edgelist() Examples
The following are 30
code examples of networkx.read_edgelist().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: remove_cycle_edges_by_minimum_feedback_arc_set_greedy.py From breaking_cycles_in_noisy_hierarchies with BSD 3-Clause "New" or "Revised" License | 6 votes |
def remove_cycle_edges_by_mfas(graph_file): g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) from remove_self_loops import remove_self_loops_from_graph self_loops = remove_self_loops_from_graph(g) scc_nodes,_,_,_ = scc_nodes_edges(g) degree_dict = get_nodes_degree_dict(g,scc_nodes) sccs = get_big_sccs(g) if len(sccs) == 0: print("After removal of self loop edgs: %s" % nx.is_directed_acyclic_graph(g)) return self_loops edges_to_be_removed = [] import timeit t1 = timeit.default_timer() greedy_local_heuristic(sccs,degree_dict,edges_to_be_removed) t2 = timeit.default_timer() print("mfas time usage: %0.4f s" % (t2 - t1)) edges_to_be_removed = list(set(edges_to_be_removed)) g.remove_edges_from(edges_to_be_removed) edges_to_be_removed += self_loops edges_to_be_removed_file = graph_file[:len(graph_file)-6] + "_removed_by_mfas.edges" write_pairs_to_file(edges_to_be_removed,edges_to_be_removed_file) return edges_to_be_removed
Example #2
Source File: test_edgelist.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_latin1(self): G = nx.Graph() try: # Python 3.x blurb = chr(1245) # just to trigger the exception name1 = 'Bj' + chr(246) + 'rk' name2 = chr(220) + 'ber' except ValueError: # Python 2.6+ name1 = 'Bj' + unichr(246) + 'rk' name2 = unichr(220) + 'ber' G.add_edge(name1, 'Radiohead', **{name2: 3}) fd, fname = tempfile.mkstemp() nx.write_edgelist(G, fname, encoding='latin-1') H = nx.read_edgelist(fname, encoding='latin-1') assert_graphs_equal(G, H) os.close(fd) os.unlink(fname)
Example #3
Source File: proNE.py From ProNE with MIT License | 6 votes |
def __init__(self, graph_file, emb_file1, emb_file2, dimension): self.graph = graph_file self.emb1 = emb_file1 self.emb2 = emb_file2 self.dimension = dimension self.G = nx.read_edgelist(self.graph, nodetype=int, create_using=nx.DiGraph()) self.G = self.G.to_undirected() self.node_number = self.G.number_of_nodes() matrix0 = scipy.sparse.lil_matrix((self.node_number, self.node_number)) for e in self.G.edges(): if e[0] != e[1]: matrix0[e[0], e[1]] = 1 matrix0[e[1], e[0]] = 1 self.matrix0 = scipy.sparse.csr_matrix(matrix0) print(matrix0.shape)
Example #4
Source File: utils.py From clusternet with MIT License | 6 votes |
def load_nofeatures(dataset, version, n = None): ''' Loads a dataset that is just an edgelist, creating sparse one-hot features. n: total number of nodes in the graph. This is the number of nodes present in the edgelist unless otherwise specified ''' # g = nx.read_edgelist('data/{}/{}{}.txt'.format(dataset, dataset, version)) # g = nx.convert_node_labels_to_integers(g) # edges = np.array([(x[0], x[1]) for x in nx.to_edgelist(g)]) edges = np.loadtxt('data/{}/{}{}.cites'.format(dataset, dataset, version), dtype=np.int) if n == None: n = int(edges.max()) + 1 adj = make_normalized_adj(torch.tensor(edges).long(), n) features = torch.eye(n).to_sparse() return adj, features, None
Example #5
Source File: introduce_cycles_to_DAG.py From breaking_cycles_in_noisy_hierarchies with BSD 3-Clause "New" or "Revised" License | 6 votes |
def introduce_cycles_2_DAG(graph_file,num_extra_edges,path_length): if path_length <= 0: print("no constraints on path length") else: print("path length: %d" % path_length) g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) extra_edges = introduce_cycles(g,num_extra_edges,path_length = path_length) extra_edges_file = graph_file[:len(graph_file)-6] + "_extra_" + str(num_extra_edges) + "_path_len_" + str(path_length) + ".edges" graph_with_extra_edges_file = graph_file[:len(graph_file)-6] + "_graph_w_extra_" + str(num_extra_edges) + "_path_len_" + str(path_length) + ".edges" print("extra edges saved in: %s" % extra_edges_file) print("graph with extra edges saved in: %s" % graph_with_extra_edges_file) from file_io import write_pairs_to_file write_pairs_to_file(extra_edges,extra_edges_file) write_pairs_to_file(extra_edges + g.edges(),graph_with_extra_edges_file) return (extra_edges_file,graph_with_extra_edges_file)
Example #6
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_latin1(self): G = nx.Graph() try: # Python 3.x blurb = chr(1245) # just to trigger the exception name1 = 'Bj' + chr(246) + 'rk' name2 = chr(220) + 'ber' except ValueError: # Python 2.6+ name1 = 'Bj' + unichr(246) + 'rk' name2 = unichr(220) + 'ber' G.add_edge(name1, 'Radiohead', **{name2: 3}) fd, fname = tempfile.mkstemp() nx.write_edgelist(G, fname, encoding = 'latin-1') H = nx.read_edgelist(fname, encoding = 'latin-1') assert_graphs_equal(G, H) os.close(fd) os.unlink(fname)
Example #7
Source File: ExperimentParser.py From ndlib with BSD 2-Clause "Simplified" License | 6 votes |
def __network_loading(self, desc): compartments = ['LOAD_NETWORK', 'FROM'] if len(desc) > 1: raise ValueError("Unsupported description") stm = desc[0].split(" ") if len(stm) != 4 or stm[0] not in compartments or stm[2] not in compartments: raise ValueError("Experiment description malformed (wrong network loading statement): check your syntax") self.__net_name = stm[1] filename = stm[3] if os.path.isfile(filename): return "%s = nx.read_edgelist('%s')\n" % (self.__net_name, filename) else: raise ValueError("Experiment description malformed (file not existing): check your syntax")
Example #8
Source File: test_edgelist.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_latin1(self): G = nx.Graph() try: # Python 3.x blurb = chr(1245) # just to trigger the exception name1 = 'Bj' + chr(246) + 'rk' name2 = chr(220) + 'ber' except ValueError: # Python 2.6+ name1 = 'Bj' + unichr(246) + 'rk' name2 = unichr(220) + 'ber' G.add_edge(name1, 'Radiohead', attr_dict={name2: 3}) fd, fname = tempfile.mkstemp() nx.write_edgelist(G, fname, encoding = 'latin-1') H = nx.read_edgelist(fname, encoding = 'latin-1') assert_graphs_equal(G, H) os.close(fd) os.unlink(fname)
Example #9
Source File: edge2vec.py From edge2vec with BSD 3-Clause "New" or "Revised" License | 6 votes |
def read_graph(edgeList,weighted=False, directed=False): ''' Reads the input network in networkx. ''' if weighted: G = nx.read_edgelist(edgeList, nodetype=str, data=(('type',int),('weight',float),('id',int)), create_using=nx.DiGraph()) else: G = nx.read_edgelist(edgeList, nodetype=str,data=(('type',int),('id',int)), create_using=nx.DiGraph()) for edge in G.edges(): G[edge[0]][edge[1]]['weight'] = 1.0 if not directed: G = G.to_undirected() # print (G.edges(data = True)) return G
Example #10
Source File: utility.py From ohmnet with MIT License | 6 votes |
def read_net(fname, weighted, directed, log): if weighted: G = nx.read_edgelist(inodetype=int, data=(('weight', float),), create_using=nx.DiGraph()) else: G = nx.read_edgelist(fname, nodetype=int, create_using=nx.DiGraph()) for edge in G.edges(): G[edge[0]][edge[1]]['weight'] = 1 if not directed: G = G.to_undirected() log.info('N: %d E: %d' % (G.number_of_nodes(), G.number_of_edges())) log.info('CC: %d' % nx.number_connected_components(G)) giant = max(nx.connected_component_subgraphs(G), key=len) log.info('N: %d E: %d' % (giant.number_of_nodes(), giant.number_of_edges())) return giant
Example #11
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_unicode(self): G = nx.Graph() try: # Python 3.x name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) except ValueError: # Python 2.6+ name1 = unichr(2344) + unichr(123) + unichr(6543) name2 = unichr(5543) + unichr(1543) + unichr(324) G.add_edge(name1, 'Radiohead', **{name2: 3}) fd, fname = tempfile.mkstemp() nx.write_edgelist(G, fname) H = nx.read_edgelist(fname) assert_graphs_equal(G, H) os.close(fd) os.unlink(fname)
Example #12
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_edgelist_digraph(self): G=self.DG (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,create_using=nx.DiGraph()) H2=nx.read_edgelist(fname,create_using=nx.DiGraph()) assert_not_equal(H,H2) # they should be different graphs G.remove_node('g') # isolated nodes are not written in edgelist assert_nodes_equal(list(H), list(G)) assert_edges_equal(list(H.edges()), list(G.edges())) os.close(fd) os.unlink(fname)
Example #13
Source File: test_edgelist.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_edgelist_multigraph(self): G = self.XG (fd, fname) = tempfile.mkstemp() nx.write_edgelist(G, fname) H = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph()) H2 = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph()) assert_not_equal(H, H2) # they should be different graphs assert_nodes_equal(list(H), list(G)) assert_edges_equal(list(H.edges()), list(G.edges())) os.close(fd) os.unlink(fname)
Example #14
Source File: test_edgelist.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_edgelist_multidigraph(self): G = self.XDG (fd, fname) = tempfile.mkstemp() nx.write_edgelist(G, fname) H = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiDiGraph()) H2 = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiDiGraph()) assert_not_equal(H, H2) # they should be different graphs assert_nodes_equal(list(H), list(G)) assert_edges_equal(list(H.edges()), list(G.edges())) os.close(fd) os.unlink(fname)
Example #15
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_edgelist_integers(self): G=nx.convert_node_labels_to_integers(self.G) (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,nodetype=int) # isolated nodes are not written in edgelist G.remove_nodes_from(list(nx.isolates(G))) assert_nodes_equal(list(H), list(G)) assert_edges_equal(list(H.edges()), list(G.edges())) os.close(fd) os.unlink(fname)
Example #16
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_edgelist_digraph(self): G=self.DG (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,create_using=nx.DiGraph()) G.remove_node('g') # isolated nodes are not written in edgelist H2=nx.read_edgelist(fname,create_using=nx.DiGraph()) assert_not_equal(H,H2) # they should be different graphs assert_nodes_equal(list(H), list(G)) assert_edges_equal(list(H.edges()), list(G.edges())) os.close(fd) os.unlink(fname)
Example #17
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_edgelist_multigraph(self): G=self.XG (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph()) H2=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph()) assert_not_equal(H,H2) # they should be different graphs assert_nodes_equal(list(H), list(G)) assert_edges_equal(list(H.edges()), list(G.edges())) os.close(fd) os.unlink(fname)
Example #18
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_edgelist_multidigraph(self): G=self.XDG (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiDiGraph()) H2=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiDiGraph()) assert_not_equal(H,H2) # they should be different graphs assert_nodes_equal(list(H), list(G)) assert_edges_equal(list(H.edges()), list(G.edges())) os.close(fd) os.unlink(fname)
Example #19
Source File: test_edgelist.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_read_edgelist_3(self): s = b"""\ # comment line 1 2 {'weight':2.0} # comment line 2 3 {'weight':3.0} """ bytesIO = io.BytesIO(s) G = nx.read_edgelist(bytesIO,nodetype=int,data=False) assert_edges_equal(G.edges(),[(1,2),(2,3)]) bytesIO = io.BytesIO(s) G = nx.read_edgelist(bytesIO,nodetype=int,data=True) assert_edges_equal(G.edges(data=True), [(1,2,{'weight':2.0}),(2,3,{'weight':3.0})])
Example #20
Source File: test_edgelist.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_edgelist_digraph(self): G=self.DG (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,create_using=nx.DiGraph()) G.remove_node('g') # isolated nodes are not written in edgelist H2=nx.read_edgelist(fname,create_using=nx.DiGraph()) assert_not_equal(H,H2) # they should be different graphs assert_nodes_equal(H.nodes(),G.nodes()) assert_edges_equal(H.edges(),G.edges()) os.close(fd) os.unlink(fname)
Example #21
Source File: test_edgelist.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_unicode(self): G = nx.Graph() try: # Python 3.x name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) except ValueError: # Python 2.6+ name1 = unichr(2344) + unichr(123) + unichr(6543) name2 = unichr(5543) + unichr(1543) + unichr(324) G.add_edge(name1, 'Radiohead', **{name2: 3}) fd, fname = tempfile.mkstemp() nx.write_edgelist(G, fname) H = nx.read_edgelist(fname) assert_graphs_equal(G, H) os.close(fd) os.unlink(fname)
Example #22
Source File: test_edgelist.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_edgelist_3(self): s = b"""\ # comment line 1 2 {'weight':2.0} # comment line 2 3 {'weight':3.0} """ bytesIO = io.BytesIO(s) G = nx.read_edgelist(bytesIO, nodetype=int, data=False) assert_edges_equal(G.edges(), [(1, 2), (2, 3)]) bytesIO = io.BytesIO(s) G = nx.read_edgelist(bytesIO, nodetype=int, data=True) assert_edges_equal(G.edges(data=True), [(1, 2, {'weight': 2.0}), (2, 3, {'weight': 3.0})])
Example #23
Source File: test_edgelist.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_edgelist_2(self): s = b"""\ # comment line 1 2 2.0 # comment line 2 3 3.0 """ bytesIO = io.BytesIO(s) G = nx.read_edgelist(bytesIO, nodetype=int, data=False) assert_edges_equal(G.edges(), [(1, 2), (2, 3)]) bytesIO = io.BytesIO(s) G = nx.read_weighted_edgelist(bytesIO, nodetype=int) assert_edges_equal(G.edges(data=True), [(1, 2, {'weight': 2.0}), (2, 3, {'weight': 3.0})])
Example #24
Source File: test_edgelist.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_edgelist_1(self): s = b"""\ # comment line 1 2 # comment line 2 3 """ bytesIO = io.BytesIO(s) G = nx.read_edgelist(bytesIO, nodetype=int) assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
Example #25
Source File: utils.py From BioNEV with MIT License | 5 votes |
def split_train_test_graph(input_edgelist, seed, testing_ratio=0.2, weighted=False): if (weighted): G = nx.read_weighted_edgelist(input_edgelist) else: G = nx.read_edgelist(input_edgelist) node_num1, edge_num1 = len(G.nodes), len(G.edges) print('Original Graph: nodes:', node_num1, 'edges:', edge_num1) testing_edges_num = int(len(G.edges) * testing_ratio) random.seed(seed) testing_pos_edges = random.sample(G.edges, testing_edges_num) G_train = copy.deepcopy(G) for edge in testing_pos_edges: node_u, node_v = edge if (G_train.degree(node_u) > 1 and G_train.degree(node_v) > 1): G_train.remove_edge(node_u, node_v) G_train.remove_nodes_from(nx.isolates(G_train)) node_num2, edge_num2 = len(G_train.nodes), len(G_train.edges) assert node_num1 == node_num2 train_graph_filename = 'graph_train.edgelist' if weighted: nx.write_edgelist(G_train, train_graph_filename, data=['weight']) else: nx.write_edgelist(G_train, train_graph_filename, data=False) node_num1, edge_num1 = len(G_train.nodes), len(G_train.edges) print('Training Graph: nodes:', node_num1, 'edges:', edge_num1) return G, G_train, testing_pos_edges, train_graph_filename
Example #26
Source File: utils.py From BioNEV with MIT License | 5 votes |
def read_for_SVD(filename, weighted=False): if weighted: G = nx.read_weighted_edgelist(filename) else: G = nx.read_edgelist(filename) return G
Example #27
Source File: utils.py From BioNEV with MIT License | 5 votes |
def read_for_OpenNE(filename, weighted=False): G = og.Graph() print("Loading training graph for learning embedding...") G.read_edgelist(filename=filename, weighted=weighted) print("Graph Loaded...") return G
Example #28
Source File: test_edgelist.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_edgelist_multidigraph(self): G=self.XDG (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiDiGraph()) H2=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiDiGraph()) assert_not_equal(H,H2) # they should be different graphs assert_nodes_equal(H.nodes(),G.nodes()) assert_edges_equal(H.edges(),G.edges()) os.close(fd) os.unlink(fname)
Example #29
Source File: test_edgelist.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_edgelist_multigraph(self): G=self.XG (fd,fname)=tempfile.mkstemp() nx.write_edgelist(G,fname) H=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph()) H2=nx.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph()) assert_not_equal(H,H2) # they should be different graphs assert_nodes_equal(H.nodes(),G.nodes()) assert_edges_equal(H.edges(),G.edges()) os.close(fd) os.unlink(fname)
Example #30
Source File: test_edgelist.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_unicode(self): G = nx.Graph() try: # Python 3.x name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) except ValueError: # Python 2.6+ name1 = unichr(2344) + unichr(123) + unichr(6543) name2 = unichr(5543) + unichr(1543) + unichr(324) G.add_edge(name1, 'Radiohead', attr_dict={name2: 3}) fd, fname = tempfile.mkstemp() nx.write_edgelist(G, fname) H = nx.read_edgelist(fname) assert_graphs_equal(G, H) os.close(fd) os.unlink(fname)