Python 使用h5py创建大量数据集-无法注册数据类型atom(可以插入重复密钥)
我试图将大量numpy结构化数组作为数据集存储在hdf5文件中。Python 使用h5py创建大量数据集-无法注册数据类型atom(可以插入重复密钥),python,numpy,h5py,Python,Numpy,H5py,我试图将大量numpy结构化数组作为数据集存储在hdf5文件中。 例如, f['tree1']=结构化数组1 。 。 f['tree60000']=结构化阵列60000 (大约有60000棵树) 在读取文件的过程中,大约70%的时间我都会出错 RuntimeError:无法注册数据类型atom(无法插入重复密钥) 只有非常大的ascii文件(10e7行,5gb)才会出现此问题。如果文件在(10e6行,500mb)附近,则不会发生此错误。如果取出数据类型并仅存储为字符串的numpy数组,也不会发
例如,
f['tree1']=结构化数组1
。
。
f['tree60000']=结构化阵列60000 (大约有60000棵树) 在读取文件的过程中,大约70%的时间我都会出错 RuntimeError:无法注册数据类型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
错误:
/Volumes/My Passport for Mac/raw_trees/bolshoi/rockstar/asciiReaderOne.py in read_in_trees(self)
129 arr = np.array(current_tree, dtype = dt)
130 # depth_sort = arr['haloid_depth_first'].argsort()
--> 131 f[tree_id] = arr
132 current_tree = []
133 first_line = False
/Library/Python/2.7/site-packages/h5py/_objects.so in h5py._objects.with_phil.wrapper (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/_objects.c:2458)()
/Library/Python/2.7/site-packages/h5py/_objects.so in h5py._objects.with_phil.wrapper (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/_objects.c:2415)()
/Library/Python/2.7/site-packages/h5py/_hl/group.pyc in __setitem__(self, name, obj)
281
282 else:
--> 283 ds = self.create_dataset(None, data=obj, dtype=base.guess_dtype(obj))
284 h5o.link(ds.id, self.id, name, lcpl=lcpl)
285
/Library/Python/2.7/site-packages/h5py/_hl/group.pyc in create_dataset(self, name, shape, dtype, data, **kwds)
101 """
102 with phil:
--> 103 dsid = dataset.make_new_dset(self, shape, dtype, data, **kwds)
104 dset = dataset.Dataset(dsid)
105 if name is not None:
/Library/Python/2.7/site-packages/h5py/_hl/dataset.pyc in make_new_dset(parent, shape, dtype, data, chunks, compression, shuffle, fletcher32, maxshape, compression_opts, fillvalue, scaleoffset, track_times)
124
125 if data is not None:
--> 126 dset_id.write(h5s.ALL, h5s.ALL, data)
127
128 return dset_id
/Library/Python/2.7/site-packages/h5py/_objects.so in h5py._objects.with_phil.wrapper (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/_objects.c:2458)()
/Library/Python/2.7/site-packages/h5py/_objects.so in h5py._objects.with_phil.wrapper (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/_objects.c:2415)()
/Library/Python/2.7/site-packages/h5py/h5d.so in h5py.h5d.DatasetID.write (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/h5d.c:3260)()
/Library/Python/2.7/site-packages/h5py/h5t.so in h5py.h5t.py_create (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/h5t.c:15314)()
/Library/Python/2.7/site-packages/h5py/h5t.so in h5py.h5t.py_create (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/h5t.c:14903)()
/Library/Python/2.7/site-packages/h5py/h5t.so in h5py.h5t._c_compound (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/h5t.c:14192)()
/Library/Python/2.7/site-packages/h5py/h5t.so in h5py.h5t.py_create (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/h5t.c:15314)()
/Library/Python/2.7/site-packages/h5py/h5t.so in h5py.h5t.py_create (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/h5t.c:14749)()
/Library/Python/2.7/site-packages/h5py/h5t.so in h5py.h5t._c_float (/Users/travis/build/MacPython/h5py-wheels/h5py/h5py/h5t.c:12379)()
RuntimeError: Unable to register datatype atom (Can't insert duplicate key)
你有错误堆栈吗?代码中哪里产生错误的指示 您报告:
error RuntimeError:无法注册数据类型atom(无法插入重复密钥)
在/usr/lib/python3/dist-packages/h5py/_-hl/datatype.py中
class Datatype(HLObject):
# Represents an HDF5 named datatype stored in a file.
# >>> MyGroup["name"] = numpy.dtype("f")
def __init__(self, bind):
""" Create a new Datatype object by binding to a low-level TypeID.
我在猜测。您的dt
有57个术语。我怀疑每次向文件中添加树
,它都会将每个字段注册为新的数据类型
In [71]: (57*10e7*.7)/(2**32)
Out[71]: 0.9289942681789397
57*10e7的70%接近2*32。如果Python/numpy使用int32作为数据类型id,那么您可能达到了这个极限
我们必须在h5py
或numpy
代码中进一步挖掘,以找到谁发出了此错误消息
通过使用以下命令向文件添加数组:
f[tree_id] = arr
您正在将每个数组放入一个新的组中的数据集中。如果每个数据集都有一个数据类型,或者数组的每个字段都有一个数据类型,那么很容易得到2*32个数据类型
另一方面,如果可以将多个arr
存储到一个组或数据集,则可以避免注册数千种数据类型。我对h5py的h5py
不太熟悉,无法建议您如何操作
我想知道这个序列是否可以对多个数据集重用数据类型:
dt1=np.dtype(dt)
gg= f.create_group('testgroup')
gg['xdtype']=dt1
# see h5py.Datatype doc
xdtype=gg['xdtype']
x=np.zeros((10,),dtype=xdtype)
gg['tree1']=x
x=np.ones((10,),dtype=xdtype)
gg['tree2']=x
在数据类型
文档之后,我尝试注册一个命名的数据类型,并将其用于添加到组中的每个数据集
In [117]: isinstance(xdtype, h5py.Datatype)
Out[117]: True
In [118]: xdtype.id
Out[118]: <h5py.h5t.TypeCompoundID at 0xb46e0d4c>
[117]中的:isinstance(xdtype,h5py.Datatype)
Out[117]:对
在[118]中:xdtype.id
出[118]:
因此,如果我正确读取def make\u new\u dset
,这将绕过py\u create
调用。听起来像树id
在ascii\u文件中重复。如果没有真正的副本,可能会有某种截断。或者界面中的一些c
代码正在使用小整数进行索引。是否有某种h5py
flush
方法?也就是说,告诉它,保存您拥有的数据集,然后重新开始?您的10e6/7错误范围接近2*32
,这可能会在32位机器上产生问题。没有重复的树。hdf5数据是在我关闭终端并在中途重新启动时保存的,但我正在寻找一种不太老套的解决方案。我发布了错误消息。我还发现代码在创建约48000个数据集后停止。每个数据集存储自己的数据类型。通常每个树中有多少个元素(行)?我想我已经找到了注册数据类型的方法,因此,它不会为每个树创建新的集合。我尝试了您的代码以重用该数据类型,但仍然出现相同的错误。它似乎没有数据集ID,而没有数据类型ID,但问题仅在指定数据类型时出现。