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