Python 优化HDF5数据集的读/写速度

Python 优化HDF5数据集的读/写速度,python,h5py,large-data,Python,H5py,Large Data,我目前正在做一个实验,在这个实验中,我从空间上扫描一个目标,并在每个离散的像素上抓取一个示波器轨迹。通常我的记录道长度是200Kpts。扫描完整个目标后,我将这些时域信号在空间上组合起来,并基本上播放一部扫描内容的电影。我的扫描区域大小为330x220像素,因此整个数据集都比我必须使用的计算机上的RAM大 首先,我只是将每个示波器记录道保存为一个numpy阵列,然后在扫描完成下采样/过滤等操作后,以一种不会出现内存问题的方式将电影拼接在一起。但是,我现在无法减少采样,因为会出现别名,因此需要访问

我目前正在做一个实验,在这个实验中,我从空间上扫描一个目标,并在每个离散的像素上抓取一个示波器轨迹。通常我的记录道长度是200Kpts。扫描完整个目标后,我将这些时域信号在空间上组合起来,并基本上播放一部扫描内容的电影。我的扫描区域大小为330x220像素,因此整个数据集都比我必须使用的计算机上的RAM大

首先,我只是将每个示波器记录道保存为一个numpy阵列,然后在扫描完成下采样/过滤等操作后,以一种不会出现内存问题的方式将电影拼接在一起。但是,我现在无法减少采样,因为会出现别名,因此需要访问原始数据

我已经开始考虑使用H5py在HDF5数据集中存储我的大型3d数据块。我的主要问题是块大小分配。我输入的数据与我想要读出的平面正交。据我所知,我写数据的主要选择是:

    #Fast write Slow read
    with h5py.File("test_h5py.hdf5","a") as f:
        dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (1,1,dataLen), dtype = 'f')
        for i in range(height):
            for j in range(width):
                dset[i,j,:] = np.random.random(200000)


有没有什么方法可以优化这两种情况,从而使它们都不能非常低效地运行

如果您想通过分块优化I/O性能,您应该阅读unidata的以下两篇文章:


如果您只想执行原始I/O性能,请考虑@ TITUSJAN建议

< P>。如果您想用组块优化I/O性能,您应该从UnIDATA中读取这两篇文章:


如果您只执行原始I/O性能,请考虑@ TITUSJAN建议

< P>您在代码中有一些性能缺陷。

  • 您正在行中使用某种奇特的索引(在读取/写入HDF5数据集时,不要更改数组DIM的数量)
  • 如果您没有读取或写入整个区块,请设置适当的区块缓存大小。

  • 减少对HDF5-Api的读或写调用量

  • 选择适当的区块大小(区块只能完全读/写,因此如果您只需要区块的一部分,其余部分应留在缓存中)
  • 以下示例使用HDF5-API的缓存。要设置正确的缓存大小,我将使用h5py_缓存。

    如果您自己进行缓存,则可以进一步提高性能

    写作

    # minimal chache size for reasonable performance would be 20*20*dataLen*4= 320 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=500*1024**2) as f:
        dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (20,20,20), dtype = 'f')
        for i in range(height):
            for j in range(width):
                # avoid fancy slicing
                dset[i:i+1,j:j+1,:] = expand_dims(expand_dims(np.random.random(200000),axis=0),axis=0)
    
    # minimal chache size for reasonable performance would be height*width*500*4= 145 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=200*1024**2) as f:
         dset=f["uncompchunk"]
         for i in xrange(0,dataLen):
             Image=np.squeeze(dset[:,:,i:i+1])
    
    阅读

    # minimal chache size for reasonable performance would be 20*20*dataLen*4= 320 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=500*1024**2) as f:
        dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (20,20,20), dtype = 'f')
        for i in range(height):
            for j in range(width):
                # avoid fancy slicing
                dset[i:i+1,j:j+1,:] = expand_dims(expand_dims(np.random.random(200000),axis=0),axis=0)
    
    # minimal chache size for reasonable performance would be height*width*500*4= 145 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=200*1024**2) as f:
         dset=f["uncompchunk"]
         for i in xrange(0,dataLen):
             Image=np.squeeze(dset[:,:,i:i+1])
    

    您的代码中存在一些性能缺陷

  • 您正在行中使用某种奇特的索引(在读取/写入HDF5数据集时,不要更改数组DIM的数量)
  • 如果您没有读取或写入整个区块,请设置适当的区块缓存大小。

  • 减少对HDF5-Api的读或写调用量

  • 选择适当的区块大小(区块只能完全读/写,因此如果您只需要区块的一部分,其余部分应留在缓存中)
  • 以下示例使用HDF5-API的缓存。要设置正确的缓存大小,我将使用h5py_缓存。

    如果您自己进行缓存,则可以进一步提高性能

    写作

    # minimal chache size for reasonable performance would be 20*20*dataLen*4= 320 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=500*1024**2) as f:
        dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (20,20,20), dtype = 'f')
        for i in range(height):
            for j in range(width):
                # avoid fancy slicing
                dset[i:i+1,j:j+1,:] = expand_dims(expand_dims(np.random.random(200000),axis=0),axis=0)
    
    # minimal chache size for reasonable performance would be height*width*500*4= 145 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=200*1024**2) as f:
         dset=f["uncompchunk"]
         for i in xrange(0,dataLen):
             Image=np.squeeze(dset[:,:,i:i+1])
    
    阅读

    # minimal chache size for reasonable performance would be 20*20*dataLen*4= 320 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=500*1024**2) as f:
        dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (20,20,20), dtype = 'f')
        for i in range(height):
            for j in range(width):
                # avoid fancy slicing
                dset[i:i+1,j:j+1,:] = expand_dims(expand_dims(np.random.random(200000),axis=0),axis=0)
    
    # minimal chache size for reasonable performance would be height*width*500*4= 145 MB, lets take a bit more
    with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=200*1024**2) as f:
         dset=f["uncompchunk"]
         for i in xrange(0,dataLen):
             Image=np.squeeze(dset[:,:,i:i+1])
    

    如果您事先知道数据集的大小,并且不想使用压缩,那么您可以使用连续存储(即无块)。您不能尝试吗?如果您事先知道数据集的大小,并且不想使用压缩,那么您可以使用连续存储(即无块)。您不能尝试吗?