【问题标题】:PyTables - big memory consumption using cols methodPyTables - 使用 cols 方法消耗大量内存
【发布时间】:2019-03-21 02:36:56
【问题描述】:

在 Pytables 中使用 cols 方法的目的是什么?我有一个大数据集,我有兴趣只从该数据集中读取一列。

这两种方法给了我相同的时间,但完全不同的可变内存消耗:

import tables
from sys import getsizeof

f = tables.open_file(myhdf5_path, 'r')

# These two methods takes the same amount of time
x = f.root.set1[:500000]['param1']
y = f.root.set1.cols.param1[:500000]

# But totally different memory consumption:
print(getsizeof(x)) # gives me 96
print(getsizeof(y)) # gives me 2000096

它们都是相同的 numpy 数组数据类型。谁能解释一下使用 cols 方法的目的是什么?

%time x = f.root.set1[:500000]['param1']  # gives ~7ms
%time y = f.root.set1.cols.param1[:500000]  # gives also about 7ms

【问题讨论】:

  • 我没有使用cols 方法。据我了解,它主要用于提取存储为另一个表中的列的表。使用此方法,您可以将嵌入的表作为 numpy 数组访问。

标签: python hdf5 pytables


【解决方案1】:

你的问题引起了我的好奇。我通常使用 table.read(field='name'),因为它补充了我使用的其他 table.read_ 方法(例如:.read_where().read_coordinates())。

在查看文档后,我发现至少有 4 种方法可以使用 PyTables 读取一列表格数据。您显示了 2 个,还有 2 个:
table.read(field='name')
table.col('name')(单数)

我对全部 4 个进行了一些测试,并对整个表(数据集)进行了 2 个测试以进行额外比较。我为所有 6 个对象调用了getsizeof(),大小因方法而异。尽管所有 4 个与 numpy 索引的行为相同,但我怀疑返回的对象存在差异。但是,我不是 PyTables 开发人员,所以这更像是推论而非事实。也可能是getsizeof() 对对象的解释不同。

代码如下:

import tables as tb
import numpy as np
from sys import getsizeof

# Create h5 file with 1 dataset

h5f = tb.open_file('SO_55254831.h5', 'w')

mydtype = np.dtype([('param1',float),('param2',float),('param3',float)])

arr = np.array(np.arange(3.*500000.).reshape(500000,3))
recarr = np.core.records.array(arr,dtype=mydtype)

h5f.create_table('/', 'set1', obj=recarr )

# Close, then Reopen file READ ONLY
h5f.close()

h5f = tb.open_file('SO_55254831.h5', 'r')

testds_1 = h5f.root.set1
print ("\nFOR: testds_1 = h5f.root.set1")
print (testds_1.dtype)
print (testds_1.shape)
print (getsizeof(testds_1)) # gives 128

testds_2 = h5f.root.set1.read()
print ("\nFOR: testds_2 = h5f.root.set1.read()")
print (getsizeof(testds_2)) # gives 12000096

x = h5f.root.set1[:500000]['param1']
print ("\nFOR: x = h5f.root.set1[:500000]['param1']")
print(getsizeof(x)) # gives 96

print ("\nFOR: y = h5f.root.set1.cols.param1[:500000]")
y = h5f.root.set1.cols.param1[:500000]
print(getsizeof(y)) # gives 4000096

print ("\nFOR: z = h5f.root.set1.read(stop=500000,field='param1')")
z = h5f.root.set1.read(stop=500000,field='param1')
print(getsizeof(z)) # also gives 4000096

print ("\nFOR: a = h5f.root.set1.col('param1')")
a = h5f.root.set1.col('param1')
print(getsizeof(a)) # also gives 4000096

h5f.close()

上面的输出:

FOR: testds_1 = h5f.root.set1
[('param1', '<f8'), ('param2', '<f8'), ('param3', '<f8')]
(500000,)
128

FOR: testds_2 = h5f.root.set1.read()
12000096

FOR: x = h5f.root.set1[:500000]['param1']
96

FOR: y = h5f.root.set1.cols.param1[:500000]
4000096

FOR: z = h5f.root.set1.read(stop=500000,field='param1')
4000096

FOR: a = h5f.root.set1.col('param1')
4000096

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2014-01-04
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多