【问题标题】:Compound datatypes with h5py: datatype inside an attribute具有 h5py 的复合数据类型:属性内的数据类型
【发布时间】:2011-08-23 14:42:11
【问题描述】:

我将Silo 与 HDF5 一起使用,但在使用 h5py 访问某些元数据时遇到问题。它会吐出一些相当不寻常的 HDF5 结构,将DATATYPE 放入DATATYPE 中。这是h5dump的输出摘录:

DATATYPE "sigma_t" H5T_STD_I32LE;
   ATTRIBUTE "silo" {
      DATATYPE  H5T_COMPOUND {
         H5T_STRING {
            STRSIZE 5;
            STRPAD H5T_STR_NULLTERM;
            CSET H5T_CSET_ASCII;
            CTYPE H5T_C_S1;
         } "meshid";
         H5T_STRING {
            STRSIZE 15;
            STRPAD H5T_STR_NULLTERM;
            CSET H5T_CSET_ASCII;
            CTYPE H5T_C_S1;
         } "value0";
         H5T_STD_I32LE "ndims";
         H5T_STD_I32LE "nvals";
         H5T_STD_I32LE "nels";
         H5T_IEEE_F32LE "time";
         H5T_STD_I32LE "use_specmf";
         H5T_STD_I32LE "centering";
         H5T_ARRAY { [3] H5T_STD_I32LE } "dims";
         H5T_ARRAY { [3] H5T_STD_I32LE } "zones";
         H5T_ARRAY { [3] H5T_STD_I32LE } "min_index";
         H5T_ARRAY { [3] H5T_STD_I32LE } "max_index";
         H5T_ARRAY { [3] H5T_IEEE_F32LE } "align";
      }
      DATASPACE  SCALAR
      DATA {
      (0): {
            "mesh",
            "/.silo/#000004",
            2,
            1,
            100,
            0,
            -1000,
            111,
            [ 10, 10, 0 ],
            [ 9, 9, 0 ],
            [ 0, 0, 0 ],
            [ 9, 9, 0 ],
            [ 0.5, 0.5, 0 ]
         }
      }
   }
   ATTRIBUTE "silo_type" {
      DATATYPE  H5T_STD_I32LE
      DATASPACE  SCALAR
      DATA {
      (0): 501
      }
   }

基本上,f['sigma_t'].attrs['silo'] 返回一个tuple,其中包含所有格式正确的数据,但没有任何数据类型的关联标签。 (我需要知道名称meshidvalue0 等)有没有办法得到这个?我很茫然。

示例文件和脚本

HDF5 file包含“sigma_t”字段,实际数据存放在/.silo/#000004中。

脚本:

import h5py
f = h5py.File('xsn.silo', 'r')
print f['sigma_t'].attrs['silo']

结果:

('mesh', '/.silo/#000004', 2, 1, 100, 0.0, -1000, 111, array([10, 10,  0], dtype=int32), array([9, 9, 0], dtype=int32), array([0, 0, 0], dtype=int32), array([9, 9, 0], dtype=int32), array([ 0.5,  0.5,  0. ], dtype=float32))

我还想要的是这样的:

('meshid','value0','ndims', ..., 'align')

这可能吗?

【问题讨论】:

  • 有什么方法可以指向示例文件?
  • 快速查看,复合 dtype 似乎不能很好地转换为 h5py,并且标签确实丢失了。虽然保留了排序,但您绝对可以使用元组的索引定义一个字典并按名称访问它们。可能也值得研究 h5py.h5t,因为这是处理此类 dtypes 的模块。
  • @diliop 这是h5py中的一个bug,看我的回答和google group page的链接。

标签: python hdf5 h5py silo


【解决方案1】:

我通过the h5py Google groups page 得到了开发者的答复:这是一个错误,将在 h5py 1.4 中修复。

我最终做的是:

import h5py
f = h5py.File('xsn.silo', 'r')
group = f['sigma_t']
attr_id = h5py.h5a.open(group.id, 'silo')
data = dict(zip(attr_id.dtype.names, group.attrs['silo'],))

【讨论】:

    【解决方案2】:

    感谢您回答赛斯!你的回答帮助了我,但这可能会让它更容易一点

        #path of table that you want to look at              
        group = f[path]                    
             #checking attributes leads to FIELD_0_NAME or TITLE
             for attribute in group.attrs:
                #I only one the ones that end with name
                if attribute.endswith('NAME'):
                    #then I take the actual name (ex:TrialTime) instead of FIELD_0_NAME
                    print group.attrs[attribute]
    

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2013-04-08
      • 2023-03-11
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 2020-03-29
      相关资源
      最近更新 更多