【发布时间】:2019-05-08 05:38:22
【问题描述】:
我正在从传感器获取数据并使用 h5py 将其存储在 HDF5 文件中。 sensordata 以字节对象的形式出现,我使用 numpy 将其转换为结构化数组。然后我将结构化数组写入 HDF5 文件。这一切都按预期工作。
现在我想从 HDF5 文件中读回数据,我只对其中的某些部分感兴趣。例如,如果我只想阅读一列。问题是直接将结构化的 numpy 数组写入 HDF5 文件会将所有数据作为单个块写入,例如形状为 (10,)。默认块大小设置为 (256,)。这意味着我为每个块读取了 256 行和所有数据列。但是,当列数增加时,这会变得非常慢。
有没有办法修改数据或更改分块参数,以便我可以读取单个数据列而不是每个块中的整个块?
我正在使用的一个最小示例如下所示:
import h5py
import ctypes
import numpy as np
class SensorStruct(ctypes.Structure):
_pack_ = 4
_fields_ = [('tc_time',ctypes.c_int64),
('pc_time',ctypes.c_double),
('nSample', ctypes.c_ushort),
('fMean', ctypes.c_float),
('fLowerbound', ctypes.c_float),
('fUpperbound', ctypes.c_float)]
def CreateFile(filename):
#Create a new HDF5 file
with h5py.File(filename, 'w', libver='latest') as f:
f.swmr_mode = True
def AddDataset(filename, dsetname, struct):
#Add a dataset to an existing HDF5 file
with h5py.File(filename, 'r+', libver='latest', swmr=True) as f:
f.create_dataset(dsetname,
dtype = struct,
shape = (0,), #Shape will update each time data is added
maxshape = (60480000,),
chunks = True, #Need to modify this somehow
compression = 'gzip')
def WriteData(filename, data):
#Append an existing dataset with new data
with h5py.File(filename, 'r+', libver='latest', swmr=True) as f:
dset = f[dsetname]
length = dset.shape[0]
maxlength = dset.maxshape[0]
newlength = length + len(data)
if newlength < maxlength:
dset.resize((newlength,))
dset[length:newlength] = data
filename = 'TESTFILE.h5'
dsetname = 'Sensor1'
struct_dt = np.dtype(SensorStruct)
#Rawdata comes in from a sensor every few seconds, returns as bytes object
rawdata1 = b"\x15\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x00\x00\x00\x00'u\x1fA\xf4Q\x01AbY?A\x16\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x01\x00\x00\x00\x1c\xe4&A[\x85\x0bA\x97\x96=A\x17\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x02\x00\x00\x00\xf6\x8b\x02A\xe5\xd5\xd5@\xc1Y\x1bA\x18\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x03\x00\x00\x00 \xec9A?W\x17A\xd0vRA\x19\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x04\x00\x00\x00\xf2\t/A\x83U\x19A\r&[A\x1a\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x05\x00\x00\x00s\x8a\x18A\xb0\x19\x04A\xc6\xb51A\x1b\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x06\x00\x00\x00P\xb6>A6\xc5 A)erA\x1c\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x07\x00\x00\x00\xe5e\x11A\x17\x9c\xff@^\xbf5A\x1d\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\x08\x00\x00\x00*\xbe\x19At\xd5\x04AN\x919A\x1e\xcd[\x07\x00\x00\x00\x00 x\x81BA\x02\xd7A\t\x00\x00\x00\xa2* A(-\x03AE\xedFA"
rawdata2 = b'\x1f\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\n\x00\x00\x00\xb6\x89&A\xd7\x8f\x07A\xe9\x00SA \xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x0b\x00\x00\x00\x91I\xfd@*\\\xdc@<\x17!A!\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x0c\x00\x00\x00,q\x12A\x81\x1f\xfa@\x81\xfe(A"\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\r\x00\x00\x00\x04@\x1cA\x03p\x05A\x05\xb03A#\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x0e\x00\x00\x00\xad\x89:A8h#A\xab\x88SA$\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x0f\x00\x00\x00I\x0f\xf5@\x15\xaa\xca@Rk\x0cA%\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x10\x00\x00\x00\xab\xeb\x1dA\x86 \x05A\x1807A&\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x11\x00\x00\x00Q\xda3A\xdc\xa6\x1cAT)ZA\'\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x12\x00\x00\x00U\xb3=A\xae\xcb\x1aA\xebmQA(\xcd[\x07\x00\x00\x00\x00 x\x01EA\x02\xd7A\x13\x00\x00\x00\x8f\x82\x0cA\x11\x15\xf3@$]&A'
data1 = np.frombuffer(rawdata1, dtype=SensorStruct)
data2 = np.frombuffer(rawdata2, dtype=SensorStruct)
CreateFile(filename)
AddDataset(filename, dsetname, SensorStruct)
WriteData(filename, data1)
WriteData(filename, data2)
我在这里尝试读取单列数据:
import time
t0 = time.time()
with h5py.File(filename, 'r', libver='latest', swmr=True) as f:
dset = f[dsetname]
#Optimize chunking so I can read one column
#My real dataset contains hundreds of columns and milions of rows
#So this minimal example may look slightly trivial
print('Chunksize: {}'.format(dset.chunks))
t = dset['pc_time']
print('Reading the time column took {} seconds'.format(time.time()-t0))
【问题讨论】:
-
我认为分块是在文件创建而不是读取时设置的,但我没有关注它。为了更快地访问字段,您可能应该将它们放在单独的数据集中,
-
是的,分块是在数据集创建时定义的,这就是为什么我还上传了创建文件的部分。我现在仍然可以编辑它。