From 7242578775a8d109c8d280ac03da0fd157f8229d Mon Sep 17 00:00:00 2001 From: reneeotten Date: Sun, 3 Dec 2017 12:16:53 -0500 Subject: [PATCH 1/3] STY: update examples files. --- examples/doc_model_loadmodel.py | 10 ++++++---- examples/doc_model_loadmodelresult.py | 9 +++++---- examples/doc_model_loadmodelresult2.py | 8 +++++--- examples/doc_model_savemodel.py | 7 +++++-- examples/doc_model_savemodelresult.py | 10 ++++++---- examples/doc_model_savemodelresult2.py | 10 ++++------ 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/examples/doc_model_loadmodel.py b/examples/doc_model_loadmodel.py index b00bed4ed..1ff8bb6c6 100644 --- a/examples/doc_model_loadmodel.py +++ b/examples/doc_model_loadmodel.py @@ -1,9 +1,12 @@ #!/usr/bin/env python -# -import numpy as np + +# 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) @@ -24,5 +27,4 @@ def mysine(x, amp, freq, shift): plt.plot(x, y, 'bo') plt.plot(x, result.best_fit, 'r-') plt.show() - -# +# diff --git a/examples/doc_model_loadmodelresult.py b/examples/doc_model_loadmodelresult.py index 6448e8ec8..ec8d32c9c 100644 --- a/examples/doc_model_loadmodelresult.py +++ b/examples/doc_model_loadmodelresult.py @@ -1,7 +1,9 @@ #!/usr/bin/env python -# -import numpy as np + +# import matplotlib.pyplot as plt +import numpy as np + from lmfit.model import load_modelresult data = np.loadtxt('model1d_gauss.dat') @@ -14,5 +16,4 @@ plt.plot(x, y, 'bo') plt.plot(x, result.best_fit, 'r-') plt.show() - -# +# diff --git a/examples/doc_model_loadmodelresult2.py b/examples/doc_model_loadmodelresult2.py index 83f7afa5b..e6f4ec894 100644 --- a/examples/doc_model_loadmodelresult2.py +++ b/examples/doc_model_loadmodelresult2.py @@ -1,7 +1,9 @@ #!/usr/bin/env python -# -import numpy as np + +# import matplotlib.pyplot as plt +import numpy as np + from lmfit.model import load_modelresult dat = np.loadtxt('NIST_Gauss2.dat') @@ -14,4 +16,4 @@ plt.plot(x, y, 'bo') plt.plot(x, result.best_fit, 'r-') plt.show() -# +# diff --git a/examples/doc_model_savemodel.py b/examples/doc_model_savemodel.py index 122f37a67..4929d6510 100644 --- a/examples/doc_model_savemodel.py +++ b/examples/doc_model_savemodel.py @@ -1,8 +1,11 @@ #!/usr/bin/env python -# + +# import numpy as np + from lmfit.model import Model, save_model + def mysine(x, amp, freq, shift): return amp * np.sin(x*freq + shift) @@ -11,4 +14,4 @@ def mysine(x, amp, freq, shift): pars = sinemodel.make_params(amp=1, freq=0.25, shift=0) save_model(sinemodel, 'sinemodel.sav') -# +# diff --git a/examples/doc_model_savemodelresult.py b/examples/doc_model_savemodelresult.py index 2b2540eed..9e5e9acfb 100644 --- a/examples/doc_model_savemodelresult.py +++ b/examples/doc_model_savemodelresult.py @@ -1,9 +1,11 @@ #!/usr/bin/env python -# -import numpy as np + +# import matplotlib.pyplot as plt -from lmfit.models import GaussianModel +import numpy as np + from lmfit.model import save_modelresult +from lmfit.models import GaussianModel data = np.loadtxt('model1d_gauss.dat') x = data[:, 0] @@ -20,4 +22,4 @@ plt.plot(x, result.init_fit, 'k--') plt.plot(x, result.best_fit, 'r-') plt.show() -# +# diff --git a/examples/doc_model_savemodelresult2.py b/examples/doc_model_savemodelresult2.py index 040d9ebf4..60fb8f57c 100644 --- a/examples/doc_model_savemodelresult2.py +++ b/examples/doc_model_savemodelresult2.py @@ -1,6 +1,8 @@ #!/usr/bin/env python -# + +# import numpy as np + from lmfit.model import save_modelresult from lmfit.models import ExponentialModel, GaussianModel @@ -13,15 +15,12 @@ gauss1 = GaussianModel(prefix='g1_') pars.update(gauss1.make_params()) - pars['g1_center'].set(value=105, min=75, max=125) pars['g1_sigma'].set(value=15, min=3) pars['g1_amplitude'].set(value=2000, min=10) gauss2 = GaussianModel(prefix='g2_') - pars.update(gauss2.make_params()) - pars['g2_center'].set(value=155, min=125, max=175) pars['g2_sigma'].set(value=15, min=3) pars['g2_amplitude'].set(value=2000, min=10) @@ -35,5 +34,4 @@ save_modelresult(result, 'nistgauss_modelresult.sav') print(result.fit_report()) - -# +# From 0dba26f8d1edf61cb4087728cda3da0b92f6f53f Mon Sep 17 00:00:00 2001 From: reneeotten Date: Sun, 3 Dec 2017 14:01:33 -0500 Subject: [PATCH 2/3] MAINT: remove unused fwhm_factor and use full expression for Gaussian models. --- lmfit/models.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lmfit/models.py b/lmfit/models.py index e7ae03c24..c6832eb3e 100644 --- a/lmfit/models.py +++ b/lmfit/models.py @@ -308,7 +308,7 @@ class GaussianModel(Model): """ - fwhm_factor = 2.354820 + fwhm_factor = 2*np.sqrt(2*np.log(2)) height_factor = 1./np.sqrt(2*np.pi) def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise', @@ -526,8 +526,6 @@ class Pearson7Model(Model): """ - fwhm_factor = 1.0 - def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise', **kwargs): kwargs.update({'prefix': prefix, 'nan_policy': nan_policy, @@ -808,7 +806,7 @@ class SkewedGaussianModel(Model): """ - fwhm_factor = 2.354820 + fwhm_factor = 2*np.sqrt(2*np.log(2)) height_factor = 1./np.sqrt(2*np.pi) def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise', From 58e52ddc766ea48db9eabd181281ca17a99747c9 Mon Sep 17 00:00:00 2001 From: reneeotten Date: Wed, 6 Dec 2017 23:32:41 -0500 Subject: [PATCH 3/3] ENH: show the fit on top of data points. As discussed on the mailinglist the default for ModelResult.plot_fit() has been changed to show the fit on top of the data points. (see https://groups.google.com/forum/#!topic/lmfit-py/rekwbFVJy2c) --- lmfit/model.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lmfit/model.py b/lmfit/model.py index 2d8d0cf1c..5867dcaa4 100644 --- a/lmfit/model.py +++ b/lmfit/model.py @@ -1649,11 +1649,6 @@ def plot_fit(self, ax=None, datafmt='o', fitfmt='-', initfmt='--', **{independent_var: x_array_dense}), initfmt, label='init', **init_kws) - ax.plot( - x_array_dense, - self.model.eval(self.params, **{independent_var: x_array_dense}), - fitfmt, label='best-fit', **fit_kws) - if yerr is None and self.weights is not None: yerr = 1.0/self.weights if yerr is not None: @@ -1662,6 +1657,11 @@ def plot_fit(self, ax=None, datafmt='o', fitfmt='-', initfmt='--', else: ax.plot(x_array, self.data, datafmt, label='data', **data_kws) + ax.plot( + x_array_dense, + self.model.eval(self.params, **{independent_var: x_array_dense}), + fitfmt, label='best-fit', **fit_kws) + ax.set_title(self.model.name) if xlabel is None: ax.set_xlabel(independent_var)