我正在尝试增加我的 HDF5 文件的缓存大小,但它似乎不起作用。 这是我的:
import h5py
with h5py.File("test.h5", 'w') as fid:
# cache settings of file
cacheSettings = list(fid.id.get_access_plist().get_cache())
print cacheSettings
# increase cache
cacheSettings[2] = int(5 * cacheSettings[2])
print cacheSettings
# read cache settings from file
fid.id.get_access_plist().set_cache(*cacheSettings)
print fid.id.get_access_plist().get_cache()
这是输出:
[0, 521, 1048576, 0.75]
[0, 521, 5242880, 0.75]
(0, 521, 1048576, 0.75)
知道为什么读取有效,但设置无效吗?
关闭并重新打开文件似乎也无济于事。
最佳答案
如果您使用的是 h5py 2.9.0 或更高版本,请参阅 Mike's answer .
根据 the docs , get_access_plist()
返回文件访问属性列表的副本。因此,修改副本不会影响原件也就不足为奇了。
高级界面似乎没有提供更改缓存设置的方法。
这是使用低级接口(interface)的方法。
propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
print(settings)
# [0, 521, 1048576, 0.75]
settings[2] *= 5
propfaid.set_cache(*settings)
settings = propfaid.get_cache()
print(settings)
# (0, 521, 5242880, 0.75)
上面创建了一个 PropFAID .然后我们可以打开文件并获得 FileID这样:
import contextlib
with contextlib.closing(h5py.h5f.open(
filename, flags=h5py.h5f.ACC_RDWR, fapl=propfaid)) as fid:
# <h5py.h5f.FileID object at 0x9abc694>
settings = list(fid.get_access_plist().get_cache())
print(settings)
# [0, 521, 5242880, 0.75]
我们可以使用 fid
通过将 fid
传递给 h5py.File
来使用高级接口(interface)打开文件:
f = h5py.File(fid)
print(f.id.get_access_plist().get_cache())
# (0, 521, 5242880, 0.75)
因此,您仍然可以使用高级接口(interface),但需要一些时间 摆弄到那里。另一方面,如果你将它提炼成最基本的东西,也许它还不错:
import h5py
import contextlib
filename = '/tmp/foo.hdf5'
propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
settings[2] *= 5
propfaid.set_cache(*settings)
with contextlib.closing(h5py.h5f.open(filename, fapl=propfaid)) as fid:
f = h5py.File(fid)
关于python - 使用 h5py 高级接口(interface)时如何设置缓存设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14653259/