|
11 | 11 | single_morph, |
12 | 12 | ) |
13 | 13 | from diffpy.morph.morphpy import morph_arrays |
| 14 | +from diffpy.utils.parsers.loaddata import loadData |
14 | 15 |
|
15 | 16 | # Support Python 2 |
16 | 17 | try: |
@@ -64,6 +65,29 @@ def are_files_same(file1, file2): |
64 | 65 | assert f1_arr[idx] == f2_arr[idx] |
65 | 66 |
|
66 | 67 |
|
| 68 | +def are_diffs_right(file1, file2, diff_file): |
| 69 | + """Assert that diff_file ordinate data is approximately file1 |
| 70 | + ordinate data minus file2 ordinate data.""" |
| 71 | + f1_data = loadData(file1) |
| 72 | + f2_data = loadData(file2) |
| 73 | + diff_data = loadData(diff_file) |
| 74 | + |
| 75 | + rmin = max(min(f1_data[:, 0]), min(f1_data[:, 1])) |
| 76 | + rmax = min(max(f2_data[:, 0]), max(f2_data[:, 1])) |
| 77 | + rnumsteps = max( |
| 78 | + len(f1_data[:, 0][(rmin <= f1_data[:, 0]) & (f1_data[:, 0] <= rmax)]), |
| 79 | + len(f2_data[:, 0][(rmin <= f2_data[:, 0]) & (f2_data[:, 0] <= rmax)]), |
| 80 | + ) |
| 81 | + |
| 82 | + share_grid = np.linspace(rmin, rmax, rnumsteps) |
| 83 | + f1_interp = np.interp(share_grid, f1_data[:, 0], f1_data[:, 1]) |
| 84 | + f2_interp = np.interp(share_grid, f2_data[:, 0], f2_data[:, 1]) |
| 85 | + diff_interp = np.interp(share_grid, diff_data[:, 0], diff_data[:, 1]) |
| 86 | + |
| 87 | + for idx, diff in enumerate(diff_interp): |
| 88 | + assert np.isclose(f1_interp[idx] - f2_interp[idx], diff) |
| 89 | + |
| 90 | + |
67 | 91 | class TestApp: |
68 | 92 | @pytest.fixture |
69 | 93 | def setup(self): |
@@ -165,6 +189,59 @@ def test_morph_outputs(self, setup, tmp_path): |
165 | 189 | expected = filter(ignore_path, tf) |
166 | 190 | are_files_same(actual, expected) |
167 | 191 |
|
| 192 | + # Similar format as test_morph_outputs |
| 193 | + def test_morph_diff_outputs(self, setup, tmp_path): |
| 194 | + morph_file = self.testfiles[0] |
| 195 | + target_file = self.testfiles[-1] |
| 196 | + |
| 197 | + # Save multiple diff morphs |
| 198 | + tmp_diff = tmp_path.joinpath("diff") |
| 199 | + tmp_diff_name = tmp_diff.resolve().as_posix() |
| 200 | + |
| 201 | + (opts, pargs) = self.parser.parse_args( |
| 202 | + [ |
| 203 | + "--multiple-targets", |
| 204 | + "--sort-by", |
| 205 | + "temperature", |
| 206 | + "-s", |
| 207 | + tmp_diff_name, |
| 208 | + "-n", |
| 209 | + "--save-names-file", |
| 210 | + tssf, |
| 211 | + "--diff", |
| 212 | + ] |
| 213 | + ) |
| 214 | + pargs = [morph_file, testsequence_dir] |
| 215 | + multiple_targets(self.parser, opts, pargs, stdout_flag=False) |
| 216 | + |
| 217 | + # Save a single diff morph |
| 218 | + diff_name = "single_diff_morph.cgr" |
| 219 | + diff_file = tmp_diff.joinpath(diff_name) |
| 220 | + df_name = diff_file.resolve().as_posix() |
| 221 | + (opts, pargs) = self.parser.parse_args(["-s", df_name, "-n", "--diff"]) |
| 222 | + pargs = [morph_file, target_file] |
| 223 | + single_morph(self.parser, opts, pargs, stdout_flag=False) |
| 224 | + |
| 225 | + # Check that the saved diff matches the morph minus target |
| 226 | + # Morphs are saved in testdata/testsequence/testsaving/succinct |
| 227 | + # Targets are stored in testdata/testsequence |
| 228 | + |
| 229 | + # Single morph diff |
| 230 | + morphed_file = test_saving_succinct / diff_name.replace( |
| 231 | + "diff", "succinct" |
| 232 | + ) |
| 233 | + are_diffs_right(morphed_file, target_file, diff_file) |
| 234 | + |
| 235 | + # Multiple morphs diff |
| 236 | + diff_files = list((tmp_diff / "Morphs").iterdir()) |
| 237 | + morphed_files = list((test_saving_succinct / "Morphs").iterdir()) |
| 238 | + target_files = self.testfiles[1:] |
| 239 | + diff_files.sort() |
| 240 | + morphed_files.sort() |
| 241 | + target_files.sort() |
| 242 | + for idx, diff_file in enumerate(diff_files): |
| 243 | + are_diffs_right(morphed_files[idx], target_files[idx], diff_file) |
| 244 | + |
168 | 245 | def test_morphsqueeze_outputs(self, setup, tmp_path): |
169 | 246 | # The file squeeze_morph has a squeeze and stretch applied |
170 | 247 | morph_file = testdata_dir / "squeeze_morph.cgr" |
|
0 commit comments