2017-07-04 72 views
0

我通常使用h5py做HDF5东西在Python和我是否想创建,我想以后扩展数据集,或者我做HDF5扩展阵列:使用PyTables

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() 

回答

0

我发现在该文档的溶液:tables.EArray

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

这里是一个描述示例代码其将两个“列”与两个不同的定义方式dtypewith块可以多次调用,它将扩展列:

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))