Skip to content

Commit 7ba2a57

Browse files
committed
Formatting tests
1 parent 6474d1a commit 7ba2a57

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

src/diffpy/morph/morph_io.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import print_function
1818

19+
import inspect
1920
import sys
2021
from pathlib import Path
2122

@@ -95,6 +96,15 @@ def single_morph_output(
9596
morphs_out += "\n".join(
9697
f"# {key} = {mr_copy[key]:.6f}" for key in mr_copy.keys()
9798
)
99+
# Special inputs (functional)
100+
if funcy_function is not None:
101+
morphs_in += '# funcy function =\n"""\n'
102+
f_code, _ = inspect.getsourcelines(funcy_function)
103+
n_leading = len(f_code[0]) - len(f_code[0].lstrip())
104+
for idx, f_line in enumerate(f_code):
105+
f_code[idx] = f_line[n_leading:]
106+
morphs_in += "".join(f_code)
107+
morphs_in += '"""\n'
98108

99109
# Printing to terminal
100110
if stdout_flag:

src/diffpy/morph/morphapp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ def single_morph(
530530
and opts.squeeze[-1] == "]"
531531
):
532532
opts.squeeze = opts.squeeze[1:-1]
533+
elif (
534+
len(opts.squeeze) > 1
535+
and opts.squeeze[0] == "("
536+
and opts.squeeze[-1] == ")"
537+
):
538+
opts.squeeze = opts.squeeze[1:-1]
533539
squeeze_coeffs = opts.squeeze.strip().split(",")
534540
squeeze_dict_in = {}
535541
for idx, coeff in enumerate(squeeze_coeffs):

tests/test_morphio.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
multiple_targets,
1111
single_morph,
1212
)
13-
14-
# from diffpy.morph.morphpy import morphpy
13+
from diffpy.morph.morphpy import morphpy
1514

1615
# Support Python 2
1716
try:
@@ -198,3 +197,25 @@ def test_morphsqueeze_outputs(self, setup, tmp_path):
198197
def test_morphfuncy_outputs(self, tmp_path):
199198
def quadratic(x, y, a0, a1, a2):
200199
return a0 + a1 * x + a2 * y**2
200+
201+
r = np.linspace(0, 10, 101)
202+
gr = np.linspace(0, 10, 101)
203+
204+
morphpy(
205+
np.array([r, gr]).T,
206+
np.array([r, quadratic(r, gr, 1, 2, 3)]).T,
207+
squeeze=[0, 0, 0],
208+
funcy=(quadratic, {"a0": 1.0, "a1": 2.0, "a2": 3.0}),
209+
apply=True,
210+
save=tmp_path / "funcy_target.cgr",
211+
verbose=True,
212+
plot=True,
213+
)
214+
215+
with open(testdata_dir.joinpath("funcy_target.cgr")) as tf:
216+
with open(tmp_path.joinpath("funcy_target.cgr")) as gf:
217+
generated = filter(ignore_path, gf)
218+
target = filter(ignore_path, tf)
219+
for x, y in zip(generated, target):
220+
assert x == y
221+
assert all(x == y for x, y in zip(generated, target))

tests/testdata/funcy_target.cgr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# funcy a2 = 3.0
1616
# funcy function =
1717
"""
18-
18+
def quadratic(x, y, a0, a1, a2):
19+
return a0 + a1 * x + a2 * y**2
1920
"""
2021

2122
# Optimized morphing parameters:

0 commit comments

Comments
 (0)