当前位置: 首页>>代码示例>>Python>>正文


Python Model.params方法代码示例

本文整理汇总了Python中lmfit.Model.params方法的典型用法代码示例。如果您正苦于以下问题:Python Model.params方法的具体用法?Python Model.params怎么用?Python Model.params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lmfit.Model的用法示例。


在下文中一共展示了Model.params方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestUserDefiniedModel

# 需要导入模块: from lmfit import Model [as 别名]
# 或者: from lmfit.Model import params [as 别名]
class TestUserDefiniedModel(unittest.TestCase):
    # mainly aimed at checking that the API does what it says it does
    # and raises the right exceptions or warnings when things are not right

    def setUp(self):
        self.x = np.linspace(-10, 10, num=1000)
        np.random.seed(1)
        self.noise = 0.01*np.random.randn(*self.x.shape)
        self.true_values = lambda: dict(amplitude=7.1, center=1.1, sigma=2.40)
        self.guess = lambda: dict(amplitude=5, center=2, sigma=4)
        # return a fresh copy
        self.model = Model(gaussian, ['x'])
        self.data = gaussian(x=self.x, **self.true_values()) + self.noise

    def test_fit_with_keyword_params(self):
        result = self.model.fit(self.data, x=self.x, **self.guess())
        assert_results_close(result.values, self.true_values())

    def test_fit_with_parameters_obj(self):
        params = self.model.params()
        for param_name, value in self.guess().items():
            params[param_name].value = value
        result = self.model.fit(self.data, params, x=self.x)
        assert_results_close(result.values, self.true_values())

    def test_missing_param_raises_error(self):

        # using keyword argument parameters
        guess_missing_sigma = self.guess()
        del guess_missing_sigma['sigma']
        f = lambda: self.model.fit(self.data, x=self.x, **guess_missing_sigma)
        self.assertRaises(ValueError, f)

        # using Parameters
        params = self.model.params()
        for param_name, value in guess_missing_sigma.items():
            params[param_name].value = value
        f = lambda: self.model.fit(self.data, params, x=self.x)

    def test_extra_param_issues_warning(self):
        # The function accepts extra params, Model will warn but not raise.
        guess = self.guess()
        guess['extra'] = 5

        def flexible_func(x, amplitude, center, sigma, **kwargs):
            return gaussian(x, amplitude, center, sigma)

        flexible_model = Model(flexible_func, ['x'])
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            flexible_model.fit(self.data, x=self.x, **guess)
        self.assertTrue(len(w) == 1)
        self.assertTrue(issubclass(w[-1].category, UserWarning))

    def test_missing_independent_variable_raises_error(self):
        f = lambda: self.model.fit(self.data, **self.guess())
        self.assertRaises(KeyError, f)

    def test_bounding(self):
        guess = self.guess()
        guess['center'] = Parameter(value=2, min=1.3)
        true_values = self.true_values()
        true_values['center'] = 1.3  # as close as it's allowed to get
        result = self.model.fit(self.data, x=self.x, **guess)
        assert_results_close(result.values, true_values, rtol=0.05)

    def test_vary_false(self):
        guess = self.guess()
        guess['center'] = Parameter(value=1.3, vary=False)
        true_values = self.true_values()
        true_values['center'] = 1.3
        result = self.model.fit(self.data, x=self.x, **guess)
        assert_results_close(result.values, true_values, rtol=0.05)

    def test_result_attributes(self):

        # result.init_values
        result = self.model.fit(self.data, x=self.x, **self.guess())
        assert_results_close(result.values, self.true_values())
        self.assertTrue(result.init_values == self.guess())

        # result.init_params
        params = self.model.params()
        for param_name, value in self.guess().items():
            params[param_name].value = value
        self.assertTrue(result.init_params == params)

        # result.best_fit
        assert_allclose(result.best_fit, self.data, atol=self.noise.max())

        # result.init_fit
        init_fit = self.model.func(x=self.x, **self.guess())
        assert_allclose(result.init_fit, init_fit)

        # result.model
        self.assertTrue(result.model is self.model)

    # testing model addition...

    def test_user_defined_gaussian_plus_constant(self):
#.........这里部分代码省略.........
开发者ID:charlesll,项目名称:lmfit-py,代码行数:103,代码来源:test_model.py


注:本文中的lmfit.Model.params方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。