其他:HDF5、山药、ASDF、泡菜 (astropy.io.misc
)¶
这个 astropy.io.misc
模块包含其他地方不适合的各种输入/输出例程,并且经常被其他人使用 astropy
子包。例如, astropy.io.misc.hdf5
包含读/写函数 Table
对象从/到HDF5文件,但用户不应直接导入这些对象。相反,用户可以通过 Table
类本身(请参见 统一文件读写接口 ). 中列出了用户直接使用的例程 astropy.io.misc
部分。
astropy.io.misc包裹¶
这个软件包包含了用于astropy数据输入/输出的各种实用程序函数。
功能¶
|
将对象Pickle到指定文件。 |
|
从指定的文件中取消pickle对象并返回内容。 |
astropy.io.misc.hdf5模块¶
此包包含用于读取和写入HDF5表的函数,这些函数不是要直接使用的,而是作为中的读/写器提供的 astropy.table
. 见 统一文件读写接口 了解更多详细信息。
功能¶
|
从HDF5文件读取表对象 |
|
将表对象写入HDF5文件 |
astropy.io.misc.yaml模块¶
这个模块包含通过YAML协议序列化核心astropy对象的函数。它提供功能 dump
, load
, and load_all
which call the corresponding functions in PyYaml 但是使用 AstropyDumper
和 AstropyLoader
类为以下astropy类定义自定义YAML标记:- astropy.units.Unit
- astropy.units.Quantity
- astropy.time.Time
- astropy.time.TimeDelta
- astropy.coordinates.SkyCoord
- astropy.coordinates.Angle
- astropy.coordinates.Latitude
- astropy.coordinates.Longitude
- astropy.coordinates.EarthLocation
- astropy.table.SerializedColumn
注解
此模块需要PyYaml 3.13或更高版本。
例子¶
- ::
>>> from astropy.io.misc import yaml >>> import astropy.units as u >>> from astropy.time import Time >>> from astropy.coordinates import EarthLocation >>> t = Time(2457389.0, format='mjd', ... location=EarthLocation(1000, 2000, 3000, unit=u.km)) >>> td = yaml.dump(t) >>> print(td) !astropy.time.Time format: mjd in_subfmt: '*' jd1: 4857390.0 jd2: -0.5 location: !astropy.coordinates.earth.EarthLocation ellipsoid: WGS84 x: !astropy.units.Quantity unit: &id001 !astropy.units.Unit {unit: km} value: 1000.0 y: !astropy.units.Quantity unit: *id001 value: 2000.0 z: !astropy.units.Quantity unit: *id001 value: 3000.0 out_subfmt: '*' precision: 3 scale: utc >>> ty = yaml.load(td) >>> ty <Time object: scale='utc' format='mjd' value=2457389.0> >>> ty.location <EarthLocation (1000., 2000., 3000.) km>
功能¶
|
使用AstropyLoader解析流中的第一个YAML文档并生成相应的Python对象。 |
|
使用AstropyLoader类解析流中的所有YAML文档并生成相应的Python对象。 |
|
使用AstropyDumper类将Python对象序列化为YAML流。 |
Classes¶
|
自定义SafeLoader,用于构造astropy核心对象以及Python元组和unicode对象。 |
|
自定义SafeDumper,表示astropy核心对象以及Python元组和unicode对象。 |
类继承图¶
astropy.io.misc.asdf包¶
这个 asdf 子包包含用于序列化的代码 astropy
类型,以便可以使用高级科学数据格式(ASDF)表示和存储它们。
如果两者 asdf 和 阿谀奉承 ,则无需进一步配置即可处理包含 阿谀奉承 类型。这个 asdf 包被设计为自动检测由定义的标记的存在 阿谀奉承 .
为了方便起见,用户可以编写 Table
对象到ASDF文件 统一文件读写接口 . 见 在表I/O中使用ASDF 下面。
有关ASDF标准的文件可以找到 here . 可以找到关于asdfpython模块的文档 here . 有关Astropy开发人员的更多详细信息,请参见 细节 .
在表I/O中使用ASDF¶
ASDF为 Table
使用 统一文件读写接口 . 这使得使用 Table
数据。
基本用法¶
给定一个表,可以将其写出ASDF文件:
from astropy.table import Table
# Create a simple table
t = Table(dtype=[('a', 'f4'), ('b', 'i4'), ('c', 'S2')])
# Write the table to an ASDF file
t.write('table.asdf')
I/O注册表根据 .asdf
输出文件的扩展名。
也可以使用 read
::
t2 = Table.read('table.asdf')
I/O注册表根据输入文件的扩展名自动选择适当的读取器函数。
如果文件扩展名不是 .asdf
可以显式指定要使用的读写器函数:
t3 = Table.read('table.zxcv', format='asdf')
高级用法¶
基本的ASDF数据结构是树,它是基本数据结构的嵌套组合(请参见 this 更详细的描述)。在顶层,树是一个 dict
.
结果就是 Table
对象(或任何对象)可以存储在ASDF树中的任意位置。上面描述的基本writer用例存储给定的 Table
在树的顶部使用默认键。基本读者案例假设 Table
存放在同一个地方。
但是,有时用户可以指定一个不同的顶级键来存储和检索 Table
来自ASDF文件。因此,ASDF I/O接口提供 data_key
作为读写时的可选关键字:
from astropy.table import Table
t = Table(dtype=[('a', 'f4'), ('b', 'i4'), ('c', 'S2')])
# Write the table to an ASDF file using a non-default key
t.write('foo.asdf', data_key='foo')
foo = Table.read('foo.asdf', data_key='foo')
这个 data_key
选项仅适用于 Table
存储在ASDF树顶部的对象。为了全面的通用性,用户可以在写入或读取ASDF文件时传递一个回调函数来精确地定义 Table
对象应该放在树中。写入案例的选项是 make_tree
. 函数回调应该只接受一个参数,即 Table
对象,并应返回 dict
表示要存储的树:
def make_custom_tree(table):
# Return a nested tree where the table is stored at the second level
return dict(foo=dict(bar=table))
t = Table(dtype=[('a', 'f4'), ('b', 'i4'), ('c', 'S2')])
# Write the table to an ASDF file using a non-default key
t.write('foobar.asdf', make_tree=make_custom_tree)
类似地,当读取ASDF文件时,用户可以传递一个自定义回调来定位ASDF树中的表。本例中的选项是 find_table
. 回调应该只接受一个参数,即 dict
表示ASDF树,它应该返回一个 Table
对象:
def find_table(tree):
# This returns the Table that was stored by the example above
return tree['foo']['bar']
foo = Table.read('foobar.asdf', find_table=find_table)
细节¶
这个 asdf 子包定义类,称为 tags ,实现序列化和反序列化的逻辑 astropy
类型。用户不应该直接引用标记实现。在处理ASDF文件时,它们的存在应该是完全透明的。
ASDF使用抽象数据类型定义 模式 . 这里提供的标记类是特定模式的特定实现。一些标签 astropy
(例如,与转换相关的模式)实现ASDF标准定义的模式。在其他情况下,标记和模式都在 astropy
(例如,与许多坐标系相关的)。由定义的单个模式的文档 astropy
可以在下面的 模式 部分。
不是全部 astropy
类型当前可由ASDF序列化。尝试将不支持的类型写入ASDF文件将导致 RepresenterError
. 为了支持新类型,必须创建新的标记和模式。看到了吗 Writing ASDF Extensions 有关其他详细信息,以及下面的示例。
示例:向Astropy ASDF扩展添加新对象¶
在本例中,我们将展示如何为新的 Model
对象,但基本原则适用于 astropy
物体。如前所述,将新对象添加到 astropy
ASDF扩展需要一个标记和一个模式。
转换的所有模式目前都在ASDF标准中定义。任何新的可序列化转换都必须具有相应的新架构。让我们考虑一个新的模型 MyModel
,一个新的模型 astropy.modeling.functional_models
有两个参数 amplitude
和 x_0
. 我们希望严格要求设置这两个参数。我们还希望指定这些参数可以是数值类型,或者 astropy.units.quantity
类型。描述此模型的模式如下所示:
%YAML 1.1
---
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
id: "http://stsci.edu/schemas/asdf/transform/mymodel-1.0.0"
tag: "tag:stsci.edu:asdf/transform/mymodel-1.0.0"
title: >
Example new model.
description: >
Example new model, which describes the distribution of ABC.
allOf:
- $ref: "transform-1.2.0"
- type: object
properties:
amplitude:
anyOf:
- $ref: "../unit/quantity-1.1.0"
- type: number
description: Amplitude of distribution.
x_0:
anyOf:
- $ref: "../unit/quantity-1.1.0"
- type: number
description: X center position.
required: ['amplitude', 'x_0]
...
所有新的转换模式都引用最新类型的基本转换模式。此模式描述所有或多个模型共有的其他模型属性,因此单个模式只处理特定于该模型的参数。另外,这个模式引用了 quantity
模式,以便模型可以保留有关单位和数量的信息。引用允许在新的自定义类型中使用以前定义的对象。
下一个组件是tag类。这个班必须有 to_tree
方法,在该方法中获取所需对象的属性,以及 from_tree
方法,根据写入ASDF文件的参数重建对象。 astropy
模型继承自 TransformType
基类标记,它处理属性(例如 name
, bounding_box
, n_inputs
)它们对所有或多个模型类通用,以限制单个标记中的冗余。每个单独的模型标记只需获取和设置特定于模型的参数:
from .basic import TransformType
from . import _parameter_to_value
class MyModelType(TransformType):
name = 'transform/mymodel'
version = '1.0.0'
types = ['astropy.modeling.functional_models.MyModel']
@classmethod
def from_tree_transform(cls, node, ctx):
return functional_models.MyModel(amplitude=node['amplitude'],
x_0=node['x_0'])
@classmethod
def to_tree_transform(cls, model, ctx):
node = {'amplitude': _parameter_to_value(amplitude),
'x_0': _parameter_to_value(x_0)}
return node
这个tag类包含将对象解构到ASDF文件并从中重建它们的所有机制。由对象名附加“Type”命名的标记类约定引用架构和版本,以及中的对象 astropy.modeling.functional_models
. 基本模型参数在 to_tree_transform
和 from_tree_transform
基地的 TransformType
类,而模型特定的参数在此处处理 MyModelType
. 由于此模型可以使用带输入参数的单位和数量,因此导入的“u参数”到“u值”可以灵活地处理基本数值和数量。