Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
test/func: improving readability
  • Loading branch information
Luiskitsu committed Apr 24, 2025
commit b2c46cbd90f6438061e1b49d6310ba4fa8c99054
7 changes: 5 additions & 2 deletions src/diffpy/morph/morphs/morphfuncy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ def morph(self, x_morph, y_morph, x_target, y_target):
-------
Import the funcy morph function:
>>> from diffpy.morph.morphs.morphfuncy import MorphFuncy

Define or import the user-supplied transformation function:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if this would be easier to read with judiciously placed blank lines, maybe after each code block, but play around till it looks nice? I am not sure if there is a docstring standard for this, in which case follow it, but if not, then I think a few blank lines could increase readability.

>>> def sine_function(x, y, amplitude, frequency):
>>> return amplitude * np.sin(frequency * x) * y

Provide initial guess for parameters:
>>> parameters = {'amplitude': 2, 'frequency': 2}

Run the funcy morph given input morph array (x_morph, y_morph)
and target array (x_target, y_target):
>>> morph = MorphFuncy()
>>> morph.function = sine_function
>>> morph.parameters = parameters
>>> x_morph_out, y_morph_out, x_target_out, y_target_out = morph(
>>> x_morph_out, y_morph_out, x_target_out, y_target_out = morph.morph(
... x_morph, y_morph, x_target, y_target)

To access parameters from the morph instance:
>>> x_morph_in = morph.x_morph_in
>>> y_morph_in = morph.y_morph_in
Expand All @@ -61,5 +65,4 @@ def morph(self, x_morph, y_morph, x_target, y_target):
self.y_morph_out = self.function(
self.x_morph_in, self.y_morph_in, **self.parameters
)

return self.xyallout
1 change: 1 addition & 0 deletions tests/test_morphfuncy.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_funcy(function, parameters, expected_function):
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason you have the function call inside parens? It should work the same way without these and this is somewhat unconventional and therefore distracting?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is same as comment above, this is modified by the hooks.

morph.morph(x_morph, y_morph, x_target, y_target)
)

assert np.allclose(y_morph_actual, y_morph_expected)
assert np.allclose(x_morph_actual, x_morph_expected)
assert np.allclose(x_target_actual, x_target)
Expand Down