|
6 | 6 | import numpy |
7 | 7 | import pytest |
8 | 8 |
|
| 9 | +import diffpy.morph.morphpy as morphpy |
| 10 | +from diffpy.morph.morphapp import create_option_parser, single_morph |
9 | 11 | from diffpy.morph.morphs.morphshift import MorphShift |
| 12 | +from tests.helper import create_morph_data_file |
10 | 13 |
|
11 | 14 | # useful variables |
12 | 15 | thisfile = locals().get("__file__", "file.py") |
@@ -44,3 +47,61 @@ def test_morph(self, setup): |
44 | 47 | assert numpy.allclose(self.x_target, x_target) |
45 | 48 | assert numpy.allclose(self.y_target, y_target) |
46 | 49 | return |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize( |
| 53 | + "hshift, wmsg_gen", |
| 54 | + [ |
| 55 | + # extrapolate below |
| 56 | + ( |
| 57 | + 0.01, |
| 58 | + lambda x: ( |
| 59 | + "Warning: points with grid value below " |
| 60 | + f"{x[0]} are extrapolated." |
| 61 | + ), |
| 62 | + ), |
| 63 | + # extrapolate above |
| 64 | + ( |
| 65 | + -0.01, |
| 66 | + lambda x: ( |
| 67 | + "Warning: points with grid value above " |
| 68 | + f"{x[1]} are extrapolated." |
| 69 | + ), |
| 70 | + ), |
| 71 | + ], |
| 72 | +) |
| 73 | +def test_morphshift_extrapolate(user_filesystem, capsys, hshift, wmsg_gen): |
| 74 | + x_morph = numpy.linspace(0, 10, 101) |
| 75 | + y_morph = numpy.sin(x_morph) |
| 76 | + x_target = x_morph.copy() |
| 77 | + y_target = y_morph.copy() |
| 78 | + with pytest.warns() as w: |
| 79 | + morphpy.morph_arrays( |
| 80 | + numpy.array([x_morph, y_morph]).T, |
| 81 | + numpy.array([x_target, y_target]).T, |
| 82 | + hshift=hshift, |
| 83 | + apply=True, |
| 84 | + ) |
| 85 | + assert len(w) == 1 |
| 86 | + assert w[0].category is UserWarning |
| 87 | + actual_wmsg = str(w[0].message) |
| 88 | + expected_wmsg = wmsg_gen([min(x_morph), max(x_morph)]) |
| 89 | + assert actual_wmsg == expected_wmsg |
| 90 | + |
| 91 | + # CLI test |
| 92 | + morph_file, target_file = create_morph_data_file( |
| 93 | + user_filesystem / "cwd_dir", x_morph, y_morph, x_target, y_target |
| 94 | + ) |
| 95 | + |
| 96 | + parser = create_option_parser() |
| 97 | + (opts, pargs) = parser.parse_args( |
| 98 | + [ |
| 99 | + f"--hshift={hshift}", |
| 100 | + f"{morph_file.as_posix()}", |
| 101 | + f"{target_file.as_posix()}", |
| 102 | + "--apply", |
| 103 | + "-n", |
| 104 | + ] |
| 105 | + ) |
| 106 | + with pytest.warns(UserWarning, match=expected_wmsg): |
| 107 | + single_morph(parser, opts, pargs, stdout_flag=False) |
0 commit comments