Skip to content

Commit 77d987b

Browse files
committed
doc/tutorial: adding morphfuncy tutorial and doc
1 parent a7431d9 commit 77d987b

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

doc/source/quickstart.rst

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -407,56 +407,74 @@ transformations.
407407
In this tutorial, we walk through how to use ``MorphFuncy`` with an example
408408
transformation. Unlike other morphs that can be run from the command line,
409409
``MorphFuncy`` requires a Python function and is therefore intended to be used
410-
within the Python API.
410+
through Python scripting.
411411

412-
1. Import the necessary modules into your Python script ::
412+
1. Import the necessary modules into your Python script:
413+
414+
.. code-block:: python
413415
414416
from diffpy.morph.morph_api import morph, morph_default_config
415417
import numpy as np
416418
417419
2. Define a custom Python function to apply a transformation to the data.
420+
The function must take ``x`` and ``y`` (1D arrays of the same length)
421+
along with named parameters, and return a transformed ``y`` array of the
422+
same length.
418423
For this example, we will use a simple linear transformation that
419-
scales the input and applies an offset ::
424+
scales the input and applies an offset:
425+
426+
.. code-block:: python
420427
421428
def linear_function(x, y, scale, offset):
422429
return (scale * x) * y + offset
423430
424431
3. In this example, we use a sine function for the morph data and generate
425432
the target data by applying the linear transformation with known scale
426-
and offset to it ::
433+
and offset to it:
434+
435+
.. code-block:: python
427436
428437
x_morph = np.linspace(0, 10, 101)
429438
y_morph = np.sin(x_morph)
430439
x_target = x_morph.copy()
431440
y_target = np.sin(x_target) * 20 * x_target + 0.8
432441
433-
4. Set up the configuration dictionary. This includes both the
442+
4. Set up the morph configuration dictionary. This includes both the
434443
transformation parameters (our initial guess) and the transformation
435-
function itself ::
444+
function itself:
445+
446+
.. code-block:: python
447+
448+
morph_config = morph_default_config(funcy={"scale": 1.2, "offset": 0.1})
449+
morph_config["function"] = linear_function
450+
451+
# morph_config now contains:
452+
# {'funcy': {'scale': 1.2, 'offset': 0.1}, 'function': linear_function}
453+
454+
5. Run the morph using the ``morph(...)``. This will apply the user-defined
455+
function and refine the parameters to best align the morph data
456+
with the target data:
436457

437-
cfg = morph_default_config(funcy={"scale": 1.2, "offset": 0.1})
438-
cfg["function"] = linear_function
458+
.. code-block:: python
439459
440-
5. Run the morph using the API function ``morph(...)``. This will apply the
441-
user-defined function and refine the parameters to best align the morph data
442-
with the target data ::
460+
morph_result = morph(x_morph, y_morph, x_target, y_target, **morph_config)
443461
444-
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
462+
6. Extract the morphed output and the fitted parameters from the result:
445463

446-
6. Extract the morphed output and the fitted parameters from the result ::
464+
.. code-block:: python
447465
448-
morphed_cfg = morph_rv["morphed_config"]
449-
x_morph_out, y_morph_out, x_target_out, y_target_out = morph_rv["morph_chain"].xyallout
466+
fitted_config = morph_result["morphed_config"]
467+
x_morph_out, y_morph_out, x_target_out, y_target_out = morph_result["morph_chain"].xyallout
450468
451-
fitted_parameters = morphed_cfg["funcy"]
452-
print("Fitted scale:", fitted_parameters["scale"])
453-
print("Fitted offset:", fitted_parameters["offset"])
469+
fitted_params = fitted_config["funcy"]
470+
print(f"Fitted scale: {fitted_params['scale']}")
471+
print(f"Fitted offset: {fitted_params['offset']}")
454472
455-
As you can see, the fitted scale and offset values match the ones used
456-
to generate the target (scale=20 & offset=0.8). This example shows how
457-
``MorphFuncy`` can be used to fit and apply custom transformations. Now
458-
it's your turn to experiment with other custom functions that may be useful
459-
for analyzing your data.
473+
As you can see, the fitted scale and offset values match the ones used
474+
to generate the target (scale=20 & offset=0.8). This example shows how
475+
``MorphFuncy`` can be used to fit and apply custom transformations. Now
476+
it's your turn to experiment with other custom functions that may be useful
477+
for analyzing your data.
460478

461479
Bug Reports
462480
===========

src/diffpy/morph/morphs/morphfuncy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55

66
class MorphFuncy(Morph):
7-
"""General morph function that applies a user-supplied function to the
7+
"""Apply a custom function to the y-axis of the morph function.
8+
9+
General morph function that applies a user-supplied function to the
810
y-coordinates of morph data to make it align with a target.
911
1012
Configuration Variables
@@ -15,7 +17,6 @@ class MorphFuncy(Morph):
1517
1618
parameters: dict
1719
A dictionary of parameters to pass to the function.
18-
These parameters are unpacked using **kwargs.
1920
2021
Returns
2122
-------

0 commit comments

Comments
 (0)