宇宙学I/O与转换#
介绍#
这个 astropy.cosmology.io
Package为读、写、转换提供了统一的接口 Cosmology
物体。此程序包的大部分功能可通过 Cosmology
类,它提供了用于读取、写入和转换的方法|Cosmology.Read|、|Cosmology.Write|、|Cosmology.To_Format|和|Cosmology.from_Format |Cosmology| 对象。
入门:阅读和写作#
这个 Cosmology
类包含两个方法:|Cosmology.Read|和|Cosmology.Write|,这两个方法使读取和写入文件成为可能。
已登记的 read
/ write
格式包括“ascii.ecsv”和“ascii.html”,如表。此外,自定义 read
/ write
格式可注册到Astropy Cosmology I/O框架中。有关内置格式的更多信息,请参见 内置宇宙学至格式/自格式 ,或 自定义宇宙学I/O格式 获取有关注册自定义格式的信息。
编写一个宇宙学实例只需要文件位置,或者,如果无法推断文件格式,则需要关键字参数“Format”。其他位置参数和关键字参数被传递给相关的编写器方法。
>>> from astropy.cosmology import Planck18
>>> Planck18.write("example_cosmology.ecsv", format="ascii.ecsv")
回读宇宙学是从 Cosmology
。
>>> from astropy.cosmology import Cosmology
>>> cosmo = Cosmology.read("example_cosmology.ecsv", format="ascii.ecsv")
>>> cosmo == Planck18
True
可以使用更具体的宇宙学类,为缺失信息提供缺省值(请参见 宇宙学I/O详细信息 )。然而,使用基类是最安全的,因为它不提供默认信息,因此需要文件具有描述宇宙学的所有必要信息。
要查看可用读/写文件格式列表,请执行以下操作:
>>> from astropy.cosmology import Cosmology
>>> Cosmology.read.list_formats()
Format Read Write Auto-identify
---------- ---- ----- -------------
ascii.ecsv Yes Yes Yes
ascii.html Yes Yes Yes
>>> Cosmology.write.list_formats()
Format Read Write Auto-identify
----------- ---- ----- -------------
ascii.ecsv Yes Yes Yes
ascii.html Yes Yes Yes
ascii.latex No Yes Yes
该列表将包括内置和注册的第三方格式。
入门:转换格式#
阅读和写作 Cosmology
对象通过中间表示法,通常是字典或 QTable
举个例子。这些中间表示可通过方法|Cosmology.to_Format|/|Cosmology.from_Format|访问。
|Cosmology.to_Format|/|Cosmology.from_Format|解析一个Cosmology与另一个Python对象之间的关系。例如,这对于迭代通过宇宙学参数的MCMC或将宇宙学模型打印成期刊格式,如LaTeX或HTML,可能是有用的。当第三方宇宙学包注册到Astropy的宇宙学I/O时, to/from_format
可用于在包之间转换宇宙学实例!
另一种预先注册的格式是“表”,用于将 Cosmology
往返于 QTable
。
>>> from astropy.cosmology import Planck18
>>> ct = Planck18.to_format("astropy.table")
>>> ct
<QTable length=1>
name H0 Om0 Tcmb0 Neff m_nu Ob0
km / (Mpc s) K eV
str8 float64 float64 float64 float64 float64[3] float64
-------- ------------ ------- ------- ------- ----------- -------
Planck18 67.66 0.30966 2.7255 3.046 0.0 .. 0.06 0.04897
现在是这样的 QTable
可用于加载一个新的宇宙实例,该实例与 普朗克2018 创造它的宇宙学。
>>> cosmo = Cosmology.from_format(ct, format="astropy.table")
>>> print(cosmo)
FlatLambdaCDM(name="Planck18", H0=67.66 km / (Mpc s), Om0=0.30966,
Tcmb0=2.7255 K, Neff=3.046, m_nu=[0. 0. 0.06] eV, Ob0=0.04897)
或许最有用的是, QTable
它本身已经 read/write
具有多种选项的方法,例如Fit,现在可与 Cosmology
。
要查看可用转换格式列表,请执行以下操作:
>>> from astropy.cosmology import Cosmology
>>> Cosmology.to_format.list_formats()
Format Read Write Auto-identify
----------------- ---- ----- -------------
astropy.cosmology Yes Yes Yes
astropy.model Yes Yes Yes
astropy.row Yes Yes Yes
astropy.table Yes Yes Yes
mapping Yes Yes Yes
yaml Yes Yes No
该列表将包括内置和注册的第三方格式。
使用宇宙学I/O#
以下页面提供了更多详细信息:
参考/API#
Asterpy.Cosmology.Connect模块#
Classes#
|
读取数据并将其分析到 |
|
以指定的格式写出此宇宙对象。 |
|
将对象转换为 |
|
把这个宇宙学转换成另一种形式。 |