IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_normalize.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. from icosagon.normalize import add_eye_sparse, \
  2. norm_adj_mat_one_node_type_sparse, \
  3. norm_adj_mat_one_node_type_dense, \
  4. norm_adj_mat_one_node_type, \
  5. norm_adj_mat_two_node_types_sparse, \
  6. norm_adj_mat_two_node_types_dense, \
  7. norm_adj_mat_two_node_types
  8. import decagon_pytorch.normalize
  9. import torch
  10. import pytest
  11. import numpy as np
  12. from math import sqrt
  13. def test_add_eye_sparse_01():
  14. adj_mat_dense = torch.rand((10, 10))
  15. adj_mat_sparse = adj_mat_dense.to_sparse()
  16. adj_mat_dense += torch.eye(10)
  17. adj_mat_sparse = add_eye_sparse(adj_mat_sparse)
  18. assert torch.all(adj_mat_sparse.to_dense() == adj_mat_dense)
  19. def test_add_eye_sparse_02():
  20. adj_mat_dense = torch.rand((10, 20))
  21. adj_mat_sparse = adj_mat_dense.to_sparse()
  22. with pytest.raises(ValueError):
  23. _ = add_eye_sparse(adj_mat_sparse)
  24. def test_add_eye_sparse_03():
  25. adj_mat_dense = torch.rand((10, 10))
  26. with pytest.raises(ValueError):
  27. _ = add_eye_sparse(adj_mat_dense)
  28. def test_add_eye_sparse_04():
  29. adj_mat_dense = np.random.rand(10, 10)
  30. with pytest.raises(ValueError):
  31. _ = add_eye_sparse(adj_mat_dense)
  32. def test_norm_adj_mat_one_node_type_sparse_01():
  33. adj_mat = torch.rand((10, 10))
  34. adj_mat = (adj_mat > .5)
  35. adj_mat = adj_mat.to_sparse()
  36. _ = norm_adj_mat_one_node_type_sparse(adj_mat)
  37. def test_norm_adj_mat_one_node_type_sparse_02():
  38. adj_mat_dense = torch.rand((10, 10))
  39. adj_mat_dense = (adj_mat_dense > .5)
  40. adj_mat_sparse = adj_mat_dense.to_sparse()
  41. adj_mat_sparse = norm_adj_mat_one_node_type_sparse(adj_mat_sparse)
  42. adj_mat_dense = norm_adj_mat_one_node_type_dense(adj_mat_dense)
  43. assert torch.all(adj_mat_sparse.to_dense() - adj_mat_dense < 0.000001)
  44. def test_norm_adj_mat_one_node_type_dense_01():
  45. adj_mat = torch.rand((10, 10))
  46. adj_mat = (adj_mat > .5)
  47. _ = norm_adj_mat_one_node_type_dense(adj_mat)
  48. def test_norm_adj_mat_one_node_type_dense_02():
  49. adj_mat = torch.tensor([
  50. [0, 1, 1, 0], # 3
  51. [1, 0, 1, 0], # 3
  52. [1, 1, 0, 1], # 4
  53. [0, 0, 1, 0] # 2
  54. # 3 3 4 2
  55. ])
  56. expect_denom = np.array([
  57. [ 3, 3, sqrt(3)*2, sqrt(6) ],
  58. [ 3, 3, sqrt(3)*2, sqrt(6) ],
  59. [ sqrt(3)*2, sqrt(3)*2, 4, sqrt(2)*2 ],
  60. [ sqrt(6), sqrt(6), sqrt(2)*2, 2 ]
  61. ], dtype=np.float32)
  62. expect = (adj_mat.detach().cpu().numpy().astype(np.float32) + np.eye(4)) / expect_denom
  63. # expect = np.array([
  64. # [1/3, 1/3, 1/3, 0],
  65. # [1/3, 1/3, 1/3, 0],
  66. # [1/4, 1/4, 1/4, 1/4],
  67. # [0, 0, 1/2, 1/2]
  68. # ], dtype=np.float32)
  69. res = decagon_pytorch.normalize.norm_adj_mat_one_node_type(adj_mat)
  70. res = res.todense().astype(np.float32)
  71. print('res:', res)
  72. print('expect:', expect)
  73. assert np.all(res - expect < 0.000001)
  74. def test_norm_adj_mat_one_node_type_dense_03():
  75. # adj_mat = torch.rand((10, 10))
  76. adj_mat = torch.tensor([
  77. [0, 1, 1, 0, 0],
  78. [1, 0, 1, 0, 1],
  79. [1, 1, 0, .5, .5],
  80. [0, 0, .5, 0, 1],
  81. [0, 1, .5, 1, 0]
  82. ])
  83. # adj_mat = (adj_mat > .5)
  84. adj_mat_dec = decagon_pytorch.normalize.norm_adj_mat_one_node_type(adj_mat)
  85. adj_mat_ico = norm_adj_mat_one_node_type_dense(adj_mat)
  86. adj_mat_dec = adj_mat_dec.todense()
  87. adj_mat_ico = adj_mat_ico.detach().cpu().numpy()
  88. print('adj_mat_dec:', adj_mat_dec)
  89. print('adj_mat_ico:', adj_mat_ico)
  90. assert np.all(adj_mat_dec - adj_mat_ico < 0.000001)
  91. def test_norm_adj_mat_two_node_types_sparse_01():
  92. adj_mat = torch.rand((10, 20))
  93. adj_mat = (adj_mat > .5)
  94. adj_mat = adj_mat.to_sparse()
  95. _ = norm_adj_mat_two_node_types_sparse(adj_mat)
  96. def test_norm_adj_mat_two_node_types_sparse_02():
  97. adj_mat_dense = torch.rand((10, 20))
  98. adj_mat_dense = (adj_mat_dense > .5)
  99. adj_mat_sparse = adj_mat_dense.to_sparse()
  100. adj_mat_sparse = norm_adj_mat_two_node_types_sparse(adj_mat_sparse)
  101. adj_mat_dense = norm_adj_mat_two_node_types_dense(adj_mat_dense)
  102. assert torch.all(adj_mat_sparse.to_dense() - adj_mat_dense < 0.000001)
  103. def test_norm_adj_mat_two_node_types_dense_01():
  104. adj_mat = torch.rand((10, 20))
  105. adj_mat = (adj_mat > .5)
  106. _ = norm_adj_mat_two_node_types_dense(adj_mat)
  107. def test_norm_adj_mat_two_node_types_dense_02():
  108. adj_mat = torch.tensor([
  109. [0, 1, 1, 0], # 2
  110. [1, 0, 1, 0], # 2
  111. [1, 1, 0, 1], # 3
  112. [0, 0, 1, 0] # 1
  113. # 2 2 3 1
  114. ])
  115. expect_denom = np.array([
  116. [ 2, 2, sqrt(6), sqrt(2) ],
  117. [ 2, 2, sqrt(6), sqrt(2) ],
  118. [ sqrt(6), sqrt(6), 3, sqrt(3) ],
  119. [ sqrt(2), sqrt(2), sqrt(3), 1 ]
  120. ], dtype=np.float32)
  121. expect = adj_mat.detach().cpu().numpy().astype(np.float32) / expect_denom
  122. res = decagon_pytorch.normalize.norm_adj_mat_two_node_types(adj_mat)
  123. res = res.todense().astype(np.float32)
  124. print('res:', res)
  125. print('expect:', expect)
  126. assert np.all(res - expect < 0.000001)
  127. def test_norm_adj_mat_two_node_types_dense_03():
  128. adj_mat = torch.tensor([
  129. [0, 1, 1, 0, 0],
  130. [1, 0, 1, 0, 1],
  131. [1, 1, 0, .5, .5],
  132. [0, 0, .5, 0, 1],
  133. [0, 1, .5, 1, 0]
  134. ])
  135. adj_mat_dec = decagon_pytorch.normalize.norm_adj_mat_two_node_types(adj_mat)
  136. adj_mat_ico = norm_adj_mat_two_node_types_dense(adj_mat)
  137. adj_mat_dec = adj_mat_dec.todense()
  138. adj_mat_ico = adj_mat_ico.detach().cpu().numpy()
  139. print('adj_mat_dec:', adj_mat_dec)
  140. print('adj_mat_ico:', adj_mat_ico)
  141. assert np.all(adj_mat_dec - adj_mat_ico < 0.000001)
  142. def test_norm_adj_mat_two_node_types_dense_04():
  143. adj_mat = torch.rand((10, 20))
  144. adj_mat_dec = decagon_pytorch.normalize.norm_adj_mat_two_node_types(adj_mat)
  145. adj_mat_ico = norm_adj_mat_two_node_types_dense(adj_mat)
  146. adj_mat_dec = adj_mat_dec.todense()
  147. adj_mat_ico = adj_mat_ico.detach().cpu().numpy()
  148. print('adj_mat_dec:', adj_mat_dec)
  149. print('adj_mat_ico:', adj_mat_ico)
  150. assert np.all(adj_mat_dec - adj_mat_ico < 0.000001)