test_graphmatrix.py 8.5 KB

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