forked from lmfit/lmfit-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…mfit#174 is solved
- Loading branch information
Showing
1 changed file
with
66 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,77 @@ | ||
""" | ||
Originally added for gh-#56 | ||
""" | ||
import numpy as np | ||
from lmfit import Parameters, minimize, report_fit | ||
from lmfit import Parameters, minimize, report_fit, Minimizer | ||
import unittest | ||
from numpy.testing import assert_, assert_equal | ||
|
||
def get_data(): | ||
x = np.arange(0, 1, 0.01) | ||
y1 = 1.5*np.exp(0.9*x) + np.random.normal(scale=0.001, size=len(x)) | ||
y2 = 2.0 + x + 1/2.*x**2 +1/3.*x**3 | ||
y2 = y2 + np.random.normal(scale=0.001, size=len(x)) | ||
return x, y1, y2 | ||
class test_copy_params(unittest.TestCase): | ||
def setUp(self): | ||
|
||
def residual(params, x, data): | ||
a = params['a'].value | ||
b = params['b'].value | ||
self.x = np.arange(0, 1, 0.01) | ||
self.y1 = 1.5 * np.exp(0.9 * self.x) + np.random.normal(scale=0.001, size=len(self.x)) | ||
self.y2 = 2.0 + self.x + 1/2. * self.x**2 +1/3. * self.x**3 | ||
self.y2 = self.y2 + np.random.normal(scale=0.001, size=len(self.x)) | ||
|
||
model = a*np.exp(b*x) | ||
return (data-model) | ||
def residual(self, params, x, data): | ||
a = params['a'].value | ||
b = params['b'].value | ||
|
||
def test_copy_params(): | ||
x, y1, y2 = get_data() | ||
model = a * np.exp(b * x) | ||
return (data - model) | ||
|
||
params = Parameters() | ||
params.add('a', value = 2.0) | ||
params.add('b', value = 2.0) | ||
def params_values(self, params): | ||
return params.valuesdict().values() | ||
|
||
# fit to first data set | ||
out1 = minimize(residual, params, args=(x, y1)) | ||
def test_copy_params(self): | ||
#checking output for gh-#56 | ||
#1. the output.params is a different instance to the supplied params instance | ||
#2. the supplied params instance changes its values after the fit. | ||
|
||
# fit to second data set | ||
out2 = minimize(residual, params, args=(x, y2)) | ||
params = Parameters() | ||
params.add('a', value = 2.0) | ||
params.add('b', value = 2.0) | ||
|
||
adiff = out1.params['a'].value - out2.params['a'].value | ||
bdiff = out1.params['b'].value - out2.params['b'].value | ||
# fit to first data set | ||
out1 = minimize(self.residual, params, args=(self.x, self.y1)) | ||
|
||
assert(abs(adiff) > 1.e-2) | ||
assert(abs(bdiff) > 1.e-2) | ||
assert_equal(self.params_values(params), self.params_values(out1.params)) | ||
|
||
# fit to second data set | ||
out2 = minimize(self.residual, params, args=(self.x, self.y2)) | ||
|
||
assert_equal(self.params_values(params), | ||
self.params_values(out2.params)) | ||
|
||
adiff = out1.params['a'].value - out2.params['a'].value | ||
bdiff = out1.params['b'].value - out2.params['b'].value | ||
|
||
assert_(not params is out1) | ||
assert_(not params is out2) | ||
assert_(not out1 is out2) | ||
|
||
assert(abs(adiff) > 1.e-2) | ||
assert(abs(bdiff) > 1.e-2) | ||
|
||
def test_copy_params_minimizer(self): | ||
# check that the params instance supplied to construct the Minimizer | ||
# object is present as Minimizer.params for the lifetime of the object | ||
|
||
params = Parameters() | ||
params.add('a', value = 2.0) | ||
params.add('b', value = 2.0) | ||
|
||
fitter = Minimizer(self.residual, params, fcn_args=(self.x, self.y1)) | ||
|
||
#checks that fitter.params is the same instance as params | ||
assert_equal(self.params_values(params), | ||
self.params_values(fitter.params)) | ||
assert_(params is fitter.params) | ||
|
||
fitter.minimize() | ||
|
||
#checks that fitter.params is the same instance as params, after a fit. | ||
assert_equal(self.params_values(params), | ||
self.params_values(fitter.params)) | ||
assert_(params is fitter.params) |