test_graphmatrix.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import pytest
  2. np = pytest.importorskip("numpy")
  3. pytest.importorskip("scipy")
  4. import networkx as nx
  5. from networkx.exception import NetworkXError
  6. from networkx.generators.degree_seq import havel_hakimi_graph
  7. def test_incidence_matrix_simple():
  8. deg = [3, 2, 2, 1, 0]
  9. G = havel_hakimi_graph(deg)
  10. deg = [(1, 0), (1, 0), (1, 0), (2, 0), (1, 0), (2, 1), (0, 1), (0, 1)]
  11. MG = nx.random_clustered_graph(deg, seed=42)
  12. I = nx.incidence_matrix(G).todense().astype(int)
  13. # fmt: off
  14. expected = np.array(
  15. [[1, 1, 1, 0],
  16. [0, 1, 0, 1],
  17. [1, 0, 0, 1],
  18. [0, 0, 1, 0],
  19. [0, 0, 0, 0]]
  20. )
  21. # fmt: on
  22. np.testing.assert_equal(I, expected)
  23. I = nx.incidence_matrix(MG).todense().astype(int)
  24. # fmt: off
  25. expected = np.array(
  26. [[1, 0, 0, 0, 0, 0, 0],
  27. [1, 0, 0, 0, 0, 0, 0],
  28. [0, 1, 0, 0, 0, 0, 0],
  29. [0, 0, 0, 0, 0, 0, 0],
  30. [0, 1, 0, 0, 0, 0, 0],
  31. [0, 0, 0, 0, 1, 1, 0],
  32. [0, 0, 0, 0, 0, 1, 1],
  33. [0, 0, 0, 0, 1, 0, 1]]
  34. )
  35. # fmt: on
  36. np.testing.assert_equal(I, expected)
  37. with pytest.raises(NetworkXError):
  38. nx.incidence_matrix(G, nodelist=[0, 1])
  39. class TestGraphMatrix:
  40. @classmethod
  41. def setup_class(cls):
  42. deg = [3, 2, 2, 1, 0]
  43. cls.G = havel_hakimi_graph(deg)
  44. # fmt: off
  45. cls.OI = np.array(
  46. [[-1, -1, -1, 0],
  47. [1, 0, 0, -1],
  48. [0, 1, 0, 1],
  49. [0, 0, 1, 0],
  50. [0, 0, 0, 0]]
  51. )
  52. cls.A = np.array(
  53. [[0, 1, 1, 1, 0],
  54. [1, 0, 1, 0, 0],
  55. [1, 1, 0, 0, 0],
  56. [1, 0, 0, 0, 0],
  57. [0, 0, 0, 0, 0]]
  58. )
  59. # fmt: on
  60. cls.WG = havel_hakimi_graph(deg)
  61. cls.WG.add_edges_from(
  62. (u, v, {"weight": 0.5, "other": 0.3}) for (u, v) in cls.G.edges()
  63. )
  64. # fmt: off
  65. cls.WA = np.array(
  66. [[0, 0.5, 0.5, 0.5, 0],
  67. [0.5, 0, 0.5, 0, 0],
  68. [0.5, 0.5, 0, 0, 0],
  69. [0.5, 0, 0, 0, 0],
  70. [0, 0, 0, 0, 0]]
  71. )
  72. # fmt: on
  73. cls.MG = nx.MultiGraph(cls.G)
  74. cls.MG2 = cls.MG.copy()
  75. cls.MG2.add_edge(0, 1)
  76. # fmt: off
  77. cls.MG2A = np.array(
  78. [[0, 2, 1, 1, 0],
  79. [2, 0, 1, 0, 0],
  80. [1, 1, 0, 0, 0],
  81. [1, 0, 0, 0, 0],
  82. [0, 0, 0, 0, 0]]
  83. )
  84. cls.MGOI = np.array(
  85. [[-1, -1, -1, -1, 0],
  86. [1, 1, 0, 0, -1],
  87. [0, 0, 1, 0, 1],
  88. [0, 0, 0, 1, 0],
  89. [0, 0, 0, 0, 0]]
  90. )
  91. # fmt: on
  92. cls.no_edges_G = nx.Graph([(1, 2), (3, 2, {"weight": 8})])
  93. cls.no_edges_A = np.array([[0, 0], [0, 0]])
  94. def test_incidence_matrix(self):
  95. "Conversion to incidence matrix"
  96. I = (
  97. nx.incidence_matrix(
  98. self.G,
  99. nodelist=sorted(self.G),
  100. edgelist=sorted(self.G.edges()),
  101. oriented=True,
  102. )
  103. .todense()
  104. .astype(int)
  105. )
  106. np.testing.assert_equal(I, self.OI)
  107. I = (
  108. nx.incidence_matrix(
  109. self.G,
  110. nodelist=sorted(self.G),
  111. edgelist=sorted(self.G.edges()),
  112. oriented=False,
  113. )
  114. .todense()
  115. .astype(int)
  116. )
  117. np.testing.assert_equal(I, np.abs(self.OI))
  118. I = (
  119. nx.incidence_matrix(
  120. self.MG,
  121. nodelist=sorted(self.MG),
  122. edgelist=sorted(self.MG.edges()),
  123. oriented=True,
  124. )
  125. .todense()
  126. .astype(int)
  127. )
  128. np.testing.assert_equal(I, self.OI)
  129. I = (
  130. nx.incidence_matrix(
  131. self.MG,
  132. nodelist=sorted(self.MG),
  133. edgelist=sorted(self.MG.edges()),
  134. oriented=False,
  135. )
  136. .todense()
  137. .astype(int)
  138. )
  139. np.testing.assert_equal(I, np.abs(self.OI))
  140. I = (
  141. nx.incidence_matrix(
  142. self.MG2,
  143. nodelist=sorted(self.MG2),
  144. edgelist=sorted(self.MG2.edges()),
  145. oriented=True,
  146. )
  147. .todense()
  148. .astype(int)
  149. )
  150. np.testing.assert_equal(I, self.MGOI)
  151. I = (
  152. nx.incidence_matrix(
  153. self.MG2,
  154. nodelist=sorted(self.MG),
  155. edgelist=sorted(self.MG2.edges()),
  156. oriented=False,
  157. )
  158. .todense()
  159. .astype(int)
  160. )
  161. np.testing.assert_equal(I, np.abs(self.MGOI))
  162. def test_weighted_incidence_matrix(self):
  163. I = (
  164. nx.incidence_matrix(
  165. self.WG,
  166. nodelist=sorted(self.WG),
  167. edgelist=sorted(self.WG.edges()),
  168. oriented=True,
  169. )
  170. .todense()
  171. .astype(int)
  172. )
  173. np.testing.assert_equal(I, self.OI)
  174. I = (
  175. nx.incidence_matrix(
  176. self.WG,
  177. nodelist=sorted(self.WG),
  178. edgelist=sorted(self.WG.edges()),
  179. oriented=False,
  180. )
  181. .todense()
  182. .astype(int)
  183. )
  184. np.testing.assert_equal(I, np.abs(self.OI))
  185. # np.testing.assert_equal(nx.incidence_matrix(self.WG,oriented=True,
  186. # weight='weight').todense(),0.5*self.OI)
  187. # np.testing.assert_equal(nx.incidence_matrix(self.WG,weight='weight').todense(),
  188. # np.abs(0.5*self.OI))
  189. # np.testing.assert_equal(nx.incidence_matrix(self.WG,oriented=True,weight='other').todense(),
  190. # 0.3*self.OI)
  191. I = nx.incidence_matrix(
  192. self.WG,
  193. nodelist=sorted(self.WG),
  194. edgelist=sorted(self.WG.edges()),
  195. oriented=True,
  196. weight="weight",
  197. ).todense()
  198. np.testing.assert_equal(I, 0.5 * self.OI)
  199. I = nx.incidence_matrix(
  200. self.WG,
  201. nodelist=sorted(self.WG),
  202. edgelist=sorted(self.WG.edges()),
  203. oriented=False,
  204. weight="weight",
  205. ).todense()
  206. np.testing.assert_equal(I, np.abs(0.5 * self.OI))
  207. I = nx.incidence_matrix(
  208. self.WG,
  209. nodelist=sorted(self.WG),
  210. edgelist=sorted(self.WG.edges()),
  211. oriented=True,
  212. weight="other",
  213. ).todense()
  214. np.testing.assert_equal(I, 0.3 * self.OI)
  215. # WMG=nx.MultiGraph(self.WG)
  216. # WMG.add_edge(0,1,weight=0.5,other=0.3)
  217. # np.testing.assert_equal(nx.incidence_matrix(WMG,weight='weight').todense(),
  218. # np.abs(0.5*self.MGOI))
  219. # np.testing.assert_equal(nx.incidence_matrix(WMG,weight='weight',oriented=True).todense(),
  220. # 0.5*self.MGOI)
  221. # np.testing.assert_equal(nx.incidence_matrix(WMG,weight='other',oriented=True).todense(),
  222. # 0.3*self.MGOI)
  223. WMG = nx.MultiGraph(self.WG)
  224. WMG.add_edge(0, 1, weight=0.5, other=0.3)
  225. I = nx.incidence_matrix(
  226. WMG,
  227. nodelist=sorted(WMG),
  228. edgelist=sorted(WMG.edges(keys=True)),
  229. oriented=True,
  230. weight="weight",
  231. ).todense()
  232. np.testing.assert_equal(I, 0.5 * self.MGOI)
  233. I = nx.incidence_matrix(
  234. WMG,
  235. nodelist=sorted(WMG),
  236. edgelist=sorted(WMG.edges(keys=True)),
  237. oriented=False,
  238. weight="weight",
  239. ).todense()
  240. np.testing.assert_equal(I, np.abs(0.5 * self.MGOI))
  241. I = nx.incidence_matrix(
  242. WMG,
  243. nodelist=sorted(WMG),
  244. edgelist=sorted(WMG.edges(keys=True)),
  245. oriented=True,
  246. weight="other",
  247. ).todense()
  248. np.testing.assert_equal(I, 0.3 * self.MGOI)
  249. def test_adjacency_matrix(self):
  250. "Conversion to adjacency matrix"
  251. np.testing.assert_equal(nx.adjacency_matrix(self.G).todense(), self.A)
  252. np.testing.assert_equal(nx.adjacency_matrix(self.MG).todense(), self.A)
  253. np.testing.assert_equal(nx.adjacency_matrix(self.MG2).todense(), self.MG2A)
  254. np.testing.assert_equal(
  255. nx.adjacency_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2]
  256. )
  257. np.testing.assert_equal(nx.adjacency_matrix(self.WG).todense(), self.WA)
  258. np.testing.assert_equal(
  259. nx.adjacency_matrix(self.WG, weight=None).todense(), self.A
  260. )
  261. np.testing.assert_equal(
  262. nx.adjacency_matrix(self.MG2, weight=None).todense(), self.MG2A
  263. )
  264. np.testing.assert_equal(
  265. nx.adjacency_matrix(self.WG, weight="other").todense(), 0.6 * self.WA
  266. )
  267. np.testing.assert_equal(
  268. nx.adjacency_matrix(self.no_edges_G, nodelist=[1, 3]).todense(),
  269. self.no_edges_A,
  270. )