From df7c6492be3aaf730643afd5c98b2401409ac738 Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 13:58:48 -0500 Subject: [PATCH 1/4] Update numerical file comparisons in tests --- tests/test_morphio.py | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/tests/test_morphio.py b/tests/test_morphio.py index b16d414..2dc07d6 100644 --- a/tests/test_morphio.py +++ b/tests/test_morphio.py @@ -50,6 +50,20 @@ def isfloat(s): return False +def compare_numeric(file1, file2): + """Assert that two files have (approximately) the same numerical + values.""" + for f1_row, f2_row in zip(file1, file2): + f1_arr = f1_row.split() + f2_arr = f2_row.split() + assert len(f1_arr) == len(f2_arr) + for idx, _ in enumerate(f1_arr): + if isfloat(f1_arr[idx]) and isfloat(f2_arr[idx]): + assert np.isclose(float(f1_arr[idx]), float(f2_arr[idx])) + else: + assert f1_arr[idx] == f2_arr[idx] + + class TestApp: @pytest.fixture def setup(self): @@ -108,7 +122,7 @@ def test_morph_outputs(self, setup, tmp_path): with open(test_saving_succinct.joinpath(file)) as tf: generated = filter(ignore_path, gf) target = filter(ignore_path, tf) - assert all(x == y for x, y in zip(generated, target)) + compare_numeric(generated, target) # Save multiple verbose morphs tmp_verbose = tmp_path.joinpath("verbose") @@ -149,7 +163,7 @@ def test_morph_outputs(self, setup, tmp_path): with open(test_saving_verbose.joinpath(file)) as tf: generated = filter(ignore_path, gf) target = filter(ignore_path, tf) - assert all(x == y for x, y in zip(generated, target)) + compare_numeric(generated, target) def test_morphsqueeze_outputs(self, setup, tmp_path): # The file squeeze_morph has a squeeze and stretch applied @@ -184,17 +198,7 @@ def test_morphsqueeze_outputs(self, setup, tmp_path): with open(target_file) as tf: morphed = filter(ignore_path, mf) target = filter(ignore_path, tf) - for m, t in zip(morphed, target): - m_row = m.split() - t_row = t.split() - assert len(m_row) == len(t_row) - for idx, _ in enumerate(m_row): - if isfloat(m_row[idx]) and isfloat(t_row[idx]): - assert np.isclose( - float(m_row[idx]), float(t_row[idx]) - ) - else: - assert m_row[idx] == t_row[idx] + compare_numeric(morphed, target) def test_morphfuncy_outputs(self, tmp_path): def quadratic(x, y, a0, a1, a2): @@ -217,14 +221,4 @@ def quadratic(x, y, a0, a1, a2): with open(tmp_path.joinpath("funcy_target.cgr")) as gf: generated = filter(ignore_path, gf) target = filter(ignore_path, tf) - for m, t in zip(generated, target): - m_row = m.split() - t_row = t.split() - assert len(m_row) == len(t_row) - for idx, _ in enumerate(m_row): - if isfloat(m_row[idx]) and isfloat(t_row[idx]): - assert np.isclose( - float(m_row[idx]), float(t_row[idx]) - ) - else: - assert m_row[idx] == t_row[idx] + compare_numeric(generated, target) From 4ba716c2f469ea9f4880c141385f3c7e4c8ede87 Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 14:01:14 -0500 Subject: [PATCH 2/4] News --- news/update_tests.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/update_tests.rst diff --git a/news/update_tests.rst b/news/update_tests.rst new file mode 100644 index 0000000..790d30b --- /dev/null +++ b/news/update_tests.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* From ff08b0a47855a440fc49364a8204a830682bc3fa Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 14:46:17 -0500 Subject: [PATCH 3/4] Dictionary numeric comparison --- tests/test_morphpy.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/test_morphpy.py b/tests/test_morphpy.py index 848fe1a..0a28827 100644 --- a/tests/test_morphpy.py +++ b/tests/test_morphpy.py @@ -91,7 +91,14 @@ class Chain: assert np.allclose( [rw], [self.morphapp_results[target_file.name]["Rw"]] ) - assert morph_results == self.morphapp_results + # Check values in dictionaries are approximately equal + for file in morph_results.keys(): + morph_params = morph_results[file] + morphapp_params = self.morphapp_results[file] + for key in morph_params.keys(): + assert morph_params[key] == pytest.approx( + morphapp_params[key], abs=1e-08 + ) def test_morphpy(self, setup_morph): morph_results = {} @@ -113,7 +120,14 @@ class Chain: assert np.allclose( [rw], [self.morphapp_results[target_file.name]["Rw"]] ) - assert morph_results == self.morphapp_results + # Check values in dictionaries are approximately equal + for file in morph_results.keys(): + morph_params = morph_results[file] + morphapp_params = self.morphapp_results[file] + for key in morph_params.keys(): + assert morph_params[key] == pytest.approx( + morphapp_params[key], abs=1e-08 + ) def test_morphfuncy(self, setup_morph): def gaussian(x, mu, sigma): From 8364a94b527b84bf9319e5d3ad3e93cd5832906a Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 14:49:35 -0500 Subject: [PATCH 4/4] Renaming for clarity --- tests/test_morphio.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/test_morphio.py b/tests/test_morphio.py index 2dc07d6..08699dc 100644 --- a/tests/test_morphio.py +++ b/tests/test_morphio.py @@ -50,7 +50,7 @@ def isfloat(s): return False -def compare_numeric(file1, file2): +def are_files_same(file1, file2): """Assert that two files have (approximately) the same numerical values.""" for f1_row, f2_row in zip(file1, file2): @@ -120,9 +120,9 @@ def test_morph_outputs(self, setup, tmp_path): for file in common: with open(tmp_succinct.joinpath(file)) as gf: with open(test_saving_succinct.joinpath(file)) as tf: - generated = filter(ignore_path, gf) - target = filter(ignore_path, tf) - compare_numeric(generated, target) + actual = filter(ignore_path, gf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected) # Save multiple verbose morphs tmp_verbose = tmp_path.joinpath("verbose") @@ -161,9 +161,9 @@ def test_morph_outputs(self, setup, tmp_path): for file in common: with open(tmp_verbose.joinpath(file)) as gf: with open(test_saving_verbose.joinpath(file)) as tf: - generated = filter(ignore_path, gf) - target = filter(ignore_path, tf) - compare_numeric(generated, target) + actual = filter(ignore_path, gf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected) def test_morphsqueeze_outputs(self, setup, tmp_path): # The file squeeze_morph has a squeeze and stretch applied @@ -196,9 +196,9 @@ def test_morphsqueeze_outputs(self, setup, tmp_path): # Check squeeze morph generates the correct output with open(sqr) as mf: with open(target_file) as tf: - morphed = filter(ignore_path, mf) - target = filter(ignore_path, tf) - compare_numeric(morphed, target) + actual = filter(ignore_path, mf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected) def test_morphfuncy_outputs(self, tmp_path): def quadratic(x, y, a0, a1, a2): @@ -219,6 +219,6 @@ def quadratic(x, y, a0, a1, a2): with open(testdata_dir.joinpath("funcy_target.cgr")) as tf: with open(tmp_path.joinpath("funcy_target.cgr")) as gf: - generated = filter(ignore_path, gf) - target = filter(ignore_path, tf) - compare_numeric(generated, target) + actual = filter(ignore_path, gf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected)