|
| 1 | +.. _extending-srmise: |
| 2 | + |
| 3 | +=================== |
| 4 | +Extending SrMise |
| 5 | +=================== |
| 6 | + |
| 7 | +The :ref:`tutorial` gives an overview of how to use SrMise with the existing |
| 8 | +peak and baseline functions. These inherit from classes providing generic peak |
| 9 | +and baseline functionality, and from which additional peaks and baselines can be |
| 10 | +implemented. The process for adding new peaks and baselines is summarized |
| 11 | +below, but see the source code for additional details. |
| 12 | + |
| 13 | +If you implement a peak or baseline likely to be of broad interest to the PDF |
| 14 | +community, please consider submitting a pull request to the GitHub |
| 15 | +`SrMise repository <https://github.com/diffpy/diffpy.srmise>`_. |
| 16 | + |
| 17 | +Organization of Functions |
| 18 | +------------------------- |
| 19 | + |
| 20 | +The ``BaseFunction`` class in ``diffpy.srmise.basefunction`` implements the |
| 21 | +functionality common to all SrMise baseline and peak functions, which are |
| 22 | +separately implemented in the ``diffpy.srmise.baselines`` and |
| 23 | +``diffpy.srmise.peaks`` subpackages. Specific baseline and peak functions |
| 24 | +inherit from the ``BaselineFunction`` and ``PeakFunction`` classes in those |
| 25 | +subpackges, as shown below. |
| 26 | + |
| 27 | +* .. py:class:: BaseFunction |
| 28 | + |
| 29 | + + .. py:class:: BaselineFunction |
| 30 | + |
| 31 | + - .. py:class:: FromSequence |
| 32 | + - .. py:class:: NanoSpherical |
| 33 | + - .. py:class:: Polynomial |
| 34 | + - *etc.* |
| 35 | + |
| 36 | + + .. py:class:: PeakFunction |
| 37 | + |
| 38 | + - .. py:class:: Gaussian |
| 39 | + - .. py:class:: GaussianOverR |
| 40 | + - *etc.* |
| 41 | + |
| 42 | +Adding Baselines |
| 43 | +------------------------------------- |
| 44 | + |
| 45 | +To add a baseline, create a new module which defines a class inheriting from |
| 46 | +``diffpy.srmise.baselines.base.BaselineFunction``. The class data and methods |
| 47 | +which need to be implemented are summarized in the source code. |
| 48 | + |
| 49 | + |
| 50 | +.. literalinclude:: ../../../diffpy/srmise/baselines/base.py |
| 51 | + :pyobject: BaselineFunction |
| 52 | + :end-before: __init__ |
| 53 | + |
| 54 | +The class methods should follow these specifications. See existing baselines |
| 55 | +for examples. |
| 56 | + |
| 57 | +.. py:method:: estimate_parameters(r, y) |
| 58 | +
|
| 59 | + Return a Numpy array of parameters estimated from the data. |
| 60 | + |
| 61 | + :param r: Grid on which the data are defined. |
| 62 | + :param y: The data. |
| 63 | + :type r: `Sequence` |
| 64 | + :type y: `Sequence` |
| 65 | + :returns: Estimated parameters |
| 66 | + :rtype: `numpy.ndarray` |
| 67 | + :raises: NotImplementedError if estimation has not been implemented. |
| 68 | + :raises: SrMiseEstimationError if estimation fails. |
| 69 | + |
| 70 | + |
| 71 | +.. py:method:: _jacobian_raw(pars, r, free) |
| 72 | + |
| 73 | + Return Jacobian for parameters evaluated over `r`. |
| 74 | + |
| 75 | + :param pars: The parameters of the baseline. |
| 76 | + :param r: Scalar or grid on which to calculate the Jacobian. |
| 77 | + :param free: Boolean values indicating if corresponding parameter is free (True) or fixed (False). |
| 78 | + :type pars: `Sequence(float)` |
| 79 | + :type r: `int`, `float`, or `Sequence(int` or `float)` |
| 80 | + :type free: `Sequence(boolean)` |
| 81 | + :returns: List of Jacobian values (or None if parameter is not free) for each parameter evaluated at `r`. |
| 82 | + :rtype: `list(numpy.ndarray(float) or float or None)` |
| 83 | + |
| 84 | +.. py:method:: _transform_derivativesraw(pars, in_format, out_format) |
| 85 | +
|
| 86 | + Return the gradient matrix of `pars` represented in format 'out_format'. |
| 87 | + |
| 88 | + :param pars: The parameters of the baseline. |
| 89 | + :param in_format: The format of `pars`. |
| 90 | + :param out_format: The desired format of `pars`. |
| 91 | + :type pars: `Sequence(float)` |
| 92 | + :type in_format: `str` |
| 93 | + :type out_format: `str` |
| 94 | + :returns: The gradient matrix for the transformation. |
| 95 | + :rtype: `numpy.ndarray` |
| 96 | + |
| 97 | +.. py:method:: _transform_parametersraw(pars, in_format, out_format) |
| 98 | +
|
| 99 | + Return parameters transformed into format 'out_format'. |
| 100 | + |
| 101 | + :param pars: The parameters of the baseline. |
| 102 | + :param in_format: The format of `pars`. |
| 103 | + :param out_format: The desired format of `pars`. |
| 104 | + :type pars: `Sequence(float)` |
| 105 | + :type in_format: `str` |
| 106 | + :type out_format: `str` |
| 107 | + :returns: The transformed parameters. |
| 108 | + :rtype: `numpy.ndarray` |
| 109 | + |
| 110 | +.. py:method:: _valueraw(pars, r) |
| 111 | +
|
| 112 | + Return value of baseline with given parameters at r. |
| 113 | + |
| 114 | + :param pars: The parameters of the baseline. |
| 115 | + :param r: Scalar or grid on which to calculate the baseline. |
| 116 | + :type pars: `Sequence(float)` |
| 117 | + :type r: `int`, `float`, or `Sequence(int` or `float)` |
| 118 | + :returns: The value of the baseline. |
| 119 | + :rtype: `float` or `numpy.ndarray(float)`. |
| 120 | + |
| 121 | + |
| 122 | +Adding Peaks |
| 123 | +-------------------------- |
| 124 | + |
| 125 | +To add a new peak function, create a new module which defines a class |
| 126 | +inheriting from ``diffpy.srmise.peaks.base.PeakFunction``. Implementing a peak |
| 127 | +function is nearly identical to implementing a baseline function, with the |
| 128 | +following differences: |
| 129 | + |
| 130 | +1) The ``estimate_parameters`` method is required. |
| 131 | +2) The "position" key must be defined in the ``parameterdict`` class member. |
| 132 | +3) Peak functions must implement the additional method ``scale_at``. |
| 133 | + |
| 134 | +.. py:method:: scale_at(pars, r, scale) |
| 135 | +
|
| 136 | + Return peak parameters such that the value at ``r`` is scaled by ``scale`` |
| 137 | + while the position of the peak's maxima remains unchanged. |
| 138 | + |
| 139 | + :param pars: The parameters of the peak. |
| 140 | + :param r: Position where the peak will be rescaled. |
| 141 | + :param scale: A scale factor > 0. |
| 142 | + :type pars: `Sequence(float)` |
| 143 | + :type r: `int` or `float` |
| 144 | + :type scale: `float` |
| 145 | + :returns: The adjusted peak parameters. |
| 146 | + :rtype: `numpy.ndarray(float)`. |
0 commit comments