2013-02-01 26 views
4

我想增加我的HDF5文件的缓存大小,但它似乎并没有工作。 这是我有:如何在使用h5py高级界面时设置缓存设置?

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) 

任何想法,为什么阅读的作品,但设置不?
关闭并重新打开文件似乎也没有帮助。

回答

8

根据the docsget_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通过传递fidh5py.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) 
+0

因此,在该行之后: 'f = h5py.File(fid)'我可以使用高级API来创建数据集吗?: 'dset = f.create_dataset(“zipped_max”,(100,100) ,compression =“gzip”,compression_opts = 9)' – zd5151

+0

是的;这就是物体的美。一旦你有了这个对象,它就拥有了这个类的所有方法,不管对象是如何创建的。 – unutbu

+0

太棒了!我一直在使用高级API,但我意识到我需要退后一步,通过低级API增加缓存大小。谢谢您的回复。 – zd5151

1

h5py-cache项目可能会有所帮助,虽然我还没有使用它:

import h5py_cache 
with h5py_cache.File('test.h5', chunk_cache_mem_size=1024**3, 'a') as f: 
f.create_dataset(...)