首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >astropy.modeling中的捆绑参数

astropy.modeling中的捆绑参数
EN

Stack Overflow用户
提问于 2017-08-04 19:07:35
回答 1查看 557关注 0票数 6

我试图在Model.tied (或Parameter.tied)属性中使用astropy.modelling,但似乎无法理解它是如何工作的。例如,假设我想创建一个包含两个参数的复合模型:flux_0flux_1。然而,我只希望flux_0用于拟合:flux_1应该始终带有1 - flux_0值。(最终,我需要扩展这个功能,以便flux_0 + flux_1 + ... + flux_n = 1。)

我为tied属性定义了一个模型类和一个“可调用”,如下所示:

代码语言:javascript
复制
>>> from astropy.modeling import Fittable1DModel, Parameter
>>>
>>> class MyModel(Fittable1DModel):
...     flux = Parameter()
...     @staticmethod
...     def evaluate(x, flux):
...         return flux
...
>>> def tie_fluxes(model):
...     flux_1 = 1 - model.flux_0
...     return flux_1
...
>>> TwoModel = MyModel + MyModel
>>>
>>> TwoModel
<class '__main__.CompoundModel0'>
Name: CompoundModel0
Inputs: ('x',)
Outputs: ('y',)
Fittable parameters: ('flux_0', 'flux_1')
Expression: [0] + [1]
Components:
    [0]: <class '__main__.MyModel'>
    Name: MyModel
    Inputs: ('x',)
    Outputs: ('y',)
    Fittable parameters: ('flux',)

    [1]: <class '__main__.MyModel'>
    Name: MyModel
    Inputs: ('x',)
    Outputs: ('y',)
    Fittable parameters: ('flux',)

然后检查tied属性。我的理解是,这应该是一本字典(见脚注),但事实并非如此:

代码语言:javascript
复制
>>> TwoModel.tied
<property object at 0x109523958>
>>>
>>> TwoModel.tied['flux_1'] = tie_fluxes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'property' object does not support item assignment

如果我尝试将它设置为字典,它将不会更新适当的Parameter

代码语言:javascript
复制
>>> TwoModel.tied = {'flux_1': tie_fluxes}
>>>
>>> TwoModel.flux_1.tied
False

但是,当我尝试立即创建一个对象而不是一个复合模型类(这不是我最终想要做的)时,对象的tied属性是一个字典。不幸的是,设置本词典仍然没有产生预期的效果:

代码语言:javascript
复制
>>> TwoSetModel = MyModel(0.2) + MyModel(0.3)
>>>
>>> TwoSetModel
<CompoundModel1(flux_0=0.2, flux_1=0.3)>
>>>
>>> TwoSetModel.tied
{'flux_1': False, 'flux_0': False}
>>>
>>> TwoSetModel.tied['flux_1'] = tie_fluxes
>>>
>>> TwoSetModel
<CompoundModel1(flux_0=0.2, flux_1=0.3)>
>>>
>>> TwoSetModel.flux_1.tied
<function tie_fluxes at 0x102987730>

因此,在本例中,tied属性确实包含正确的函数,但是参数的value没有相应地更新。

我在这里做错什么了?我是否完全误解了tied属性?

(在上述示例中,我使用的是Python3.5.2和Astropy 1.3.3 )

脚注:

运行help(TwoModel),我得到以下信息:

代码语言:javascript
复制
⁝
 |  tied : dict, optional
 |      Dictionary ``{parameter_name: callable}`` of parameters which are
 |      linked to some other parameter. The dictionary values are callables
 |      providing the linking relationship.
 |
 |      Alternatively the `~astropy.modeling.Parameter.tied` property of a
 |      parameter may be used to set the ``tied`` constraint on individual
 |      parameters.
⁝
 |  Examples
 |  --------
 |  >>> from astropy.modeling import models
 |  >>> def tie_center(model):
 |  ...         mean = 50 * model.stddev
 |  ...         return mean
 |  >>> tied_parameters = {'mean': tie_center}
 |
 |  Specify that ``'mean'`` is a tied parameter in one of two ways:
 |
 |  >>> g1 = models.Gaussian1D(amplitude=10, mean=5, stddev=.3,
 |  ...                        tied=tied_parameters)
 |
 |  or
 |
 |  >>> g1 = models.Gaussian1D(amplitude=10, mean=5, stddev=.3)
 |  >>> g1.mean.tied
 |  False
 |  >>> g1.mean.tied = tie_center
 |  >>> g1.mean.tied
 |  <function tie_center at 0x...>
⁝
EN

回答 1

Stack Overflow用户

发布于 2017-09-19 03:30:04

下面的示例类似于“无意义文档”中给出的示例。

两个一维高斯函数的复合Model=Sum

约束: mean_1=2*mean_

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from astropy.modeling import models, fitting


def tie_center(model):
    mean = 2* model.mean_0
    return mean

tied_parameters = {'mean_1': tie_center}
np.random.seed(42)
g1 = models.Gaussian1D(2, 0.4, 0.3)
g2 = models.Gaussian1D(2.5, 0.2, 0.2)
TwoGaussians = (models.Gaussian1D + 
models.Gaussian1D).rename('TwoGaussians')
x = np.linspace(-1, 1, 200)
y = g1(x) + g2(x) + np.random.normal(0., 0.2, x.shape)

gg_init = TwoGaussians(amplitude_0=1.4, mean_0=1.2, stddev_0=0.1,\
amplitude_1=1.0,stddev_1=0.2, tied=tied_parameters)
fitter = fitting.SLSQPLSQFitter()
gg_fit = fitter(gg_init, x, y)


plt.figure(figsize=(8,5))
plt.plot(x, y, 'ko')
plt.plot(x, gg_fit(x))
plt.xlabel('Position')
plt.ylabel('Flux')
plt.show()
print(gg_fit.mean_0,gg_fit.mean_1)

当三个一维高斯函数的Compund_model=和约束时,三种均值的和应总是等于1。

代码语言:javascript
复制
def tie_center(model):
    mean = 1-(model.mean_0+ model.mean_1)
    return mean
tied_parameters = {'mean_2': tie_center}

np.random.seed(42)
g1 = models.Gaussian1D(2, 0.4, 0.3)
g2 = models.Gaussian1D(2.5, 0.2, 0.2)
g3 = models.Gaussian1D(1.5, 0.4, 0.1)
ThreeGaussians = (models.Gaussian1D + models.Gaussian1D + 
models.Gaussian1D).rename('ThreeGaussians')
x = np.linspace(-1, 1, 200)
y = g1(x) + g2(x) + g3(x) + np.random.normal(0., 0.2, x.shape)

gg_init = ThreeGaussians(amplitude_0=1.4, mean_0=0.3, stddev_0=0.1, 
amplitude_1=1.0, mean_1=0.3,stddev_1=0.2, \
amplitude_2=1.5,stddev_2=0.1,tied=tied_parameters)
fitter = fitting.SLSQPLSQFitter()
gg_fit = fitter(gg_init, x, y)
plt.figure(figsize=(8,5))
plt.plot(x, y, 'ko')
plt.plot(x, gg_fit(x))
plt.xlabel('Position')
plt.ylabel('Flux')
plt.show()
print(gg_fit.mean_0,gg_fit.mean_1, gg_fit.mean_2)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45513897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档