- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Model.tied
(或 Parameter.tied
)属性在 astropy.modelling 中,但似乎无法弄清楚它是如何工作的。例如,假设我想创建一个具有两个参数的复合模型:flux_0
和 flux_1
.但是,我只想要 flux_0
用于装配:flux_1
应始终携带值 1 - flux_0
. (最终,我需要扩展此功能以便 flux_0 + flux_1 + ... + flux_n = 1
。)
我为 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',)
tied
属性。我的理解是这应该是一本字典(见脚注),但它不是:
>>> 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
属性?
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 文档中给出的示例。
复合模型=两个一维高斯函数之和。
约束:mean_1=2*mean_0
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)
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)
关于python - 在 astropy.modeling 中绑定(bind)参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45513897/
我尝试在 astropy 表的 numpy 数组中转换一个 astropy 表列表。在第一个实例中,我尝试了 np.asarray(list) 和 np.array(list) 但列表中的 astro
所以我正在尝试使用 Astropy 并且我确实下载了它。但是,我收到一个错误。我正在尝试使用 Python 2 而不是 Python 3。 代码只是: import astropy astropy.t
我正在为 Raspberry Pi 开发一个应用程序,以控制 AltAzimutal 支架上的业余望远镜。 方位轴与天顶不完全对齐。在这种情况下,可以指向 2 或 3 颗星并找到视望远镜和赤道坐标之间
我目前是天文学的初学者,作为项目的一部分,我必须导入一些拟合文件或分析。但是,当我运行代码来导入 astropy.io 时,出现权限错误。 Traceback (most recent call la
我正在 Astropy 中进行坐标转换。但这不能正常工作。情况如下,我有一个 J2000 中的恒星坐标,我想将其转换为实际的纪元(或其他特定日期)。我正在使用 Stellarium 提供的坐标验证代码
我有一个适合的事件数据文件,我需要通过添加一个新的数据列来修改其中一个表,该数据列由存储在同一个表的预先存在的列中的数据派生。我遇到的问题是关闭修改后的文件。这是代码: data = fits.ope
我正在尝试使用 astropy.modeling 包将高斯拟合到一组数据点,但我得到的只是一条平线。见下文: 这是我的代码: %pylab inline from astropy.modeling i
我在为 python 安装 astropy 时遇到问题。当我在终端中输入“pip install astropy”时,我收到以下消息: Collecting astropy Using cached
我正在尝试使用 Python 和 astropy.io 从 FITS 文件中的二进制表中提取数据。该表包含一个事件数组,其中包含超过 200 万个事件。我想要做的是将某些事件的 TIME 值存储在一个
我有一个辐射源的多波段目录(来自 SourceExtractor,如果你想知道的话),我已将其读入以下形式的天体表: Source # | FLUX_APER_BAND1 | FLUXERR_APER
我正在尝试在 Python 中创建一个 FITS 文件,但在将 header 和 PrimaryHDU 一起编译时我似乎遇到了问题。 我做了一个简单的例子,它将给出我得到的错误: import num
我有一个保存星星数据的 astropy.table.table 对象。每颗星一行,列中包含星名、最大星等数据。 我知道 astropy 表的内部表示是每一列的字典,行作为字典对象的切片即时返回。 我需
我有一个 for 循环,它生成几个变量,例如 a、b 和 c。我想在 for 循环结束后,垂直堆叠变量。这是我想做的。 (为了举例,假设变量只是数字(1,2,3))。 from astropy.tab
我最近将我的 python 代码升级到了 Python 3.4.2。我运行的是 Ubuntu 14.10。 我使用了 pip3 和 apt-get python3- 的组合来升级我所有常用的模块:nu
我在 equatorial coordinates 中有一组点我需要将它们投影到平面上,即:天顶角或 azimuthal projection . astropy 显然能够执行这种类型的投影,amon
考虑一个astropy Table() object . from astropy.io import ascii weather_data = """ day,precip,type ,1.
我有一个看起来像这样的 astropy 表: $ print(astro_table) id xcentroid ycentroid sharpness roun
在以前的 astropy 版本中,可以按照以下方式处理不确定性的传播: from astropy.nddata import NDData, StdDevUncertainty x = NDData(
我在这里看到了几个关于 PyEphem 的答案,以及它如何产生日落/日出时间,但是如果我能找到仅使用 Astropy 包的解决方案,这对我来说会更有用。目前,我发现最接近的是 astropy.coor
我有两个源,其赤道坐标 (ra, dec) 和 (ra_0, dec_0) 位于距离 r 和 r_0,我需要计算它们之间的 3D 距离。 据我所知,我使用的两种方法应该给出相同的结果,但实际上没有。
我是一名优秀的程序员,十分优秀!