2017-07-04 70 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))