从HDF5的组中获取多个数据集 [英] Getting multiple datasets from group in HDF5
问题描述
我正在比较两个不同的hdf5文件,以确保它们匹配.我想在hdf5文件中创建一个包含该组中所有数据集的列表,以便可以循环遍历所有数据集,而不必手动输入它们.我似乎找不到办法做到这一点.目前,我正在使用以下代码获取数据集:
I am comparing two different hdf5 files to make sure that they match. I want to create a list with all of the datasets in the group in the hdf5 file so that I can have a loop run through all of the datasets, instead of entering them manually. I cant seem to find away to do this. Currently I am getting the data set by using this code:
tdata21 = ft['/PACKET_0/0xeda9_data_0004']
组的名称位于"PACKET_0"组中.排列完所有数据集后,我将在此循环中比较数据集中的数据:
The names of the sets are located in the "PACKET_0" group. Once I arrange all of the datasets, I compare the data in the datasets in this loop:
for i in range(len(data1)):
print "%d\t%g\t%g" % (i, data1[i],tdata1[i])
if(data1[i]!=tdata1[i]):
x="data file: data1 \nline:"+ str(i) + "\norgianl data:" + str(data1[i]) + "\nrecieved data:" + str(tdata1[i]) + "\n\n"
correct.append(x)
如果有一种比较聪明的方法来比较hdf5文件,我希望可以看到它,但是主要是我正在寻找一种方法来将组中所有数据集的名称放入列表中.谢谢
If there is an smartier way to compare hdf5 files I would like to see it as will, but mainly I am just looking for a way to get the names of all of the datasets in the group into a list. Thank you
推荐答案
要获取HDF5组或文件中存在的数据集或组,只需在该组或文件上调用list()
.以您的示例为例,
To get the datasets or groups that exist in an HDF5 group or file, just call list()
on that group or file. Using your example, you'd have
datasets = list(ft['/PACKET_0'])
您还可以通过以下操作直接遍历它们:
You can also just iterate over them directly, by doing:
for name, data in ft['/PACKET_0'].items():
# do stuff for each dataset
如果要比较两个数据集是否相等(即它们具有相同的数据),最简单的方法是:
If you want to compare two datasets for equality (i.e., they have the same data), the easiest way would be to do this:
(dataset1.value == dataset2.value).all()
这将从每个数据集中返回NumPy数组,逐个元素地比较这些数组,如果它们在任何地方都匹配则返回True
,否则返回False
.
This returns NumPy arrays from each dataset, compares those arrays element-by-element, and returns True
if they match everywhere and False
otherwise.
您可以结合使用这两个概念来比较两个不同文件中的每个数据集.
You can combine these two concepts to compare every dataset in two different files.
这篇关于从HDF5的组中获取多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!