以HDF5格式存储时间序列
问题描述:
我想将时间序列(传感器数据)的结果存储到HDF5文件中。我似乎无法将值分配给我的数据集。很显然,我做错了什么,我只是不知道是什么......以HDF5格式存储时间序列
代码:
from datetime import datetime, timezone
import h5py
TIME_SERIES_FLOAT = np.dtype([("time", h5py.special_dtype(vlen=str)),
("value", np.float)])
h5 = h5py.File('balh.h5', "w")
dset = create_dataset('data', (1, 2), chunks=True, maxshape=(None, 2), dtype=TIME_SERIES_FLOAT)
dset[0]['time'] = datetime.now(timezone.utc).astimezone().isoformat()
dset[0]['value'] = 0.0
然后,更新代码调整大小的数据集,并增加了更多的价值。显然,这样做,每个值是无效的:
size = list(dset.shape)
size[0] += 1
dset.resize(tuple(size))
dset[size[0]-1]['time'] = datetime.now(timezone.utc).astimezone().isoformat()
dset[size[0]-1]['value'] = value
一个更好的方法是将一些数据整理成np.array
,然后补充说,每隔一段时间...
这是明智的......
答
我?需要更多的咖啡...
定义的类型是含有一个字符串(又名的时间)的元组和浮子(又名的值),以便增加一个,我需要:
dset[-1] = (datetime.now(timezone.utc).astimezone().isoformat(), value)
这其实很简单!
添加许多条目做是这样的:
l = [('stamp', x) for x in range(10)]
size = list(dset.shape)
tmp = size[0]
size[0] += len(l)
dset.resize(tuple(size))
for x in range(len(l)):
dset[tmp+x] = l[x]
尽管如此,这种感觉有点笨重,次优...