【问题标题】:Read HDF5 based file as a numpy array in Python在 Python 中将基于 HDF5 的文件读取为 numpy 数组
【发布时间】:2015-05-01 04:38:12
【问题描述】:

如何将 .hws 文件加载为 numpy 数组?
根据http://kingler.net/2007/05/22/90 中的描述,它说它是基于 HDF5 的格式,所以我发现https://confluence.slac.stanford.edu/display/PSDM/How+to+access+HDF5+data+from+Python 可能有用。但是,按照页面中描述的说明进行操作:

hdf5_file_name = '/reg/d/psdm/XPP/xppcom10/hdf5/xppcom10-r0546.h5'
dataset_name   = '/Configure:0000/Run:0000/CalibCycle:0000/Camera::FrameV1/XppSb4Pim.1:Tm6740.1/image'
event_number   = 5
file    = h5py.File(hdf5_file_name, 'r')  
dataset = file[dataset_name]
arr1ev  = dataset[event_number]
file.close()

在我修复前三行后,第六行出现错误:

file_name = '~/Desktop/audioData_A.hws'
item = h5py.File(file_name, 'r')
print item.name 
ds = item['/']
print len(ds)
arr1ev = ds[1]

返回:

<HDF5 group "/" (1 members)>
1
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-59-b33014aeccc8> in <module>()
      4 ds = item['/']
      5 print len(ds)
----> 6 arr1ev = ds[1]

/usr/local/lib/python2.7/site-packages/h5py/_objects.so in     h5py._objects.with_phil.wrapper (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/_objects.c:2405)()

/usr/local/lib/python2.7/site-packages/h5py/_objects.so in   h5py._objects.with_phil.wrapper (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/_objects.c:2362)()

/usr/local/lib/python2.7/site-packages/h5py/_hl/group.pyc in __getitem__(self,  name)
    158                 raise ValueError("Invalid HDF5 object reference")
    159         else:
--> 160             oid = h5o.open(self.id, self._e(name), lapl=self._lapl)  
    161 
    162         otype = h5i.get_type(oid) 

/usr/local/lib/python2.7/site-packages/h5py/_hl/base.pyc in _e(self, name, lcpl)
    119         else:
    120             try:
--> 121                 name = name.encode('ascii')
    122                 coding = h5t.CSET_ASCII
    123             except UnicodeEncodeError:

AttributeError: 'int' object has no attribute 'encode'

问题是我不知道如何获取dataset_nameevent_number的信息。通过item.parent,在我的情况下是第二行,我猜对应的值应该是/1,这是行不通的。

如果您需要,请在链接中找到该文件: https://drive.google.com/file/d/0B1UyTlIs325wbWhwR3NTVmFpWTg/view?usp=sharing

【问题讨论】:

    标签: python arrays numpy hdf5


    【解决方案1】:

    我下载了您的文件并查看了它。阅读.hws 文件后,您会得到一个字典,其中只有一个键"wfm_group0"(您可以使用item.keys() 查看文件中的键)。该键的值再次类似于字典,键为 "axes""id""traces""vector"

    不知道你想从那里去哪里,但也许你可以玩弄这些信息,看看它会把你带到哪里。

    【讨论】:

    • 谢谢,通过递归检查key,终于找到了数据,并将其转换为numpy数组。它确实是一个“分层”存储。
    猜你喜欢
    • 2018-03-25
    • 2017-09-23
    • 2016-02-10
    • 1970-01-01
    • 2018-09-29
    • 2018-01-03
    • 2015-03-26
    • 2013-06-11
    • 2012-09-19
    相关资源
    最近更新 更多