[英]How to automatically resize an HDF5 dataset with h5py?
簡短的回答:沒有。
我不是底層 HDF5 庫的專家,但我認為它們沒有這種能力(而 h5py 只是一個包裝器)。 (有點)好消息:如果您嘗試寫入超出分配的大小,h5py 將引發異常。 下面的代碼擴展了您的示例以進行演示。
with h5py.File('SO_68389770.h5','w') as h5f:
dset = h5f.create_dataset("my_dataset", (1024,), maxshape=(None,))
size = 100
for i in range(10):
arr = np.random.random(size)
start, end = i*size, i*size+size
dset[start:end] = arr
這適用於range(10)
。 您將收到range(11)
此錯誤:
TypeError: Can't broadcast (100,) -> (24,)
下面的代碼通過在寫入之前檢查dset.shape[0]
干凈地處理任何大小。
with h5py.File('SO_68389770.h5','w') as h5f:
dset = h5f.create_dataset("my_dataset", (1024,), maxshape=(None,))
size = 100
for i in range(13):
arr = np.random.random(size)
start, end = i*size, i*size+size
if dset.shape[0] >= end :
dset[start:end] = arr
else:
print(f'insufficient dset size, end={end}; resizing')
dset.resize(end,axis=0)
dset[start:end] = arr
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.