diff --git a/news/morphsqueeze.rst b/news/morphsqueeze.rst new file mode 100644 index 0000000..6651944 --- /dev/null +++ b/news/morphsqueeze.rst @@ -0,0 +1,26 @@ +**Added:** + +* Squeeze morph, test with sine wave + +**Changed:** + +* Added cubic and quadratic terms, as well as +* squeeze_1 and squeeze_2 parameters. +* I was not keeping the y-axis constant and +* corrected for it + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* I was not keeping the y-values constant, fixed now + +**Security:** + +* diff --git a/src/diffpy/morph/morphs/morphsqueeze.py b/src/diffpy/morph/morphs/morphsqueeze.py new file mode 100644 index 0000000..42f37f9 --- /dev/null +++ b/src/diffpy/morph/morphs/morphsqueeze.py @@ -0,0 +1,55 @@ +"""class MorphSqueeze -- apply a non-linear squeeze to the morph. +This morph non-linearly adjusts the x-coordinates. +The y-values are the same as initial, just on a new grid. +""" + +from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph + + +class MorphSqueeze(Morph): + summary = "Squeeze morph by desired amount (non-linear transformation)" + xinlabel = LABEL_RA # This label need to change to be more generic + yinlabel = LABEL_GR # This label need to change to be more generic + xoutlabel = LABEL_RA # This label need to change to be more generic + youtlabel = LABEL_GR # This label need to change to be more generic + parnames = ["squeeze_1", "squeeze_2"] + + def morph(self, x_morph, y_morph, x_target, y_target): + """Resample arrays onto the specified grid using a non-linear squeeze. + + Parameters + ---------- + x_morph : array-like + The input x-values to be transformed. + y_morph : array-like + The input y-values. + x_target, y_target : array-like + The target grid arrays (left unchanged by this morph). + + Returns + ------- + (x_morph_out, y_morph_out, x_target, y_target) + """ + # Initialize the parent class to set up attributes + Morph.morph(self, x_morph, y_morph, x_target, y_target) + + # If squeeze_1 and squeeze_2 are zero, return original output + if self.squeeze_1 == 0 and self.squeeze_2 == 0: + return self.xyallout + + # Compute new x positions using the non-linear squeeze transformation: + new_x = ( + self.x_morph_in + + self.squeeze_1 * self.x_morph_in**2 + + self.squeeze_2 * self.x_morph_in**3 + ) + + self.x_morph_out = new_x + + # The y-axis should be the same + self.y_morph_out = self.y_morph_in + + return self.xyallout + + +# End of class MorphSqueeze diff --git a/tests/test_morphsqueeze.py b/tests/test_morphsqueeze.py new file mode 100644 index 0000000..27b0209 --- /dev/null +++ b/tests/test_morphsqueeze.py @@ -0,0 +1,65 @@ +import numpy as np +import pytest + +from diffpy.morph.morphs.morphsqueeze import MorphSqueeze + + +class TestMorphSqueeze: + @pytest.fixture + def setup(self): + # Create data for testing + self.x_morph = np.linspace(0, 10, 1000) + self.y_morph = np.sin(self.x_morph) + self.x_target = self.x_morph.copy() + self.y_target = self.y_morph.copy() + return + + def test_no_squeeze(self, setup): + """When both squeeze are zero, the input should be unchanged.""" + morph = MorphSqueeze() + morph.squeeze_1 = 0.0 + morph.squeeze_2 = 0.0 + + x_morph, y_morph, x_target, y_target = morph( + self.x_morph, self.y_morph, self.x_target, self.y_target + ) + + # Verify that the morph output matches the original input + assert np.allclose(x_morph, self.x_morph) + assert np.allclose(y_morph, self.y_morph) + # And the target arrays remain unchanged + assert np.allclose(x_target, self.x_target) + assert np.allclose(y_target, self.y_target) + + def test_morph_with_squeeze(self, setup): + """Test that with a non-zero squeeze, + x_morph is transformed non-linearly.""" + morph = MorphSqueeze() + morph.squeeze_1 = -0.07 + morph.squeeze_2 = 0.1 + + x_new, y_new, x_target, y_target = morph( + self.x_morph, self.y_morph, self.x_target, self.y_target + ) + + # Check that target arrays remain unchanged + assert np.allclose(self.y_target, y_target) + + # For this test, we expect: + # x_new = x_morph + squeeze_1 * x_morph ** 2 + + # squeeze_2 * x_morph ** 3 + expected_x = ( + self.x_morph + + morph.squeeze_1 * self.x_morph**2 + + morph.squeeze_2 * self.x_morph**3 + ) + expected_y = np.sin(self.x_morph) + + res = sum(np.fabs(expected_y - y_new)) + res2 = sum(np.fabs(expected_x - x_new)) + assert res < 0.1 + assert res2 < 0.1 + + +if __name__ == "__main__": + TestMorphSqueeze()