def test_set_node_attributes(graph_type): # Test single value G = nx.path_graph(3, create_using=graph_type) vals = 100 attr = "hello" nx.set_node_attributes(G, vals, attr) assert G.nodes[0][attr] == vals assert G.nodes[1][attr] == vals assert G.nodes[2][attr] == vals # Test dictionary G = nx.path_graph(3, create_using=graph_type) vals = dict(zip(sorted(G.nodes()), range(len(G)))) attr = "hi" nx.set_node_attributes(G, vals, attr) assert G.nodes[0][attr] == 0 assert G.nodes[1][attr] == 1 assert G.nodes[2][attr] == 2 # Test dictionary of dictionaries G = nx.path_graph(3, create_using=graph_type) d = {"hi": 0, "hello": 200} vals = dict.fromkeys(G.nodes(), d) vals.pop(0) nx.set_node_attributes(G, vals) assert G.nodes[0] == {} assert G.nodes[1]["hi"] == 0 assert G.nodes[2]["hello"] == 200
def test_set_edge_attributes(graph_type): # Test single value G = nx.path_graph(3, create_using=graph_type) attr = "hello" vals = 3 nx.set_edge_attributes(G, vals, attr) assert G[0][1][attr] == vals assert G[1][2][attr] == vals # Test multiple values G = nx.path_graph(3, create_using=graph_type) attr = "hi" edges = [(0, 1), (1, 2)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert G[0][1][attr] == 0 assert G[1][2][attr] == 1 # Test dictionary of dictionaries G = nx.path_graph(3, create_using=graph_type) d = {"hi": 0, "hello": 200} edges = [(0, 1)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert G[0][1]["hi"] == 0 assert G[0][1]["hello"] == 200 assert G[1][2] == {}
def setup_class(cls): cls.G = nx.path_graph(9) cls.DG = nx.path_graph(9, create_using=nx.DiGraph()) cls.Gv = nx.to_undirected(cls.DG) cls.DGv = nx.to_directed(cls.G) cls.Rv = cls.DG.reverse() cls.graphs = [cls.G, cls.DG, cls.Gv, cls.DGv, cls.Rv] for G in cls.graphs: print(G.edges, G.nodes, G.degree)
def test_normalized_P4(self): """Edge betweenness centrality: P4""" G = nx.path_graph(4) b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) b_answer = {(0, 1): 3, (1, 2): 4, (2, 3): 3} for n in sorted(G.edges()): assert almost_equal(b[n], b_answer[n])
def test_path(self): G = nx.path_graph(10) assert list(nx.square_clustering(G).values()) == [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] assert nx.square_clustering(G) == { 0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0, }
def test_path(self): G = nx.path_graph(10) assert list(nx.builtin.clustering(G, weight="weight").values()) == [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] assert nx.builtin.clustering(G, weight="weight") == { 0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0, }
def test_path(self): G = nx.path_graph(10, create_using=nx.DiGraph()) assert list(nx.builtin.clustering(G).values()) == [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] assert nx.builtin.clustering(G) == { 0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0, }
def test_all_simple_edge_paths_corner_cases(self): assert list(nx.builtin.all_simple_edge_paths(nx.empty_graph(2), 0, 0)) == [] assert list(nx.builtin.all_simple_edge_paths(nx.empty_graph(2), 0, 1)) == [] assert list(nx.builtin.all_simple_edge_paths(nx.path_graph(9), 0, 8, 0)) == []
def test_all_simple_edge_paths_on_non_trivial_graph(self): """you may need to draw this graph to make sure it is reasonable""" G = nx.path_graph(5, create_using=nx.DiGraph()) G.add_edges_from([(0, 5), (1, 5), (1, 3), (5, 4), (4, 2), (4, 3)]) paths = nx.builtin.all_simple_edge_paths(G, 1, [2, 3]) assert {tuple(p) for p in paths} == { ((1, 2), ), ((1, 3), (3, 4), (4, 2)), ((1, 5), (5, 4), (4, 2)), ((1, 3), ), ((1, 2), (2, 3)), ((1, 5), (5, 4), (4, 3)), ((1, 5), (5, 4), (4, 2), (2, 3)), } paths = nx.builtin.all_simple_edge_paths(G, 1, [2, 3], cutoff=3) assert {tuple(p) for p in paths} == { ((1, 2), ), ((1, 3), (3, 4), (4, 2)), ((1, 5), (5, 4), (4, 2)), ((1, 3), ), ((1, 2), (2, 3)), ((1, 5), (5, 4), (4, 3)), } paths = nx.builtin.all_simple_edge_paths(G, 1, [2, 3], cutoff=2) assert {tuple(p) for p in paths} == {((1, 2), ), ((1, 3), ), ((1, 2), (2, 3))}
def test_P3(self): """Betweenness centrality: P3""" G = nx.path_graph(3) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_P3_unweighted(self): """Eigenvector centrality: P3""" G = nx.path_graph(3) b_answer = {0: 0.5, 1: 0.7071, 2: 0.5} b = nx.eigenvector_centrality_numpy(G, weight=None) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_to_undirected_as_view(self): H = nx.path_graph(2, create_using=self.Graph) H2 = H.to_undirected(as_view=True) assert H is H2._graph assert H2.has_edge(0, 1) assert H2.has_edge(1, 0) pytest.raises(nx.NetworkXError, H2.add_node, -1) pytest.raises(nx.NetworkXError, H2.add_edge, 1, 2)
def test_digraph(self): G = nx.path_graph(3, create_using=nx.DiGraph()) c = nx.builtin.closeness_centrality(G) cr = nx.builtin.closeness_centrality(G.reverse()) d = {0: 0.0, 1: 0.500, 2: 0.667} dr = {0: 0.667, 1: 0.500, 2: 0.0} for n in sorted(self.P3): assert almost_equal(c[n], d[n], places=3) assert almost_equal(cr[n], dr[n], places=3)
def test_all_simple_edge_paths_with_two_targets_emits_two_paths(self): G = nx.path_graph(4) G.add_edge(2, 4) paths = nx.builtin.all_simple_edge_paths(G, 0, [3, 4]) assert {tuple(p) for p in paths} == { ((0, 1), (1, 2), (2, 3)), ((0, 1), (1, 2), (2, 4)), }
def test_digraph_all_simple_edge_paths_with_two_targets_cutoff(self): G = nx.path_graph(4, create_using=nx.DiGraph()) G.add_edge(2, 4) paths = nx.builtin.all_simple_edge_paths(G, 0, [3, 4], cutoff=3) assert {tuple(p) for p in paths} == { ((0, 1), (1, 2), (2, 3)), ((0, 1), (1, 2), (2, 4)), }
def test_hkn_harary_graph(self): # When k == 1, the hkn_harary_graph(k,n) is # the path_graph(n) for (k, n) in [(1, 6), (1, 7)]: G1 = hkn_harary_graph(k, n) G2 = nx.path_graph(n) assert is_isomorphic(G1, G2) # When k is even, the hkn_harary_graph(k,n) is # the circulant_graph(n, list(range(1,k/2+1))) for (k, n) in [(2, 6), (2, 7), (4, 6), (4, 7)]: G1 = hkn_harary_graph(k, n) G2 = nx.circulant_graph(n, list(range(1, k // 2 + 1))) assert is_isomorphic(G1, G2) # When k is odd and n is even, the hkn_harary_graph(k,n) is # the circulant_graph(n, list(range(1,(k+1)/2)) plus [n/2]) for (k, n) in [(3, 6), (5, 8), (7, 10)]: G1 = hkn_harary_graph(k, n) L = list(range(1, (k + 1) // 2)) L.append(n // 2) G2 = nx.circulant_graph(n, L) assert is_isomorphic(G1, G2) # When k is odd and n is odd, the hkn_harary_graph(k,n) is # the circulant_graph(n, list(range(1,(k+1)/2))) with # n//2+1 edges added between node i and node i+n//2+1 for (k, n) in [(3, 5), (5, 9), (7, 11)]: G1 = hkn_harary_graph(k, n) G2 = nx.circulant_graph(n, list(range(1, (k + 1) // 2))) eSet1 = set(G1.edges) eSet2 = set(G2.edges) eSet3 = set() half = n // 2 for i in range(0, half + 1): # add half+1 edges between i and i+half eSet3.add((i, (i + half) % n)) if os.environ.get("DEPLOYMENT", None) != "standalone" and k == 7: eSet1.remove((8, 3)) eSet1.remove((6, 1)) eSet1.remove((10, 5)) eSet1.add((3, 8)) eSet1.add((1, 6)) eSet1.add((5, 10)) assert eSet1 == eSet2 | eSet3 # Raise NetworkXError if k<1 k = 0 n = 0 pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n) # Raise NetworkXError if n<k+1 k = 6 n = 6 pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)
def test_minimum_networkx(): s = graphscope.session(cluster_type="hosts", num_workers=2) s.as_default() # case-1 run app G = nx.path_graph(10) nx.builtin.pagerank(G) # case-2 transfer nx graph to gs graph nx_g = nx.Graph(dist=True) nx_g.add_nodes_from(range(100), type="node") gs_g = s.g(nx_g) s.close()
def setup_class(cls): cls.K = nx.krackhardt_kite_graph() cls.P3 = nx.path_graph(3) cls.P4 = nx.path_graph(4) cls.K5 = nx.complete_graph(5) cls.C4 = nx.cycle_graph(4) cls.T = nx.balanced_tree(r=2, h=2) cls.Gb = nx.Graph() cls.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)]) F = nx.florentine_families_graph() cls.F = F cls.LM = nx.les_miserables_graph() # Create random undirected, unweighted graph for testing incremental version cls.undirected_G = nx.fast_gnp_random_graph(n=100, p=0.6, seed=123) cls.undirected_G_cc = nx.builtin.closeness_centrality(cls.undirected_G)
def setup_method(self): G = nx.path_graph(5, nx.DiGraph) # Add some node, edge, and graph attributes. for i in range(5): G.nodes[i]["name"] = f"node{i}" G.edges[0, 1]["name"] = "edge01" G.edges[3, 4]["name"] = "edge34" G.graph["name"] = "graph" # Get the subgraph induced by the first and last edges. self.G = G self.H = G.edge_subgraph([(0, 1), (3, 4)])
def test_maxiter(self): with pytest.raises(nx.PowerIterationFailedConvergence): alpha = 0.1 G = nx.path_graph(3) max_iter = 0 try: b = nx.builtin.katz_centrality(G, alpha, max_iter=max_iter) except nx.NetworkXError as e: assert str( max_iter) in e.args[0], "max_iter value not in error msg" raise # So that the decorater sees the exception.
def test_beta_as_scalar(self): alpha = 0.1 beta = 0.1 b_answer = { 0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162 } G = nx.path_graph(3) b = nx.katz_centrality_numpy(G, alpha, beta) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_beta_as_dict(self): alpha = 0.1 beta = {0: 1.0, 1: 1.0, 2: 1.0} b_answer = { 0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162 } G = nx.path_graph(3) b = nx.builtin.katz_centrality(G, alpha, beta) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_P3(self): """Katz centrality: P3""" alpha = 0.1 G = nx.path_graph(3) b_answer = { 0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162 } b = nx.builtin.katz_centrality(G, alpha) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_P3(self): """Weighted betweenness centrality: P3""" G = nx.path_graph(3) for e in G.edges: G.edges[e]["weight"] = 1 G[1][2]["weight"] = 10 b_answer = {0: 0.0, 1: 1.0, 2: 0.0} b = nx.builtin.betweenness_centrality(G, weight="weight", normalized=False) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_path_graph(self): P10 = nx.path_graph(10) assert list(nx.builtin.edge_boundary(P10, [0, 1, 2])) == [(2, 3)] assert sorted(nx.builtin.edge_boundary(P10, [3, 4, 5])) == [(3, 2), (5, 6)] assert sorted(nx.builtin.edge_boundary(P10, [2, 3, 4, 5, 6])) == [ (2, 1), (6, 7), ] assert list(nx.builtin.edge_boundary(P10, [7, 8, 9])) == [(7, 6)] assert sorted(nx.builtin.edge_boundary(P10, [0, 1, 2], [2, 3, 4])) == [ (1, 2), (2, 3), ]
def test_zero_centrality(self): G = nx.path_graph(3) prev_cc = nx.builtin.closeness_centrality(G) edge = self.pick_remove_edge(G) test_cc = nx.incremental_closeness_centrality(G, edge, prev_cc, insertion=False) G.remove_edges_from([edge]) real_cc = nx.builtin.closeness_centrality(G) shared_items = set(test_cc.items()) & set(real_cc.items()) assert len(shared_items) == len(real_cc) assert 0 in test_cc.values()
def test_center_wrong_dimensions(self): G = nx.path_graph(1) # in graphscope.nx the ids of the two methods are not identical. # assert id(nx.spring_layout) == id(nx.fruchterman_reingold_layout) pytest.raises(ValueError, nx.random_layout, G, center=(1, 1, 1)) pytest.raises(ValueError, nx.circular_layout, G, center=(1, 1, 1)) pytest.raises(ValueError, nx.planar_layout, G, center=(1, 1, 1)) pytest.raises(ValueError, nx.spring_layout, G, center=(1, 1, 1)) pytest.raises(ValueError, nx.spring_layout, G, dim=3, center=(1, 1)) pytest.raises(ValueError, nx.spectral_layout, G, center=(1, 1, 1)) pytest.raises(ValueError, nx.spectral_layout, G, dim=3, center=(1, 1)) pytest.raises(ValueError, nx.shell_layout, G, center=(1, 1, 1)) pytest.raises(ValueError, nx.spiral_layout, G, center=(1, 1, 1)) pytest.raises(ValueError, nx.kamada_kawai_layout, G, center=(1, 1, 1))
def test_path(self): G = nx.path_graph(10) assert list(nx.builtin.triangles(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] assert nx.builtin.triangles(G) == { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, }
def test_degree_p4(self): G = nx.path_graph(4) answer = {1: 2.0, 2: 1.5} nd = nx.builtin.average_degree_connectivity(G) assert nd == answer D = G.to_directed() answer = {2: 2.0, 4: 1.5} nd = nx.builtin.average_degree_connectivity(D) assert nd == answer answer = {1: 2.0, 2: 1.5} D = G.to_directed() nd = nx.builtin.average_degree_connectivity(D, source="in", target="in") assert nd == answer
def test_ladder_graph(self): for i, G in [ (0, nx.empty_graph(0)), (1, nx.path_graph(2)), ]: assert is_isomorphic(nx.ladder_graph(i), G) pytest.raises(nx.NetworkXError, nx.ladder_graph, 2, create_using=nx.DiGraph) g = nx.ladder_graph(2) mg = nx.ladder_graph(2, create_using=nx.MultiGraph) assert_edges_equal(mg.edges(), g.edges())