Download annotations/annotation_100/test.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 13.5 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_100/test.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_100/test.py
-
curl -L -o test.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_100/test.py
13.5 kB
| import unittest | |
| import os | |
| import json | |
| import shutil | |
| import sys | |
| import numpy.testing as npt | |
| from MPSC.canonical import ( | |
| IntraConsistency as CanonicalIntraConsistency, | |
| VertexType as CanonicalVertexType, | |
| VertexGroup as CanonicalVertexGroup, | |
| EdgeGroup as CanonicalEdgeGroup, | |
| NPartiteGraph as CanonicalNPartiteGraph, | |
| run as canonical_run, | |
| ) | |
| from MPSC.graph import ( | |
| IntraConsistency as GeneratedIntraConsistency, | |
| VertexType as GeneratedVertexType, | |
| VertexGroup as GeneratedVertexGroup, | |
| EdgeGroup as GeneratedEdgeGroup, | |
| NPartiteGraph as GeneratedNPartiteGraph, | |
| run as generated_run, | |
| ) | |
| class TestMPSCFunctionalEquivalence(unittest.TestCase): | |
| """ | |
| Test suite to verify functional equivalence between the canonical and generated | |
| implementations of the MPSC graph ranker. | |
| """ | |
| def setUpClass(cls): | |
| """Set up a temporary directory with mock data files for testing.""" | |
| cls.test_dir = 'MPSC' | |
| cls.data_dir = os.path.join(cls.test_dir) # The canonical code uses BASE_DIR | |
| # Define paths for mock data | |
| cls.vertex_dir = os.path.join(cls.data_dir, "data/vertex") | |
| cls.edge_dir = os.path.join(cls.data_dir, "data/edge") | |
| os.makedirs(cls.vertex_dir, exist_ok=True) | |
| os.makedirs(cls.edge_dir, exist_ok=True) | |
| # --- Create Mock Data --- | |
| # 1. Vertex data (small but representative) | |
| cls.code_data = [ | |
| "def add(a, b): return a + b", | |
| "def add(x, y): return x + y", # Lexically similar to the first | |
| "def sub(a, b): return a - b", | |
| ] | |
| cls.spec_data = ["adds two numbers", "subtracts two numbers"] | |
| cls.testcase_data = ["assert add(1, 2) == 3", "assert sub(3, 1) == 2"] | |
| with open(os.path.join(cls.vertex_dir, "code.json"), "w") as f: | |
| json.dump(cls.code_data, f) | |
| with open(os.path.join(cls.vertex_dir, "spec.json"), "w") as f: | |
| json.dump(cls.spec_data, f) | |
| with open(os.path.join(cls.vertex_dir, "testcase.json"), "w") as f: | |
| json.dump(cls.testcase_data, f) | |
| # 2. Edge data (inter-consistency scores) | |
| code_spec_edges = { | |
| cls.code_data[0]: {cls.spec_data[0]: 1.0, cls.spec_data[1]: 0.1}, | |
| cls.code_data[1]: {cls.spec_data[0]: 1.0, cls.spec_data[1]: 0.1}, | |
| cls.code_data[2]: {cls.spec_data[0]: 0.0, cls.spec_data[1]: 1.0}, | |
| } | |
| code_testcase_edges = { | |
| cls.code_data[0]: {cls.testcase_data[0]: 1.0, cls.testcase_data[1]: 0.0}, | |
| cls.code_data[1]: {cls.testcase_data[0]: 1.0, cls.testcase_data[1]: 0.0}, | |
| cls.code_data[2]: {cls.testcase_data[0]: 0.0, cls.testcase_data[1]: 1.0}, | |
| } | |
| spec_testcase_edges = { | |
| cls.spec_data[0]: {cls.testcase_data[0]: 1.0, cls.testcase_data[1]: 0.0}, | |
| cls.spec_data[1]: {cls.testcase_data[0]: 0.0, cls.testcase_data[1]: 1.0}, | |
| } | |
| with open(os.path.join(cls.edge_dir, "code-spec.json"), "w") as f: | |
| json.dump(code_spec_edges, f) | |
| with open(os.path.join(cls.edge_dir, "code-testcase.json"), "w") as f: | |
| json.dump(code_testcase_edges, f) | |
| with open(os.path.join(cls.edge_dir, "spec-testcase.json"), "w") as f: | |
| json.dump(spec_testcase_edges, f) | |
| # --- Pre-load VertexGroup and EdgeGroup instances for tests --- | |
| cls.canonical_v_code = CanonicalVertexGroup(CanonicalVertexType.code, 'data/vertex/code.json', 3) | |
| cls.canonical_v_spec = CanonicalVertexGroup(CanonicalVertexType.spec, 'data/vertex/spec.json', 2) | |
| cls.canonical_v_test = CanonicalVertexGroup(CanonicalVertexType.testcase, 'data/vertex/testcase.json', 2) | |
| cls.canonical_vertex_groups = [cls.canonical_v_code, cls.canonical_v_spec, cls.canonical_v_test] | |
| cls.canonical_edge_groups = [ | |
| CanonicalEdgeGroup(cls.canonical_v_code, cls.canonical_v_spec, 'data/edge/code-spec.json'), | |
| CanonicalEdgeGroup(cls.canonical_v_code, cls.canonical_v_test, 'data/edge/code-testcase.json'), | |
| CanonicalEdgeGroup(cls.canonical_v_spec, cls.canonical_v_test, 'data/edge/spec-testcase.json') | |
| ] | |
| cls.generated_v_code = GeneratedVertexGroup(GeneratedVertexType.code, 'data/vertex/code.json', 3) | |
| cls.generated_v_spec = GeneratedVertexGroup(GeneratedVertexType.spec, 'data/vertex/spec.json', 2) | |
| cls.generated_v_test = GeneratedVertexGroup(GeneratedVertexType.testcase, 'data/vertex/testcase.json', 2) | |
| cls.generated_vertex_groups = [cls.generated_v_code, cls.generated_v_spec, cls.generated_v_test] | |
| cls.generated_edge_groups = [ | |
| GeneratedEdgeGroup(cls.generated_v_code, cls.generated_v_spec, 'data/edge/code-spec.json'), | |
| GeneratedEdgeGroup(cls.generated_v_code, cls.generated_v_test, 'data/edge/code-testcase.json'), | |
| GeneratedEdgeGroup(cls.generated_v_spec, cls.generated_v_test, 'data/edge/spec-testcase.json') | |
| ] | |
| def tearDownClass(cls): | |
| """Clean up the temporary directory and its contents.""" | |
| sys.path.pop(0) | |
| def test_01_enum_equivalence(self): | |
| """Test if IntraConsistency and VertexType enums are identical.""" | |
| # Extract (name, value) tuples for comparison instead of comparing the objects directly. | |
| # This is necessary because the enums are loaded from two different modules. | |
| canonical_intra_list = [(e.name, e.value) for e in CanonicalIntraConsistency] | |
| generated_intra_list = [(e.name, e.value) for e in GeneratedIntraConsistency] | |
| self.assertListEqual(canonical_intra_list, generated_intra_list, "IntraConsistency enums do not have matching members or values.") | |
| canonical_vertex_list = [(e.name, e.value) for e in CanonicalVertexType] | |
| generated_vertex_list = [(e.name, e.value) for e in GeneratedVertexType] | |
| self.assertListEqual(canonical_vertex_list, generated_vertex_list, "VertexType enums do not have matching members or values.") | |
| def test_02_vertex_group_initialization_and_len(self): | |
| """Test VertexGroup initialization and length.""" | |
| self.assertEqual(len(self.canonical_v_code), len(self.generated_v_code)) | |
| self.assertListEqual(self.canonical_v_code.vertex_group, self.generated_v_code.vertex_group) | |
| self.assertEqual(len(self.canonical_v_spec), len(self.generated_v_spec)) | |
| self.assertListEqual(self.canonical_v_spec.vertex_group, self.generated_v_spec.vertex_group) | |
| def test_03_vertex_group_uniform_s0(self): | |
| """Test UNIFORM prior generation.""" | |
| canonical_s0 = self.canonical_v_code.get_uniform_s0() | |
| generated_s0 = self.generated_v_code.get_uniform_s0() | |
| npt.assert_allclose(canonical_s0, generated_s0, rtol=1e-7, err_msg="Uniform s0 vectors do not match") | |
| def test_04_vertex_group_mbr_s0(self): | |
| """Test LEXICAL (MBR) prior generation.""" | |
| canonical_s0 = self.canonical_v_code.get_mbr_s0() | |
| generated_s0 = self.generated_v_code.get_mbr_s0() | |
| npt.assert_allclose(canonical_s0, generated_s0, rtol=1e-7, err_msg="Lexical (MBR) s0 vectors do not match") | |
| def test_05_edge_group_adj_matrix(self): | |
| """Test EdgeGroup adjacency matrix loading.""" | |
| canonical_adj = self.canonical_edge_groups[0].get_adj_matrix() | |
| generated_adj = self.generated_edge_groups[0].get_adj_matrix() | |
| npt.assert_allclose(canonical_adj, generated_adj, rtol=1e-7, err_msg="Adjacency matrices do not match") | |
| def test_06_npartite_graph_initialization(self): | |
| """Test NPartiteGraph initialization for all consistency measures.""" | |
| for measure in CanonicalIntraConsistency: | |
| with self.subTest(consistency_measure=measure.name): | |
| # Initialize both graphs with the same parameters | |
| canonical_graph = CanonicalNPartiteGraph( | |
| self.canonical_vertex_groups, self.canonical_edge_groups, intra_consistency_measure=measure | |
| ) | |
| generated_graph = GeneratedNPartiteGraph( | |
| self.generated_vertex_groups, self.generated_edge_groups, intra_consistency_measure=GeneratedIntraConsistency[measure.name] | |
| ) | |
| # Compare key internal matrices and vectors | |
| npt.assert_allclose(canonical_graph.adj_matrix, generated_graph.adj_matrix, rtol=1e-7, err_msg=f"Adj matrix mismatch for {measure.name}") | |
| npt.assert_allclose(canonical_graph.degree_vector, generated_graph.degree_vector, rtol=1e-7, err_msg=f"Degree vector mismatch for {measure.name}") | |
| npt.assert_allclose(canonical_graph.transition_matrix, generated_graph.transition_matrix, rtol=1e-7, err_msg=f"Transition matrix mismatch for {measure.name}") | |
| npt.assert_allclose(canonical_graph.s0, generated_graph.s0, rtol=1e-7, err_msg=f"s0 vector mismatch for {measure.name}") | |
| self.assertAlmostEqual(canonical_graph.alpha, generated_graph.alpha, places=7, msg=f"Alpha value mismatch for {measure.name}") | |
| def test_07_npartite_graph_fit_and_get_scores(self): | |
| """Test the fit process and final score extraction.""" | |
| # We test with SEMANTIC as it is the most complex initialization | |
| measure = CanonicalIntraConsistency.SEMANTIC | |
| canonical_graph = CanonicalNPartiteGraph( | |
| self.canonical_vertex_groups, self.canonical_edge_groups, intra_consistency_measure=measure | |
| ) | |
| generated_graph = GeneratedNPartiteGraph( | |
| self.generated_vertex_groups, self.generated_edge_groups, intra_consistency_measure=GeneratedIntraConsistency[measure.name] | |
| ) | |
| # Run fit and compare iteration counts | |
| canonical_iters = canonical_graph.fit() | |
| generated_iters = generated_graph.fit() | |
| self.assertEqual(canonical_iters, generated_iters, "Mismatch in number of iterations to converge") | |
| # Compare final score vectors | |
| npt.assert_allclose(canonical_graph.score_vector, generated_graph.score_vector, rtol=1e-6, err_msg="Final score vectors do not match") | |
| # Compare ranked output for the 'code' perspective | |
| canonical_scores = canonical_graph.get_vertex_group_score(CanonicalVertexType.code) | |
| generated_scores = generated_graph.get_vertex_group_score(GeneratedVertexType.code) | |
| self.assertEqual(len(canonical_scores), len(generated_scores), "Mismatch in number of returned scores") | |
| # Separate labels and values for comparison | |
| canonical_labels, canonical_values = zip(*canonical_scores) | |
| generated_labels, generated_values = zip(*generated_scores) | |
| self.assertListEqual(list(canonical_labels), list(generated_labels), "Order of ranked items does not match") | |
| npt.assert_allclose(canonical_values, generated_values, rtol=1e-6, err_msg="Values of ranked items do not match") | |
| def test_08_end_to_end_run_function(self): | |
| """Test the top-level 'run' function for all consistency measures.""" | |
| for measure in CanonicalIntraConsistency: | |
| with self.subTest(consistency_measure=measure.name): | |
| # Execute the canonical run function | |
| canonical_ranked_list, canonical_iters = canonical_run( | |
| self.canonical_vertex_groups, | |
| self.canonical_edge_groups, | |
| target_vertex_type=CanonicalVertexType.code, | |
| intra_consistency_measure=measure, | |
| alpha=0.85 # Note: alpha is unused in the implementation but passed for signature consistency | |
| ) | |
| # Execute the generated run function | |
| generated_ranked_list, generated_iters = generated_run( | |
| self.generated_vertex_groups, | |
| self.generated_edge_groups, | |
| target_vertex_type=GeneratedVertexType.code, | |
| intra_consistency_measure=GeneratedIntraConsistency[measure.name], | |
| alpha=0.85 | |
| ) | |
| # Compare the results | |
| self.assertEqual(canonical_iters, generated_iters, f"Run function iteration count mismatch for {measure.name}") | |
| self.assertEqual(len(canonical_ranked_list), len(generated_ranked_list), f"Run function output list length mismatch for {measure.name}") | |
| # Unzip and compare contents | |
| c_labels, c_scores = zip(*canonical_ranked_list) | |
| g_labels, g_scores = zip(*generated_ranked_list) | |
| self.assertListEqual(list(c_labels), list(g_labels), f"Run function item order mismatch for {measure.name}") | |
| npt.assert_allclose(c_scores, g_scores, rtol=1e-6, err_msg=f"Run function score values mismatch for {measure.name}") | |
| if __name__ == '__main__': | |
| # To run this test, you must first save the provided canonical code | |
| # into a file named 'canonical_code.py' in the same directory as this test script. | |
| if not os.path.exists("canonical_code.py"): | |
| print("FATAL: Please save the provided code implementation as 'canonical_code.py' before running the test.") | |
| else: | |
| unittest.main(verbosity=2) | |