Skip to content

Commit

Permalink
adding functions to save data as h5
Browse files Browse the repository at this point in the history
  • Loading branch information
beckynevin committed Feb 6, 2024
1 parent 5b37b09 commit 07a433d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/scripts/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pickle

import h5py

class ModelLoader:
def save_model_pkl(self, path, model_name, posterior):
Expand Down Expand Up @@ -70,3 +70,33 @@ def load_data_pkl(self,
with open(path + data_name + ".pkl", "rb") as file:
data = pickle.load(file)
return data

def save_data_h5(self,
data_name,
data,
path='../saveddata/'):
"""
Save data to an h5 file.
:param path: Location to save the data
:param data_name: Name of the data
:param data: Data to be saved
"""
file_name = path + data_name + ".h5"
with h5py.File(file_name, "w") as file:
file.create_dataset(data_name, data=data)

def load_data_h5(self,
data_name,
path='../saveddata/'):
"""
Load data from an h5 file.
:param path: Location to load the data from
:param data_name: Name of the data
:return: Loaded data
"""
file_name = path + data_name + ".h5"
with h5py.File(file_name, "r") as file:
data = file[data_name][...]
return data

0 comments on commit 07a433d

Please sign in to comment.