def test_saveload_modelresult_exception(): """Make sure the proper exceptions are raised when needed.""" model, _pars = create_model_params(x, y) save_model(model, SAVE_MODEL) with pytest.raises(AttributeError, match=r'needs saved ModelResult'): load_modelresult(SAVE_MODEL) clear_savefile(SAVE_MODEL)
def test_saveload_modelresult_exception(): """Make sure the proper exceptions are raised when needed.""" model, _pars = create_model_params(x, y) save_model(model, SAVE_MODEL) with pytest.raises(AttributeError, match=r'needs saved ModelResult'): load_modelresult(SAVE_MODEL) clear_savefile(SAVE_MODEL)
def do_save_model(with_dill=True): x, y = XDAT, YDAT if DILL_AVAILABLE and with_dill: lmfit.jsonutils.HAS_DILL = True else: lmfit.jsonutils.HAS_DILL = False model, params = create_model_params(x, y) save_model(model, SAVE_MODEL) file_exists = wait_for_file(SAVE_MODEL, timeout=10) assert (file_exists) text = '' with open(SAVE_MODEL, 'r') as fh: text = fh.read() assert_between(len(text), 1000, 2500)
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)
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)
# <examples/doc_model_savemodel.py> import numpy as np from lmfit.model import Model, save_model def mysine(x, amp, freq, shift): return amp * np.sin(x * freq + shift) sinemodel = Model(mysine) pars = sinemodel.make_params(amp=1, freq=0.25, shift=0) save_model(sinemodel, 'sinemodel.sav') # <end examples/doc_model_savemodel.py>
#!/usr/bin/env python # <examples/doc_mode_savemodel.py> import numpy as np from lmfit.model import Model, save_model def mysine(x, amp, freq, shift): return amp * np.sin(x*freq + shift) sinemodel = Model(mysine) pars = sinemodel.make_params(amp=1, freq=0.25, shift=0) save_model(sinemodel, 'sinemodel.sav') # <end examples/doc_model_savemodel.py>