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

Fix failed docs building, update the docs, and allow tests for CLI to fail #89

Merged
merged 6 commits into from
May 5, 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
4 changes: 2 additions & 2 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ build:
pre_install:
- python -m pip install --upgrade pip
- pip install torch==1.13.1 -f https://download.pytorch.org/whl/cpu
- pip install -e ".[optional]" -f "https://data.pyg.org/whl/torch-1.13.1+cpu.html"
- pip install -e ".[basic]"
- pip install torch-geometric torch-scatter torch-sparse -f "https://data.pyg.org/whl/torch-1.13.1+cpu.html"
- pip install pypots
- pip install sphinx==6.2.1 docutils==0.19 sphinxcontrib-bibtex furo
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
"last_updated": f"{date_now.year}/{date_now.month}/{date_now.day}",
}

html_favicon = "_static/figs/PyPOTS_logo.svg"

html_sidebars = {
"**": [
"sidebar/scroll-start.html",
Expand Down
12 changes: 3 additions & 9 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Welcome to PyPOTS docs!

-----------------


⦿ `Motivation`: Due to all kinds of reasons like failure of collection sensors, communication error, and unexpected malfunction, missing values are common to see in time series from the real-world environment. This makes partially-observed time series (POTS) a pervasive problem in open-world modeling and prevents advanced data analysis. Although this problem is important, the area of data mining on POTS still lacks a dedicated toolkit. PyPOTS is created to fill in this blank.

⦿ `Mission`: PyPOTS is born to become a handy toolbox that is going to make data mining on POTS easy rather than tedious, to help engineers and researchers focus more on the core problems in their hands rather than on how to deal with the missing parts in their data. PyPOTS will keep integrating classical and the latest state-of-the-art data mining algorithms for partially-observed multivariate time series. For sure, besides various algorithms, PyPOTS is going to have unified APIs together with detailed documentation and interactive examples across algorithms as tutorials.
Expand All @@ -96,17 +97,10 @@ The rest of this readme file is organized as follows:

❖ Installation
^^^^^^^^^^^^^^^
PyPOTS now is available on `Anaconda <https://anaconda.org/conda-forge/pypots>`_ ❗️

Install it with ``conda install pypots``, you may need to specify the channel with option ``-c conda-forge``

Install the latest release from PyPI:

pip install pypots
PyPOTS is available on both `PyPI <https://pypi.python.org/pypi/pypots>`_ and `Anaconda <https://anaconda.org/conda-forge/pypots>`_ ❗️

or install from the source code with the latest features not officially released in a version:
Refer to the page `Installation <install.html>`_ to see how to install PyPOTS.

pip install https://github.com/WenjieDu/PyPOTS/archive/main.zip

❖ Usage
^^^^^^^^
Expand Down
9 changes: 4 additions & 5 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ It is recommended to use **pip** or **conda** for PyPOTS installation as shown b

# by pip
pip install pypots # the first time installation
pip install --upgrade pypots # update pypots if needed
pip install --upgrade pypots # update pypots to the latest version

.. code-block:: bash

# by conda
conda install -c conda-forge pypots
conda install -c conda-forge pypots # the first time installation
conda update -c conda-forge pypots # update pypots to the latest version

Alternatively, you can install from the latest source code which may be not officially released yet:

.. code-block:: bash

git clone https://github.com/WenjieDu/PyPOTS PyPOTS_latest --depth 1
cd PyPOTS_latest
pip install .
pip install https://github.com/WenjieDu/PyPOTS/archive/main.zip

Required Dependencies
"""""""""""""""""""""
Expand Down
3 changes: 0 additions & 3 deletions docs/pypots.classification.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pypots.classification package
=============================

Submodules
----------

pypots.classification.base module
---------------------------------

Expand Down
3 changes: 0 additions & 3 deletions docs/pypots.clustering.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pypots.clustering package
=========================

Submodules
----------

pypots.clustering.base module
-----------------------------

Expand Down
30 changes: 0 additions & 30 deletions docs/pypots.data.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pypots.data package
===================

Submodules
----------

pypots.data.base module
-----------------------

Expand All @@ -13,33 +10,6 @@ pypots.data.base module
:show-inheritance:
:inherited-members:

pypots.data.dataset\_for\_brits module
--------------------------------------

.. automodule:: pypots.data.dataset_for_brits
:members:
:undoc-members:
:show-inheritance:
:inherited-members:

pypots.data.dataset\_for\_grud module
-------------------------------------

.. automodule:: pypots.data.dataset_for_grud
:members:
:undoc-members:
:show-inheritance:
:inherited-members:

pypots.data.dataset\_for\_mit module
------------------------------------

.. automodule:: pypots.data.dataset_for_mit
:members:
:undoc-members:
:show-inheritance:
:inherited-members:

pypots.data.generating module
-----------------------------

Expand Down
3 changes: 0 additions & 3 deletions docs/pypots.forecasting.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pypots.forecasting package
==========================

Submodules
----------

pypots.forecasting.base module
------------------------------

Expand Down
23 changes: 10 additions & 13 deletions docs/pypots.imputation.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pypots.imputation package
=========================

Submodules
----------

pypots.imputation.base module
-----------------------------

Expand All @@ -13,37 +10,37 @@ pypots.imputation.base module
:show-inheritance:
:inherited-members:

pypots.imputation.brits module
pypots.imputation.saits module
------------------------------

.. automodule:: pypots.imputation.brits
.. automodule:: pypots.imputation.saits
:members:
:undoc-members:
:show-inheritance:
:inherited-members:

pypots.imputation.locf module
-----------------------------
pypots.imputation.transformer module
------------------------------------

.. automodule:: pypots.imputation.locf
.. automodule:: pypots.imputation.transformer
:members:
:undoc-members:
:show-inheritance:
:inherited-members:

pypots.imputation.saits module
pypots.imputation.brits module
------------------------------

.. automodule:: pypots.imputation.saits
.. automodule:: pypots.imputation.brits
:members:
:undoc-members:
:show-inheritance:
:inherited-members:

pypots.imputation.transformer module
------------------------------------
pypots.imputation.locf module
-----------------------------

.. automodule:: pypots.imputation.transformer
.. automodule:: pypots.imputation.locf
:members:
:undoc-members:
:show-inheritance:
Expand Down
7 changes: 2 additions & 5 deletions docs/pypots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ Subpackages
.. toctree::
:maxdepth: 4

pypots.imputation
pypots.classification
pypots.clustering
pypots.data
pypots.forecasting
pypots.imputation
pypots.data
pypots.utils

Submodules
----------

pypots.base module
------------------

Expand Down
12 changes: 9 additions & 3 deletions docs/pypots.utils.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pypots.utils package
====================

Submodules
----------

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

Expand Down Expand Up @@ -31,6 +28,15 @@ pypots.utils.metrics module
:show-inheritance:
:inherited-members:

pypots.utils.random module
--------------------------

.. automodule:: pypots.utils.random
:members:
:undoc-members:
:show-inheritance:
:inherited-members:

Module contents
---------------

Expand Down
18 changes: 6 additions & 12 deletions pypots/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,16 @@ class BaseModel(ABC):

"""

# leverage typing to show type hints in IDEs
# SAVING_STRATEGY = Literal["best", "better"]
SAVING_STRATEGY = [None, "best", "better"]

def __init__(
self,
device: Optional[Union[str, torch.device]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
):

assert model_saving_strategy in [
None,
"best",
"better",
], f"saving_strategy must be one of {self.SAVING_STRATEGY}, but got f{model_saving_strategy}."
saving_strategies = [None, "best", "better"]
assert (
model_saving_strategy in saving_strategies
), f"saving_strategy must be one of {saving_strategies}, but got f{model_saving_strategy}."

self.device = None
self.saving_path = saving_path
Expand Down Expand Up @@ -121,7 +115,7 @@ def __init__(
f"the tensorboard file will be saved to {tb_saving_path}"
)

def save_log_into_tb_file(self, step: int, stage: str, loss_dict: dict) -> None:
def _save_log_into_tb_file(self, step: int, stage: str, loss_dict: dict) -> None:
"""Saving training logs into the tensorboard file specified by the given path `tb_file_saving_path`.

Parameters
Expand Down Expand Up @@ -190,7 +184,7 @@ def save_model(
f'Failed to save the model to "{saving_path}" because of the below error! \n{e}'
)

def auto_save_model_if_necessary(
def _auto_save_model_if_necessary(
self,
training_finished: bool = True,
saving_name: str = None,
Expand Down
6 changes: 3 additions & 3 deletions pypots/classification/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def _train_model(

# save training loss logs into the tensorboard file for every step if in need
if self.summary_writer is not None:
self.save_log_into_tb_file(training_step, "training", results)
self._save_log_into_tb_file(training_step, "training", results)

# mean training loss of the current epoch
mean_train_loss = np.mean(epoch_train_loss_collector)
Expand All @@ -232,7 +232,7 @@ def _train_model(
val_loss_dict = {
"classification_loss": mean_val_loss,
}
self.save_log_into_tb_file(epoch, "validating", val_loss_dict)
self._save_log_into_tb_file(epoch, "validating", val_loss_dict)

logger.info(
f"epoch {epoch}: "
Expand All @@ -249,7 +249,7 @@ def _train_model(
self.best_model_dict = self.model.state_dict()
self.patience = self.original_patience
# save the model if necessary
self.auto_save_model_if_necessary(
self._auto_save_model_if_necessary(
training_finished=False,
saving_name=f"{self.__class__.__name__}_epoch{epoch}_loss{mean_loss}",
)
Expand Down
24 changes: 2 additions & 22 deletions pypots/classification/brits/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,12 @@

from pypots.classification.base import BaseNNClassifier
from pypots.classification.brits.dataset import DatasetForBRITS
from pypots.classification.brits.modules import RITS
from pypots.imputation.brits.model import (
RITS as imputation_RITS,
_BRITS as imputation_BRITS,
)


class RITS(imputation_RITS):
def __init__(
self,
n_steps: int,
n_features: int,
rnn_hidden_size: int,
n_classes: int,
device: Union[str, torch.device],
):
super().__init__(n_steps, n_features, rnn_hidden_size, device)
self.dropout = nn.Dropout(p=0.25)
self.classifier = nn.Linear(self.rnn_hidden_size, n_classes)

def forward(self, inputs: dict, direction: str = "forward") -> dict:
ret_dict = super().forward(inputs, direction)
logits = self.classifier(ret_dict["final_hidden_state"])
ret_dict["prediction"] = torch.softmax(logits, dim=1)
return ret_dict


class _BRITS(imputation_BRITS, nn.Module):
def __init__(
self,
Expand Down Expand Up @@ -361,7 +341,7 @@ def fit(
self.model.eval() # set the model as eval status to freeze it.

# Step 3: save the model if necessary
self.auto_save_model_if_necessary(training_finished=True)
self._auto_save_model_if_necessary(training_finished=True)

def classify(self, X: Union[dict, str], file_type: str = "h5py"):
"""Classify the input data with the trained model.
Expand Down
35 changes: 35 additions & 0 deletions pypots/classification/brits/modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
PyTorch BRITS model for both the time-series imputation task and the classification task.
"""

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

from typing import Union

import torch
import torch.nn as nn

from pypots.imputation.brits.model import (
RITS as imputation_RITS,
)


class RITS(imputation_RITS):
def __init__(
self,
n_steps: int,
n_features: int,
rnn_hidden_size: int,
n_classes: int,
device: Union[str, torch.device],
):
super().__init__(n_steps, n_features, rnn_hidden_size, device)
self.dropout = nn.Dropout(p=0.25)
self.classifier = nn.Linear(self.rnn_hidden_size, n_classes)

def forward(self, inputs: dict, direction: str = "forward") -> dict:
ret_dict = super().forward(inputs, direction)
logits = self.classifier(ret_dict["final_hidden_state"])
ret_dict["prediction"] = torch.softmax(logits, dim=1)
return ret_dict
Loading
Loading