Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: add test for not enough shared grid points the bad case.
  • Loading branch information
ycexiao committed Aug 20, 2025
commit f5375120bd701a5a9c9c65b08bc9129840db18de
8 changes: 6 additions & 2 deletions src/diffpy/morph/refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ def _residual(self, pvals):
rvec = _y_target - _y_morph
if len(rvec) < len(pvals):
raise ValueError(
f"\nNumber of shared grid points: {len(rvec)}\n"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Print the size of the function and the number of parameters.

f"Number of parameters: {len(pvals)}\n"
"Not enough shared grid points between morphed function "
"and target function to fit the chosen parameters. "
"Please adjust the functions or "
"between morphed function and target function to fit "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please check, is "between morphed function" repeated here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it is a typo. It is fixed in a newer commit.

"the chosen parameters.\n"
"Please make sure the overlapping domain between the morphed "
"function and the target function is sufficiently large, or "
"reduce the number of parameters."
)
# If first time computing residual
Expand Down
58 changes: 58 additions & 0 deletions tests/test_refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,64 @@ def stretch(x, y, stretch):

assert res < err

def test_refine_grid_bad(self, user_filesystem):
grid = numpy.arange(2)
func = numpy.sin(grid)
grid1, func1, grid2, func2 = grid, func, grid, func
config = {
"stretch": 0.005,
"scale": 1.0,
"smear": 0,
}
chain = MorphChain(config)
refiner = Refiner(chain, grid1, func1, grid2, func2)
refpars = ["stretch", "scale", "smear"]
expected_error_message = (
"\nNumber of shared grid points: 2\n"
"Number of parameters: 3\n"
"Not enough shared grid points between morphed function "
"between morphed function and target function to fit "
"the chosen parameters.\n"
"Please make sure the overlapping domain between the morphed "
"function and the target function is sufficiently large, or "
"reduce the number of parameters."
)
with pytest.raises(
ValueError,
) as error:
refiner.refine(*refpars)
actual_error_message = str(error.value)
assert actual_error_message == expected_error_message

# call from command line
Comment thread
ycexiao marked this conversation as resolved.
import subprocess

data_dir_path = user_filesystem / "cwd_dir"
morph_file = data_dir_path / "morph_data"
morph_data_text = [
str(grid1[i]) + " " + str(func1[i]) for i in range(len(grid1))
]
morph_data_text = "\n".join(morph_data_text)
morph_file.write_text(morph_data_text)
target_file = data_dir_path / "target_data"
target_data_text = [
str(grid2[i]) + " " + str(func2[i]) for i in range(len(grid2))
]
target_data_text = "\n".join(target_data_text)
target_file.write_text(target_data_text)
run_cmd = ["diffpy.morph"]
for key, value in config.items():
run_cmd.append(f"--{key}")
run_cmd.append(f"{value}")
run_cmd.extend([str(morph_file), str(target_file)])
run_cmd.append("-n")
result = subprocess.run(run_cmd, capture_output=True, text=True)
expected_error_message = (
"diffpy.morph: error: " + expected_error_message
)
actual_error_message = result.stderr.strip()
assert actual_error_message == expected_error_message


# End of class TestRefine

Expand Down
Loading