def test_project_weighted_jaccard(self): edges = [ ("A", "B", 2 / 5.0), ("A", "C", 1 / 2.0), ("B", "C", 1 / 5.0), ("B", "D", 1 / 5.0), ("B", "E", 2 / 6.0), ("E", "F", 1 / 3.0), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.overlap_weighted_projected_graph(self.G, "ABCDEF") assert edges_equal(list(P.edges()), Panswer.edges()) for u, v in list(P.edges()): assert P[u][v]["weight"] == Panswer[u][v]["weight"] edges = [ ("A", "B", 3 / 3.0), ("A", "E", 1 / 3.0), ("A", "C", 1 / 3.0), ("A", "D", 1 / 3.0), ("B", "E", 1 / 3.0), ("B", "C", 1 / 3.0), ("B", "D", 1 / 3.0), ("C", "D", 1 / 1.0), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.overlap_weighted_projected_graph(self.N, "ABCDE") assert edges_equal(list(P.edges()), Panswer.edges()) for u, v in P.edges(): assert P[u][v]["weight"] == Panswer[u][v]["weight"]
def test_project_weighted_ratio(self): edges = [ ("A", "B", 2 / 6.0), ("A", "C", 1 / 6.0), ("B", "C", 1 / 6.0), ("B", "D", 1 / 6.0), ("B", "E", 2 / 6.0), ("E", "F", 1 / 6.0), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.weighted_projected_graph(self.G, "ABCDEF", ratio=True) assert edges_equal(list(P.edges()), Panswer.edges()) for u, v in list(P.edges()): assert P[u][v]["weight"] == Panswer[u][v]["weight"] edges = [ ("A", "B", 3 / 3.0), ("A", "E", 1 / 3.0), ("A", "C", 1 / 3.0), ("A", "D", 1 / 3.0), ("B", "E", 1 / 3.0), ("B", "C", 1 / 3.0), ("B", "D", 1 / 3.0), ("C", "D", 1 / 3.0), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.weighted_projected_graph(self.N, "ABCDE", ratio=True) assert edges_equal(list(P.edges()), Panswer.edges()) for u, v in list(P.edges()): assert P[u][v]["weight"] == Panswer[u][v]["weight"]
def test_graph(self): g = nx.cycle_graph(10) G = nx.Graph() G.add_nodes_from(g) G.add_weighted_edges_from((u, v, u) for u, v in g.edges()) # Dict of dicts dod = to_dict_of_dicts(G) GG = from_dict_of_dicts(dod, create_using=nx.Graph) assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert edges_equal(sorted(G.edges()), sorted(GG.edges())) GW = to_networkx_graph(dod, create_using=nx.Graph) assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert edges_equal(sorted(G.edges()), sorted(GW.edges())) GI = nx.Graph(dod) assert sorted(G.nodes()) == sorted(GI.nodes()) assert sorted(G.edges()) == sorted(GI.edges()) # Dict of lists dol = to_dict_of_lists(G) GG = from_dict_of_lists(dol, create_using=nx.Graph) # dict of lists throws away edge data so set it to none enone = [(u, v, {}) for (u, v, d) in G.edges(data=True)] assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert edges_equal(enone, sorted(GG.edges(data=True))) GW = to_networkx_graph(dol, create_using=nx.Graph) assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert edges_equal(enone, sorted(GW.edges(data=True))) GI = nx.Graph(dol) assert nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) assert edges_equal(enone, sorted(GI.edges(data=True)))
def test_selfloops(graph_type): G = nx.complete_graph(3, create_using=graph_type) G.add_edge(0, 0) assert nodes_equal(nx.nodes_with_selfloops(G), [0]) assert edges_equal(nx.selfloop_edges(G), [(0, 0)]) assert edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {})]) assert nx.number_of_selfloops(G) == 1
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_dorogovtsev_goltsev_mendes_graph(self): G = nx.dorogovtsev_goltsev_mendes_graph(0) assert edges_equal(G.edges(), [(0, 1)]) assert nodes_equal(list(G), [0, 1]) G = nx.dorogovtsev_goltsev_mendes_graph(1) assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)]) assert nx.average_clustering(G) == 1.0 assert sorted(nx.triangles(G).values()) == [1, 1, 1] G = nx.dorogovtsev_goltsev_mendes_graph(10) assert nx.number_of_nodes(G) == 29526 assert nx.number_of_edges(G) == 59049 assert G.degree(0) == 1024 assert G.degree(1) == 1024 assert G.degree(2) == 1024 pytest.raises( nx.NetworkXError, nx.dorogovtsev_goltsev_mendes_graph, 7, create_using=nx.DiGraph, ) pytest.raises( nx.NetworkXError, nx.dorogovtsev_goltsev_mendes_graph, 7, create_using=nx.MultiGraph, )
def test_negative_weights(self): """Negative weights""" G = nx.Graph() G.add_edge(1, 2, weight=2) G.add_edge(1, 3, weight=-2) G.add_edge(2, 3, weight=1) G.add_edge(2, 4, weight=-1) G.add_edge(3, 4, weight=-6) assert edges_equal(nx.max_weight_matching(G), matching_dict_to_set({ 1: 2, 2: 1 })) assert edges_equal(nx.max_weight_matching(G, 1), matching_dict_to_set({ 1: 3, 2: 4, 3: 1, 4: 2 })) assert edges_equal(nx.min_weight_matching(G), matching_dict_to_set({ 1: 2, 3: 4 })) assert edges_equal(nx.min_weight_matching(G, 1), matching_dict_to_set({ 1: 2, 3: 4 }))
def test_from_edgelist(self): # Pandas DataFrame G = nx.cycle_graph(10) G.add_weighted_edges_from((u, v, u) for u, v in list(G.edges)) edgelist = nx.to_edgelist(G) source = [] target = [] weight = [] # N.B the iterate order of edgelist may not all the same for s, t, d in edgelist: source.append(s) target.append(t) weight.append(d["weight"]) edges = pd.DataFrame({ "source": source, "target": target, "weight": weight }) GG = nx.from_pandas_edgelist(edges, edge_attr="weight") assert nodes_equal(G.nodes(), GG.nodes()) assert edges_equal(G.edges(), GG.edges()) GW = nx.to_networkx_graph(edges, create_using=nx.Graph) assert nodes_equal(G.nodes(), GW.nodes()) assert edges_equal(G.edges(), GW.edges())
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.0 }), (12, 14, { "weight": 2.0 }), (12, 15, { "weight": 2.0 }), ], ) 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)
def test_transitive_closure_dag(self): G = nx.DiGraph([(1, 2), (2, 3), (3, 4)]) transitive_closure = nx.algorithms.dag.transitive_closure_dag 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)]) pytest.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 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 G.get_edge_data(u, v) == H.get_edge_data(u, v)
def test_nested_s_blossom_expand(self): """Create nested S-blossom, augment, expand recursively:""" G = nx.Graph() G.add_weighted_edges_from([ (1, 2, 8), (1, 3, 8), (2, 3, 10), (2, 4, 12), (3, 5, 12), (4, 5, 14), (4, 6, 12), (5, 7, 12), (6, 7, 14), (7, 8, 12), ]) answer = matching_dict_to_set({ 1: 2, 2: 1, 3: 5, 4: 6, 5: 3, 6: 4, 7: 8, 8: 7 }) assert edges_equal(nx.max_weight_matching(G), answer) assert edges_equal(nx.min_weight_matching(G), answer)
def test_nasty_blossom1(self): """Create blossom, relabel as T in more than one way, expand, augment: """ G = nx.Graph() G.add_weighted_edges_from([ (1, 2, 45), (1, 5, 45), (2, 3, 50), (3, 4, 45), (4, 5, 50), (1, 6, 30), (3, 9, 35), (4, 8, 35), (5, 7, 26), (9, 10, 5), ]) ansdict = { 1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9 } answer = matching_dict_to_set(ansdict) assert edges_equal(nx.max_weight_matching(G), answer) assert edges_equal(nx.min_weight_matching(G), answer)
def test_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)] assert edges_equal(nx.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(nx.transitive_closure(G).edges(), solution) G = nx.DiGraph([(1, 2), (2, 3), (3, 1)]) solution = [(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) G = nx.Graph([(1, 2), (2, 3), (3, 4)]) pytest.raises(nx.NetworkXNotImplemented, nx.transitive_closure, G) # test if edge data is copied G = nx.DiGraph([(1, 2, {"a": 3}), (2, 3, {"b": 0}), (3, 4)]) H = nx.transitive_closure(G) for u, v in G.edges(): assert G.get_edge_data(u, v) == H.get_edge_data(u, v) k = 10 G = nx.DiGraph((i, i + 1, {"f": "b", "weight": i}) for i in range(k)) H = nx.transitive_closure(G) for u, v in G.edges(): assert G.get_edge_data(u, v) == H.get_edge_data(u, v)
def test_trivial5(self): """Path""" G = nx.Graph() G.add_edge(1, 2, weight=5) G.add_edge(2, 3, weight=11) G.add_edge(3, 4, weight=5) assert edges_equal(nx.max_weight_matching(G), matching_dict_to_set({ 2: 3, 3: 2 })) assert edges_equal(nx.max_weight_matching(G, 1), matching_dict_to_set({ 1: 2, 2: 1, 3: 4, 4: 3 })) assert edges_equal(nx.min_weight_matching(G), matching_dict_to_set({ 1: 2, 3: 4 })) assert edges_equal(nx.min_weight_matching(G, 1), matching_dict_to_set({ 1: 2, 3: 4 }))
def test_s_blossom_relabel_expand(self): """Create S-blossom, relabel as T, expand:""" G = nx.Graph() G.add_weighted_edges_from([ (1, 2, 23), (1, 5, 22), (1, 6, 15), (2, 3, 25), (3, 4, 22), (4, 5, 25), (4, 8, 14), (5, 7, 13), ]) answer = matching_dict_to_set({ 1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4 }) assert edges_equal(nx.max_weight_matching(G), answer) assert edges_equal(nx.min_weight_matching(G), answer)
def test_nested_s_blossom_relabel_expand(self): """Create nested S-blossom, relabel as T, expand:""" G = nx.Graph() G.add_weighted_edges_from([ (1, 2, 19), (1, 3, 20), (1, 8, 8), (2, 3, 25), (2, 4, 18), (3, 5, 18), (4, 5, 13), (4, 7, 7), (5, 6, 7), ]) answer = matching_dict_to_set({ 1: 8, 2: 3, 3: 2, 4: 7, 5: 6, 6: 5, 7: 4, 8: 1 }) assert edges_equal(nx.max_weight_matching(G), answer) assert edges_equal(nx.min_weight_matching(G), answer)
def test_nested_s_blossom_relabel(self): """Create S-blossom, relabel as S, include in nested S-blossom:""" G = nx.Graph() G.add_weighted_edges_from([ (1, 2, 10), (1, 7, 10), (2, 3, 12), (3, 4, 20), (3, 5, 20), (4, 5, 25), (5, 6, 10), (6, 7, 10), (7, 8, 8), ]) answer = matching_dict_to_set({ 1: 2, 2: 1, 3: 4, 4: 3, 5: 6, 6: 5, 7: 8, 8: 7 }) assert edges_equal(nx.max_weight_matching(G), answer) assert edges_equal(nx.min_weight_matching(G), answer)
def test_edge_attr3(self): G = self.Graph() G.add_edges_from([(1, 2, { "weight": 32 }), (3, 4, { "weight": 64 })], foo="foo") assert edges_equal( G.edges(data=True), [ (1, 2, { "foo": "foo", "weight": 32 }), (3, 4, { "foo": "foo", "weight": 64 }), ], ) G.remove_edges_from([(1, 2), (3, 4)]) G.add_edge(1, 2, data=7, spam="bar", bar="foo") assert edges_equal(G.edges(data=True), [(1, 2, { "data": 7, "spam": "bar", "bar": "foo" })])
def test_digraphs(self): for dest, source in [ (to_dict_of_dicts, from_dict_of_dicts), (to_dict_of_lists, from_dict_of_lists), ]: G = cycle_graph(10) # Dict of [dicts, lists] dod = dest(G) GG = source(dod) assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert edges_equal(sorted(G.edges()), sorted(GG.edges())) GW = to_networkx_graph(dod) assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert edges_equal(sorted(G.edges()), sorted(GW.edges())) GI = nx.Graph(dod) assert nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) assert edges_equal(sorted(G.edges()), sorted(GI.edges())) G = cycle_graph(10, create_using=nx.DiGraph) dod = dest(G) GG = source(dod, create_using=nx.DiGraph) assert sorted(G.nodes()) == sorted(GG.nodes()) assert sorted(G.edges()) == sorted(GG.edges()) GW = to_networkx_graph(dod, create_using=nx.DiGraph) assert sorted(G.nodes()) == sorted(GW.nodes()) assert sorted(G.edges()) == sorted(GW.edges()) GI = nx.DiGraph(dod) assert sorted(G.nodes()) == sorted(GI.nodes()) assert sorted(G.edges()) == sorted(GI.edges())
def test_edges(self): assert edges_equal(self.G.edges(), list(nx.edges(self.G))) assert 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 sorted(self.DG.edges(nbunch=[0, 1, 3])) == sorted( nx.edges(self.DG, nbunch=[0, 1, 3]))
def test_project_weighted_newman(self): edges = [ ("A", "B", 1.5), ("A", "C", 0.5), ("B", "C", 0.5), ("B", "D", 1), ("B", "E", 2), ("E", "F", 1), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.collaboration_weighted_projected_graph(self.G, "ABCDEF") assert edges_equal(list(P.edges()), Panswer.edges()) for u, v in list(P.edges()): assert P[u][v]["weight"] == Panswer[u][v]["weight"] edges = [ ("A", "B", 11 / 6.0), ("A", "E", 1 / 2.0), ("A", "C", 1 / 3.0), ("A", "D", 1 / 3.0), ("B", "E", 1 / 2.0), ("B", "C", 1 / 3.0), ("B", "D", 1 / 3.0), ("C", "D", 1 / 3.0), ] Panswer = nx.Graph() Panswer.add_weighted_edges_from(edges) P = bipartite.collaboration_weighted_projected_graph(self.N, "ABCDE") assert edges_equal(list(P.edges()), Panswer.edges()) for u, v in list(P.edges()): assert P[u][v]["weight"] == Panswer[u][v]["weight"]
def test_nonisomorphic_trees(self): def f(x): return list(nx.nonisomorphic_trees(x)) assert edges_equal(f(3)[0].edges(), [(0, 1), (0, 2)]) assert edges_equal(f(4)[0].edges(), [(0, 1), (0, 3), (1, 2)]) assert edges_equal(f(4)[1].edges(), [(0, 1), (0, 2), (0, 3)])
def test_restricted_induced_subgraph_chains(self): """Test subgraph chains that both restrict and show nodes/edges. A restricted_view subgraph should allow induced subgraphs using G.subgraph that automagically without a chain (meaning the result is a subgraph view of the original graph not a subgraph-of-subgraph. """ hide_nodes = [3, 4, 5] hide_edges = [(6, 7)] RG = nx.restricted_view(self.G, hide_nodes, hide_edges) nodes = [4, 5, 6, 7, 8] SG = nx.induced_subgraph(RG, nodes) SSG = RG.subgraph(nodes) assert RG._graph is self.G assert SSG._graph is self.G assert SG._graph is RG assert edges_equal(SG.edges, SSG.edges) # should be same as morphing the graph CG = self.G.copy() CG.remove_nodes_from(hide_nodes) CG.remove_edges_from(hide_edges) assert edges_equal(CG.edges(nodes), SSG.edges) CG.remove_nodes_from([0, 1, 2, 3]) assert edges_equal(CG.edges, SSG.edges) # switch order: subgraph first, then restricted view SSSG = self.G.subgraph(nodes) RSG = nx.restricted_view(SSSG, hide_nodes, hide_edges) assert RSG._graph is not self.G assert edges_equal(RSG.edges, CG.edges)
def test_edges(self): G = self.K3 assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)]) assert edges_equal(G.edges(0), [(0, 1), (0, 2)]) assert edges_equal(G.edges([0, 1]), [(0, 1), (0, 2), (1, 2)]) with pytest.raises(nx.NetworkXError): G.edges(-1)
def test_path_projected_graph(self): G = nx.path_graph(4) P = bipartite.projected_graph(G, [1, 3]) assert nodes_equal(list(P), [1, 3]) assert edges_equal(list(P.edges()), [(1, 3)]) P = bipartite.projected_graph(G, [0, 2]) assert nodes_equal(list(P), [0, 2]) assert edges_equal(list(P.edges()), [(0, 2)])
def test_selfloop_edges_attr(graph_type): G = nx.complete_graph(3, create_using=graph_type) G.add_edge(0, 0) G.add_edge(1, 1, weight=2) assert edges_equal( nx.selfloop_edges(G, data=True), [(0, 0, {}), (1, 1, {"weight": 2})] ) assert edges_equal(nx.selfloop_edges(G, data="weight"), [(0, 0, None), (1, 1, 2)])
def test_edges_data(self): G = self.K3 all_edges = [(0, 1, {}), (0, 2, {}), (1, 2, {})] assert edges_equal(G.edges(data=True), all_edges) assert edges_equal(G.edges(0, data=True), [(0, 1, {}), (0, 2, {})]) assert edges_equal(G.edges([0, 1], data=True), all_edges) with pytest.raises(nx.NetworkXError): G.edges(-1, True)
def test_edge_attr2(self): G = self.Graph() G.add_edges_from([(1, 2), (3, 4)], foo="foo") assert edges_equal(G.edges(data=True), [(1, 2, { "foo": "foo" }), (3, 4, { "foo": "foo" })]) assert edges_equal(G.edges(data="foo"), [(1, 2, "foo"), (3, 4, "foo")])
def test_path_weighted_projected_graph(self): G = nx.path_graph(4) P = bipartite.weighted_projected_graph(G, [1, 3]) assert nodes_equal(list(P), [1, 3]) assert edges_equal(list(P.edges()), [(1, 3)]) P[1][3]["weight"] = 1 P = bipartite.weighted_projected_graph(G, [0, 2]) assert nodes_equal(list(P), [0, 2]) assert edges_equal(list(P.edges()), [(0, 2)]) P[0][2]["weight"] = 1
def test_selfloops_attr(self): G = self.K3.copy() G.add_edge(0, 0) G.add_edge(1, 1, weight=2) assert edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {}), (1, 1, { "weight": 2 })]) assert edges_equal(nx.selfloop_edges(G, data="weight"), [(0, 0, None), (1, 1, 2)])