如何使用PyTables将数值多维数组放入HDF5文件中?
据我所知,我不能将数组字段放入pytables表中。
我还需要存储有关此数组的一些信息,以便能够对其进行数学计算。
有什么建议吗?
发布于 2012-01-13 06:45:59
也许有一种更简单的方法,但据我所知,这就是你要做的:
import numpy as np
import tables
# Generate some data
x = np.random.random((100,100,100))
# Store "x" in a chunked array...
f = tables.open_file('test.hdf', 'w')
atom = tables.Atom.from_dtype(x.dtype)
ds = f.createCArray(f.root, 'somename', atom, x.shape)
ds[:] = x
f.close()如果您想指定要使用的压缩,可以查看tables.Filters。例如。
import numpy as np
import tables
# Generate some data
x = np.random.random((100,100,100))
# Store "x" in a chunked array with level 5 BLOSC compression...
f = tables.open_file('test.hdf', 'w')
atom = tables.Atom.from_dtype(x.dtype)
filters = tables.Filters(complib='blosc', complevel=5)
ds = f.createCArray(f.root, 'somename', atom, x.shape, filters=filters)
ds[:] = x
f.close()可能有一种更简单的方法来解决很多问题...我已经很长一段时间没有将pytables用于类似表的数据之外的任何事情了。
注意:在pytables 3.0中,将f.createCArray重命名为f.create_carray。它还可以直接接受数组,而无需指定atom,
f.create_carray('/', 'somename', obj=x, filters=filters)https://stackoverflow.com/questions/8843062
复制相似问题