From df7c6492be3aaf730643afd5c98b2401409ac738 Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 13:58:48 -0500 Subject: [PATCH 1/7] 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 b16d414a..2dc07d6e 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/7] 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 00000000..790d30b1 --- /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/7] 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 848fe1a9..0a288271 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/7] 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 2dc07d6e..08699dcb 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) From 0f8af4a47642000a6fcd63b1eb2fab2ba039371e Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 14:54:05 -0500 Subject: [PATCH 5/7] Change order in which r-grid is compared --- src/diffpy/morph/morphapp.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/diffpy/morph/morphapp.py b/src/diffpy/morph/morphapp.py index 70737bf2..36689532 100755 --- a/src/diffpy/morph/morphapp.py +++ b/src/diffpy/morph/morphapp.py @@ -106,6 +106,12 @@ def custom_error(self, msg): type="float", help="Maximum r-value to use for PDF comparisons.", ) + parser.add_option( + "--rgrid", + dest="rgrid", + metavar="GRID", + help="Choose the to refine on. ", + ) parser.add_option( "--tolerance", "-t", @@ -506,8 +512,6 @@ def single_morph( # Set up the morphs chain = morphs.MorphChain(config) - # Add the r-range morph, we will remove it when saving and plotting - chain.append(morphs.MorphRGrid()) refpars = [] # Python-Specific Morphs @@ -632,6 +636,10 @@ def single_morph( refpars.append("qdamp") config["qdamp"] = opts.qdamp + # Add the r-range morph, we will remove it when saving and plotting + mrg = morphs.MorphRGrid() + chain.append(mrg) + # Now remove non-refinable parameters if opts.exclude is not None: refpars = list(set(refpars) - set(opts.exclude)) @@ -674,7 +682,8 @@ def single_morph( rw = tools.getRw(chain) pcc = tools.get_pearson(chain) # Replace the MorphRGrid with Morph identity - chain[0] = morphs.Morph() + # This removes the r-range morph as mentioned above + mrg = morphs.Morph() chain(x_morph, y_morph, x_target, y_target) # FOR FUTURE MAINTAINERS From 934842052135c24ead67178c71c55d70dabc3dc0 Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 14:57:35 -0500 Subject: [PATCH 6/7] News --- news/config_order.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/config_order.rst diff --git a/news/config_order.rst b/news/config_order.rst new file mode 100644 index 00000000..718a0f61 --- /dev/null +++ b/news/config_order.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* The interpolation of the morphed/objective function onto the target function grid is now done at the end of the morphing chain. Prior, it was done before. This change is desirable as the target function grid may be much smaller/larger than that of the objective, but a morph (e.g. stretch) accounts for that difference. Then, we ensure the morph is done before we regrid for comparison. + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* From 13e1163dbe684c9abf6d4f6178f6365797b15bfd Mon Sep 17 00:00:00 2001 From: Sparks29032 Date: Wed, 9 Jul 2025 15:03:58 -0500 Subject: [PATCH 7/7] Remove undeveloped option --- src/diffpy/morph/morphapp.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/diffpy/morph/morphapp.py b/src/diffpy/morph/morphapp.py index 36689532..43f71ce1 100755 --- a/src/diffpy/morph/morphapp.py +++ b/src/diffpy/morph/morphapp.py @@ -106,12 +106,6 @@ def custom_error(self, msg): type="float", help="Maximum r-value to use for PDF comparisons.", ) - parser.add_option( - "--rgrid", - dest="rgrid", - metavar="GRID", - help="Choose the to refine on. ", - ) parser.add_option( "--tolerance", "-t",