From 07a433d2e48016b03d761d03087c9061f40a165d Mon Sep 17 00:00:00 2001 From: beckynevin Date: Tue, 6 Feb 2024 14:31:30 -0700 Subject: [PATCH] adding functions to save data as h5 --- src/scripts/io.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/scripts/io.py b/src/scripts/io.py index 6d7fd1e..68c7a28 100644 --- a/src/scripts/io.py +++ b/src/scripts/io.py @@ -1,5 +1,5 @@ import pickle - +import h5py class ModelLoader: def save_model_pkl(self, path, model_name, posterior): @@ -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