首页 > 解决方案 > 访问包含异常数据类型的 HDF5 文件中的数据集

问题描述

我想使用 Pandas 之类的工具分析 .h5 文件中的一些数据集,但是我遇到的问题似乎是由于数据集中的某些数据类型不受我目前使用的工具支持的结果。我已经做了一些阅读,但我对工具和问题的理解不够好,无法有效地进行故障排除,并且正在寻求有关此事的指导。

我可以访问数据集,HDFView但是使用它将所有数据导出到文本文件,然后将这些文本文件读入其他内容并不理想,而且并不总是以我知道如何使用的格式打印出数据(即布尔值打印出来就像B@38b27cdc每个这样的表达式看起来都是唯一的)。我尝试了一些使用 python(h5py 和 PyTables)和 MATLAB 访问文件的尝试。下面给出了一些代码示例和输出。

使用 numpy 的 h5py 示例

f = h5py.File('filename.h5', 'r')
group = f["Run 1"]
dataset = group["datasetIAmInterestedIn"]

错误输出结束摘要。如果您想在此处查看完整输出,请告诉我。

TypeError: No NumPy equivalent for TypeBitfieldID exists

PyTable 示例

f = tab.File('filename.h5', 'r')
group = f.get_node("/Run 1")
group.datasetIAmInterestedIn

命令的输出

/Run 1/datasetIAmInterestedIn(UnImplemented(58023,)) ''
NOTE: The UnImplemented object represents a PyTables unimplemented dataset present in the 'filename.h5' HDF5 file.  If you want to see this kind of HDF5 dataset implemented in PyTables, please contact the developers.

(我已将其包括在内以防万一)

MATLAB

data = hdf5read("filename.h5","/Run 1/datasetIAmInterestedIn")

命令的输出

Error using hdf5readc
Call to HDF5 library failed (unsupportedDatatype): "Datatype of an attribute value is not supported. Please disable the
reading of attribute values by setting the 'ReadAttributes' argument
to false. Type HELP HDF5INFO for more information.".

Error in hdf5read (line 100)
[data, attributes] = hdf5readc(settings.filename, ...

我探索使用的尝试ReadAttributes没有产生任何有用的信息或结果。

标签: pythonmatlabhdf5h5pypytables

解决方案


如果您有复合数据类型,则需要先定义复合数据,然后才能读取它。例如(这里的代码在 MATLAB 中)你有一个复合数据类型,它存储带有字符串(不同大小)的日志条目,一个存储日期时间和整数 ID 的双精度浮点数:

char32_type = H5T.copy('H5T_FORTRAN_S1');
H5T.set_size(char32_type, 32);
char32_size = H5T.get_size(char32_type);

char64_type = H5T.copy('H5T_FORTRAN_S1');
H5T.set_size(char64_type, 64);
char64_size = H5T.get_size(char64_type);

int_type = H5T.copy('H5T_NATIVE_INT');
int_size = H5T.get_size(int_type);

dbl_type = H5T.copy('H5T_NATIVE_DOUBLE');
dbl_size = H5T.get_size(dbl_type);

sizes = [
    char32_size
    dbl_size
    char64_size
    int_size
    ];

offset = [ 0 ; cumsum(sizes) ]; % zero-based

log_type = H5T.create('H5T_COMPOUND', sum(sizes));
H5T.insert(log_type, 'user',        offset(1), char32_type);
H5T.insert(log_type, 'datetime',    offset(2), dbl_type);
H5T.insert(log_type, 'action',      offset(3), char64_type);
H5T.insert(log_type, 'id',          offset(4), int_type);

您可以使用此复合类型log_type来读取/写入数据。例如,读取/logHDF5 文件中的数据集,如下所示:

plist     = 'H5P_DEFAULT';
fid       = H5F.open(log_file ,'H5F_ACC_RDONLY', plist);
log_dset  = H5D.open(fid, '/log');
mem_space = 'H5S_ALL';
log = H5D.read(log_dset, log_type, mem_space, 'H5S_ALL', 'H5P_DEFAULT');


% Close the dataset and file once you're done
H5D.close(log_dset);
H5F.close(fid);

read ( log) 的输出将是一个结构。字符串将是不方便的列格式的 char 数组,因此需要转置。在这里,我将它们转换为元胞数组,但您可能需要 String 类或其他类。我将日期时间转换为字符串格式的日期数组。场地很好,id不需要修整。

log.user    = cellstr(log.user');
log.action  = cellstr(log.action');
log.datestr = cellstr(datestr(log.datetime));
% log.id is okay as is

如果您不知道复合类型的格式,那么您可能会遇到麻烦?


推荐阅读