【问题标题】:Extending array in HDF5 using PyTables使用 PyTables 在 HDF5 中扩展数组
【发布时间】:2017-07-05 20:42:28
【问题描述】:

我通常使用h5py 在 Python 中完成 HDF5 的工作,如果我想创建一个以后想要扩展的数据集,或者,我会这样做:

f = h5py.File('foo.h5', 'w')
d = f.create_dataset('whatever', (5, 5), maxshape=(None, 5), dtype='i8', chunks=True)
...
d.resize((23, 5))
...

maxshape(None, ...) 将第一个维度设置为“无穷大”,因此它是可扩展的。

现在我有一个项目,我需要坚持使用 PyTables,并希望逐步构建大型数组。有没有办法在 PyTables 中扩展arrays

大致是这样的:

import tables as tb
import numpy as np

filename = "foo.h5"
h5file = tb.File(filename, "a")

gbar = h5file.create_group(h5file.root, "bar", "Pressure")
h5file.create_array(gbar, 'left', np.array((1, 2, 3, 4)), "...")

# now extend the shape of (4,) and append more arrays iteratively???

h5file.close()

【问题讨论】:

    标签: pytables


    【解决方案1】:

    我在文档中找到了解决方案:tables.EArray

    http://www.pytables.org/usersguide/libref/homogenous_storage.html#earrayclassdescr

    这是一个描述性示例代码,它添加了两个“列”,具有两种不同的dtype 定义方式。 with 块可以被多次调用,它会扩展列:

    import tables as tb
    import numpy as np
    
    filename = 'foo.h5'
    
    with tb.File(filename, "a") as f:        
        if "/foo" not in f:
            group = f.create_group("/", 'foo', 'Foo Information')
        else:
            group = f.root.foo
    
        if "col1" not in group:
            a = tb.Atom.from_dtype(np.dtype('<f8'), dflt=0.0)
            arr = f.create_earray(group, 'col1', a, (0,), "Bar")
        else:
            arr = getattr(group, "col1")
    
        arr.append(np.arange(10))
        arr.append(np.arange(40, 45))
    
        if "col2" not in group:
            b = tb.Int64Atom()
            arr = f.create_earray(group, 'col2', b, (0,), "Baz")
        else:
            arr = getattr(group, "col")
    
        arr.append(np.arange(7))
        arr.append(np.arange(30, 38))
    

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 2020-12-04
      • 2021-03-27
      • 2015-10-21
      • 1970-01-01
      • 2021-12-04
      • 2016-11-12
      • 2012-04-03
      • 1970-01-01
      相关资源
      最近更新 更多