处理asdf文件的python工具

asdf的Python项目详细描述


概述

本节概述了用于创建 以及读取ASDF文件。

创建文件

我们将把几个 numpy 数组和其他数据存储到asdf文件中。我们 要做到这一点,可以创建一个"tree",它只是一个dict,我们将它作为 输入到 asdffile 的构造函数:

importasdfimportnumpyasnp# Create some datasequence=np.array([xforxinrange(100)])squares=np.array([x**2forxinrange(100)])random=np.random.random(100)# Store the data in an arbitrarily nested dictionarytree={'foo':42,'name':'Monty','sequence':sequence,'powers':{'squares':squares},'random':random}# Create the ASDF file object from our data treeaf=asdf.AsdfFile(tree)# Write the data to a new fileaf.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: Space Telescope Science Institute, homepage: 'http://github.com/spacetelescope/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'

读取文件

要读取现有的asdf文件,我们只需使用 ASDF软件包:

importasdfaf=asdf.open('example.asdf')

open 函数也用作上下文处理程序:

withasdf.open('example.asdf')asaf:...

要访问存储在文件中的数据,请使用顶级的asdffile.tree 属性:

>>>importasdf>>>af=asdf.open('example.asdf')>>>af.tree{'asdf_library':{'author':'Space Telescope Science Institute','homepage':'http://github.com/spacetelescope/asdf','name':'asdf','version':'1.3.1'},'foo':42,'name':'Monty','powers':{'squares':<array(unloaded)shape:[100]dtype:int64>},'random':<array(unloaded)shape:[100]dtype:float64>,'sequence':<array(unloaded)shape:[100]dtype:int64>}

树只是一个python dict ,节点和其他节点一样被访问 词典条目:

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

数组数据在被显式访问之前保持卸载状态:

>>>af.tree['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])>>>importnumpyasnp>>>expected=[x**2forxinrange(100)]>>>np.equal(af.tree['powers']['squares'],expected).all()True

默认情况下,未压缩的数据块被内存映射以提高效率 访问。使用 复制阵列可以禁用内存映射 阅读时打开选项:

af=asdf.open('example.asdf',copy_arrays=True)

有关更多信息和高级用法示例,请参见 文档

扩展ASDF

开箱即用, asdf 包自动序列化并 反序列化本机python类型。可以通过 实现与自定义用户类型相对应的自定义标记类型。更多 有关扩展asdf的信息,请参见 官方网站 文档

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java应用程序不是在Eclipse中运行,而是在命令行中运行   swing Java图形组件问题。似乎找不到错误   我需要键盘。close();让代码正常工作?   Springboot中的java HttpSession   抽象语法树我想添加一个语句。我试图解析它,java解析器异常被抛出。如何克服它?   java Hibernate:清理会话   具有不连续子集的java划分问题   java正则表达式查找最后一个冒号后的字符串   java从SpringShell执行OS命令   Java扫描器字符串输入   java字符串索引越界异常(charAt)   java执行器服务终止被卡住   Springockito没有继承java@ContextConfiguration   java如何为一个servlet映射多个url   java安卓获取命令的stderr   java生成类型。表:数据库中的大数字   安卓 Getter Setter返回NothingJava