【发布时间】:2020-10-26 05:21:32
【问题描述】:
TL;DR:我有一个带有 float32 Col 的 PyTable,在将 numpy-float32-array 写入其中时出现错误。 (如何)我可以在 PyTables 表的列中存储一个 numpy-array (float32) 吗?
我是 PyTables 的新手 - 根据 TFtables(在 Tensorflow 中使用 HDF5 的库)的建议,我使用它来存储我所有的 HDF5 数据(目前分批分布在几个文件中,每三个数据集) 在单个 HDF5 文件的表中。数据集是
'data' : (n_elements, 1024, 1024, 4)@float32
'label' : (n_elements, 1024, 1024, 1)@uint8
'weights' : (n_elements, 1024, 1024, 1)@float32
n_elements 分布在多个文件中,我现在要合并为一个文件(以允许无序访问)。
因此,当我构建表格时,我认为每个数据集代表一列。我以通用方式构建了所有内容,允许对任意数量的数据集执行此操作:
# gets dtypes (and shapes) of the dsets (accessed by dset_keys = ['data', 'label', 'weights']
dtypes, shapes = _determine_shape(hdf5_files, dset_keys)
# to dynamically generate a table, I'm using a dict (not a class as in the PyTables tutorials)
# the dict is (conform with the doc): { 'col_name' : Col()-class-descendent }
table_description = {dset_keys[i]: tables.Col.from_dtype(dtypes[i]) for i in range(len(dset_keys))}
# create a file, a group-node and attach a table to it
h5file = tables.open_file(destination_file, mode="w", title="merged")
group = h5file.create_group("/", 'main', 'Node for data table')
table = h5file.create_table(group, 'data_table', table_description, "Collected data with %s" % (str(val_keys)))
我为每个 dset(使用 h5py 读取)获得的 dtype 显然是读取 dset 返回的 numpy 数组(ndarray):float32 或 uint8。所以 Col() 类型是 Float32Col 和 UInt8Col。我天真地假设我现在可以将 float32-array 写入此 col,但使用以下内容填充数据:
dummy_data = np.zeros([1024,1024,3], float32) # normally data read from other files
sample = table.row
sample['data'] = dummy_data
结果为@987654331@。所以现在我觉得自己可以在里面写一个数组,但是没有提供“ArrayCol()”类型,PyTables doc 中也没有任何关于是否或如何可能的提示将数组写入列。我该怎么做?
Col() 类中有“形状”参数,它是后代,所以应该是可能的,否则这些有什么用?!
【问题讨论】:
-
不介意对为什么这个问题被否决提出一些建设性的批评......我已经做了很多工作。
-
是的,我也不明白为什么这个问题被否决了,所以我投了赞成票——因为我有同样的问题,而且描述得很好。
标签: python arrays numpy pytables