comparison env/lib/python3.9/site-packages/networkx/algorithms/bipartite/tests/test_matrix.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import pytest
2
3 np = pytest.importorskip("numpy")
4 sp = pytest.importorskip("scipy")
5 sparse = pytest.importorskip("scipy.sparse")
6
7
8 import networkx as nx
9 from networkx.algorithms import bipartite
10 from networkx.testing.utils import assert_edges_equal
11
12
13 class TestBiadjacencyMatrix:
14 def test_biadjacency_matrix_weight(self):
15 G = nx.path_graph(5)
16 G.add_edge(0, 1, weight=2, other=4)
17 X = [1, 3]
18 Y = [0, 2, 4]
19 M = bipartite.biadjacency_matrix(G, X, weight="weight")
20 assert M[0, 0] == 2
21 M = bipartite.biadjacency_matrix(G, X, weight="other")
22 assert M[0, 0] == 4
23
24 def test_biadjacency_matrix(self):
25 tops = [2, 5, 10]
26 bots = [5, 10, 15]
27 for i in range(len(tops)):
28 G = bipartite.random_graph(tops[i], bots[i], 0.2)
29 top = [n for n, d in G.nodes(data=True) if d["bipartite"] == 0]
30 M = bipartite.biadjacency_matrix(G, top)
31 assert M.shape[0] == tops[i]
32 assert M.shape[1] == bots[i]
33
34 def test_biadjacency_matrix_order(self):
35 G = nx.path_graph(5)
36 G.add_edge(0, 1, weight=2)
37 X = [3, 1]
38 Y = [4, 2, 0]
39 M = bipartite.biadjacency_matrix(G, X, Y, weight="weight")
40 assert M[1, 2] == 2
41
42 def test_null_graph(self):
43 with pytest.raises(nx.NetworkXError):
44 bipartite.biadjacency_matrix(nx.Graph(), [])
45
46 def test_empty_graph(self):
47 with pytest.raises(nx.NetworkXError):
48 bipartite.biadjacency_matrix(nx.Graph([(1, 0)]), [])
49
50 def test_duplicate_row(self):
51 with pytest.raises(nx.NetworkXError):
52 bipartite.biadjacency_matrix(nx.Graph([(1, 0)]), [1, 1])
53
54 def test_duplicate_col(self):
55 with pytest.raises(nx.NetworkXError):
56 bipartite.biadjacency_matrix(nx.Graph([(1, 0)]), [0], [1, 1])
57
58 def test_format_keyword(self):
59 with pytest.raises(nx.NetworkXError):
60 bipartite.biadjacency_matrix(nx.Graph([(1, 0)]), [0], format="foo")
61
62 def test_from_biadjacency_roundtrip(self):
63 B1 = nx.path_graph(5)
64 M = bipartite.biadjacency_matrix(B1, [0, 2, 4])
65 B2 = bipartite.from_biadjacency_matrix(M)
66 assert nx.is_isomorphic(B1, B2)
67
68 def test_from_biadjacency_weight(self):
69 M = sparse.csc_matrix([[1, 2], [0, 3]])
70 B = bipartite.from_biadjacency_matrix(M)
71 assert_edges_equal(B.edges(), [(0, 2), (0, 3), (1, 3)])
72 B = bipartite.from_biadjacency_matrix(M, edge_attribute="weight")
73 e = [(0, 2, {"weight": 1}), (0, 3, {"weight": 2}), (1, 3, {"weight": 3})]
74 assert_edges_equal(B.edges(data=True), e)
75
76 def test_from_biadjacency_multigraph(self):
77 M = sparse.csc_matrix([[1, 2], [0, 3]])
78 B = bipartite.from_biadjacency_matrix(M, create_using=nx.MultiGraph())
79 assert_edges_equal(B.edges(), [(0, 2), (0, 3), (0, 3), (1, 3), (1, 3), (1, 3)])