Append simulation data using HDF5(使用HDF5追加仿真数据)
本文介绍了使用HDF5追加仿真数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我当前多次运行模拟,希望保存这些模拟的结果,以便可以将其用于可视化。
模拟运行100次,每个模拟生成大约100万个数据点(即100万集的100万个值),我现在想高效地存储这些数据点。每一集的目标都是在所有100个模拟中生成每个值的平均值。
我的main
文件如下:
# Defining the test simulation environment
def test_simulation:
environment = environment(
periods = 1000000
parameter_x = ...
parameter_y = ...
)
# Defining the simulation
environment.simulation()
# Save simulation data
hf = h5py.File('runs/simulation_runs.h5', 'a')
hf.create_dataset('data', data=environment.value_history, compression='gzip', chunks=True)
hf.close()
# Run the simulation 100 times
for i in range(100):
print(f'--- Iteration {i} ---')
test_simulation()
value_history
在game()
内生成,即根据
def simulation:
for episode in range(periods):
value = doSomething()
self.value_history.append(value)
现在,我在进入下一次模拟时收到以下错误消息:
ValueError: Unable to create dataset (name already exists)
我知道当前代码不断尝试创建新文件并生成错误,因为它已经存在。现在,我希望重新打开在第一个模拟中创建的文件,追加下一个模拟中的数据,然后再次保存。
推荐答案
下面的示例显示如何将所有这些想法结合在一起。它创建2个文件:
- 在第一个循环中使用
maxshape()
参数创建一个可调整大小的数据集,然后在后续循环中使用dataset.resize()
--输出为simulation_runs1.h5
为每个模拟创建唯一的数据集--输出为simulation_runs2.h5
。
注1:如果无法将所有数据保存在系统内存中,可以将仿真结果增量保存到H5文件中。只是稍微复杂了一点。
注2:我添加了一个
mode
变量来控制是为第一个模拟(i==0
)创建一个新文件,还是以追加模式打开现有文件以供后续模拟使用。
import h5py
import numpy as np
# Create some psuedo-test data
def test_simulation(i):
periods = 100
times = 100
# Define the simulation with some random data
val_hist = np.random.random(periods*times).reshape(periods,times)
a0, a1 = val_hist.shape[0], val_hist.shape[1]
if i == 0:
mode='w'
else:
mode='a'
# Save simulation data (resize dataset)
with h5py.File('runs/simulation_runs1.h5', mode) as hf:
if 'data' not in list(hf.keys()):
print('create new dataset')
hf.create_dataset('data', shape=(1,a0,a1), maxshape=(None,a0,a1), data=val_hist,
compression='gzip', chunks=True)
else:
print('resize existing dataset')
d0 = hf['data'].shape[0]
hf['data'].resize( (d0+1,a0,a1) )
hf['data'][d0:d0+1,:,:] = val_hist
# Save simulation data (unique datasets)
with h5py.File('runs/simulation_runs2.h5', mode) as hf:
hf.create_dataset(f'data_{i:03}', data=val_hist,
compression='gzip', chunks=True)
# Run the simulation 100 times
for i in range(10):
print(f'--- Iteration {i} ---')
test_simulation(i)
这篇关于使用HDF5追加仿真数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用HDF5追加仿真数据


猜你喜欢
- 计算测试数量的Python单元测试 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01