内置宇宙学至格式/自格式#

要查看可用转换格式列表,请执行以下操作:

>>> 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

宇宙学#

Cosmology I/O,使用|Cosmology.To_Format|和|Cosmology.from_Format|。

此模块提供了将 Cosmology 对象往返于另一对象 Cosmology 对象。这些函数已注册到 convert_registry 格式名称为“AXTYPY.COSMOMology”。您可能不需要使用这些函数,因为它们主要用于完整性和测试。

>>> from astropy.cosmology import Cosmology, Planck18
>>> Planck18.to_format("astropy.cosmology") is Planck18
True
>>> Cosmology.from_format(Planck18) is Planck18
True

映射#

Cosmology <->映射I/O,使用|Cosmology.to_Format|和|Cosmology.from_Format|。

此模块提供了将 Cosmology 实例映射到 (dict -like)对象,反之亦然,从映射对象返回 Cosmology 举个例子。这些函数已注册到 convert_registry 在格式名称“MAPING”下。映射对象是一个 dict -类对象,将宇宙学的参数和元数据作为项。 dict 是Python中的基本数据结构,而 Cosmology 对于在多种序列化和存储格式之间进行转换,甚至将参数传递给函数都很有用。

我们从一个简单的例子开始,输出一个 Cosmology 作为一种映射。

>>> from astropy.cosmology import Cosmology, Planck18
>>> cm = Planck18.to_format('mapping')
>>> cm
{'cosmology': <class 'astropy.cosmology...FlatLambdaCDM'>,
 'name': 'Planck18', 'H0': <Quantity 67.66 km / (Mpc s)>, 'Om0': 0.30966,
 'Tcmb0': <Quantity 2.7255 K>, 'Neff': 3.046,
 'm_nu': <Quantity [0. , 0. , 0.06] eV>, 'Ob0': 0.04897,
 'meta': ...

cm 是一种 dict ,以宇宙学的参数和元数据为项。

我们该如何利用这一点 dict ?一种用途是拆开 dict 转换成一个函数:

>>> def function(H0, Tcmb0, **kwargs): ...
>>> function(**cm)

另一个用途是合并 dict 与另一个人 dict

>>> cm2 = {'H0': 70, 'Tcmb0': 2.7}
>>> cm | cm2
{'cosmology': <class 'astropy.cosmology...FlatLambdaCDM'>, ..., 'H0': 70, ...}

最引人注目的是, dict 也可以用来构造一个新的宇宙学实例 普朗克2018 它是从宇宙学中产生的。

>>> cosmo = Cosmology.from_format(cm, format="mapping")
>>> 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)

Cosmology.from_Format|如何知道返回 FlatLambdaCDM 班级?映射对象有一个字段 cosmology 它可以是宇宙学类的字符串名称(例如“FlatLambdaCDM”)或类本身。

在两种情况下可以省略此字段。

  1. 如果宇宙学课程作为 cosmology |Cosmology.from_Format|的关键字参数,

  2. 如果一个特定的宇宙学课程,例如 FlatLambdaCDM ,用于解析数据。

对于第一点,我们可以通过宇宙学课程作为 cosmology |Cosmology.from_Format|的关键字参数。

>>> del cm["cosmology"]  # remove cosmology class
>>> Cosmology.from_format(cm, cosmology="FlatLambdaCDM")
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)

对于第二点,我们可以使用特定的宇宙学类来解析数据。

>>> from astropy.cosmology import FlatLambdaCDM
>>> FlatLambdaCDM.from_format(cm)
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)

此外,类的默认参数值用于填充数据中缺少的任何信息。例如,如果 Tcmb0 缺失,则使用默认值0.0K。

>>> del cm["Tcmb0"]  # show FlatLambdaCDM provides default
>>> FlatLambdaCDM.from_format(cm)
FlatLambdaCDM(name="Planck18", H0=..., Tcmb0=0.0 K, ...)

如果不是 missing 信息,有 extra 信息,有几个选择。第一种方法是使用 move_to_meta 关键字参数将不在Cosmology构造函数中的字段移动到Cosmology的元数据。

>>> cm2 = cm | {"extra": 42, "cosmology": "FlatLambdaCDM"}
>>> cosmo = Cosmology.from_format(cm2, move_to_meta=True)
>>> cosmo.meta
OrderedDict([('extra', 42), ...])

或者,也可以使用 rename 关键字参数可用于重命名映射到 Cosmology 。当映射的键不是 Cosmology 构造函数。

>>> cm3 = dict(cm)  # copy
>>> cm3["cosmo_cls"] = "FlatLambdaCDM"
>>> cm3["cosmo_name"] = cm3.pop("name")
>>> rename = {'cosmo_cls': 'cosmology', 'cosmo_name': 'name'}
>>> Cosmology.from_format(cm3, rename=rename)
FlatLambdaCDM(name="Planck18", H0=67.66 km / (Mpc s), Om0=0.30966,
                Tcmb0=0.0 K, Neff=3.046, m_nu=None, Ob0=0.04897)

让我们仔细研究一下|Cosmology.to_Format|,因为有很多选项可以根据特定需求定制输出。

字典类型可能会随 cls 关键字参数:

>>> from collections import OrderedDict
>>> Planck18.to_format('mapping', cls=OrderedDict)
OrderedDict([('cosmology', <class 'astropy.cosmology...FlatLambdaCDM'>),
    ('name', 'Planck18'), ('H0', <Quantity 67.66 km / (Mpc s)>),
    ('Om0', 0.30966), ('Tcmb0', <Quantity 2.7255 K>), ('Neff', 3.046),
    ('m_nu', <Quantity [0.  , 0.  , 0.06] eV>), ('Ob0', 0.04897),
    ('meta', ...

有时,使用宇宙学类的名称而不是类型本身更有用。关键字参数 cosmology_as_str 可以使用:

>>> Planck18.to_format('mapping', cosmology_as_str=True)
{'cosmology': 'FlatLambdaCDM', ...

元数据通常作为嵌套映射包括在内。要将元数据移到主映射中,请使用关键字参数 move_from_meta 。这个Kwarg颠倒了 move_to_meta 在……里面 Cosmology.to_format("mapping", move_to_meta=...) 将额外的项移动到元数据中(如果宇宙学构造函数没有仅包含变量关键字的参数-- **kwargs )。

>>> from astropy.cosmology import Planck18
>>> Planck18.to_format('mapping', move_from_meta=True)
{'cosmology': <class 'astropy.cosmology...FlatLambdaCDM'>,
    'name': 'Planck18', 'Oc0': 0.2607, 'n': 0.9665, 'sigma8': 0.8102, ...

最后,映射中的键可以用 rename 关键字。

>>> rename = {'cosmology': 'cosmo_cls', 'name': 'cosmo_name'}
>>> Planck18.to_format('mapping', rename=rename)
{'cosmo_cls': <class 'astropy.cosmology...FlatLambdaCDM'>,
 'cosmo_name': 'Planck18', ...

表格#

Cosmology <-> Table I/O,使用|Cosmology.To_Format|和|Cosmology.from_Format|。

此模块提供了将 Cosmology 对象往返于 Table 对象。这些函数已注册到 convert_registry 在格式名为“Asterpy.table”的情况下。 Table 其本身具有丰富的I/O方法,这使得该转换对于进一步与其他格式的互操作性很有用。

宇宙学作为一种 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

宇宙学类和其他元数据,例如纸质参考文献,都在表的元数据中。

>>> ct.meta
OrderedDict([..., ('cosmology', 'FlatLambdaCDM')])

宇宙学支持类似星表的协议(请参见 桌状物体 )大意相同:

>>> from astropy.table import QTable
>>> QTable(Planck18)
<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

若要将Cosmology类从元数据移动到表行,请设置 cosmology_in_meta 参数为 False

>>> Planck18.to_format("astropy.table", cosmology_in_meta=False)
<QTable length=1>
    cosmology     name        H0        Om0    Tcmb0    Neff      m_nu      Ob0
                        km / (Mpc s)            K                 eV
    str13       str8     float64    float64 float64 float64  float64[3] float64
------------- -------- ------------ ------- ------- ------- ----------- -------
FlatLambdaCDM Planck18        67.66 0.30966  2.7255   3.046 0.0 .. 0.06 0.04897

Astropy推荐 QTable 对于包含以下各项的表 Quantity 柱子。但是,返回的类型可以使用 cls 论据:

>>> from astropy.table import Table
>>> Planck18.to_format("astropy.table", cls=Table)
<Table length=1>
...

宇宙学的领域可以使用 rename 争论。

>>> Planck18.to_format("astropy.table", rename={"H0": "Hubble"})
<QTable length=1>
    name      Hubble      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

可以将适当格式化的表格转换为 Cosmology 实例。自.以来 Table 可以容纳任意元数据,我们可以忠实地往返于 Cosmology 穿过 Table ,例如构造一个 Planck18 宇宙学与它产生的实例相同。

>>> ct = Planck18.to_format("astropy.table")
>>> cosmo = Cosmology.from_format(ct, format="astropy.table")
>>> 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)
>>> cosmo == Planck18
True

这个 cosmology 如果将宇宙学类(或其字符串名称)作为 cosmology |Cosmology.from_Format|的关键字参数。

>>> del ct.meta["cosmology"]  # remove cosmology from metadata
>>> Cosmology.from_format(ct, cosmology="FlatLambdaCDM")
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)

或者,可以使用特定的宇宙学类来解析数据。

>>> from astropy.cosmology import FlatLambdaCDM
>>> FlatLambdaCDM.from_format(ct)
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)

当使用特定的宇宙学类时,类的默认参数值被用来填充任何缺失的信息。

>>> del ct["Tcmb0"]  # show FlatLambdaCDM provides default
>>> FlatLambdaCDM.from_format(ct)
FlatLambdaCDM(name="Planck18", H0=67.66 km / (Mpc s), Om0=0.30966,
                Tcmb0=0.0 K, Neff=3.046, m_nu=None, Ob0=0.04897)

对于具有多行宇宙学参数的表, index 参数才能选择正确的行。索引可以是行号的整数,如果表按列编制索引,则可以是该列的值。如果表未编制索引,并且 index 是一个字符串,则将“name”列用作索引列。

下面是一个例子,其中 index 是必需的,并且可以是一个整数(用于行号)或一个宇宙的名称,例如‘Planck15’。

>>> from astropy.cosmology import Planck13, Planck15, Planck18
>>> from astropy.table import vstack
>>> cts = vstack([c.to_format("astropy.table")
...               for c in (Planck13, Planck15, Planck18)],
...              metadata_conflicts='silent')
>>> cts
<QTable length=3>
    name        H0        Om0    Tcmb0    Neff      m_nu      Ob0
            km / (Mpc s)            K                 eV
    str8     float64    float64 float64 float64  float64[3]  float64
-------- ------------ ------- ------- ------- ----------- --------
Planck13        67.77 0.30712  2.7255   3.046 0.0 .. 0.06 0.048252
Planck15        67.74  0.3075  2.7255   3.046 0.0 .. 0.06   0.0486
Planck18        67.66 0.30966  2.7255   3.046 0.0 .. 0.06  0.04897
>>> cosmo = Cosmology.from_format(cts, index=1, format="astropy.table")
>>> cosmo == Planck15
True

可以重命名表中的字段以匹配 Cosmology 类的签名使用 rename 争论。当表的列名与类的参数名不匹配时,这很有用。

>>> renamed_table = Planck18.to_format("astropy.table", rename={"H0": "Hubble"})
>>> renamed_table
<QTable length=1>
    name      Hubble      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
>>> cosmo = Cosmology.from_format(renamed_table, format="astropy.table",
...                               rename={"Hubble": "H0"})
>>> cosmo == Planck18
True

型号#

Cosmology <->模型I/O,使用|Cosmology.To_Format|和|Cosmology.from_Format|。

此模块提供了将 Cosmology 对象往返于 Model 。这些函数已注册到 convert_registry 在格式名为“Astally.Model”下。

vbl.使用 format="astropy.model" 宇宙学的任何红移(S)方法都可以转化为 astropy.modeling.Model 。每个 Cosmology Parameter 被转换为 astropy.modeling.Model Parameter 和模型的红移方法。 __call__ / evaluate 。现在你可以用数据来拟合宇宙学了!

>>> from astropy.cosmology import Cosmology, Planck18
>>> model = Planck18.to_format("astropy.model", method="lookback_time")
>>> model
<FlatLambdaCDMCosmologyLookbackTimeModel(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,
    name='Planck18')>

这个 普朗克2018 宇宙学可以通过|Cosmology.from_Format|恢复。

>>> print(Cosmology.from_format(model))
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)

YAML#

Cosmology <->YAML I/O,使用|Cosmology.To_Format|和|Cosmology.from_Format|。

此模块提供了将 Cosmology 对象往返于 yaml 代表权。这些函数已注册到 convert_registry 格式名称为“YAML”。此格式主要供其他I/O功能使用,例如 Table 的元数据序列化,这本身需要YAML序列化。

>>> from astropy.cosmology import Planck18
>>> yml = Planck18.to_format("yaml")
>>> yml  
"!astropy.cosmology...FlatLambdaCDM\nH0: !astropy.units.Quantity...
>>> print(Cosmology.from_format(yml, format="yaml"))
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)

#

Cosmology <-> Row I/O,使用|Cosmology.To_Format|和|Cosmology.from_Format|。

A Cosmology 作为一名 Row 将以宇宙学的名称和参数作为列。

>>> from astropy.cosmology import Planck18
>>> cr = Planck18.to_format("astropy.row")
>>> cr
<Row index=0>
    cosmology     name        H0        Om0    Tcmb0    Neff      m_nu      Ob0
                        km / (Mpc s)            K                 eV
    str13       str8     float64    float64 float64 float64  float64[3] float64
------------- -------- ------------ ------- ------- ------- ----------- -------
FlatLambdaCDM Planck18        67.66 0.30966  2.7255   3.046 0.0 .. 0.06 0.04897

宇宙学类和其他元数据,例如纸质参考文献,都在表的元数据中。

>>> cr.meta
OrderedDict([('Oc0', 0.2607), ('n', 0.9665), ...])

现在可以使用此行加载一个新的宇宙学实例,该实例与 Planck18 它是从宇宙学中产生的。

>>> cosmo = Cosmology.from_format(cr, format="astropy.row")
>>> 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)

有关参数选项的详细信息,请参见 表格

内置宇宙学读写格式#

要查看可用读/写文件格式列表,请执行以下操作:

>>> from astropy.cosmology import Cosmology
>>> Cosmology.write.list_formats()
   Format   Read Write Auto-identify
----------- ---- ----- -------------
 ascii.ecsv  Yes   Yes           Yes
 ascii.html  Yes   Yes           Yes
ascii.latex   No   Yes           Yes

ECSV#

Cosmology <->ECSV I/O,使用|Cosmology.Read|和|Cosmology.Write|。

此模块提供了写入/读取 Cosmology 对象到ECSV文件或从ECSV文件接收。这些函数已注册到 readwrite_registry 在格式名称“ascii.ecsv”下。

我们假定设置如下:

>>> from pathlib import Path
>>> from tempfile import TemporaryDirectory
>>> temp_dir = TemporaryDirectory()

要查看从ECSV文件读取宇宙学,我们首先将宇宙学写入ECSV文件:

>>> from astropy.cosmology import Cosmology, Planck18
>>> file = Path(temp_dir.name) / "file.ecsv"
>>> Planck18.write(file)
>>> with open(file) as f: print(f.read())
# %ECSV 1.0
# ---
# datatype:
# - {name: name, datatype: string}
...
# meta: !!omap
# - {Oc0: 0.2607}
...
# schema: astropy-2.0
name H0 Om0 Tcmb0 Neff m_nu Ob0
Planck18 67.66 0.30966 2.7255 3.046 [0.0,0.0,0.06] 0.04897

现在我们可以从ECSV文件中读取宇宙学,构建一个与 Planck18 它是从宇宙学中产生的。

>>> cosmo = Cosmology.read(file)
>>> 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)
>>> cosmo == Planck18
True

如果文件已存在,则尝试写入将引发错误,除非 overwrite=True

>>> Planck18.write(file, overwrite=True)

默认情况下,宇宙学类被写入表元数据。属性将其更改为表的列。 cosmology_in_meta 关键字参数。

>>> file = Path(temp_dir.name) / "file2.ecsv"
>>> Planck18.write(file, cosmology_in_meta=False)
>>> with open(file) as f: print(f.read())
# %ECSV 1.0
# ---
# datatype:
# - {name: cosmology, datatype: string}
# - {name: name, datatype: string}
...
# meta: !!omap
# - {Oc0: 0.2607}
...
# schema: astropy-2.0
cosmology name H0 Om0 Tcmb0 Neff m_nu Ob0
FlatLambdaCDM Planck18 67.66 0.30966 2.7255 3.046 [0.0,0.0,0.06] 0.04897

这个 cosmology 如果将宇宙学类(或其字符串名称)作为 cosmology |Cosmology.Read|的关键字参数。或者,可以使用特定的宇宙学类来解析数据。

>>> from astropy.cosmology import FlatLambdaCDM
>>> print(FlatLambdaCDM.read(file))
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)

当使用特定的宇宙学类时,类的默认参数值被用来填充任何缺失的信息。

对于包含多行宇宙学参数的文件, index 参数才能选择正确的行。索引可以是行号的整数,如果表按列编制索引,则可以是该列的值。如果表未编制索引,并且 index 是一个字符串,则将“name”列用作索引列。

下面是一个例子,其中 index 是必需的,并且可以是一个整数(用于行号)或一个宇宙的名称,例如‘Planck15’。

>>> from astropy.cosmology import Planck13, Planck15, Planck18
>>> from astropy.table import vstack
>>> cts = vstack([c.to_format("astropy.table")
...               for c in (Planck13, Planck15, Planck18)],
...              metadata_conflicts='silent')
>>> file = Path(temp_dir.name) / "file3.ecsv"
>>> cts.write(file)
>>> with open(file) as f: print(f.read())
# %ECSV 1.0
# ---
# datatype:
# - {name: name, datatype: string}
...
# meta: !!omap
# - {Oc0: 0.2607}
...
# schema: astropy-2.0
name H0 Om0 Tcmb0 Neff m_nu Ob0
Planck13 67.77 0.30712 2.7255 3.046 [0.0,0.0,0.06] 0.048252
Planck15 67.74 0.3075 2.7255 3.046 [0.0,0.0,0.06] 0.0486
Planck18 67.66 0.30966 2.7255 3.046 [0.0,0.0,0.06] 0.04897
>>> cosmo = Cosmology.read(file, index="Planck15", format="ascii.ecsv")
>>> cosmo == Planck15
True

可以重命名文件中表的字段以匹配 Cosmology 类的签名使用 rename 争论。当文件的列名与类的参数名不匹配时,这很有用。

>>> file = Path(temp_dir.name) / "file4.ecsv"
>>> Planck18.write(file, rename={"H0": "Hubble"})
>>> with open(file) as f: print(f.read())
 # %ECSV 1.0
# ---
# datatype:
# - {name: name, datatype: string}
...
# meta: !!omap
# - {Oc0: 0.2607}
...
# schema: astropy-2.0
name Hubble Om0 Tcmb0 Neff m_nu Ob0
...
>>> cosmo = Cosmology.read(file, rename={"Hubble": "H0"})
>>> cosmo == Planck18
True

默认情况下 Cosmology 实例是使用 QTable 作为中间表示形式(有关详细信息,请参阅|Cosmology.to_Format|,带有 format="astropy.table" )。这个 Table 类型可以使用 cls 关键字参数。

>>> from astropy.table import Table
>>> file = Path(temp_dir.name) / "file5.ecsv"
>>> Planck18.write(file, cls=Table)

对于大多数用例,默认情况下 clsQTable 由于ECSV格式与表类型无关,因此建议使用ECSV格式,并在很大程度上与其他表类型区分。例如,如果需要不同的ECSV模式,则可能需要使用不同的表类型。

将其他关键字参数传递给 QTable.readQTable.write

Latex#

Cosmology <->LaTeX I/O,使用|Cosmology.Read|和|Cosmology.Write|。

我们假定设置如下:

>>> from pathlib import Path
>>> from tempfile import TemporaryDirectory
>>> temp_dir = TemporaryDirectory()

将宇宙学写入LaTeX文件将生成一个以宇宙学的类型、名称和参数为列的表。

>>> from astropy.cosmology import Cosmology, Planck18
>>> file = Path(temp_dir.name) / "file.tex"
>>> Planck18.write(file, format="ascii.latex")
>>> with open(file) as f: print(f.read())
\begin{table}
\begin{tabular}{cccccccc}
cosmology & name & $H_0$ & $\Omega_{m,0}$ & $T_{0}$ & $N_{eff}$ & $m_{nu}$ & $\Omega_{b,0}$ \\
&  & $\mathrm{km\,Mpc^{-1}\,s^{-1}}$ &  & $\mathrm{K}$ &  & $\mathrm{eV}$ &  \\
FlatLambdaCDM & Planck18 & 67.66 & 0.30966 & 2.7255 & 3.046 & 0.0 .. 0.06 & 0.04897 \\
\end{tabular}
\end{table}

表中不包括宇宙学的元数据。

要将宇宙学保存在现有文件中,请使用 overwrite=True ;否则,将引发错误。

>>> Planck18.write(file, format="ascii.latex", overwrite=True)

若要使用不同的表类作为基础编写器,请使用 cls 科瓦格。有关可用表类的详细信息,请参阅Astropy表类和上的文档 Cosmology.to_format("astropy.table")

默认情况下,参数名称转换为LaTeX格式。要禁用此功能,请设置 latex_names=False

>>> file = Path(temp_dir.name) / "file2.tex"
>>> Planck18.write(file, format="ascii.latex", latex_names=False)
>>> with open(file) as f: print(f.read())
\begin{table}
\begin{tabular}{cccccccc}
cosmology & name & H0 & Om0 & Tcmb0 & Neff & m_nu & Ob0 \\
&  & $\mathrm{km\,Mpc^{-1}\,s^{-1}}$ &  & $\mathrm{K}$ &  & $\mathrm{eV}$ &  \\
FlatLambdaCDM & Planck18 & 67.66 & 0.30966 & 2.7255 & 3.046 & 0.0 .. 0.06 & 0.04897 \\
\end{tabular}
\end{table}

HTML#

Cosmology <->html I/O,使用|Cosmology.Read|和|Cosmology.Write|。

我们假定设置如下:

>>> from pathlib import Path
>>> from tempfile import TemporaryDirectory
>>> temp_dir = TemporaryDirectory()

将宇宙学写入html文件将生成一个以宇宙学的类型、名称和参数为列的表。

>>> from astropy.cosmology import Planck18
>>> file = Path(temp_dir.name) / "file.html"
>>> Planck18.write(file)
>>> with open(file) as f: print(f.read())
<html>
<head>
<meta charset="utf-8"/>
<meta content="text/html;charset=UTF-8" http-equiv="Content-type"/>
</head>
<body>
<table>
<thead>
<tr>
    <th>cosmology</th> <th>name</th> <th>H0</th> <th>Om0</th> <th>Tcmb0</th>
    <th>Neff</th> <th colspan="3">m_nu</th> <th>Ob0</th>
</tr>
</thead>
<tr>
    <td>FlatLambdaCDM</td> <td>Planck18</td> <td>67.66</td> <td>0.30966</td>
    <td>2.7255</td> <td>3.046</td> <td>0.0</td> <td>0.0</td> <td>0.06</td>
    <td>0.04897</td>
</tr>
</table>
</body>
</html>

文件中没有包含宇宙学的元数据。

要将宇宙学保存在现有文件中,请使用 overwrite=True ;否则,将引发错误。

>>> Planck18.write(file, overwrite=True)

若要使用不同的表类作为基础编写器,请使用 cls 科瓦格。有关可用表类的详细信息,请参阅Astropy表类和上的文档 Cosmology.to_format("astropy.table")

默认情况下,参数名称不会转换为LaTeX/MathJax格式。要启用此功能,请设置 latex_names=True

>>> file = Path(temp_dir.name) / "file2.html"
>>> Planck18.write(file, latex_names=True)
>>> with open(file) as f: print(f.read())
<html>
...
<thead>
    <tr>
    <th>cosmology</th>
    <th>name</th>
    <th>$$H_{0}$$</th>
    <th>$$\Omega_{m,0}$$</th>
    <th>$$T_{0}$$</th>
    <th>$$N_{eff}$$</th>
    <th colspan="3">$$m_{nu}$$</th>
    <th>$$\Omega_{b,0}$$</th>
    </tr>
...

备注

包含Cosmology HTML表的HTML文件应该包含启用MathJax的脚本。

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script type="text/javascript" id="MathJax-script" async
    src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
</script>