如果您使用的是h5py2.9.0或更高版本,请参见答案。
从h5py 2.9.0版开始,现在可以直接通过主h5py.File
界面使用此行为。有迹象表明,控制“原始数据块缓存”三个参数- rdcc_nbytes
,rdcc_w0
和rdcc_nslots
-这是记录在这里。OP正在尝试调整rdcc_nbytes
设置,现在可以简单地将其设置为
import h5py
with h5py.File("test.h5", "w", rdcc_nbytes=5242880) as fid:
# Use fid for something here
唯一的区别是您必须知道实际需要多少空间,而不是像OP希望的那样乘以5。当前的默认值与找到的OP相同。当然,如果您真的想以编程方式执行此操作,则可以将其打开一次,获取缓存,将其关闭,然后使用所需的参数重新打开。
根据docs,get_access_plist()
返回文件访问属性列表的 。因此,修改副本不会影响原始副本也就不足为奇了。
看来高级界面没有提供更改缓存设置的方法。
这是使用低级界面的方法。
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
:
f = h5py.File(fid)
print(f.id.get_access_plist().get_cache())
# (0, 521, 5242880, 0.75)
因此,您仍然可以使用高级接口,但是要花些时间才能到达那里。另一方面,如果仅将其提炼成必需品,也许还不错:
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)