|
|
@@ -6,6 +6,7 @@ import decagon_pytorch.normalize |
|
|
|
import torch
|
|
|
|
import pytest
|
|
|
|
import numpy as np
|
|
|
|
from math import sqrt
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_eye_sparse_01():
|
|
|
@@ -53,7 +54,7 @@ def test_norm_adj_mat_one_node_type_sparse_02(): |
|
|
|
adj_mat_sparse = adj_mat_dense.to_sparse()
|
|
|
|
adj_mat_sparse = norm_adj_mat_one_node_type_sparse(adj_mat_sparse)
|
|
|
|
adj_mat_dense = norm_adj_mat_one_node_type_dense(adj_mat_dense)
|
|
|
|
assert torch.all(adj_mat_sparse.to_dense() == adj_mat_dense)
|
|
|
|
assert torch.all(adj_mat_sparse.to_dense() - adj_mat_dense < 0.000001)
|
|
|
|
|
|
|
|
|
|
|
|
def test_norm_adj_mat_one_node_type_dense_01():
|
|
|
@@ -68,28 +69,42 @@ def test_norm_adj_mat_one_node_type_dense_02(): |
|
|
|
[1, 0, 1, 0], # 3
|
|
|
|
[1, 1, 0, 1], # 4
|
|
|
|
[0, 0, 1, 0] # 2
|
|
|
|
# 3 3 4 2
|
|
|
|
])
|
|
|
|
expect = np.array([
|
|
|
|
[1/3, 1/3, 1/3, 0],
|
|
|
|
[1/3, 1/3, 1/3, 0],
|
|
|
|
[1/4, 1/4, 1/4, 1/4],
|
|
|
|
[0, 0, 1/2, 1/2]
|
|
|
|
expect_denom = np.array([
|
|
|
|
[ 3, 3, sqrt(3)*2, sqrt(6) ],
|
|
|
|
[ 3, 3, sqrt(3)*2, sqrt(6) ],
|
|
|
|
[ sqrt(3)*2, sqrt(3)*2, 4, sqrt(2)*2 ],
|
|
|
|
[ sqrt(6), sqrt(6), sqrt(2)*2, 2 ]
|
|
|
|
], dtype=np.float32)
|
|
|
|
expect = (adj_mat.detach().cpu().numpy().astype(np.float32) + np.eye(4)) / expect_denom
|
|
|
|
# expect = np.array([
|
|
|
|
# [1/3, 1/3, 1/3, 0],
|
|
|
|
# [1/3, 1/3, 1/3, 0],
|
|
|
|
# [1/4, 1/4, 1/4, 1/4],
|
|
|
|
# [0, 0, 1/2, 1/2]
|
|
|
|
# ], dtype=np.float32)
|
|
|
|
res = decagon_pytorch.normalize.norm_adj_mat_one_node_type(adj_mat)
|
|
|
|
res = res.todense().astype(np.float32)
|
|
|
|
print('res:', res)
|
|
|
|
print('expect:', expect)
|
|
|
|
assert torch.all(res == expect)
|
|
|
|
assert np.all(res - expect < 0.000001)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip
|
|
|
|
def test_norm_adj_mat_one_node_type_dense_03():
|
|
|
|
adj_mat = torch.rand((10, 10))
|
|
|
|
adj_mat = (adj_mat > .5)
|
|
|
|
# adj_mat = torch.rand((10, 10))
|
|
|
|
adj_mat = torch.tensor([
|
|
|
|
[0, 1, 1, 0, 0],
|
|
|
|
[1, 0, 1, 0, 1],
|
|
|
|
[1, 1, 0, .5, .5],
|
|
|
|
[0, 0, .5, 0, 1],
|
|
|
|
[0, 1, .5, 1, 0]
|
|
|
|
])
|
|
|
|
# adj_mat = (adj_mat > .5)
|
|
|
|
adj_mat_dec = decagon_pytorch.normalize.norm_adj_mat_one_node_type(adj_mat)
|
|
|
|
adj_mat_ico = norm_adj_mat_one_node_type_dense(adj_mat)
|
|
|
|
adj_mat_dec = adj_mat_dec.todense()
|
|
|
|
adj_mat_ico = adj_mat_ico.detach().cpu().numpy()
|
|
|
|
print('adj_mat_dec:', adj_mat_dec)
|
|
|
|
print('adj_mat_ico:', adj_mat_ico)
|
|
|
|
assert np.all(adj_mat_dec == adj_mat_ico)
|
|
|
|
assert np.all(adj_mat_dec - adj_mat_ico < 0.000001)
|