Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save_dict_into_h5() and sliding_window(), add .gitignore, and update the docs #180

Merged
merged 6 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add save_dict_into_h5();
  • Loading branch information
WenjieDu committed Aug 28, 2023
commit 737cc58fe1b72a19d36447c19c2c7e27ac0f611a
3 changes: 3 additions & 0 deletions pypots/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
pickle_load,
pickle_dump,
)
from .saving import save_dict_into_h5

__all__ = [
# datasets
Expand All @@ -39,4 +40,6 @@
"mcar",
"pickle_load",
"pickle_dump",
# saving
"save_dict_into_h5",
]
43 changes: 43 additions & 0 deletions pypots/data/saving.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Data saving utilities.
"""

# Created by Wenjie Du <[email protected]>
# License: GLP-v3


import os

import h5py

from pypots.utils.file import create_dir_if_not_exist
from pypots.utils.logging import logger


def save_dict_into_h5(data_dict: dict, saving_dir: str) -> None:
"""Save the given data (in a dictionary) into the given h5 file.

Parameters
----------
data_dict : dict,
The data to be saved, should be a Python dictionary.

saving_dir : str,
The h5 file to save the data.

"""

def save_set(handle, name, data):
if isinstance(data, dict):
single_set_handle = handle.create_group(name)
for key, value in data.items():
save_set(single_set_handle, key, value)
else:
handle.create_dataset(name, data=data)

create_dir_if_not_exist(saving_dir)
saving_path = os.path.join(saving_dir, "datasets.h5")
with h5py.File(saving_path, "w") as hf:
for k, v in data_dict.items():
save_set(hf, k, v)
logger.info(f"Successfully saved the given data into {saving_path}.")