Skip to content

Commit f537512

Browse files
committed
test: add test for not enough shared grid points the bad case.
1 parent bc24486 commit f537512

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/diffpy/morph/refine.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ def _residual(self, pvals):
8585
rvec = _y_target - _y_morph
8686
if len(rvec) < len(pvals):
8787
raise ValueError(
88+
f"\nNumber of shared grid points: {len(rvec)}\n"
89+
f"Number of parameters: {len(pvals)}\n"
8890
"Not enough shared grid points between morphed function "
89-
"and target function to fit the chosen parameters. "
90-
"Please adjust the functions or "
91+
"between morphed function and target function to fit "
92+
"the chosen parameters.\n"
93+
"Please make sure the overlapping domain between the morphed "
94+
"function and the target function is sufficiently large, or "
9195
"reduce the number of parameters."
9296
)
9397
# If first time computing residual

tests/test_refine.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,64 @@ def stretch(x, y, stretch):
181181

182182
assert res < err
183183

184+
def test_refine_grid_bad(self, user_filesystem):
185+
grid = numpy.arange(2)
186+
func = numpy.sin(grid)
187+
grid1, func1, grid2, func2 = grid, func, grid, func
188+
config = {
189+
"stretch": 0.005,
190+
"scale": 1.0,
191+
"smear": 0,
192+
}
193+
chain = MorphChain(config)
194+
refiner = Refiner(chain, grid1, func1, grid2, func2)
195+
refpars = ["stretch", "scale", "smear"]
196+
expected_error_message = (
197+
"\nNumber of shared grid points: 2\n"
198+
"Number of parameters: 3\n"
199+
"Not enough shared grid points between morphed function "
200+
"between morphed function and target function to fit "
201+
"the chosen parameters.\n"
202+
"Please make sure the overlapping domain between the morphed "
203+
"function and the target function is sufficiently large, or "
204+
"reduce the number of parameters."
205+
)
206+
with pytest.raises(
207+
ValueError,
208+
) as error:
209+
refiner.refine(*refpars)
210+
actual_error_message = str(error.value)
211+
assert actual_error_message == expected_error_message
212+
213+
# call from command line
214+
import subprocess
215+
216+
data_dir_path = user_filesystem / "cwd_dir"
217+
morph_file = data_dir_path / "morph_data"
218+
morph_data_text = [
219+
str(grid1[i]) + " " + str(func1[i]) for i in range(len(grid1))
220+
]
221+
morph_data_text = "\n".join(morph_data_text)
222+
morph_file.write_text(morph_data_text)
223+
target_file = data_dir_path / "target_data"
224+
target_data_text = [
225+
str(grid2[i]) + " " + str(func2[i]) for i in range(len(grid2))
226+
]
227+
target_data_text = "\n".join(target_data_text)
228+
target_file.write_text(target_data_text)
229+
run_cmd = ["diffpy.morph"]
230+
for key, value in config.items():
231+
run_cmd.append(f"--{key}")
232+
run_cmd.append(f"{value}")
233+
run_cmd.extend([str(morph_file), str(target_file)])
234+
run_cmd.append("-n")
235+
result = subprocess.run(run_cmd, capture_output=True, text=True)
236+
expected_error_message = (
237+
"diffpy.morph: error: " + expected_error_message
238+
)
239+
actual_error_message = result.stderr.strip()
240+
assert actual_error_message == expected_error_message
241+
184242

185243
# End of class TestRefine
186244

0 commit comments

Comments
 (0)