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.

Traceback (most recent call last):
  File "/Users/Newville/opt/anaconda3/lib/python3.7/site-packages/sphinx_gallery/gen_rst.py", line 440, in _memory_usage
    out = func()
  File "/Users/Newville/opt/anaconda3/lib/python3.7/site-packages/sphinx_gallery/gen_rst.py", line 425, 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/Codes/lmfit-py/lmfit/model.py", line 1022, in fit
    output.fit(data=data, weights=weights)
  File "/Users/Newville/Codes/lmfit-py/lmfit/model.py", line 1369, in fit
    self.init_fit = self.model.eval(params=self.params, **self.userkws)
  File "/Users/Newville/Codes/lmfit-py/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
##
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>

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

Gallery generated by Sphinx-Gallery