Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
gyzhou2000 committed Jul 6, 2024
1 parent 0b975ff commit b37e904
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 77 deletions.
1 change: 0 additions & 1 deletion tests/utils/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ def test_check():
assert check_is_numpy([1, 2, 3], np.array([4, 5, 6]), {'key': 'value'}, 42) == True
assert check_is_numpy(None) == False
assert check_is_numpy(np.array([[1, 2, 3], [4, 5, 6]])) == True

20 changes: 1 addition & 19 deletions tests/utils/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import networkx as nx
import scipy.sparse as ssp
from gammagl.utils.convert import to_scipy_sparse_matrix, to_networkx
from gammagl.data import Graph

def test_convert():
edge_index = tlx.convert_to_tensor([
Expand All @@ -18,24 +19,6 @@ def test_convert():
), shape=(4, 4))
assert np.allclose(scipy_matrix.toarray(), expected_scipy_matrix.toarray())

class NodeStore:
def __init__(self, num_nodes, key):
self.num_nodes = num_nodes
self._key = key

class EdgeStore:
def __init__(self, edge_index, key):
self.edge_index = edge_index
self._key = key

class Graph:
def __init__(self, edge_index, num_nodes):
self.edge_index = edge_index
self.num_nodes = num_nodes
self.node_offsets = {'default': 0}
self.node_stores = [NodeStore(num_nodes, 'default')]
self.edge_stores = [EdgeStore(edge_index, 'default')]

edge_index = tlx.convert_to_tensor([
[0, 1, 1, 2, 2, 3],
[1, 0, 2, 1, 3, 2],
Expand All @@ -48,4 +31,3 @@ def __init__(self, edge_index, num_nodes):
expected_edges = [(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2)]
expected_G.add_edges_from(expected_edges)
assert nx.is_isomorphic(G, expected_G)

9 changes: 3 additions & 6 deletions tests/utils/test_corrupt_graph.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from gammagl.data import Graph
import torch
from gammagl.utils.corrupt_graph import dfde_norm_g
import tensorlayerx as tlx
import numpy as np


def test_dfde_norm_g():
x = tlx.convert_to_tensor(np.array([[1, 0], [0, 1], [1, 1], [0, 0]], dtype=np.float32))
edge_index = tlx.convert_to_tensor(np.array([[0, 1, 2, 3], [1, 2, 3, 0]], dtype=np.int64))
Expand All @@ -12,7 +13,3 @@ def test_dfde_norm_g():
assert new_g.edge_index.shape[0] == 2, "Edge index shape mismatch"
assert new_g.x.shape[1] == x.shape[1], "Node feature shape mismatch"
assert new_g.edge_weight.shape[0] == new_g.edge_index.shape[1], "Edge weight shape mismatch"




11 changes: 2 additions & 9 deletions tests/utils/test_data_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import tempfile
from gammagl.utils.data_utils import download_file,save_cache,load_cache


def test_download_file():
url = "https://raw.githubusercontent.com/tensorflow/tensorflow/master/README.md"
save_path = tempfile.mktemp()
Expand All @@ -14,12 +16,3 @@ def test_save_load_cache():
assert os.path.exists(cache_path), "Cache file was not created."
loaded_obj = load_cache(cache_path)
assert loaded_obj == test_obj, "Loaded object does not match the saved object."

def run_tests():
test_download_file()
print("test_download_file passed.")
test_save_load_cache()
print("test_save_load_cache passed.")

if __name__ == "__main__":
run_tests()
5 changes: 1 addition & 4 deletions tests/utils/test_device.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# test_set_device.py
import tensorlayerx as tlx
import pytest
from gammagl.utils.device import set_device


def test_set_device_gpu():
if tlx.is_gpu_available():
try:
Expand All @@ -17,6 +17,3 @@ def test_set_device_cpu():
assert tlx.get_device() == "CPU:0", "Failed to set CPU device"
except Exception as e:
pytest.fail(f"Setting CPU device raised an exception: {e}")

if __name__ == "__main__":
pytest.main()
11 changes: 4 additions & 7 deletions tests/utils/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class Example:
def method1(self, a: int, b: str = "default") -> None:
def method1(self, a: int, b: str) -> None:
pass

def method2(self, x: float) -> None:
Expand All @@ -14,8 +14,8 @@ def test_inspector():
inspector = Inspector(example)

# Inspect the methods
inspector.inspect(example.method1, pop_first=True)
inspector.inspect(example.method2, pop_first=True)
inspector.inspect(example.method1, pop_first=False)
inspector.inspect(example.method2, pop_first=False)

# Debugging information
print("Inspector params after inspecting method1 and method2:")
Expand All @@ -24,7 +24,7 @@ def test_inspector():
# Test keys method
keys = inspector.keys()
expected_keys = {"a", "b", "x"}
assert keys == expected_keys, f"Unexpected keys: {keys}" #通不过,为什么?没找到原因
assert keys == expected_keys, f"Unexpected keys: {keys}"

# Test implements method
assert inspector.implements("method1") is True, "method1 should be implemented"
Expand All @@ -47,6 +47,3 @@ def test_inspector():
params = inspector.distribute("method2", {"x": 2.5})
expected_params = {"x": 2.5}
assert params == expected_params, f"Unexpected distributed params: {params}"

if __name__ == "__main__":
pytest.main()
2 changes: 0 additions & 2 deletions tests/utils/test_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@ def test_calc_gcn_norm():
])
weights_np = tlx.convert_to_numpy(weights)
assert np.allclose(weights_np, expected_weights)


14 changes: 7 additions & 7 deletions tests/utils/test_num_nodes.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import numpy as np
import torch
from gammagl.utils.num_nodes import maybe_num_nodes, maybe_num_nodes_dict
from copy import copy
from gammagl.utils.check import check_is_numpy
import tensorlayerx as tlx


def test_num_nodes():
edge_index_dict = {
('social', 'user-user'): np.array([[0, 1, 1, 2], [1, 0, 2, 1]]),
('knowledge', 'concept-concept'): np.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])
('social', 'user-user'): tlx.convert_to_tensor([[0, 1, 1, 2], [1, 0, 2, 1]]),
('knowledge', 'concept-concept'): tlx.convert_to_tensor([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])
}
edge_index_tensor = torch.tensor(edge_index_dict[('social', 'user-user')])
num_nodes = maybe_num_nodes(edge_index_tensor)
edge_index_tensor = edge_index_dict[('social', 'user-user')]
num_nodes = maybe_num_nodes(edge_index_tensor)
assert num_nodes == 3
num_nodes_dict = maybe_num_nodes_dict(edge_index_dict)
expected_num_nodes_dict = {'social': 3, 'knowledge': 4}
num_nodes_dict = maybe_num_nodes_dict(edge_index_dict)
expected_num_nodes_dict = {'social': 3, 'user-user': 3, 'knowledge': 4, 'concept-concept': 4}
assert num_nodes_dict == expected_num_nodes_dict
15 changes: 0 additions & 15 deletions tests/utils/test_ops.py

This file was deleted.

4 changes: 3 additions & 1 deletion tests/utils/test_read_embeddings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import tempfile
from gammagl.utils.read_embeddings import read_embeddings


def test_read_embeddings():
# Create a temporary file with example embeddings
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as tmpfile:
Expand All @@ -25,4 +27,4 @@ def test_read_embeddings():
# Clean up the temporary file
import os
os.remove(tmpfile_name)
print("Test passed!")
print("Test passed!")
9 changes: 7 additions & 2 deletions tests/utils/test_shortest_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
from gammagl.data import Graph
from gammagl.utils.convert import to_networkx
from gammagl.utils.shortest_path import floyd_warshall_source_to_all,all_pairs_shortest_path,shortest_path_distance,batched_shortest_path_distance


def test_shortest_path():
edges = [(0, 1), (1, 2), (2, 3), (3, 0), (1, 3)]
num_nodes = 4
G = nx.Graph()
G.add_edges_from(edges)
edge_index = np.array(edges).T
data = Graph(x=None,edge_index=edge_index)
G_nx =G
G_nx = G
source = 0
node_paths, edge_paths = floyd_warshall_source_to_all(G_nx, source)
print(node_paths)
print(edge_paths)

assert node_paths[3] == [0, 1, 3], f"Expected [0, 1, 3], but got {node_paths[3]}"
assert edge_paths[3] == [0, 3], f"Expected [0, 3], but got {edge_paths[3]}"
node_paths, edge_paths = all_pairs_shortest_path(G_nx)
Expand All @@ -25,4 +30,4 @@ def test_shortest_path():
batch_data = [data, data]
node_paths, edge_paths = batched_shortest_path_distance(batch_data)
assert node_paths[0][3] == [0, 1, 3], f"Expected [0, 1, 3], but got {node_paths[0][3]}"
assert edge_paths[0][3] == [0, 3], f"Expected [0, 3], but got {edge_paths[0][3]}"
assert edge_paths[0][3] == [0, 3], f"Expected [0, 3], but got {edge_paths[0][3]}"
4 changes: 2 additions & 2 deletions tests/utils/test_smiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from gammagl.data import Graph
from gammagl.utils.smiles import from_smiles


def test_from_smiles():
smiles = 'CCO'
graph = from_smiles(smiles)
Expand All @@ -17,7 +18,7 @@ def test_from_smiles():
[0, 1], [1, 0],
[1, 2], [2, 1],
]
expected_edge_indices = tlx.convert_to_tensor(expected_edge_indices).T
expected_edge_indices = tlx.transpose(tlx.convert_to_tensor(expected_edge_indices))
expected_edge_features = [
[1, 0, 0], [1, 0, 0],
[1, 0, 0], [1, 0, 0],
Expand All @@ -26,4 +27,3 @@ def test_from_smiles():
assert tlx.convert_to_numpy(graph.x).tolist() == tlx.convert_to_numpy(expected_node_features).tolist()
assert tlx.convert_to_numpy(graph.edge_index).tolist() == tlx.convert_to_numpy(expected_edge_indices).tolist()
assert tlx.convert_to_numpy(graph.edge_attr).tolist() == tlx.convert_to_numpy(expected_edge_features).tolist()

3 changes: 2 additions & 1 deletion tests/utils/test_spm_calc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import scipy.sparse as sp
from gammagl.utils.spm_calc import calc_A_norm_hat


def test_calc_A_norm_hat():
edge_index = np.array([
[0, 1, 1, 2],
Expand All @@ -17,4 +19,3 @@ def test_calc_A_norm_hat():
expected_output = D_invsqrt_corr @ A @ D_invsqrt_corr
result = calc_A_norm_hat(edge_index).toarray()
assert np.allclose(result, expected_output)

1 change: 0 additions & 1 deletion tests/utils/test_to_dense_adj.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import tensorlayerx as tlx
from gammagl.mpops import unsorted_segment_sum
from gammagl.utils.to_dense_adj import to_dense_adj
Expand Down

0 comments on commit b37e904

Please sign in to comment.