【问题标题】:How to resize an HDF5 array with `h5py`如何使用“h5py”调整 HDF5 数组的大小
【发布时间】:2014-05-24 18:17:53
【问题描述】:

如何使用h5py Python 库调整 HDF5 数组的大小?

我尝试使用.resize 方法并在chunks 设置为True 的数组上。唉,我还是错过了一些东西。

In [1]: import h5py

In [2]: f = h5py.File('foo.hdf5', 'w')

In [3]: d = f.create_dataset('data', (3, 3), dtype='i8', chunks=True)

In [4]: d.resize((6, 3))
/home/mrocklin/Software/anaconda/lib/python2.7/site-packages/h5py/_hl/dataset.pyc in resize(self, size, axis)
--> 277         self.id.set_extent(size)
ValueError: unable to set extend dataset (Dataset: Unable to initialize object)

In [11]: h5py.__version__ 
Out[11]: '2.2.1'

【问题讨论】:

  • 也许这与数组的数据类型有关...也许尝试更标准的数据类型,例如初始化数组的文档中显示的数据类型?
  • 刚试过没有指定dtype(我认为它默认为float)。同样的错误
  • 您在create_dataset 上缺少maxshape 吗?
  • @SlightlyCuban 解决了它。 maxshape 是否在磁盘上分配了那么多空间?为什么不将其设置为无限?
  • @MRocklin 您使用的是什么版本的 h5py?我刚刚使用 2.2.1 尝试过,没有问题。

标签: python hdf5 h5py


【解决方案1】:

正如 Oren 所说,如果您想稍后更改数组大小,则需要在创建 dataset 时使用 maxshape。将维度设置为 None 允许您稍后将该维度的大小调整为 2**64(h5 的限制):

In [1]: import h5py

In [2]: f = h5py.File('foo.hdf5', 'w')

In [3]: d = f.create_dataset('data', (3, 3), maxshape=(None, 3), dtype='i8', chunks=True)

In [4]: d.resize((6, 3))

In [5]: h5py.__version__
Out[5]: '2.2.1'

请参阅docs 了解更多信息。

【讨论】:

    【解决方案2】:

    你需要改变这一行:

    d = f.create_dataset('data', (3, 3), dtype='i8', chunks=True)
    

    d = f.create_dataset('data', (3, 3), maxshape=(?, ?), dtype='i8', chunks=True) 
    
    d.resize((?, ?))
    

    ? 更改为您想要的任何大小(您也可以将其设置为 None

    在这里阅读: http://docs.h5py.org/en/latest/high/dataset.html#resizable-datasets

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2022-11-14
      • 2016-03-23
      • 2016-04-04
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2016-07-16
      • 2015-09-01
      相关资源
      最近更新 更多