跳转到主要内容

ASDF标准的Python实现

项目描述

CI Status Downstream CI Status https://readthedocs.org/projects/asdf/badge/?version=latest https://codecov.io/gh/asdf-format/asdf/branch/main/graphs/badge.svg https://zenodo.org/badge/18112754.svg https://img.shields.io/pypi/l/asdf.svg pre-commit https://img.shields.io/badge/code%20style-black-000000.svg

高级科学数据格式(ASDF)是一种用于科学数据交换的下一代格式。此包包含ASDF标准的Python实现。有关ASDF标准的更多信息,请参阅此处

ASDF格式具有以下特性

  • 使用YAML(YAML)实现的分层、可读的元数据格式

  • 数值数组以二进制数据块的形式存储,可进行内存映射。数据块可以选择压缩。

  • 可以使用模式(使用JSON Schema实现)自动验证数据结构

  • 原生Python数据类型(数值类型、字符串、字典、列表)会自动序列化

  • ASDF可以扩展以序列化自定义数据类型

ASDF正在github上积极开发[GitHub链接]。有关贡献的更多信息,请参阅下文。

概述

本节概述了ASDF包的基本用法,用于创建和读取ASDF文件。

创建文件

我们将存储几个numpy数组和其他数据到ASDF文件中。我们通过创建一个“树”,它只是一个dict,并将其作为输入提供给AsdfFile构造函数来实现这一点。

import asdf
import numpy as np

# Create some data
sequence = np.arange(100)
squares = sequence**2
random = np.random.random(100)

# Store the data in an arbitrarily nested dictionary
tree = {
    "foo": 42,
    "name": "Monty",
    "sequence": sequence,
    "powers": {"squares": squares},
    "random": random,
}

# Create the ASDF file object from our data tree
af = asdf.AsdfFile(tree)

# Write the data to a new file
af.write_to("example.asdf")

如果打开新创建文件的数据部分,我们可以看到ASDF的一些关键特性

#ASDF 1.0.0
#ASDF_STANDARD 1.2.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.1.0
asdf_library: !core/software-1.0.0 {author: The ASDF Developers, homepage: 'http://github.com/asdf-format/asdf',
  name: asdf, version: 2.0.0}
history:
  extensions:
  - !core/extension_metadata-1.0.0
    extension_class: asdf.extension.BuiltinExtension
    software: {name: asdf, version: 2.0.0}
foo: 42
name: Monty
powers:
  squares: !core/ndarray-1.0.0
    source: 1
    datatype: int64
    byteorder: little
    shape: [100]
random: !core/ndarray-1.0.0
  source: 2
  datatype: float64
  byteorder: little
  shape: [100]
sequence: !core/ndarray-1.0.0
  source: 0
  datatype: int64
  byteorder: little
  shape: [100]
...

文件中的元数据与存储的树的结构相匹配。它是分层的并且可读的。请注意,已向用户未明确提供的树中添加了元数据。注意,数值数组数据本身并不存储在元数据树中。相反,它存储在元数据部分下面的二进制数据块中(未在上图中显示)。

在写入文件时,可以压缩数组数据

af.write_to("compressed.asdf", all_array_compression="zlib")

内置的压缩算法有 'zlib' 和 'bzp2'。当安装了lz4包时,'lz4'算法变得可用。其他压缩算法可能通过扩展获得。

读取文件

要读取现有的ASDF文件,我们只需使用asdf包的顶级open函数。

import asdf

af = asdf.open("example.asdf")

open函数也作为上下文管理器

with asdf.open("example.asdf") as af:
    ...

要快速了解文件中存储的数据,请使用顶级AsdfFile.info()方法。

>>> import asdf
>>> af = asdf.open("example.asdf")
>>> af.info()
root (AsdfObject)
├─asdf_library (Software)
│ ├─author (str): The ASDF Developers
│ ├─homepage (str): http://github.com/asdf-format/asdf
│ ├─name (str): asdf
│ └─version (str): 2.8.0
├─history (dict)
│ └─extensions (list)
│   └─[0] (ExtensionMetadata)
│     ├─extension_class (str): asdf.extension.BuiltinExtension
│     └─software (Software)
│       ├─name (str): asdf
│       └─version (str): 2.8.0
├─foo (int): 42
├─name (str): Monty
├─powers (dict)
│ └─squares (NDArrayType): shape=(100,), dtype=int64
├─random (NDArrayType): shape=(100,), dtype=float64
└─sequence (NDArrayType): shape=(100,), dtype=int64

AsdfFile的行为就像一个Python dict,节点像任何其他字典条目一样访问。

>>> af["name"]
'Monty'
>>> af["powers"]
{'squares': <array (unloaded) shape: [100] dtype: int64>}

数组数据在明确访问之前保持未加载状态

>>> af["powers"]["squares"]
array([   0,    1,    4,    9,   16,   25,   36,   49,   64,   81,  100,
        121,  144,  169,  196,  225,  256,  289,  324,  361,  400,  441,
        484,  529,  576,  625,  676,  729,  784,  841,  900,  961, 1024,
       1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849,
       1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916,
       3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225,
       4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776,
       5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569,
       7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604,
       9801])

>>> import numpy as np
>>> expected = [x**2 for x in range(100)]
>>> np.equal(af["powers"]["squares"], expected).all()
True

默认情况下,未压缩的数据块会进行内存映射以提高访问效率。可以通过在读取时使用open的memmap选项来禁用内存映射。

af = asdf.open("example.asdf", memmap=False)

有关更多信息以及高级用法示例,请参阅文档

扩展ASDF

默认情况下,asdf包会自动序列化和反序列化原生Python类型。通过实现与自定义用户类型对应的自定义标记,可以扩展asdf。有关扩展ASDF的更多信息,请参阅官方文档

安装

ASDF Python软件包的稳定版本已在PyPi注册。点击此处。可以使用

pip

安装最新稳定版本。

$ pip install asdf

ASDF的最新开发版本可在github的main分支找到。要克隆项目

$ git clone https://github.com/asdf-format/asdf

要安装

$ cd asdf
$ pip install .

要在开发模式下安装

$ pip install -e .

测试

要从存储库的源检查中安装测试依赖项

$ pip install -e ".[tests]"

要从存储库的源检查中运行单元测试

$ pytest

也可以从已安装的软件包版本中运行测试套件。

$ pip install "asdf[tests]"
$ pytest --pyargs asdf

还可以使用tox运行测试。

$ pip install tox

列出所有可用的环境

$ tox -va

运行特定环境

$ tox -e <envname>

文档

有关此软件包的更详细文档,请点击此处

有关ASDF标准本身的更多信息,请点击此处

ASDF有两个邮件列表

许可证

ASDF采用BSD 3条款许可方式。有关任何包含的软件的许可信息,请参阅LICENSE.rst许可文件夹

贡献

我们欢迎对项目的反馈和贡献。代码、文档或一般反馈的贡献都受到欢迎。请遵循贡献指南提交问题或拉取请求。

我们致力于遵守行为准则,为所有用户提供一个友好的社区。

项目详情


发布历史 发布通知 | RSS源

下载文件

下载适用于您平台的文件。如果您不确定该选择哪个,请了解有关安装软件包的更多信息。

源分发

asdf-3.5.0.tar.gz (804.6 kB 查看哈希值)

上传时间

构建分发

asdf-3.5.0-py3-none-any.whl (967.6 kB 查看哈希值)

上传时间 Python 3

由以下支持