我试图在中使用^{flux_0
和flux_1
。但是,我只希望在拟合中使用flux_0
,即flux_1
应该始终携带值1 - flux_0
。(最后,我需要扩展这个功能,以便flux_0 + flux_1 + ... + flux_n = 1
。)
我为tied
属性定义了一个模型类和一个“callable”,如下所示:
>>> 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
属性。我的理解是这应该是一本字典(见脚注),但它不是:
如果我试图将其设置为字典,它不会更新相应的Parameter
:
>>> TwoModel.tied = {'flux_1': tie_fluxes}
>>>
>>> TwoModel.flux_1.tied
False
但是当我尝试立即创建一个对象而不是一个复合模型类(这不是我最后想要做的),对象的tied
属性是一个字典。不幸的是,设置此词典仍无法产生所需的效果:
>>> 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和astropy1.3.3)
脚注:
运行help(TwoModel)
,我得到以下信息:
⁝
| 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...>
⁝
下面的例子与astropy文档中给出的示例类似。在
复合模型=两个一维高斯函数之和。在
约束:平均值1=2*平均值0
当Compund_模型=三个一维高斯函数之和时,astropy中的捆绑参数 约束:所有三个平均值之和应始终等于一。在
^{pr2}$相关问题 更多 >
编程相关推荐