简体   繁体   English

使用 h5py 删除 hdf5 数据集

[英]Deleting hdf5 dataset using h5py

Is there any way to remove a dataset from an hdf5 file, preferably using h5py?有没有办法从 hdf5 文件中删除数据集,最好使用 h5py? Or alternatively, is it possible to overwrite a dataset while keeping the other datasets intact?或者,是否可以覆盖一个数据集同时保持其他数据集完好无损?

To my understanding, h5py can read/write hdf5 files in 5 modes据我了解,h5py 可以在 5 种模式下读/写 hdf5 文件

f = h5py.File("filename.hdf5",'mode')

where mode can be r for read, r+ for read-write, a for read-write but creates a new file if it doesn't exist, w for write/overwrite, and w- which is same as w but fails if file already exists.其中,模式可以是r读取, r+为读写, a用于读写而是创建一个新的文件,如果它不存在, w写/改写, w-这是一样的w但如果文件已经失败存在。 I have tried all but none seem to work.我已经尝试了所有但似乎没有工作。

Any suggestions are much appreciated.任何建议都非常感谢。

Yes, this can be done.是的,这是可以做到的。

with h5py.File(input,  "a") as f:
    del f[datasetname]

You will need to have the file open in a writeable mode, for example append (as above) or write.您需要以可写模式打开文件,例如追加(如上)或写入。

As noted by @seppo-enarvi in the comments the purpose of the previously recommended f.__delitem__(datasetname) function is to implement the del operator, so that one can delete a dataset using del f[datasetname]正如@seppo-enarvi 在评论中所指出的,之前推荐的f.__delitem__(datasetname)函数的目的是实现del运算符,以便可以使用del f[datasetname]删除数据集

I tried this out and the only way I could actually reduce the size of the file is by copying everything to a new file and just leaving out the dataset I was not interested in:我试过了,我实际上可以减小文件大小的唯一方法是将所有内容复制到一个新文件中,而只留下我不感兴趣的数据集:

fs = h5py.File('WFA.h5', 'r')
fd = h5py.File('WFA_red.h5', 'w')
for a in fs.attrs:
    fd.attrs[a] = fs.attrs[a]
for d in fs:
    if not 'SFS_TRANSITION' in d: fs.copy(d, fd)

I do not understand what has your question to do with the file open modes.我不明白您的问题与文件打开模式有什么关系。 For read/write r+ is the way to go.对于读/写 r+ 是要走的路。

To my knowledge, removing is not easy/possible, in particular no matter what you do the file size will not shrink.据我所知,删除并不容易/不可能,特别是无论你做什么,文件大小都不会缩小。

But overwriting content is no problem但是覆盖内容没问题

f['mydataset'][:] = 0

I wanted to make you aware of a development one of my colleagues made and put online in opensource.我想让您了解我的一位同事在开源中进行的一项开发工作。 It's called h5nav .它被称为h5nav You can download it with pip install ( https://pypi.org/project/h5nav/ ).您可以使用 pip install ( https://pypi.org/project/h5nav/ ) 下载它。

pip install h5nav

h5nav toto.h5
ls
rm the_group_you_want_to_delete
exit

Note that you'll still have to use h5repack to lower the size of your file.请注意,您仍然必须使用 h5repack 来减小文件的大小。

Best, Jérôme最好的,杰罗姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM