用pytables压缩数组

时间:2013-11-21 10:37:15

标签: python hdf5 pytables

我正在尝试像这样压缩我的数组

import numpy as np
import tables
from contextlib import closing

FILTERS = tables.Filters(complib='zlib', complevel=5)

data = np.zeros(10**7)

with closing(tables.open_file('compressed', mode='w', filters=FILTERS)) as hdf:
    hdf.create_array('/', 'array', obj=data)

with closing(tables.open_file('uncompressed', mode='w')) as hdf:
    hdf.create_array('/', 'array', obj=data)

但它根本不起作用

-rw-rw-r-- 1 user user 80002360 2013-11-21 15:27 compressed
-rw-rw-r-- 1 user user 80002304 2013-11-21 15:28 uncompressed

我在这里做错了吗?

1 个答案:

答案 0 :(得分:8)

阵列本身无法压缩。压缩需要分块,因此您必须使用分块数组(CArrays)或可扩展数组(EArray)。这可能是1个字符的更改,因为您只想调用create_carray()方法而不是create_array()方法。

import numpy as np
import tables
from contextlib import closing

FILTERS = tables.Filters(complib='zlib', complevel=5)

data = np.zeros(10**7)

with closing(tables.open_file('compressed', mode='w', filters=FILTERS)) as hdf:
    hdf.create_carray('/', 'array', obj=data)

with closing(tables.open_file('uncompressed', mode='w')) as hdf:
    hdf.create_array('/', 'array', obj=data)