Skip to content

Commit 540d6e2

Browse files
committed
Merge
1 parent 873a4f0 commit 540d6e2

File tree

4 files changed

+219
-5
lines changed

4 files changed

+219
-5
lines changed

src/diffpy/morph/morphs/morphrgrid.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ class MorphRGrid(Morph):
5454
def morph(self, x_morph, y_morph, x_target, y_target):
5555
"""Resample arrays onto specified grid."""
5656
Morph.morph(self, x_morph, y_morph, x_target, y_target)
57-
rmininc = max(self.x_target_in[0], self.x_morph_in[0])
58-
r_step_target = (self.x_target_in[-1] - self.x_target_in[0]) / (
57+
rmininc = max(min(self.x_target_in), min(self.x_morph_in))
58+
r_step_target = (max(self.x_target_in) - min(self.x_target_in)) / (
5959
len(self.x_target_in) - 1
6060
)
61-
r_step_morph = (self.x_morph_in[-1] - self.x_morph_in[0]) / (
61+
r_step_morph = (max(self.x_morph_in) - min(self.x_morph_in)) / (
6262
len(self.x_morph_in) - 1
6363
)
6464
rstepinc = max(r_step_target, r_step_morph)
6565
rmaxinc = min(
66-
self.x_target_in[-1] + r_step_target,
67-
self.x_morph_in[-1] + r_step_morph,
66+
max(self.x_target_in) + r_step_target,
67+
max(self.x_morph_in) + r_step_morph,
6868
)
6969
if self.rmin is None or self.rmin < rmininc:
7070
self.rmin = rmininc

tests/test_morphfuncx.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import numpy as np
2+
import pytest
3+
4+
# from diffpy.morph.morphs.morphfuncx import MorphFuncx
5+
from diffpy.morph.morphs.morphfuncy import MorphFuncy
6+
7+
8+
def sine_function(x, y, amplitude, frequency):
9+
return amplitude * np.sin(frequency * x) * y
10+
11+
12+
def exponential_decay_function(x, y, amplitude, decay_rate):
13+
return amplitude * np.exp(-decay_rate * x) * y
14+
15+
16+
def gaussian_function(x, y, amplitude, mean, sigma):
17+
return amplitude * np.exp(-((x - mean) ** 2) / (2 * sigma**2)) * y
18+
19+
20+
def polynomial_function(x, y, a, b, c):
21+
return (a * x**2 + b * x + c) * y
22+
23+
24+
def logarithmic_function(x, y, scale):
25+
return scale * np.log(1 + x) * y
26+
27+
28+
# FIXME:
29+
@pytest.mark.parametrize(
30+
"function, parameters, expected_function",
31+
[
32+
(
33+
sine_function,
34+
{"amplitude": 2, "frequency": 5},
35+
lambda x, y: 2 * np.sin(5 * x) * y,
36+
),
37+
(
38+
exponential_decay_function,
39+
{"amplitude": 5, "decay_rate": 0.1},
40+
lambda x, y: 5 * np.exp(-0.1 * x) * y,
41+
),
42+
(
43+
gaussian_function,
44+
{"amplitude": 1, "mean": 5, "sigma": 1},
45+
lambda x, y: np.exp(-((x - 5) ** 2) / (2 * 1**2)) * y,
46+
),
47+
(
48+
polynomial_function,
49+
{"a": 1, "b": 2, "c": 0},
50+
lambda x, y: (x**2 + 2 * x) * y,
51+
),
52+
(
53+
logarithmic_function,
54+
{"scale": 0.5},
55+
lambda x, y: 0.5 * np.log(1 + x) * y,
56+
),
57+
],
58+
)
59+
def test_funcy(function, parameters, expected_function):
60+
x_morph = np.linspace(0, 10, 101)
61+
y_morph = np.sin(x_morph)
62+
x_target = x_morph.copy()
63+
y_target = y_morph.copy()
64+
x_morph_expected = x_morph
65+
y_morph_expected = expected_function(x_morph, y_morph)
66+
morph = MorphFuncy()
67+
morph.funcy_function = function
68+
morph.funcy = parameters
69+
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = (
70+
morph.morph(x_morph, y_morph, x_target, y_target)
71+
)
72+
73+
assert np.allclose(y_morph_actual, y_morph_expected)
74+
assert np.allclose(x_morph_actual, x_morph_expected)
75+
assert np.allclose(x_target_actual, x_target)
76+
assert np.allclose(y_target_actual, y_target)

tests/test_morphfuncxy.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import numpy as np
2+
import pytest
3+
4+
# from diffpy.morph.morphs.morphfuncxy import MorphFuncxy
5+
from diffpy.morph.morphs.morphfuncy import MorphFuncy
6+
7+
8+
def sine_function(x, y, amplitude, frequency):
9+
return amplitude * np.sin(frequency * x) * y
10+
11+
12+
def exponential_decay_function(x, y, amplitude, decay_rate):
13+
return amplitude * np.exp(-decay_rate * x) * y
14+
15+
16+
def gaussian_function(x, y, amplitude, mean, sigma):
17+
return amplitude * np.exp(-((x - mean) ** 2) / (2 * sigma**2)) * y
18+
19+
20+
def polynomial_function(x, y, a, b, c):
21+
return (a * x**2 + b * x + c) * y
22+
23+
24+
def logarithmic_function(x, y, scale):
25+
return scale * np.log(1 + x) * y
26+
27+
28+
# FIXME:
29+
@pytest.mark.parametrize(
30+
"function, parameters, expected_function",
31+
[
32+
(
33+
sine_function,
34+
{"amplitude": 2, "frequency": 5},
35+
lambda x, y: 2 * np.sin(5 * x) * y,
36+
),
37+
(
38+
exponential_decay_function,
39+
{"amplitude": 5, "decay_rate": 0.1},
40+
lambda x, y: 5 * np.exp(-0.1 * x) * y,
41+
),
42+
(
43+
gaussian_function,
44+
{"amplitude": 1, "mean": 5, "sigma": 1},
45+
lambda x, y: np.exp(-((x - 5) ** 2) / (2 * 1**2)) * y,
46+
),
47+
(
48+
polynomial_function,
49+
{"a": 1, "b": 2, "c": 0},
50+
lambda x, y: (x**2 + 2 * x) * y,
51+
),
52+
(
53+
logarithmic_function,
54+
{"scale": 0.5},
55+
lambda x, y: 0.5 * np.log(1 + x) * y,
56+
),
57+
],
58+
)
59+
def test_funcy(function, parameters, expected_function):
60+
x_morph = np.linspace(0, 10, 101)
61+
y_morph = np.sin(x_morph)
62+
x_target = x_morph.copy()
63+
y_target = y_morph.copy()
64+
x_morph_expected = x_morph
65+
y_morph_expected = expected_function(x_morph, y_morph)
66+
morph = MorphFuncy()
67+
morph.funcy_function = function
68+
morph.funcy = parameters
69+
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = (
70+
morph.morph(x_morph, y_morph, x_target, y_target)
71+
)
72+
73+
assert np.allclose(y_morph_actual, y_morph_expected)
74+
assert np.allclose(x_morph_actual, x_morph_expected)
75+
assert np.allclose(x_target_actual, x_target)
76+
assert np.allclose(y_target_actual, y_target)

tests/test_morphpy.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,68 @@ def gaussian_like_function(x, y, mu):
249249
assert pytest.approx(abs(morph_info["smear"])) == 4.0
250250
assert pytest.approx(morph_info["funcy"]["mu"]) == 50.0
251251

252+
# FIXME:
253+
def test_morphfuncx(self, setup_morph):
254+
def gaussian(x, mu, sigma):
255+
return np.exp(-((x - mu) ** 2) / (2 * sigma**2)) / (
256+
sigma * np.sqrt(2 * np.pi)
257+
)
258+
259+
def gaussian_like_function(x, y, mu):
260+
return gaussian((x + y) / 2, mu, 3)
261+
262+
morph_r = np.linspace(0, 100, 1001)
263+
morph_gr = np.linspace(0, 100, 1001)
264+
265+
target_r = np.linspace(0, 100, 1001)
266+
target_gr = 0.5 * gaussian(target_r, 50, 5) + 0.05
267+
268+
morph_info, _ = morph_arrays(
269+
np.array([morph_r, morph_gr]).T,
270+
np.array([target_r, target_gr]).T,
271+
scale=1,
272+
smear=3.75,
273+
vshift=0.01,
274+
funcy=(gaussian_like_function, {"mu": 47.5}),
275+
tolerance=1e-12,
276+
)
277+
278+
assert pytest.approx(morph_info["scale"]) == 0.5
279+
assert pytest.approx(morph_info["vshift"]) == 0.05
280+
assert pytest.approx(abs(morph_info["smear"])) == 4.0
281+
assert pytest.approx(morph_info["funcy"]["mu"]) == 50.0
282+
283+
# FIXME:
284+
def test_morphfuncxy(self, setup_morph):
285+
def gaussian(x, mu, sigma):
286+
return np.exp(-((x - mu) ** 2) / (2 * sigma**2)) / (
287+
sigma * np.sqrt(2 * np.pi)
288+
)
289+
290+
def gaussian_like_function(x, y, mu):
291+
return gaussian((x + y) / 2, mu, 3)
292+
293+
morph_r = np.linspace(0, 100, 1001)
294+
morph_gr = np.linspace(0, 100, 1001)
295+
296+
target_r = np.linspace(0, 100, 1001)
297+
target_gr = 0.5 * gaussian(target_r, 50, 5) + 0.05
298+
299+
morph_info, _ = morph_arrays(
300+
np.array([morph_r, morph_gr]).T,
301+
np.array([target_r, target_gr]).T,
302+
scale=1,
303+
smear=3.75,
304+
vshift=0.01,
305+
funcy=(gaussian_like_function, {"mu": 47.5}),
306+
tolerance=1e-12,
307+
)
308+
309+
assert pytest.approx(morph_info["scale"]) == 0.5
310+
assert pytest.approx(morph_info["vshift"]) == 0.05
311+
assert pytest.approx(abs(morph_info["smear"])) == 4.0
312+
assert pytest.approx(morph_info["funcy"]["mu"]) == 50.0
313+
252314
def test_morphpy_outputs(self, tmp_path):
253315
r = np.linspace(0, 1, 11)
254316
gr = np.linspace(0, 1, 11)

0 commit comments

Comments
 (0)