Skip to content
Merged
Prev Previous commit
Next Next commit
Add morph funcx (scrappy)
  • Loading branch information
Sparks29032 committed Jul 31, 2025
commit a3c9d0097bbb4e51034e3c8c8e67ff34fe9eb5d2
77 changes: 77 additions & 0 deletions src/diffpy/morph/morphs/morphfuncx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""Class MorphFuncx -- apply a user-supplied python function to the
x-axis."""

from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph


class MorphFuncx(Morph):
"""Apply a custom function to the x-axis of the morph function.

General morph function that applies a user-supplied function to the
y-coordinates of morph data to make it align with a target.

Configuration Variables
-----------------------
function: callable
The user-supplied function that applies a transformation to the
y-coordinates of the data.

parameters: dict
A dictionary of parameters to pass to the function.

Returns
-------
A tuple (x_morph_out, y_morph_out, x_target_out, y_target_out)
where the target values remain the same and the morph data is
transformed according to the user-specified function and parameters
The morphed data is returned on the same grid as the unmorphed data

Example (FIX)
-------------
Import the funcy morph function:

>>> from diffpy.morph.morphs.morphfuncy import MorphFuncy

Define or import the user-supplied transformation function:

>>> 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.funcy = parameters
>>> 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
>>> x_target_in = morph.x_target_in
>>> y_target_in = morph.y_target_in
>>> parameters_out = morph.funcy
"""

# Define input output types
summary = "Apply a Python function to the y-axis data"
xinlabel = LABEL_RA
yinlabel = LABEL_GR
xoutlabel = LABEL_RA
youtlabel = LABEL_GR
parnames = ["function", "funcy"]

def morph(self, x_morph, y_morph, x_target, y_target):
"""Apply the user-supplied Python function to the y-coordinates
of the morph data."""
Morph.morph(self, x_morph, y_morph, x_target, y_target)
self.x_morph_out = self.function(
self.x_morph_in, self.y_morph_in, **self.funcy
)
return self.xyallout