Skip to content

Put the MorphRGrid at the end of the morphing chain #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/config_order.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**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:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
23 changes: 23 additions & 0 deletions news/update_tests.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
9 changes: 6 additions & 3 deletions src/diffpy/morph/morphapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,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
Expand Down Expand Up @@ -632,6 +630,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))
Expand Down Expand Up @@ -674,7 +676,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
Expand Down
58 changes: 26 additions & 32 deletions tests/test_morphio.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ def isfloat(s):
return False


def are_files_same(file1, file2):
"""Assert that two files have (approximately) the same numerical
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks as if it is a copy paste of the one in the other PR. Can we put it into conftest. Py and make it available to all tests as a fixture?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is from the other PR. After merging with main, this change is no longer present.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only needed in the io tests as that is the only test creating files. As such, no need to put into contest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok

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):
Expand Down Expand Up @@ -106,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)
assert all(x == y for x, y in zip(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")
Expand Down Expand Up @@ -147,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)
assert all(x == y for x, y in zip(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
Expand Down Expand Up @@ -182,19 +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)
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]
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):
Expand All @@ -215,16 +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)
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]
actual = filter(ignore_path, gf)
expected = filter(ignore_path, tf)
are_files_same(actual, expected)
18 changes: 16 additions & 2 deletions tests/test_morphpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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):
Expand Down
Loading