我试图将大量的numpy结构化数组作为数据集存储在hdf5文件中。
例如,
f['tree1']=结构化阵列1
。
。
f['tree60000']=结构化阵列60000
(大约有60000棵树)
大约70%的阅读过程中,我得到了错误 运行时错误:无法注册数据类型atom(无法插入重复的密钥)
只有非常大的ascii文件(10e7行,5gb)才会出现此问题。如果文件在附近(10e6行,500mb),则不会发生这种情况。如果我去掉数据类型,只将其存储为一个numpy字符串数组,也不会发生这种情况。在
如果我中途停止读取文件,关闭终端,再次打开它,然后继续从一半开始读取文件,直到最后(我保存结束的行号),我可以解决这个问题。我尝试在python函数本身中打开和关闭hdf5文件,但这没有起作用。在
dt = [
('scale', 'f4'),
('haloid', 'i8'),
('scale_desc', 'f4'),
('haloid_desc', 'i8'),
('num_prog', 'i4'),
('pid', 'i8'),
('upid', 'i8'),
('pid_desc', 'i8'),
('phantom', 'i4'),
('mvir_sam', 'f4'),
('mvir', 'f4'),
('rvir', 'f4'),
('rs', 'f4'),
('vrms', 'f4'),
('mmp', 'i4'),
('scale_lastmm', 'f4'),
('vmax', 'f4'),
('x', 'f4'),
('y', 'f4'),
('z', 'f4'),
('vx', 'f4'),
('vy', 'f4'),
('vz', 'f4'),
('jx', 'f4'),
('jy', 'f4'),
('jz', 'f4'),
('spin', 'f4'),
('haloid_breadth_first', 'i8'),
('haloid_depth_first', 'i8'),
('haloid_tree_root', 'i8'),
('haloid_orig', 'i8'),
('snap_num', 'i4'),
('haloid_next_coprog_depthfirst', 'i8'),
('haloid_last_prog_depthfirst', 'i8'),
('haloid_last_mainleaf_depthfirst', 'i8'),
('rs_klypin', 'f4'),
('mvir_all', 'f4'),
('m200b', 'f4'),
('m200c', 'f4'),
('m500c', 'f4'),
('m2500c', 'f4'),
('xoff', 'f4'),
('voff', 'f4'),
('spin_bullock', 'f4'),
('b_to_a', 'f4'),
('c_to_a', 'f4'),
('axisA_x', 'f4'),
('axisA_y', 'f4'),
('axisA_z', 'f4'),
('b_to_a_500c', 'f4'),
('c_to_a_500c', 'f4'),
('axisA_x_500c', 'f4'),
('axisA_y_500c', 'f4'),
('axisA_z_500c', 'f4'),
('t_by_u', 'f4'),
('mass_pe_behroozi', 'f4'),
('mass_pe_diemer', 'f4')
]
def read_in_trees(self):
"""Store each tree as an hdf5 dataset.
"""
with open(self.fname) as ascii_file:
with h5py.File(self.hdf5_name,"r+") as f:
tree_id = ""
current_tree = []
for line in ascii_file:
if(line[0]=='#'): #new tree
arr = np.array(current_tree, dtype = dt)
f[tree_id] = arr
current_tree = []
tree_id = line[6:].strip('\n')
else: #read in next tree element
current_tree.append(tuple(line.split()))
return
错误:
^{pr2}$
你有错误堆栈吗?指示代码中错误产生的位置?在
您报告:
error RuntimeError: Unable to register datatype atom (Can't insert duplicate key)
在/usr/lib/python3/dist packages/h5py/\u hl中/数据类型.py在
我在这里猜一猜。你的
^{pr2}$dt
有57个术语。我怀疑每次向文件添加tree
时,它都会将每个字段注册为一个新的datatype
。在57*10e7的70%接近2*32。如果Python/numpy使用int32作为数据类型id,那么您可能达到了这个限制。在
我们必须在
h5py
或numpy
代码中挖掘更多信息,以找到谁发出了这个错误消息。在通过向文件中添加一个数组:
将每个数组放入一个新的
Group
中的数据集中。如果每个数据集都有一个数据类型,或者数组的每个字段都有一个数据类型,那么很容易就可以得到2*32个数据类型。在另一方面,如果您可以将多个
arr
存储到一个组或数据集,则可以避免数千个数据类型的注册。我对h5py
还不太熟悉,所以不能建议你怎么做。在我想知道这个序列是否能为多个数据集重用数据类型:
在
Datatype
文档之后,我尝试注册一个命名的数据类型,并将其用于添加到组中的每个数据集。在因此,如果我正确地读取了
def make_new_dset
,那么这将绕过py_create
调用。在相关问题 更多 >
编程相关推荐