Python:如何在PyTables中存储numpy多维数组?

时间:2021-12-18 21:31:39

How can I put a numpy multidimensional array in a HDF5 file using PyTables?

如何使用PyTables在HDF5文件中放置一个numpy多维数组?

From what I can tell I can't put an array field in a pytables table.

根据我所知道的,我不能将数组字段放在一个pytables表中。

I also need to store some info about this array and be able to do mathematical computations on it.

我还需要存储一些关于这个数组的信息,并能够对它进行数学计算。

Any suggestions?

有什么建议吗?

1 个解决方案

#1


33  

There may be a simpler way, but this is how you'd go about doing it, as far as I know:

也许有一种更简单的方法,但据我所知,这就是你要做的事情:

import numpy as np
import tables

# Generate some data
x = np.random.random((100,100,100))

# Store "x" in a chunked array...
f = tables.openFile('test.hdf', 'w')
atom = tables.Atom.from_dtype(x.dtype)
ds = f.createCArray(f.root, 'somename', atom, x.shape)
ds[:] = x
f.close()

If you want to specify the compression to use, have a look at tables.Filters. E.g.

如果您想指定要使用的压缩,请查看表. filter。如。

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.openFile('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()

There's probably a simpler way for a lot of this... I haven't used pytables for anything other than table-like data in a long while.

也许有一种更简单的方法来解决这些问题……在很长一段时间内,我还没有使用pytables作为表类数据。

Note: with pytables 3.0, f.createCArray was renamed to f.create_carray. It can also accept the array directly, without specifying the atom,

注:pytables 3.0, f。createCArray被重命名为f.create_carray。它也可以直接接受数组,而不指定原子,

f.create_carray('/', 'somename', obj=x, filters=filters)

#1


33  

There may be a simpler way, but this is how you'd go about doing it, as far as I know:

也许有一种更简单的方法,但据我所知,这就是你要做的事情:

import numpy as np
import tables

# Generate some data
x = np.random.random((100,100,100))

# Store "x" in a chunked array...
f = tables.openFile('test.hdf', 'w')
atom = tables.Atom.from_dtype(x.dtype)
ds = f.createCArray(f.root, 'somename', atom, x.shape)
ds[:] = x
f.close()

If you want to specify the compression to use, have a look at tables.Filters. E.g.

如果您想指定要使用的压缩,请查看表. filter。如。

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.openFile('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()

There's probably a simpler way for a lot of this... I haven't used pytables for anything other than table-like data in a long while.

也许有一种更简单的方法来解决这些问题……在很长一段时间内,我还没有使用pytables作为表类数据。

Note: with pytables 3.0, f.createCArray was renamed to f.create_carray. It can also accept the array directly, without specifying the atom,

注:pytables 3.0, f。createCArray被重命名为f.create_carray。它也可以直接接受数组,而不指定原子,

f.create_carray('/', 'somename', obj=x, filters=filters)