How to use the lmfit.models.ConstantModel function in lmfit

To help you get started, we’ve selected a few lmfit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lmfit / lmfit-py / tests / test_model.py View on Github external
def test_composite_with_expression(self):
        expression_model = models.ExpressionModel("exp(-x/x0)", name='exp')
        amp_model = models.ConstantModel(prefix='amp_')
        off_model = models.ConstantModel(prefix='off_', name="off")

        comp_model = off_model + amp_model * expression_model

        x = self.x
        true_values = self.true_values()
        data = comp_model.eval(x=x, **true_values) + self.noise
        # data = 0.25 + 1 * np.exp(-x / 2.)

        params = comp_model.make_params(**self.guess())

        result = comp_model.fit(data, x=x, params=params)
        assert_results_close(result.values, true_values, rtol=0.01, atol=0.01)

        data_components = comp_model.eval_components(x=x)
        self.assertIn('exp', data_components)
github lmfit / lmfit-py / tests / test_model.py View on Github external
def test_user_defined_gaussian_plus_constant(self):
        data = self.data + 5.0
        model = self.model + models.ConstantModel()
        guess = self.guess()
        pars = model.make_params(c=10.1, **guess)
        true_values = self.true_values()
        true_values['c'] = 5.0

        result = model.fit(data, pars, x=self.x)
        assert_results_close(result.values, true_values, rtol=0.01, atol=0.01)
github lmfit / lmfit-py / tests / test_model.py View on Github external
def test_eval_components(self):
        model1 = models.GaussianModel(prefix='g1_')
        model2 = models.GaussianModel(prefix='g2_')
        model3 = models.ConstantModel(prefix='bkg_')
        mod = model1 + model2 + model3
        pars = mod.make_params()

        values1 = dict(amplitude=7.10, center=1.1, sigma=2.40)
        values2 = dict(amplitude=12.2, center=2.5, sigma=0.5)
        data = (1.01 + gaussian(x=self.x, **values1) +
                gaussian(x=self.x, **values2) + 0.05*self.noise)

        pars['g1_sigma'].set(2)
        pars['g1_center'].set(1, max=1.5)
        pars['g1_amplitude'].set(3)
        pars['g2_sigma'].set(1)
        pars['g2_center'].set(2.6, min=2.0)
        pars['g2_amplitude'].set(1)
        pars['bkg_c'].set(1.88)
github lmfit / lmfit-py / tests / test_lineshapes_models.py View on Github external
def test_guess_modelparams():
    x = np.linspace(-10, 10, 501)

    mod = models.ConstantModel()
    y = 6.0 + x*0.005
    pars = mod.guess(y)
    assert_allclose(pars['c'].value, 6.0, rtol=0.01)

    mod = models.ComplexConstantModel(prefix='f_')
    y = 6.0 + x*0.005 + (4.0 - 0.02*x)*1j
    pars = mod.guess(y)
    assert_allclose(pars['f_re'].value, 6.0, rtol=0.01)
    assert_allclose(pars['f_im'].value, 4.0, rtol=0.01)

    mod = models.QuadraticModel(prefix='g_')
    y = -0.2 + 3.0*x + 0.005*x**2
    pars = mod.guess(y, x=x)
    assert_allclose(pars['g_a'].value, 0.005, rtol=0.01)
    assert_allclose(pars['g_b'].value, 3.0, rtol=0.01)
    assert_allclose(pars['g_c'].value, -0.2, rtol=0.01)
github lmfit / lmfit-py / tests / test_model.py View on Github external
def setUp(self):
        self.true_values = lambda: dict(c=5)
        self.guess = lambda: dict(c=2)
        self.model_constructor = models.ConstantModel
        super().setUp()
github xraypy / xraylarch / larch / xrf / xrf_calib.py View on Github external
family = 'Lb1'
    try:
        eknown = xray_line(elem, family).energy/1000.0
    except:
        eknown = 0.001
    llim = max(0, roi.left - roi.bgr_width)
    hlim = min(len(chans)-1, roi.right + roi.bgr_width)
    segcounts = counts[llim:hlim]
    maxcounts = max(segcounts)
    ccen = llim + np.where(segcounts==maxcounts)[0][0]
    ecen = ccen * mca.slope + mca.offset
    bkgcounts = counts[llim] + counts[hlim]
    if maxcounts < 2*bkgcounts:
        mca.init_calib[roiname] = (eknown, ecen, 0.0, ccen, None)
    else:
        model = GaussianModel() + ConstantModel()
        params = model.make_params(amplitude=maxcounts,
                                   sigma=(chans[hlim]-chans[llim])/2.0,
                                   center=ccen-llim, c=0.00)
        params['center'].min = -10
        params['center'].max = hlim - llim + 10
        params['c'].min = -10
        out = model.fit(counts[llim:hlim], params, x=chans[llim:hlim])
        ccen = llim + out.params['center'].value
        ecen = ccen * mca.slope + mca.offset
        fwhm = out.params['fwhm'].value * mca.slope
        mca.init_calib[roiname] = (eknown, ecen, fwhm, ccen, out)
github scikit-beam / scikit-beam / skxray / fitting / api.py View on Github external
DonaichModel, PowerLawModel, ExponentialModel,
                          StepModel, RectangleModel)

from .models import (GaussianModel, LorentzianModel, Lorentzian2Model,
                     ComptonModel, ElasticModel)

from lmfit.lineshapes import (pearson7, breit_wigner, damped_oscillator,
                              logistic, lognormal, students_t, expgaussian,
                              donaich, skewed_gaussian, skewed_voigt, step,
                              rectangle, exponential, powerlaw, linear,
                              parabolic)
from .lineshapes import (gaussian, lorentzian, lorentzian2, voigt, pvoigt,
                         gaussian_tail, gausssian_step, elastic, compton)

# valid models
model_list = [ConstantModel, LinearModel, QuadraticModel, ParabolicModel,
              PolynomialModel, GaussianModel, LorentzianModel, VoigtModel,
              PseudoVoigtModel, Pearson7Model, StudentsTModel, BreitWignerModel,
              LognormalModel, DampedOscillatorModel, ExponentialGaussianModel,
              SkewedGaussianModel, DonaichModel, PowerLawModel,
              ExponentialModel, StepModel, RectangleModel, Lorentzian2Model,
              ComptonModel, ElasticModel]

model_list.sort(key=lambda s: str(s).split('.')[-1])


lineshapes_list = [gaussian, lorentzian, voigt, pvoigt, pearson7,
                   breit_wigner, damped_oscillator, logistic,
                   lognormal, students_t, expgaussian, donaich,
                   skewed_gaussian, skewed_voigt, step, rectangle,
                   exponential, powerlaw, linear, parabolic,
                   lorentzian2, compton, elastic, gausssian_step,
github bluesky / databroker / examples / pipeline / pipeline_demo.py View on Github external
if ln_sty is None:
        ln_sty = dict()

        ax.set_title(title)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        txt = ax.annotate('', (0, 0), xytext=(1, 1), xycoords='axes fraction')

    ln_sty = 'bo-'
    if fit:
        ln_sty = 'bo'

    ln, = ax.plot([], [], ln_sty)
    if fit:
        ln2, = ax.plot([], [], 'g-')
        m = GaussianModel() + lmfit.models.ConstantModel()
        param = m.make_params()
        for k in param:
            param[k].value = 1

        param['area'].min = 1
        param['area'].max = 150
        param['sigma'].min = 1
        param['sigma'].max = 150
        param['center'].min = 0
        param['center'].max = 150
    time_tracker = {'old': time.time()}

    def inner(y, x):
        '''
        Update line with this data.  relim, autoscale, trigger redraw
github lmfit / lmfit-py / lmfit / models.py View on Github external
def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise',
                 **kwargs):
        kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
                       'independent_vars': independent_vars})

        def constant(x, c=0.0):
            return c
        super(ConstantModel, self).__init__(constant, **kwargs)