使用h5py创建HDF5复合属性 [英] Creating HDF5 compound attributes using h5py

查看:270
本文介绍了使用h5py创建HDF5复合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用h5py创建一些包含具有复合数据类型属性的简单HDF5数据集.目标是具有两个整数的属性.这是我要创建的两个属性示例.

I'm trying to create some simple HDF5 datasets that contain attributes with a compound datatype using h5py. The goal is an attribute that has two integers. Here are two example of attributes I'd like to create.

我的尝试最终以两个值组成的数组,例如

My attempts end up with an array of two values such as

如何使用h5py对此进行编码并获得包含两个整数的单个值? 当前代码类似于

How can I code this using h5py and get a single value that contains two integers? Current code looks something like

dt_type = np.dtype({"names": ["val1"],"formats": [('<i4', 2)]})
# also tried   np.dtype({"names": ["val1", "val2"],"formats": [('<i4', 1), ('<i4', 1)]})
dataset.attrs.create('time.start', [('23', '3')], dtype=dt_type)

如何指定类型或创建的属性来获取第一个示例?

How can I specify the type or the attribute create to get the first example?

推荐答案

要使用dt_type创建数组,必须正确嵌套列表和元组:

To make an array with dt_type, you have to properly nest lists and tuples:

In [162]: arr = np.array([(['23','3'],)], dt_type)                                             
In [163]: arr                                                                                  
Out[163]: array([([23,  3],)], dtype=[('val1', '<i4', (2,))])

这是具有复合dtype的(1,)数组. dtype有1个字段,但该字段中有2个值.

This is (1,) array with a compound dtype. The dtype has 1 field, but 2 values within that field.

使用替代dtype:

In [165]: dt2 = np.dtype({"names": ["val1", "val2"],"formats": ['<i4', '<i4']})                
In [166]: arr2 = np.array([('23','3',)], dt2)                                                  
In [167]: arr2                                                                                 
Out[167]: array([(23, 3)], dtype=[('val1', '<i4'), ('val2', '<i4')])

或最简单的数组:

In [168]: arr3 = np.array([23,2])                                                              
In [169]: arr3                                                                                 
Out[169]: array([23,  2])

写入数据集:

In [170]: ds.attrs.create('arr', arr)                                                          
In [172]: ds.attrs.create('arr2', arr2)                                                        
In [173]: ds.attrs.create('arr3', arr3) 

检查提取:

In [175]: ds.attrs['arr']                                                                      
Out[175]: array([([23,  3],)], dtype=[('val1', '<i4', (2,))])
In [176]: ds.attrs['arr2']                                                                     
Out[176]: array([(23, 3)], dtype=[('val1', '<i4'), ('val2', '<i4')])
In [177]: ds.attrs['arr3']                                                                     
Out[177]: array([23,  2])

转储:

1203:~/mypy$ h5dump compound.h5
HDF5 "compound.h5" {
GROUP "/" {
   DATASET "test" {
      DATATYPE  H5T_STD_I64LE
      DATASPACE  SIMPLE { ( 10 ) / ( 10 ) }
      DATA {
      (0): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
      }
      ATTRIBUTE "arr" {
         DATATYPE  H5T_COMPOUND {
            H5T_ARRAY { [2] H5T_STD_I32LE } "val1";
         }
         DATASPACE  SIMPLE { ( 1 ) / ( 1 ) }
         DATA {
         (0): {
               [ 23, 3 ]
            }
         }
      }
      ATTRIBUTE "arr2" {
         DATATYPE  H5T_COMPOUND {
            H5T_STD_I32LE "val1";
            H5T_STD_I32LE "val2";
         }
         DATASPACE  SIMPLE { ( 1 ) / ( 1 ) }
         DATA {
         (0): {
               23,
               3
            }
         }
      }
      ATTRIBUTE "arr3" {
         DATATYPE  H5T_STD_I64LE
         DATASPACE  SIMPLE { ( 2 ) / ( 2 ) }
         DATA {
         (0): 23, 2
         }
      }
   }
}
}

这篇关于使用h5py创建HDF5复合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆