Skip to content

test: created a test for squeeze morph using sine wave. Created funct… #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions news/morphsqueeze.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
**Added:**

* Squeeze morph, test with sine wave

**Changed:**

* Added cubic and quadratic terms, as well as
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none of these are needed. We just need one news item per PR basically, not a history of everything that happened on the way. So what is needed is, under Added, something like "Squeeze morph that carries out a non-linear stretch to the independent variable axis."

* squeeze_1 and squeeze_2 parameters.
* I was not keeping the y-axis constant and
* corrected for it

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* I was not keeping the y-values constant, fixed now

**Security:**

* <news item>
55 changes: 55 additions & 0 deletions src/diffpy/morph/morphs/morphsqueeze.py
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions tests/test_morphsqueeze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import numpy as np
import pytest

from diffpy.morph.morphs.morphsqueeze import MorphSqueeze


class TestMorphSqueeze:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your reason for making a class here? We usually just write simpler functions but annotate them to show the intended behavior. Please see diffpy.utils for examples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following test_morphstretch.py. Will change this

@pytest.fixture
def setup(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to be a fixture. If we are going to reuse it in multiple places we can make it a fixture, but then put it in conftest.py

# 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is easier to follow using the expected and actual syntax

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__":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this not needed

TestMorphSqueeze()