|
| 1 | +"""Class MorphFuncx -- apply a user-supplied python function to the |
| 2 | +x-axis.""" |
| 3 | + |
| 4 | +from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph |
| 5 | + |
| 6 | + |
| 7 | +class MorphFuncx(Morph): |
| 8 | + """Apply a custom function to the x-axis of the morph function. |
| 9 | +
|
| 10 | + General morph function that applies a user-supplied function to the |
| 11 | + y-coordinates of morph data to make it align with a target. |
| 12 | +
|
| 13 | + Configuration Variables |
| 14 | + ----------------------- |
| 15 | + function: callable |
| 16 | + The user-supplied function that applies a transformation to the |
| 17 | + y-coordinates of the data. |
| 18 | +
|
| 19 | + parameters: dict |
| 20 | + A dictionary of parameters to pass to the function. |
| 21 | +
|
| 22 | + Returns |
| 23 | + ------- |
| 24 | + A tuple (x_morph_out, y_morph_out, x_target_out, y_target_out) |
| 25 | + where the target values remain the same and the morph data is |
| 26 | + transformed according to the user-specified function and parameters |
| 27 | + The morphed data is returned on the same grid as the unmorphed data |
| 28 | +
|
| 29 | + Example (FIX) |
| 30 | + ------------- |
| 31 | + Import the funcy morph function: |
| 32 | +
|
| 33 | + >>> from diffpy.morph.morphs.morphfuncy import MorphFuncy |
| 34 | +
|
| 35 | + Define or import the user-supplied transformation function: |
| 36 | +
|
| 37 | + >>> def sine_function(x, y, amplitude, frequency): |
| 38 | + >>> return amplitude * np.sin(frequency * x) * y |
| 39 | +
|
| 40 | + Provide initial guess for parameters: |
| 41 | +
|
| 42 | + >>> parameters = {'amplitude': 2, 'frequency': 2} |
| 43 | +
|
| 44 | + Run the funcy morph given input morph array (x_morph, y_morph)and target |
| 45 | + array (x_target, y_target): |
| 46 | +
|
| 47 | + >>> morph = MorphFuncy() |
| 48 | + >>> morph.function = sine_function |
| 49 | + >>> morph.funcy = parameters |
| 50 | + >>> x_morph_out, y_morph_out, x_target_out, y_target_out = |
| 51 | + ... morph.morph(x_morph, y_morph, x_target, y_target) |
| 52 | +
|
| 53 | + To access parameters from the morph instance: |
| 54 | +
|
| 55 | + >>> x_morph_in = morph.x_morph_in |
| 56 | + >>> y_morph_in = morph.y_morph_in |
| 57 | + >>> x_target_in = morph.x_target_in |
| 58 | + >>> y_target_in = morph.y_target_in |
| 59 | + >>> parameters_out = morph.funcy |
| 60 | + """ |
| 61 | + |
| 62 | + # Define input output types |
| 63 | + summary = "Apply a Python function to the y-axis data" |
| 64 | + xinlabel = LABEL_RA |
| 65 | + yinlabel = LABEL_GR |
| 66 | + xoutlabel = LABEL_RA |
| 67 | + youtlabel = LABEL_GR |
| 68 | + parnames = ["function", "funcy"] |
| 69 | + |
| 70 | + def morph(self, x_morph, y_morph, x_target, y_target): |
| 71 | + """Apply the user-supplied Python function to the y-coordinates |
| 72 | + of the morph data.""" |
| 73 | + Morph.morph(self, x_morph, y_morph, x_target, y_target) |
| 74 | + self.x_morph_out = self.function( |
| 75 | + self.x_morph_in, self.y_morph_in, **self.funcy |
| 76 | + ) |
| 77 | + return self.xyallout |
0 commit comments