Recording data in a long running python simulation

前端 未结 2 409
深忆病人
深忆病人 2021-01-29 04:40

I am running a simulation from which I need to record some small numpy arrays every cycle. My current solution is to load, write then save as follows:

existing_d         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 05:31

    I have found a good working solution using the h5py library. Performance is far better as there is no reading data and I have cut down on the number of nump array append operations. A short example:

    with h5py.File("logfile_name", "a") as f:
      ds = f.create_dataset("weights", shape=(3,2,100000), maxshape=(3, 2, None))
      ds[:,:,cycle_num] = weight_matrix
    

    I am not sure if the numpy style slicing means the matrix gets copied but there is a write_direct(source, source_sel=None, dest_sel=None) function to avoid this happening which could be useful for larger matrices.

提交回复
热议问题