astropy.modellingでModel.tied
(またはParameter.tied
)属性を使用しようとしていますが、動作が分からないようです。たとえば、2つのパラメータ、すなわちflux_0
とflux_1
を持つ複合モデルを作成したいとしましょう。しかし、私はflux_0
をフィッティングに使用したいだけです:flux_1
は常に1 - flux_0
の値を持ちます。 (。結局、私はflux_0 + flux_1 + ... + flux_n = 1
ように、この機能を拡張する必要があります)astropy.modelingのパラメータを束縛する
私はモデルクラスを定義し、そのようなtied
属性の「呼び出し可能」:その後、私はtied
属性を検査
>>> 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',)
。私の理解では、(脚注参照)、これは辞書でなければならないことであるが、そうではありません。
>>> 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
を更新しません:
>>> 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
属性を完全に誤解していますか?
(私は上記の例でAstropy 1.3.3でのPython 3.5.2を使用しています)
は脚注:
が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...>
⁝
良い質問 - 私はあなたの質問に答えることができるはずですので、このコードのほとんどを書いています。私はその日に外出しようとしていますが、私はできるだけ早く元気に戻そうとします。残念なことに、複合モデルの制約を扱うIIRCはややこしいことがあります。 – Iguananaut
私はあなたに今話すことの一つは、パラメータの値を手動で更新するときにモデルの制約が適用されるという誤解に陥っているように見えます。これはそうではありません。制約は、フィッティング時にモデルにフィットするときにのみ使用され、パラメータに制約を設定します。フィッティング制約の外は強制されません。私は前にこれを変えることを考えました、あなたが期待しているように動作するように。ある時点で問題が開いていましたが、今見つからないようです... – Iguananaut
私はhttps://github.com/astropy/astropy/issues/2265でこれを参照しています。 – Iguananaut