Skip to content

Commit 2c7dbe5

Browse files
Give user option to save on original grid (#304)
* Allow original grid saving * Add uncertainties for main parameters. (#299) * Add uncertainties for main parameters. * Works for all parameters now * Fix other tests * loosen tolerance to 1e-4 from 1e-5 * Lower tolerance more * Remove comment * [pre-commit.ci] auto fixes from pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * Rebasing * Remove duplicated test * Indent --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cb901e8 commit 2c7dbe5

17 files changed

Lines changed: 81194 additions & 3 deletions

File tree

docs/source/morphpy.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ get_diff: bool
8383
Return the difference function (morphed function minus target function) instead of
8484
the morphed function (default). When save is enabled, the difference function
8585
is saved instead of the morphed function.
86+
original_grid: bool
87+
Save/return the morphed function on the original grid.
88+
Automatically disabled when --get-diff enabled.
89+
Default: save/return the morphed function on the
90+
grid defined by the intersection of the original
91+
and target grids.
8692
verbose: bool
8793
Print additional header details to saved files. These include details about the morph
8894
inputs and outputs.

news/choose-grid.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* User now has option to select which grid they want the morphed function returned on. The default grid is still the intersection of the original and target grid. The option --original-grid changes the returned grid to be the original grid instead.
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/morph/morphapp.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ def morph_error(self, msg, error):
103103
"grid of the objective and target function."
104104
),
105105
)
106+
parser.add_option(
107+
"-o",
108+
"--original-grid",
109+
action="store_true",
110+
dest="original_grid",
111+
help=(
112+
"Save/return the morphed function on the original grid. "
113+
"Automatically disabled when --get-diff enabled. "
114+
"Default: save/return the morphed function on the "
115+
"grid defined by the intersection of the original "
116+
"and target grids. "
117+
),
118+
)
106119
parser.add_option(
107120
"-v",
108121
"--verbose",
@@ -797,7 +810,8 @@ def single_morph(
797810
pcc = tools.get_pearson(chain)
798811
# Replace the MorphRGrid with Morph identity
799812
# This removes the r-range morph as mentioned above
800-
mrg = morphs.Morph()
813+
if opts.original_grid is not None:
814+
chain[chain.index(mrg)] = morphs.Morph()
801815
chain(x_morph, y_morph, x_target, y_target)
802816

803817
# FOR FUTURE MAINTAINERS

tests/test_morphio.py

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from diffpy.morph.morphapp import (
99
create_option_parser,
10+
multiple_morphs,
1011
multiple_targets,
1112
single_morph,
1213
)
@@ -28,6 +29,11 @@
2829
test_saving_succinct = testsaving_dir.joinpath("succinct")
2930
test_saving_verbose = testsaving_dir.joinpath("verbose")
3031
test_saving_verbose_unc = testsaving_dir.joinpath("verbose_unc")
32+
33+
test_saving_grid = testsaving_dir.joinpath("grids")
34+
test_saving_grid_default = test_saving_grid.joinpath("default")
35+
test_saving_grid_original = test_saving_grid.joinpath("original")
36+
3137
tssf = testdata_dir.joinpath("testsequence_serialfile.json")
3238

3339

@@ -220,7 +226,7 @@ def test_morph_outputs(self, setup, tmp_path):
220226
pargs = [morph_file, testsequence_dir]
221227
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
222228

223-
# Save a single verbose morph
229+
# Save a single verbose morph with un certainties
224230
svum = tmp_verbose_unc.joinpath("single_verbose_unc_morph.cgr")
225231
svum_name = svum.resolve().as_posix()
226232
opts, pargs = self.parser.parse_args(
@@ -239,7 +245,6 @@ def test_morph_outputs(self, setup, tmp_path):
239245
pargs = [morph_file, target_file]
240246
single_morph(self.parser, opts, pargs, stdout_flag=False)
241247

242-
# Check the saved files are the same for verbose
243248
common = []
244249
for item in tmp_verbose_unc.glob("**/*.*"):
245250
if item.is_file():
@@ -251,6 +256,78 @@ def test_morph_outputs(self, setup, tmp_path):
251256
expected = filter(ignore_path, tf)
252257
are_files_same(actual, expected)
253258

259+
def test_grid_selection(self, setup, tmp_path):
260+
morph_file = self.testfiles[0]
261+
262+
# Save on default grid
263+
tmp_default = tmp_path.joinpath("default")
264+
tmp_default_name = tmp_default.resolve().as_posix()
265+
266+
opts, pargs = self.parser.parse_args(
267+
[
268+
"--multiple-morphs",
269+
"-s",
270+
tmp_default_name,
271+
"-n",
272+
"--save-names-file",
273+
tssf,
274+
"--xmax",
275+
"35",
276+
]
277+
)
278+
279+
pargs = [testsequence_dir, morph_file]
280+
multiple_morphs(self.parser, opts, pargs, stdout_flag=False)
281+
282+
tmp_default_morphs = tmp_default.joinpath("Morphs")
283+
284+
# Check the saved files are the same for default grid
285+
common = []
286+
for item in tmp_default_morphs.glob("**/*.*"):
287+
if item.is_file():
288+
common.append(item.relative_to(tmp_default_morphs).as_posix())
289+
for file in common:
290+
with open(tmp_default_morphs.joinpath(file)) as gf:
291+
with open(test_saving_grid_default.joinpath(file)) as tf:
292+
actual = filter(ignore_path, gf)
293+
expected = filter(ignore_path, tf)
294+
are_files_same(actual, expected)
295+
296+
# Save on original grid
297+
tmp_original = tmp_path.joinpath("original")
298+
tmp_original_name = tmp_original.resolve().as_posix()
299+
300+
opts, pargs = self.parser.parse_args(
301+
[
302+
"--multiple-morphs",
303+
"-s",
304+
tmp_original_name,
305+
"-n",
306+
"--save-names-file",
307+
tssf,
308+
"--xmax",
309+
"35",
310+
"-o",
311+
]
312+
)
313+
314+
pargs = [testsequence_dir, morph_file]
315+
multiple_morphs(self.parser, opts, pargs, stdout_flag=False)
316+
317+
tmp_original_morphs = tmp_default.joinpath("Morphs")
318+
319+
# Check the saved files are the same for original grid
320+
common = []
321+
for item in tmp_original_morphs.glob("**/*.*"):
322+
if item.is_file():
323+
common.append(item.relative_to(tmp_original_morphs).as_posix())
324+
for file in common:
325+
with open(tmp_original_morphs.joinpath(file)) as gf:
326+
with open(test_saving_grid_original.joinpath(file)) as tf:
327+
actual = filter(ignore_path, gf)
328+
expected = filter(ignore_path, tf)
329+
are_files_same(actual, expected)
330+
254331
# Similar format as test_morph_outputs
255332
def test_morph_diff_outputs(self, setup, tmp_path):
256333
morph_file = self.testfiles[0]

tests/test_morphpy.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ def test_morph_opts(self, setup_morph):
148148
except AttributeError:
149149
pass
150150

151+
def test_morphpy_grid_selection(self, setup_morph):
152+
morph_file = self.testfiles[0]
153+
_, grm = morph(morph_file, morph_file)
154+
# Notice that this is a strict less than, not a less than or equal to.
155+
# This is because xmax is an exclusive function (see MorphRGrid).
156+
grm_truncated_grid = grm[:, 0][grm[:, 0] < 35]
157+
grm_truncated_func = grm[:, 1][grm[:, 0] < 35]
158+
grm_truncated = np.array([grm_truncated_grid, grm_truncated_func]).T
159+
160+
_, grt_default = morph(morph_file, morph_file, xmax=35)
161+
assert np.allclose(grt_default, grm_truncated)
162+
163+
_, grt_original = morph(
164+
morph_file, morph_file, xmax=35, original_grid=True
165+
)
166+
assert np.allclose(grt_original, grm)
167+
151168
def test_morph(self, setup_morph):
152169
morph_results = {}
153170
morph_file = self.testfiles[0]

0 commit comments

Comments
 (0)