Python networkx.edges() Examples
The following are 30
code examples of networkx.edges().
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: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_non_edges(self): # All possible edges exist graph = nx.complete_graph(5) nedges = list(nx.non_edges(graph)) assert_equal(len(nedges), 0) graph = nx.path_graph(4) expected = [(0, 2), (0, 3), (1, 3)] nedges = list(nx.non_edges(graph)) for (u, v) in expected: assert_true((u, v) in nedges or (v, u) in nedges) graph = nx.star_graph(4) expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] nedges = list(nx.non_edges(graph)) for (u, v) in expected: assert_true((u, v) in nedges or (v, u) in nedges) # Directed graphs graph = nx.DiGraph() graph.add_edges_from([(0, 2), (2, 0), (2, 1)]) expected = [(0, 1), (1, 0), (1, 2)] nedges = list(nx.non_edges(graph)) for e in expected: assert_true(e in nedges)
Example #2
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_non_edges(self): # All possible edges exist graph = nx.complete_graph(5) nedges = list(nx.non_edges(graph)) assert_equal(len(nedges), 0) graph = nx.path_graph(4) expected = [(0, 2), (0, 3), (1, 3)] nedges = list(nx.non_edges(graph)) for (u, v) in expected: assert_true((u, v) in nedges or (v, u) in nedges) graph = nx.star_graph(4) expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] nedges = list(nx.non_edges(graph)) for (u, v) in expected: assert_true((u, v) in nedges or (v, u) in nedges) # Directed graphs graph = nx.DiGraph() graph.add_edges_from([(0, 2), (2, 0), (2, 1)]) expected = [(0, 1), (1, 0), (1, 2)] nedges = list(nx.non_edges(graph)) for e in expected: assert_true(e in nedges)
Example #3
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_info_digraph(self): G = nx.DiGraph(name='path_graph(5)') nx.add_path(G, [0, 1, 2, 3, 4]) info = nx.info(G) expected_graph_info = '\n'.join(['Name: path_graph(5)', 'Type: DiGraph', 'Number of nodes: 5', 'Number of edges: 4', 'Average in degree: 0.8000', 'Average out degree: 0.8000']) assert_equal(info, expected_graph_info) info = nx.info(G, n=1) expected_node_info = '\n'.join( ['Node 1 has the following properties:', 'Degree: 2', 'Neighbors: 2']) assert_equal(info, expected_node_info) assert_raises(nx.NetworkXError, nx.info, G, n=-1)
Example #4
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_info_digraph(self): G = nx.DiGraph(name='path_graph(5)') nx.add_path(G, [0, 1, 2, 3, 4]) info = nx.info(G) expected_graph_info = '\n'.join(['Name: path_graph(5)', 'Type: DiGraph', 'Number of nodes: 5', 'Number of edges: 4', 'Average in degree: 0.8000', 'Average out degree: 0.8000']) assert_equal(info, expected_graph_info) info = nx.info(G, n=1) expected_node_info = '\n'.join( ['Node 1 has the following properties:', 'Degree: 2', 'Neighbors: 2']) assert_equal(info, expected_node_info) assert_raises(nx.NetworkXError, nx.info, G, n=-1)
Example #5
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_add_cycle(self): G = self.G.copy() nlist = [12, 13, 14, 15] oklists = [[(12, 13), (12, 15), (13, 14), (14, 15)], [(12, 13), (13, 14), (14, 15), (15, 12)]] nx.add_cycle(G, nlist) assert_true(sorted(G.edges(nlist)) in oklists) G = self.G.copy() oklists = [[(12, 13, {'weight': 1.}), (12, 15, {'weight': 1.}), (13, 14, {'weight': 1.}), (14, 15, {'weight': 1.})], [(12, 13, {'weight': 1.}), (13, 14, {'weight': 1.}), (14, 15, {'weight': 1.}), (15, 12, {'weight': 1.})]] nx.add_cycle(G, nlist, weight=1.0) assert_true(sorted(G.edges(nlist, data=True)) in oklists)
Example #6
Source File: calculation_helper.py From GEMSEC with GNU General Public License v3.0 | 6 votes |
def overlap_generator(args, graph): """ Function to generate weight for all of the edges. """ if args.overlap_weighting == "normalized_overlap": overlap_weighter = normalized_overlap elif args.overlap_weighting == "overlap": overlap_weighter = overlap elif args.overlap_weighting == "min_norm": overlap_weighter = min_norm else: overlap_weighter = unit print(" ") print("Weight calculation started.") print(" ") edges = nx.edges(graph) weights = {e: overlap_weighter(graph, e[0], e[1]) for e in tqdm(edges)} weights_prime = {(e[1], e[0]): value for e, value in weights.items()} weights.update(weights_prime) print(" ") return weights
Example #7
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_info(self): G=nx.path_graph(5) info=nx.info(G) expected_graph_info='\n'.join(['Name: path_graph(5)', 'Type: Graph', 'Number of nodes: 5', 'Number of edges: 4', 'Average degree: 1.6000']) assert_equal(info,expected_graph_info) info=nx.info(G,n=1) expected_node_info='\n'.join( ['Node 1 has the following properties:', 'Degree: 2', 'Neighbors: 0 2']) assert_equal(info,expected_node_info)
Example #8
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_info_digraph(self): G=nx.DiGraph(name='path_graph(5)') G.add_path([0,1,2,3,4]) info=nx.info(G) expected_graph_info='\n'.join(['Name: path_graph(5)', 'Type: DiGraph', 'Number of nodes: 5', 'Number of edges: 4', 'Average in degree: 0.8000', 'Average out degree: 0.8000']) assert_equal(info,expected_graph_info) info=nx.info(G,n=1) expected_node_info='\n'.join( ['Node 1 has the following properties:', 'Degree: 2', 'Neighbors: 2']) assert_equal(info,expected_node_info) assert_raises(nx.NetworkXError,nx.info,G,n=-1)
Example #9
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_non_edges(self): # All possible edges exist graph = nx.complete_graph(5) nedges = list(nx.non_edges(graph)) assert_equal(len(nedges), 0) graph = nx.path_graph(4) expected = [(0, 2), (0, 3), (1, 3)] nedges = list(nx.non_edges(graph)) for (u, v) in expected: assert_true( (u, v) in nedges or (v, u) in nedges ) graph = nx.star_graph(4) expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] nedges = list(nx.non_edges(graph)) for (u, v) in expected: assert_true( (u, v) in nedges or (v, u) in nedges ) # Directed graphs graph = nx.DiGraph() graph.add_edges_from([(0, 2), (2, 0), (2, 1)]) expected = [(0, 1), (1, 0), (1, 2)] nedges = list(nx.non_edges(graph)) for e in expected: assert_true(e in nedges)
Example #10
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_set_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph()] for G in graphs: G = nx.path_graph(3, create_using=G) # Test single value attr = 'hello' vals = 3 nx.set_edge_attributes(G, attr, vals) assert_equal(G[0][1][attr], vals) assert_equal(G[1][2][attr], vals) # Test multiple values attr = 'hi' edges = [(0,1), (1,2)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, attr, vals) assert_equal(G[0][1][attr], 0) assert_equal(G[1][2][attr], 1)
Example #11
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_add_star(self): G = self.G.copy() nlist = [12, 13, 14, 15] nx.add_star(G, nlist) assert_edges_equal(G.edges(nlist), [(12, 13), (12, 14), (12, 15)]) G = self.G.copy() nx.add_star(G, nlist, weight=2.0) assert_edges_equal(G.edges(nlist, data=True), [(12, 13, {'weight': 2.}), (12, 14, {'weight': 2.}), (12, 15, {'weight': 2.})]) G = self.G.copy() nlist = [12] nx.add_star(G, nlist) assert_nodes_equal(G, list(self.G) + nlist) G = self.G.copy() nlist = [] nx.add_star(G, nlist) assert_nodes_equal(G.nodes, self.Gnodes) assert_edges_equal(G.edges, self.G.edges)
Example #12
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_set_edge_attributes_multi(): graphs = [nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], vals) assert_equal(G[1][2][0][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1, 0), (1, 2, 0)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], 0) assert_equal(G[1][2][0][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1, 0)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1][0]['hi'], 0) assert_equal(G[0][1][0]['hello'], 200) assert_equal(G[1][2][0], {})
Example #13
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_add_cycle(self): G = self.G.copy() nlist = [12, 13, 14, 15] oklists = [[(12, 13), (12, 15), (13, 14), (14, 15)], [(12, 13), (13, 14), (14, 15), (15, 12)]] nx.add_cycle(G, nlist) assert_true(sorted(G.edges(nlist)) in oklists) G = self.G.copy() oklists = [[(12, 13, {'weight': 1.}), (12, 15, {'weight': 1.}), (13, 14, {'weight': 1.}), (14, 15, {'weight': 1.})], [(12, 13, {'weight': 1.}), (13, 14, {'weight': 1.}), (14, 15, {'weight': 1.}), (15, 12, {'weight': 1.})]] nx.add_cycle(G, nlist, weight=1.0) assert_true(sorted(G.edges(nlist, data=True)) in oklists) G = self.G.copy() nlist = [12] nx.add_cycle(G, nlist) assert_nodes_equal(G, list(self.G) + nlist) G = self.G.copy() nlist = [] nx.add_cycle(G, nlist) assert_nodes_equal(G.nodes, self.Gnodes) assert_edges_equal(G.edges, self.G.edges)
Example #14
Source File: refex.py From RolX with GNU General Public License v3.0 | 5 votes |
def dataset_reader(path): edges = pd.read_csv(path).values.tolist() graph = nx.from_edgelist(edges) return graph
Example #15
Source File: refex.py From RolX with GNU General Public License v3.0 | 5 votes |
def basic_stat_extractor(self): self.base_features = [] self.sub_graph_container = {} for node in tqdm(range(0,len(self.nodes))): sub_g, overall_counts, nebs = inducer(self.graph, node) in_counts = len(nx.edges(sub_g)) self.sub_graph_container[node] = nebs deg = nx.degree(sub_g, node) trans = nx.clustering(sub_g, node) self.base_features.append([in_counts, overall_counts, float(in_counts)/float(overall_counts), float(overall_counts - in_counts)/float(overall_counts),deg, trans]) self.features = {} self.features[0] = np.array(self.base_features) print("") del self.base_features
Example #16
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_edges(self): assert_edges_equal(self.G.edges(), list(nx.edges(self.G))) assert_equal(sorted(self.DG.edges()), sorted(nx.edges(self.DG))) assert_edges_equal(self.G.edges(nbunch=[0, 1, 3]), list(nx.edges(self.G, nbunch=[0, 1, 3]))) assert_equal(sorted(self.DG.edges(nbunch=[0, 1, 3])), sorted(nx.edges(self.DG, nbunch=[0, 1, 3])))
Example #17
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_add_star(self): G = self.G.copy() nlist = [12, 13, 14, 15] nx.add_star(G, nlist) assert_edges_equal(G.edges(nlist), [(12, 13), (12, 14), (12, 15)]) G = self.G.copy() nx.add_star(G, nlist, weight=2.0) assert_edges_equal(G.edges(nlist, data=True), [(12, 13, {'weight': 2.}), (12, 14, {'weight': 2.}), (12, 15, {'weight': 2.})])
Example #18
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_add_path(self): G = self.G.copy() nlist = [12, 13, 14, 15] nx.add_path(G, nlist) assert_edges_equal(G.edges(nlist), [(12, 13), (13, 14), (14, 15)]) G = self.G.copy() nx.add_path(G, nlist, weight=2.0) assert_edges_equal(G.edges(nlist, data=True), [(12, 13, {'weight': 2.}), (13, 14, {'weight': 2.}), (14, 15, {'weight': 2.})])
Example #19
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_restricted_view(self): H = nx.restricted_view(self.G, [0, 2, 5], [(1, 2), (3, 4)]) assert_equal(set(H.nodes), {1, 3, 4}) assert_equal(set(H.edges), {(1, 1)})
Example #20
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_set_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], vals) assert_equal(G[1][2][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1), (1, 2)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], 0) assert_equal(G[1][2][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1]['hi'], 0) assert_equal(G[0][1]['hello'], 200) assert_equal(G[1][2], {})
Example #21
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_set_edge_attributes_multi(): graphs = [nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], vals) assert_equal(G[1][2][0][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1, 0), (1, 2, 0)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], 0) assert_equal(G[1][2][0][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1, 0)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1][0]['hi'], 0) assert_equal(G[0][1][0]['hello'], 200) assert_equal(G[1][2][0], {})
Example #22
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_set_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], vals) assert_equal(G[1][2][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1), (1, 2)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], 0) assert_equal(G[1][2][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1]['hi'], 0) assert_equal(G[0][1]['hello'], 200) assert_equal(G[1][2], {})
Example #23
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_restricted_view(self): H = nx.restricted_view(self.G, [0, 2, 5], [(1, 2), (3, 4)]) assert_equal(set(H.nodes), {1, 3, 4}) assert_equal(set(H.edges), {(1, 1)})
Example #24
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_edges(self): assert_edges_equal(self.G.edges(), list(nx.edges(self.G))) assert_equal(sorted(self.DG.edges()), sorted(nx.edges(self.DG))) assert_edges_equal(self.G.edges(nbunch=[0, 1, 3]), list(nx.edges(self.G, nbunch=[0, 1, 3]))) assert_equal(sorted(self.DG.edges(nbunch=[0, 1, 3])), sorted(nx.edges(self.DG, nbunch=[0, 1, 3])))
Example #25
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_edges(self): assert_equal(self.G.edges(),nx.edges(self.G)) assert_equal(self.DG.edges(),nx.edges(self.DG)) assert_equal(self.G.edges(nbunch=[0,1,3]),nx.edges(self.G,nbunch=[0,1,3])) assert_equal(self.DG.edges(nbunch=[0,1,3]),nx.edges(self.DG,nbunch=[0,1,3]))
Example #26
Source File: calculation_helper.py From M-NMF with GNU General Public License v3.0 | 5 votes |
def graph_reader(input_path): """ Function to read a csv edge list and transform it to a networkx graph object. :param input_path: Path to the edge list csv. :return graph: NetworkX grapg object. """ edges = pd.read_csv(input_path) graph = nx.from_edgelist(edges.values.tolist()) return graph
Example #27
Source File: calculation_helper.py From M-NMF with GNU General Public License v3.0 | 5 votes |
def modularity_generator(G): """ Function to generate a modularity matrix. :param G: Graph object. :return laps: Modularity matrix. """ print("Modularity calculation.\n") degs = nx.degree(G) e_count = len(nx.edges(G)) modu = np.array([[float(degs[n_1]*degs[n_2])/(2*e_count) for n_1 in nx.nodes(G)] for n_2 in tqdm(nx.nodes(G))], dtype=np.float64) return modu
Example #28
Source File: calculation_helper.py From LabelPropagation with GNU General Public License v3.0 | 5 votes |
def overlap_generator(metric, graph): """ Calculating the overlap for each edge. :param metric: Weight metric. :param graph: NetworkX object. :return : Edge weight hash table. """ edges =[(edge[0], edge[1]) for edge in nx.edges(graph)] edges = edges + [(edge[1], edge[0]) for edge in nx.edges(graph)] return {edge: metric(graph, edge[0], edge[1]) for edge in tqdm(edges)}
Example #29
Source File: calculation_helper.py From GEMSEC with GNU General Public License v3.0 | 5 votes |
def preprocess_transition_probs(self): """ Preprocessing of transition probabilities for guiding the random walks. """ G = self.G is_directed = self.is_directed alias_nodes = {} print("") print("Preprocesing.\n") for node in tqdm(G.nodes()): unnormalized_probs = [G[node][nbr]["weight"] for nbr in sorted(G.neighbors(node))] norm_const = sum(unnormalized_probs) normalized_probs = [float(u_prob)/norm_const for u_prob in unnormalized_probs] alias_nodes[node] = alias_setup(normalized_probs) alias_edges = {} triads = {} if is_directed: for edge in G.edges(): alias_edges[edge] = self.get_alias_edge(edge[0], edge[1]) else: for edge in tqdm(G.edges()): alias_edges[edge] = self.get_alias_edge(edge[0], edge[1]) alias_edges[(edge[1], edge[0])] = self.get_alias_edge(edge[1], edge[0]) self.alias_nodes = alias_nodes self.alias_edges = alias_edges return
Example #30
Source File: calculation_helper.py From GEMSEC with GNU General Public License v3.0 | 5 votes |
def __init__(self, nx_G, is_directed, p, q): self.G = nx_G self.nodes = nx.nodes(self.G) print("Edge weighting.\n") for edge in tqdm(self.G.edges()): self.G[edge[0]][edge[1]]["weight"] = 1.0 self.G[edge[1]][edge[0]]["weight"] = 1.0 self.is_directed = is_directed self.p = p self.q = q