From b37e9044e8fb0257ebe6a7a5d6ac763cc5cbbe9e Mon Sep 17 00:00:00 2001 From: gyzhou2000 Date: Sat, 6 Jul 2024 18:59:26 +0800 Subject: [PATCH] update --- tests/utils/test_check.py | 1 - tests/utils/test_convert.py | 20 +------------------- tests/utils/test_corrupt_graph.py | 9 +++------ tests/utils/test_data_utils.py | 11 ++--------- tests/utils/test_device.py | 5 +---- tests/utils/test_inspector.py | 11 ++++------- tests/utils/test_norm.py | 2 -- tests/utils/test_num_nodes.py | 14 +++++++------- tests/utils/test_ops.py | 15 --------------- tests/utils/test_read_embeddings.py | 4 +++- tests/utils/test_shortest_path.py | 9 +++++++-- tests/utils/test_smiles.py | 4 ++-- tests/utils/test_spm_calc.py | 3 ++- tests/utils/test_to_dense_adj.py | 1 - 14 files changed, 32 insertions(+), 77 deletions(-) delete mode 100644 tests/utils/test_ops.py diff --git a/tests/utils/test_check.py b/tests/utils/test_check.py index d51c9e9a..f39551f6 100644 --- a/tests/utils/test_check.py +++ b/tests/utils/test_check.py @@ -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 - diff --git a/tests/utils/test_convert.py b/tests/utils/test_convert.py index b7e8baf2..fd3c354a 100644 --- a/tests/utils/test_convert.py +++ b/tests/utils/test_convert.py @@ -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([ @@ -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], @@ -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) - diff --git a/tests/utils/test_corrupt_graph.py b/tests/utils/test_corrupt_graph.py index 603efdfb..0aa65237 100644 --- a/tests/utils/test_corrupt_graph.py +++ b/tests/utils/test_corrupt_graph.py @@ -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)) @@ -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" - - - - diff --git a/tests/utils/test_data_utils.py b/tests/utils/test_data_utils.py index 1cffcd61..9aa24b7c 100644 --- a/tests/utils/test_data_utils.py +++ b/tests/utils/test_data_utils.py @@ -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() @@ -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() diff --git a/tests/utils/test_device.py b/tests/utils/test_device.py index 8e5716a5..32ef97a2 100644 --- a/tests/utils/test_device.py +++ b/tests/utils/test_device.py @@ -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: @@ -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() diff --git a/tests/utils/test_inspector.py b/tests/utils/test_inspector.py index 32afce0f..e03b6e21 100644 --- a/tests/utils/test_inspector.py +++ b/tests/utils/test_inspector.py @@ -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: @@ -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:") @@ -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" @@ -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() diff --git a/tests/utils/test_norm.py b/tests/utils/test_norm.py index 8e29a80c..ad63a486 100644 --- a/tests/utils/test_norm.py +++ b/tests/utils/test_norm.py @@ -24,5 +24,3 @@ def test_calc_gcn_norm(): ]) weights_np = tlx.convert_to_numpy(weights) assert np.allclose(weights_np, expected_weights) - - diff --git a/tests/utils/test_num_nodes.py b/tests/utils/test_num_nodes.py index 5e7e662c..3b37cf3a 100644 --- a/tests/utils/test_num_nodes.py +++ b/tests/utils/test_num_nodes.py @@ -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 diff --git a/tests/utils/test_ops.py b/tests/utils/test_ops.py deleted file mode 100644 index 75118d39..00000000 --- a/tests/utils/test_ops.py +++ /dev/null @@ -1,15 +0,0 @@ -import numpy as np -import paddle -from gammagl.utils.ops import get_index_from_counts - -def test_get_index_from_counts(): - counts_np = np.array([2, 3, 4]) - counts_pd = paddle.to_tensor(counts_np) - expected_index_np = np.array([0, 2, 5, 9]) - expected_index_pd = paddle.to_tensor(expected_index_np) - index_np = get_index_from_counts(counts_np) - index_pd = get_index_from_counts(counts_pd) - assert np.array_equal(index_np, expected_index_np) - assert paddle.allclose(index_pd, expected_index_pd) - - diff --git a/tests/utils/test_read_embeddings.py b/tests/utils/test_read_embeddings.py index adb883b5..b6c562b6 100644 --- a/tests/utils/test_read_embeddings.py +++ b/tests/utils/test_read_embeddings.py @@ -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: @@ -25,4 +27,4 @@ def test_read_embeddings(): # Clean up the temporary file import os os.remove(tmpfile_name) - print("Test passed!") \ No newline at end of file + print("Test passed!") diff --git a/tests/utils/test_shortest_path.py b/tests/utils/test_shortest_path.py index fce86106..ad7ac8eb 100644 --- a/tests/utils/test_shortest_path.py +++ b/tests/utils/test_shortest_path.py @@ -4,6 +4,8 @@ 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 @@ -11,9 +13,12 @@ def test_shortest_path(): 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) @@ -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]}" \ No newline at end of file + assert edge_paths[0][3] == [0, 3], f"Expected [0, 3], but got {edge_paths[0][3]}" diff --git a/tests/utils/test_smiles.py b/tests/utils/test_smiles.py index 8ffecebd..635520b8 100644 --- a/tests/utils/test_smiles.py +++ b/tests/utils/test_smiles.py @@ -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) @@ -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], @@ -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() - diff --git a/tests/utils/test_spm_calc.py b/tests/utils/test_spm_calc.py index 22be69cb..42441e3b 100644 --- a/tests/utils/test_spm_calc.py +++ b/tests/utils/test_spm_calc.py @@ -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], @@ -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) - \ No newline at end of file diff --git a/tests/utils/test_to_dense_adj.py b/tests/utils/test_to_dense_adj.py index 20d0eabe..8e93bdb6 100644 --- a/tests/utils/test_to_dense_adj.py +++ b/tests/utils/test_to_dense_adj.py @@ -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