Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_save_load_modelresult(dill):
"""Save/load ModelResult with/without dill."""
if dill:
pytest.importorskip("dill")
else:
lmfit.jsonutils.HAS_DILL = False
# create model, perform fit, save ModelResult and perform some tests
model, params = create_model_params(x, y)
result = model.fit(y, params, x=x)
save_modelresult(result, SAVE_MODELRESULT)
file_exists = wait_for_file(SAVE_MODELRESULT, timeout=10)
assert file_exists
text = ''
with open(SAVE_MODELRESULT, 'r') as fh:
text = fh.read()
assert_between(len(text), 8000, 25000)
# load the saved ModelResult from file and compare results
result_saved = load_modelresult(SAVE_MODELRESULT)
check_fit_results(result_saved)
clear_savefile(SAVE_MODEL)
#
import numpy as np
from lmfit.model import save_modelresult
from lmfit.models import GaussianModel
data = np.loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
gmodel = GaussianModel()
result = gmodel.fit(y, x=x, amplitude=5, center=5, sigma=1)
save_modelresult(result, 'gauss_modelresult.sav')
print(result.fit_report())
#
#
import numpy as np
from lmfit.models import GaussianModel
from lmfit.model import save_modelresult
import matplotlib.pyplot as plt
data = np.loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
gmodel = GaussianModel()
result = gmodel.fit(y, x=x, amplitude=5, center=5, sigma=1)
save_modelresult(result, 'gauss_modelresult.sav')
print(result.fit_report())
plt.plot(x, y, 'bo')
plt.plot(x, result.init_fit, 'k--')
plt.plot(x, result.best_fit, 'r-')
plt.show()
#
def onSaveFitResult(self, event=None):
dgroup = self.get_datagroup()
deffile = dgroup.filename.replace('.', '_') + '.fitresult'
wcards = 'Fit Results(*.fitresult)|*.fitresult|All files (*.*)|*.*'
outfile = FileSave(self, 'Save Fit Result',
default_file=deffile,
wildcard=wcards)
if outfile is not None:
try:
save_modelresult(dgroup.fit_history[-1], outfile)
except IOError:
print('could not write %s' % outfile)
def autosave_modelresult(self, result, fname=None):
"""autosave model result to user larch folder"""
xyfitdir = os.path.join(site_config.usr_larchdir, 'xyfit')
if not os.path.exists(xyfitdir):
try:
os.makedirs(xyfitdir)
except OSError:
print("Warning: cannot create XYFit user folder")
return
if not HAS_MODELSAVE:
print("Warning: cannot save model results: upgrade lmfit")
return
if fname is None:
fname = 'autosave.fitresult'
fname = os.path.join(xyfitdir, fname)
save_modelresult(result, fname)
def autosave_modelresult(self, result, fname=None):
"""autosave model result to user larch folder"""
confdir = os.path.join(site_config.usr_larchdir, 'xas_viewer')
if not os.path.exists(confdir):
try:
os.makedirs(confdir)
except OSError:
print("Warning: cannot create XAS_Viewer user folder")
return
if not HAS_MODELSAVE:
print("Warning: cannot save model results: upgrade lmfit")
return
if fname is None:
fname = 'autosave.fitmodel'
save_modelresult(result, os.path.join(confdir, fname))
def onSaveFitResult(self, event=None):
deffile = self.datagroup.filename.replace('.', '_') + 'peak.modl'
sfile = FileSave(self, 'Save Fit Model', default_file=deffile,
wildcard=ModelWcards)
if sfile is not None:
result = self.get_fitresult()
save_modelresult(result, sfile)
def autosave_modelresult(self, result, fname=None):
"""autosave model result to user larch folder"""
xyfitdir = os.path.join(site_config.usr_larchdir, 'xyfit')
if not os.path.exists(xyfitdir):
try:
os.makedirs(xyfitdir)
except OSError:
print("Warning: cannot create XYFit user folder")
return
if not HAS_MODELSAVE:
print("Warning: cannot save model results: upgrade lmfit")
return
if fname is None:
fname = 'autosave.fitresult'
fname = os.path.join(xyfitdir, fname)
save_modelresult(result, fname)
'guess': guess,
'param_group': param_group,
'confidence_intervals': confidence_intervals,
'confidence_report': confidence_report,
'f_test': f_test, 'chi2_map': chi2_map,
'is_param': isParameter,
'isparam': isParameter,
'minimize': minimize,
'ufloat': ufloat,
'fit_report': fit_report,
'Parameters': _Parameters,
'Parameter': Parameter,
'lm_minimize': minimize,
'lm_save_model': save_model,
'lm_load_model': load_model,
'lm_save_modelresult': save_modelresult,
'lm_load_modelresult': load_modelresult,
}
for name in ('BreitWignerModel', 'ComplexConstantModel',
'ConstantModel', 'DampedHarmonicOscillatorModel',
'DampedOscillatorModel', 'DonaichModel',
'ExponentialGaussianModel', 'ExponentialModel',
'ExpressionModel', 'GaussianModel', 'Interpreter',
'LinearModel', 'LognormalModel', 'LorentzianModel',
'MoffatModel', 'ParabolicModel', 'Pearson7Model',
'PolynomialModel', 'PowerLawModel',
'PseudoVoigtModel', 'QuadraticModel',
'RectangleModel', 'SkewedGaussianModel',
'StepModel', 'StudentsTModel', 'VoigtModel'):
val = getattr(lmfit.models, name, None)
if val is not None: