处理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 包自动序列化并 反序列化本机python类型。可以通过 实现与自定义用户类型相对应的自定义标记类型。更多 有关扩展asdf的信息,请参见 官方网站 文档