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 all commits
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
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ignore special files or folds
*~
.idea
.DS_Store

# ignore all building results
dist
build
docs/_build
*.egg-info

# ignore all testing/running results
.run
.coverage
.pytest_cache
*__pycache__*
*testing_results*

# ignore specific kinds of files like all PDFs
*.pdf
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
prune tests
prune pypots/*/template
prune pypots/*/README.md
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
<a href="https://docs.pypots.com/en/latest/install.html#reasons-of-version-limitations-on-dependencies">
<img alt="Python version" src="https://img.shields.io/badge/Python-v3.7--3.10-E97040?logo=python&logoColor=white">
</a>
<img alt="powered by Pytorch" src="https://img.shields.io/badge/PyTorch-❤️-F8C6B5?logo=pytorch&logoColor=white">
<a href="https://pypi.org/project/PyPOTS">
<img alt="the latest release version" src="https://img.shields.io/github/v/release/wenjiedu/pypots?color=EE781F&include_prereleases&label=Release">
<a href="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/WenjieDu/PyPOTS">
<img alt="powered by Pytorch" src="https://img.shields.io/badge/PyTorch-❤️-F8C6B5?logo=pytorch&logoColor=white">
</a>
<a href="https://github.com/WenjieDu/PyPOTS/releases">
<img alt="the latest release version" src="https://img.shields.io/github/v/release/wenjiedu/pypots?color=EE781F&include_prereleases&label=Release&logo=github&logoColor=white">
</a>
<a href="https://github.com/WenjieDu/PyPOTS/blob/main/LICENSE">
<img alt="GPL-v3 license" src="https://img.shields.io/badge/License-GPL--v3-E9BB41">
<img alt="GPL-v3 license" src="https://img.shields.io/badge/License-GPL--v3-E9BB41?logo=opensourceinitiative&logoColor=white">
</a>
<a href="https://github.com/WenjieDu/PyPOTS/blob/main/README.md#-community">
<img alt="Community" src="https://img.shields.io/badge/join_us-community!-C8A062">
Expand Down Expand Up @@ -44,7 +46,7 @@
<img alt="Conda downloads" src="https://img.shields.io/conda/dn/conda-forge/pypots?label=Conda%20Downloads&color=AED0ED&logo=anaconda&logoColor=white">
</a>
<a href="https://pepy.tech/project/pypots">
<img alt="PyPI downloads" src="https://static.pepy.tech/personalized-badge/pypots?period=total&units=international_system&left_color=grey&right_color=blue&left_text=PyPI%20Downloads">
<img alt="PyPI downloads" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FWenjieDu%2FWenjieDu%2Fmain%2Ffigs%2Fprojects%2Fpypots_downloads.json">
</a>

</p>
Expand Down
12 changes: 7 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ Welcome to PyPOTS docs!
:target: https://github.com/WenjieDu/PyPOTS
:alt: PyPOTS logo

.. centered:: A Python Toolbox for Data Mining on Partially-Observed Time Series
**A Python Toolbox for Data Mining on Partially-Observed Time Series**

.. image:: https://img.shields.io/badge/Python-v3.7--3.10-E97040?logo=python&logoColor=white
:alt: Python version
:target: https://docs.pypots.com/en/latest/install.html#reasons-of-version-limitations-on-dependencies

.. image:: https://img.shields.io/badge/PyTorch-❤️-F8C6B5?logo=pytorch&logoColor=white
:alt: powered by Pytorch
:target: https://github.com/WenjieDu/PyPOTS

.. image:: https://img.shields.io/github/v/release/wenjiedu/pypots?color=EE781F&include_prereleases&label=Release
.. image:: https://img.shields.io/github/v/release/wenjiedu/pypots?color=EE781F&include_prereleases&label=Release&logo=github&logoColor=white
:alt: the latest release version
:target: https://pypi.org/project/pypots
:target: https://github.com/WenjieDu/PyPOTS/releases

.. image:: https://img.shields.io/badge/License-GPL--v3-E9BB41
.. image:: https://img.shields.io/badge/License-GPL--v3-E9BB41?logo=opensourceinitiative&logoColor=white
:alt: GPL-v3 license
:target: https://github.com/WenjieDu/PyPOTS/blob/main/LICENSE

Expand Down Expand Up @@ -62,7 +64,7 @@ Welcome to PyPOTS docs!
:alt: Conda downloads
:target: https://anaconda.org/conda-forge/pypots

.. image:: https://static.pepy.tech/personalized-badge/pypots?period=total&units=international_system&left_color=grey&right_color=blue&left_text=PyPI%20Downloads
.. image:: https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FWenjieDu%2FWenjieDu%2Fmain%2Ffigs%2Fprojects%2Fpypots_downloads.json
:alt: PyPI downloads
:target: https://pepy.tech/project/pypots

Expand Down
4 changes: 2 additions & 2 deletions docs/pypots.utils.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pypots.utils package
====================

pypots.utils.files module
pypots.utils.file module
-------------------------

.. automodule:: pypots.utils.files
.. automodule:: pypots.utils.file
:members:
:undoc-members:
:show-inheritance:
Expand Down
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}.")
43 changes: 43 additions & 0 deletions pypots/data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,46 @@ def cal_delta_for_single_sample(mask: np.ndarray) -> np.ndarray:
delta_collector.append(delta)
delta = np.asarray(delta_collector)
return delta


def sliding_window(time_series, n_steps, sliding_len=None):
"""Generate time series samples with sliding window method, truncating windows from time-series data
with a given sequence length.

Given a time series of shape [seq_len, n_features] (seq_len is the total sequence length of the time series), this
sliding_window function will generate time-series samples from this given time series with sliding window method.
The number of generated samples is seq_len//sliding_len. And the final returned numpy ndarray has a shape
[seq_len//sliding_len, n_steps, n_features].

Parameters
----------
time_series : np.ndarray,
time series data, len(shape)=2, [total_length, feature_num]

n_steps : int,
The number of time steps in the generated data samples.

sliding_len : int, default = None,
The size of the sliding window. It will be set as the same with n_steps if None.

Returns
-------
samples : np.ndarray,
The generated time-series data samples of shape [seq_len//sliding_len, n_steps, n_features].

"""
sliding_len = n_steps if sliding_len is None else sliding_len
total_len = time_series.shape[0]
start_indices = np.asarray(range(total_len // sliding_len)) * sliding_len

# remove the last one if left length is not enough
if total_len - start_indices[-1] * sliding_len < n_steps:
start_indices = start_indices[:-1]

sample_collector = []
for idx in start_indices:
sample_collector.append(time_series[idx : idx + n_steps])

samples = np.asarray(sample_collector).astype("float32")

return samples
Loading