.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_examples_documentation_model_loadmodel.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_documentation_model_loadmodel.py:


doc_model_loadmodel.py
======================

.. note:: This example *does* actually work, but running from within  sphinx-gallery fails to find symbols saved in the save file.



.. code-block:: pytb

    Traceback (most recent call last):
      File "/Users/Newville/anaconda3/lib/python3.7/site-packages/sphinx_gallery/gen_rst.py", line 480, in _memory_usage
        out = func()
      File "/Users/Newville/anaconda3/lib/python3.7/site-packages/sphinx_gallery/gen_rst.py", line 465, in __call__
        exec(self.code, self.globals)
      File "/Users/Newville/Codes/lmfit-py/examples/documentation/model_loadmodel.py", line 32, in <module>
        result = model.fit(y, params, x=x)
      File "/Users/Newville/anaconda3/lib/python3.7/site-packages/lmfit-0.9.15-py3.7.egg/lmfit/model.py", line 1022, in fit
        output.fit(data=data, weights=weights)
      File "/Users/Newville/anaconda3/lib/python3.7/site-packages/lmfit-0.9.15-py3.7.egg/lmfit/model.py", line 1369, in fit
        self.init_fit = self.model.eval(params=self.params, **self.userkws)
      File "/Users/Newville/anaconda3/lib/python3.7/site-packages/lmfit-0.9.15-py3.7.egg/lmfit/model.py", line 848, in eval
        return self.func(**self.make_funcargs(params, kwargs))
      File "/Users/Newville/Codes/lmfit-py/examples/documentation/model_savemodel.py", line 18, in mysine
        return amp * np.sin(x*freq + shift)
    NameError: name 'np' is not defined





.. code-block:: default

    ##
    import warnings
    warnings.filterwarnings("ignore")
    ##
    # <examples/doc_model_loadmodel.py>
    import matplotlib.pyplot as plt
    import numpy as np

    from lmfit.model import load_model


    def mysine(x, amp, freq, shift):
        return amp * np.sin(x*freq + shift)


    data = np.loadtxt('sinedata.dat')
    x = data[:, 0]
    y = data[:, 1]

    model = load_model('sinemodel.sav', funcdefs={'mysine': mysine})
    params = model.make_params(amp=3, freq=0.52, shift=0)
    params['shift'].max = 1
    params['shift'].min = -1
    params['amp'].min = 0.0

    result = model.fit(y, params, x=x)
    print(result.fit_report())

    plt.plot(x, y, 'bo')
    plt.plot(x, result.best_fit, 'r-')
    plt.show()
    # <end examples/doc_model_loadmodel.py>


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.075 seconds)


.. _sphx_glr_download_examples_documentation_model_loadmodel.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: model_loadmodel.py <model_loadmodel.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: model_loadmodel.ipynb <model_loadmodel.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
