Skip to content

Commit 1c38990

Browse files
committed
Merge
1 parent b0f07b0 commit 1c38990

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
@@ -207,6 +207,68 @@ def gaussian_like_function(x, y, mu):
207207
assert pytest.approx(abs(morph_info["smear"])) == 4.0
208208
assert pytest.approx(morph_info["funcy"]["mu"]) == 50.0
209209

210+
# FIXME:
211+
def test_morphfuncx(self, setup_morph):
212+
def gaussian(x, mu, sigma):
213+
return np.exp(-((x - mu) ** 2) / (2 * sigma**2)) / (
214+
sigma * np.sqrt(2 * np.pi)
215+
)
216+
217+
def gaussian_like_function(x, y, mu):
218+
return gaussian((x + y) / 2, mu, 3)
219+
220+
morph_r = np.linspace(0, 100, 1001)
221+
morph_gr = np.linspace(0, 100, 1001)
222+
223+
target_r = np.linspace(0, 100, 1001)
224+
target_gr = 0.5 * gaussian(target_r, 50, 5) + 0.05
225+
226+
morph_info, _ = morph_arrays(
227+
np.array([morph_r, morph_gr]).T,
228+
np.array([target_r, target_gr]).T,
229+
scale=1,
230+
smear=3.75,
231+
vshift=0.01,
232+
funcy=(gaussian_like_function, {"mu": 47.5}),
233+
tolerance=1e-12,
234+
)
235+
236+
assert pytest.approx(morph_info["scale"]) == 0.5
237+
assert pytest.approx(morph_info["vshift"]) == 0.05
238+
assert pytest.approx(abs(morph_info["smear"])) == 4.0
239+
assert pytest.approx(morph_info["funcy"]["mu"]) == 50.0
240+
241+
# FIXME:
242+
def test_morphfuncxy(self, setup_morph):
243+
def gaussian(x, mu, sigma):
244+
return np.exp(-((x - mu) ** 2) / (2 * sigma**2)) / (
245+
sigma * np.sqrt(2 * np.pi)
246+
)
247+
248+
def gaussian_like_function(x, y, mu):
249+
return gaussian((x + y) / 2, mu, 3)
250+
251+
morph_r = np.linspace(0, 100, 1001)
252+
morph_gr = np.linspace(0, 100, 1001)
253+
254+
target_r = np.linspace(0, 100, 1001)
255+
target_gr = 0.5 * gaussian(target_r, 50, 5) + 0.05
256+
257+
morph_info, _ = morph_arrays(
258+
np.array([morph_r, morph_gr]).T,
259+
np.array([target_r, target_gr]).T,
260+
scale=1,
261+
smear=3.75,
262+
vshift=0.01,
263+
funcy=(gaussian_like_function, {"mu": 47.5}),
264+
tolerance=1e-12,
265+
)
266+
267+
assert pytest.approx(morph_info["scale"]) == 0.5
268+
assert pytest.approx(morph_info["vshift"]) == 0.05
269+
assert pytest.approx(abs(morph_info["smear"])) == 4.0
270+
assert pytest.approx(morph_info["funcy"]["mu"]) == 50.0
271+
210272
def test_morphpy_outputs(self, tmp_path):
211273
r = np.linspace(0, 1, 11)
212274
gr = np.linspace(0, 1, 11)

0 commit comments

Comments
 (0)