Beispiel #1
0
def do_load_model():
    x, y = XDAT, YDAT
    model = load_model(SAVE_MODEL)
    params = model.make_params()

    params['exp_decay'].set(100)
    params['exp_amplitude'].set(100)
    params['g1_center'].set(105, min=75, max=125)
    params['g1_sigma'].set(15, min=3)
    params['g1_amplitude'].set(2000, min=10)

    params['g2_center'].set(155, min=125, max=175)
    params['g2_sigma'].set(15, min=3)
    params['g2_amplitude'].set(2000, min=10)

    result = model.fit(y,  params, x=x)
    check_fit_results(result)
Beispiel #2
0
def test_save_load_model(dill):
    """Save/load Model with/without dill."""
    if dill:
        pytest.importorskip("dill")
    else:
        lmfit.jsonutils.HAS_DILL = False

    # create/save Model and perform some tests
    model, _pars = create_model_params(x, y)
    save_model(model, SAVE_MODEL)

    file_exists = wait_for_file(SAVE_MODEL, timeout=10)
    assert file_exists

    with open(SAVE_MODEL, 'r') as fh:
        text = fh.read()
    assert 1000 < len(text) < 2500

    # load the Model, perform fit and assert results
    saved_model = load_model(SAVE_MODEL)
    params = saved_model.make_params()

    params['exp_decay'].set(100)
    params['exp_amplitude'].set(100)
    params['g1_center'].set(105, min=75, max=125)
    params['g1_sigma'].set(15, min=3)
    params['g1_amplitude'].set(2000, min=10)

    params['g2_center'].set(155, min=125, max=175)
    params['g2_sigma'].set(15, min=3)
    params['g2_amplitude'].set(2000, min=10)

    result = saved_model.fit(y, params, x=x)
    check_fit_results(result)

    clear_savefile(SAVE_MODEL)
Beispiel #3
0
def test_save_load_model(dill):
    """Save/load Model with/without dill."""
    if dill:
        pytest.importorskip("dill")
    else:
        lmfit.jsonutils.HAS_DILL = False

    # create/save Model and perform some tests
    model, _pars = create_model_params(x, y)
    save_model(model, SAVE_MODEL)

    file_exists = wait_for_file(SAVE_MODEL, timeout=10)
    assert file_exists

    with open(SAVE_MODEL, 'r') as fh:
        text = fh.read()
    assert 1000 < len(text) < 2500

    # load the Model, perform fit and assert results
    saved_model = load_model(SAVE_MODEL)
    params = saved_model.make_params()

    params['exp_decay'].set(100)
    params['exp_amplitude'].set(100)
    params['g1_center'].set(105, min=75, max=125)
    params['g1_sigma'].set(15, min=3)
    params['g1_amplitude'].set(2000, min=10)

    params['g2_center'].set(155, min=125, max=175)
    params['g2_sigma'].set(15, min=3)
    params['g2_amplitude'].set(2000, min=10)

    result = saved_model.fit(y, params, x=x)
    check_fit_results(result)

    clear_savefile(SAVE_MODEL)
Beispiel #4
0
# <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>
Beispiel #5
0
#!/usr/bin/env python

# <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>
Beispiel #6
0
#Finally, plot the raw data, fit data, and print the chisqr and the separation

from astropy.io import fits
from lmfit.model import ModelResult, save_model, load_model

#import fits file from the local directory
#Then save the specific range of the data into variables
#And normalized data
spec68 = fits.open('../1Ds_LAEs_forYuzheng/1D_spectrum_MUSEID_68.fits')
x68 = spec68[1].data['delta_v_Lya'][1956:1996]
y68 = spec68[1].data['flux'][1956:1996]
e68 = spec68[1].data['flux_err'][1956:1996]
Ne68 = e68 / y68.max()

#Load preset model
FW2 = load_model('FW2SG.sav')

#plot the graph
result = FW2.fit(Ny68, params_FW, x=x68, weights=Ne68)
pyplot.step(x68, Ny68, '-', where='mid')
pyplot.plot(x68, result.best_fit, '--')
pyplot.title('2 Skewed gaussian using FWHM as parameters')
pyplot.show()

#print out the result
sep_result = result.params['x0_2'] - result.params['x0_1']
print('The chi square is {:.10f}'.format(result.chisqr))
print('The separation is {:.2f}'.format(sep_result))
#print(result.fit_report())
result.params