def test_read_sparse6(self): data = b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM' G = nx.from_sparse6_bytes(data) fh = BytesIO(data) Gin = nx.read_sparse6(fh) assert_nodes_equal(G.nodes(), Gin.nodes()) assert_edges_equal(G.edges(), Gin.edges())
def test_read_equals_from_bytes(self): data = b'DF{' G = nx.from_graph6_bytes(data) fh = BytesIO(data) Gin = nx.read_graph6(fh) assert_nodes_equal(G.nodes(), Gin.nodes()) assert_edges_equal(G.edges(), Gin.edges())
def test_from_biadjacency_weight(self): M = sparse.csc_matrix([[1,2],[0,3]]) B = bipartite.from_biadjacency_matrix(M) assert_edges_equal(B.edges(),[(0,2),(0,3),(1,3)]) B = bipartite.from_biadjacency_matrix(M, edge_attribute='weight') e = [(0,2,{'weight':1}),(0,3,{'weight':2}),(1,3,{'weight':3})] assert_edges_equal(B.edges(data=True),e)
def test_steiner_tree(self): S = steiner_tree(self.G, self.term_nodes) expected_steiner_tree = [(1, 2, {'weight': 10}), (2, 3, {'weight': 10}), (2, 7, {'weight': 1}), (3, 4, {'weight': 10}), (5, 7, {'weight': 1})] assert_edges_equal(list(S.edges(data=True)), expected_steiner_tree)
def test_roundtrip(self): for i in list(range(13)) + [31, 47, 62, 63, 64, 72]: G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i) f = BytesIO() nx.write_graph6(G, f) f.seek(0) H = nx.read_graph6(f) assert_nodes_equal(G.nodes(), H.nodes()) assert_edges_equal(G.edges(), H.edges())
def test_transitive_closure(self): G = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) transitive_closure = nx.algorithms.dag.transitive_closure solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] assert_edges_equal(transitive_closure(G).edges(), solution) G = nx.DiGraph([(1, 2), (2, 3), (2, 4)]) solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)] assert_edges_equal(transitive_closure(G).edges(), solution) G = nx.Graph([(1, 2), (2, 3), (3, 4)]) assert_raises(nx.NetworkXNotImplemented, transitive_closure, G)
def test_read_write_inverse(self): for i in list(range(13)) + [31, 47, 62, 63, 64, 72]: m = min(2 * i, i * i // 2) g = nx.random_graphs.gnm_random_graph(i, m, seed=i) gstr = BytesIO() nx.write_sparse6(g, gstr, header=False) # Strip the trailing newline. gstr = gstr.getvalue().rstrip() g2 = nx.from_sparse6_bytes(gstr) assert_equal(g2.order(), g.order()) assert_edges_equal(g2.edges(), g.edges())
def test_from_sparse6_bytes(self): data = b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM' G = nx.from_sparse6_bytes(data) assert_nodes_equal(sorted(G.nodes()), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]) assert_edges_equal(G.edges(), [(0, 1), (0, 2), (0, 3), (1, 12), (1, 14), (2, 13), (2, 15), (3, 16), (3, 17), (4, 7), (4, 9), (4, 11), (5, 6), (5, 8), (5, 9), (6, 10), (6, 11), (7, 8), (7, 10), (8, 12), (9, 15), (10, 14), (11, 13), (12, 16), (13, 17), (14, 17), (15, 16)])
def test_transitive_closure(self): G = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) transitive_closure = nx.algorithms.dag.transitive_closure solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] assert_edges_equal(transitive_closure(G).edges(), solution) G = nx.DiGraph([(1, 2), (2, 3), (2, 4)]) solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)] assert_edges_equal(transitive_closure(G).edges(), solution) G = nx.Graph([(1, 2), (2, 3), (3, 4)]) assert_raises(nx.NetworkXNotImplemented, transitive_closure, G) # test if edge data is copied G = nx.DiGraph([(1, 2, {"a": 3}), (2, 3, {"b": 0}), (3, 4)]) H = transitive_closure(G) for u, v in G.edges(): assert_equal(G.get_edge_data(u, v), H.get_edge_data(u, v)) k = 10 G = nx.DiGraph((i, i+1, {"foo": "bar", "weight": i}) for i in range(k)) H = transitive_closure(G) for u, v in G.edges(): assert_equal(G.get_edge_data(u, v), H.get_edge_data(u, v))
def test_metric_closure(self): M = metric_closure(self.G) mc = [(1, 2, {'distance': 10, 'path': [1, 2]}), (1, 3, {'distance': 20, 'path': [1, 2, 3]}), (1, 4, {'distance': 22, 'path': [1, 2, 7, 5, 4]}), (1, 5, {'distance': 12, 'path': [1, 2, 7, 5]}), (1, 6, {'distance': 22, 'path': [1, 2, 7, 5, 6]}), (1, 7, {'distance': 11, 'path': [1, 2, 7]}), (2, 3, {'distance': 10, 'path': [2, 3]}), (2, 4, {'distance': 12, 'path': [2, 7, 5, 4]}), (2, 5, {'distance': 2, 'path': [2, 7, 5]}), (2, 6, {'distance': 12, 'path': [2, 7, 5, 6]}), (2, 7, {'distance': 1, 'path': [2, 7]}), (3, 4, {'distance': 10, 'path': [3, 4]}), (3, 5, {'distance': 12, 'path': [3, 2, 7, 5]}), (3, 6, {'distance': 22, 'path': [3, 2, 7, 5, 6]}), (3, 7, {'distance': 11, 'path': [3, 2, 7]}), (4, 5, {'distance': 10, 'path': [4, 5]}), (4, 6, {'distance': 20, 'path': [4, 5, 6]}), (4, 7, {'distance': 11, 'path': [4, 5, 7]}), (5, 6, {'distance': 10, 'path': [5, 6]}), (5, 7, {'distance': 1, 'path': [5, 7]}), (6, 7, {'distance': 11, 'path': [6, 5, 7]})] assert_edges_equal(list(M.edges(data=True)), mc)
def test_from_biadjacency_multigraph(self): M = sparse.csc_matrix([[1, 2], [0, 3]]) B = bipartite.from_biadjacency_matrix(M, create_using=nx.MultiGraph()) assert_edges_equal(B.edges(), [(0, 2), (0, 3), (0, 3), (1, 3), (1, 3), (1, 3)])
def test_from_graph6_bytes(self): data = b'DF{' G=nx.from_graph6_bytes(data) assert_nodes_equal(G.nodes(),[0, 1, 2, 3, 4]) assert_edges_equal(G.edges(), [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)])
def test_metric_closure(self): M = metric_closure(self.G) mc = [(1, 2, { 'distance': 10, 'path': [1, 2] }), (1, 3, { 'distance': 20, 'path': [1, 2, 3] }), (1, 4, { 'distance': 22, 'path': [1, 2, 7, 5, 4] }), (1, 5, { 'distance': 12, 'path': [1, 2, 7, 5] }), (1, 6, { 'distance': 22, 'path': [1, 2, 7, 5, 6] }), (1, 7, { 'distance': 11, 'path': [1, 2, 7] }), (2, 3, { 'distance': 10, 'path': [2, 3] }), (2, 4, { 'distance': 12, 'path': [2, 7, 5, 4] }), (2, 5, { 'distance': 2, 'path': [2, 7, 5] }), (2, 6, { 'distance': 12, 'path': [2, 7, 5, 6] }), (2, 7, { 'distance': 1, 'path': [2, 7] }), (3, 4, { 'distance': 10, 'path': [3, 4] }), (3, 5, { 'distance': 12, 'path': [3, 2, 7, 5] }), (3, 6, { 'distance': 22, 'path': [3, 2, 7, 5, 6] }), (3, 7, { 'distance': 11, 'path': [3, 2, 7] }), (4, 5, { 'distance': 10, 'path': [4, 5] }), (4, 6, { 'distance': 20, 'path': [4, 5, 6] }), (4, 7, { 'distance': 11, 'path': [4, 5, 7] }), (5, 6, { 'distance': 10, 'path': [5, 6] }), (5, 7, { 'distance': 1, 'path': [5, 7] }), (6, 7, { 'distance': 11, 'path': [6, 5, 7] })] assert_edges_equal(list(M.edges(data=True)), mc)
def test_create2(self): G = nx.Graph() G.add_edges_from([(0, 1), (1, 2), (2, 3)]) L = nx.line_graph(G, create_using=nx.DiGraph()) assert_edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))])
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.})]) G = self.G.copy() nlist = [None] nx.add_path(G, nlist) assert_edges_equal(G.edges(nlist), []) assert_nodes_equal(G, list(self.G) + [None]) G = self.G.copy() nlist = iter([None]) nx.add_path(G, nlist) assert_edges_equal(G.edges([None]), []) assert_nodes_equal(G, list(self.G) + [None]) G = self.G.copy() nlist = [12] nx.add_path(G, nlist) assert_edges_equal(G.edges(nlist), []) assert_nodes_equal(G, list(self.G) + [12]) G = self.G.copy() nlist = iter([12]) nx.add_path(G, nlist) assert_edges_equal(G.edges([12]), []) assert_nodes_equal(G, list(self.G) + [12]) G = self.G.copy() nlist = [] nx.add_path(G, nlist) assert_edges_equal(G.edges, self.G.edges) assert_nodes_equal(G, list(self.G)) G = self.G.copy() nlist = iter([]) nx.add_path(G, nlist) assert_edges_equal(G.edges, self.G.edges) assert_nodes_equal(G, list(self.G))
def test_reflexive_transitive_closure(self): G = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] soln = sorted(solution + [(n, n) for n in G]) assert_edges_equal(nx.transitive_closure(G).edges(), solution) assert_edges_equal(nx.transitive_closure(G, False).edges(), solution) assert_edges_equal(nx.transitive_closure(G, True).edges(), soln) assert_edges_equal(nx.transitive_closure(G, None).edges(), solution) G = nx.DiGraph([(1, 2), (2, 3), (2, 4)]) solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)] soln = sorted(solution + [(n, n) for n in G]) assert_edges_equal(nx.transitive_closure(G).edges(), solution) assert_edges_equal(nx.transitive_closure(G, False).edges(), solution) assert_edges_equal(nx.transitive_closure(G, True).edges(), soln) assert_edges_equal(nx.transitive_closure(G, None).edges(), solution) G = nx.DiGraph([(1, 2), (2, 3), (3, 1)]) solution = sorted([(1, 2), (2, 1), (2, 3), (3, 2), (1, 3), (3, 1)]) soln = sorted(solution + [(n, n) for n in G]) assert_edges_equal(sorted(nx.transitive_closure(G).edges()), soln) assert_edges_equal(sorted(nx.transitive_closure(G, False).edges()), soln) assert_edges_equal(sorted(nx.transitive_closure(G, None).edges()), solution) assert_edges_equal(sorted(nx.transitive_closure(G, True).edges()), soln)
def test_edge_lookup(self): G = self.Graph() G.add_edge(1, 2, foo='bar') G.add_edge(1, 2, 'key', foo='biz') assert_edges_equal(G.edges[1, 2, 0], {'foo': 'bar'}) assert_edges_equal(G.edges[1, 2, 'key'], {'foo': 'biz'})
def test_from_graph6_bytes(self): data = b'DF{' G = nx.from_graph6_bytes(data) assert_nodes_equal(G.nodes(), [0, 1, 2, 3, 4]) assert_edges_equal(G.edges(), [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)])
def test_edge_attr(self): G = self.Graph() G.add_edge(1, 2, foo="bar") assert_edges_equal(G.edges(data=True), [(1, 2, {"foo": "bar"})]) assert_edges_equal(G.edges(data="foo"), [(1, 2, "bar")])
def test_edge_lookup(self): G = self.Graph() G.add_edge(1, 2, foo="bar") assert_edges_equal(G.edges[1, 2], {"foo": "bar"})
def test_from_biadjacency_multigraph(self): M = sparse.csc_matrix([[1,2],[0,3]]) B = bipartite.from_biadjacency_matrix(M, create_using=nx.MultiGraph()) assert_edges_equal(B.edges(),[(0,2),(0,3),(0,3),(1,3),(1,3),(1,3)])
def test_digraph2(self): G = nx.DiGraph() G.add_edges_from([(0, 1), (1, 2), (2, 3)]) L = nx.line_graph(G) assert_edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))])