From eb68c5d3556ccf7a2102b542057fc1c447a0f608 Mon Sep 17 00:00:00 2001 From: mmRoshani Date: Sat, 21 Dec 2024 00:48:39 +0330 Subject: [PATCH] DOC[app]: github pages --- .github/workflows/docs.yml | 35 ++++++++++++++++ .gitignore | 8 ++++ Makefile | 3 +- README.md | 1 + docs/Makefile | 20 +++++++++ docs/conf.py | 42 +++++++++++++++++++ docs/index.rst | 20 +++++++++ docs/make.bat | 35 ++++++++++++++++ docs/modules.rst | 8 ++++ docs/safe_pfl_distance.rst | 7 ++++ docs/utils.rst | 4 ++ .../__init__.py | 0 safe_pfl_distance/cosine.py | 6 +++ safe_pfl_distance/euclidean.py | 7 ++++ safe_pfl_distance/jensen_shannon.py | 15 +++++++ safe_pfl_distance/safe_pfl_distance.py | 0 safe_pfl_distance/utils/model_loader.py | 26 ++++++++++++ safe_pfl_distance/wasserstein.py | 7 ++++ 18 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/docs.yml create mode 100644 docs/Makefile create mode 100644 docs/conf.py create mode 100644 docs/index.rst create mode 100644 docs/make.bat create mode 100644 docs/modules.rst create mode 100644 docs/safe_pfl_distance.rst create mode 100644 docs/utils.rst rename {safe-pfl-distance => safe_pfl_distance}/__init__.py (100%) create mode 100644 safe_pfl_distance/cosine.py create mode 100644 safe_pfl_distance/euclidean.py create mode 100644 safe_pfl_distance/jensen_shannon.py create mode 100644 safe_pfl_distance/safe_pfl_distance.py create mode 100644 safe_pfl_distance/utils/model_loader.py create mode 100644 safe_pfl_distance/wasserstein.py diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..8f92e1e --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,35 @@ +name: docs +on: + push: + branches: + - main + +jobs: + docs: + name: Docs + runs-on: ubuntu-latest + steps: + + - uses: actions/checkout@v2 + + - name: Install Python + uses: actions/setup-python@v1 + with: + python-version: 3.8 + + - name: Install requirements + run: | + pip3 install sphinx-rtd-theme + + - name: Build docs + run: | + cd docs + make html + # https://github.com/peaceiris/actions-gh-pages + - name: Deploy + if: success() + uses: peaceiris/actions-gh-pages@v3 + with: + publish_branch: gh-pages + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: docs/_build/html/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 15201ac..91f88fe 100644 --- a/.gitignore +++ b/.gitignore @@ -169,3 +169,11 @@ cython_debug/ # PyPI configuration file .pypirc + + +.vscode/ +.vscode/* + +docs/_build/ +docs/_static/ +docs/_templates \ No newline at end of file diff --git a/Makefile b/Makefile index d3d6b78..3ee2d8e 100644 --- a/Makefile +++ b/Makefile @@ -44,4 +44,5 @@ download: download-clean: pip download --no-deps safe-pfl-distance - +doc-generate: + @cd docs && make clean && make html \ No newline at end of file diff --git a/README.md b/README.md index 09207f3..35967c1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # SAFE-PFL distance calculator ![Python](https://img.shields.io/badge/python-3.12-blue.svg) +![Docs](https://github.com/safe-pfl/distances/workflows/docs/badge.svg) [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsafe-pfl%2Fdistances.svg?type=shield&issueType=license)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsafe-pfl%2Fdistances?ref=badge_shield&issueType=license) [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsafe-pfl%2Fdistances.svg?type=shield&issueType=security)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsafe-pfl%2Fdistances?ref=badge_shield&issueType=security) diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..d4bb2cb --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..beeaaad --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,42 @@ +# Configuration file for the Sphinx documentation builder. +# +# For the full list of built-in configuration values, see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Project information ----------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information + + +import os +import sys + +# Add the root directory of the project to the Python path +sys.path.insert(0, os.path.abspath("../")) + +project = "safe-pfl-distance" +copyright = "2024, MohammadMojtaba Roshani" +author = "MohammadMojtaba Roshani" + +# -- General configuration --------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration + +extensions = [ + "sphinx.ext.duration", + "sphinx.ext.doctest", + "sphinx.ext.autodoc", + "sphinx.ext.autosummary", +] +templates_path = ["_templates"] +exclude_patterns = [ + "_build", + "Thumbs.db", +] + + +autodoc_mock_imports = ["external_library"] + +# -- Options for HTML output ------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output + +html_theme = "sphinx_rtd_theme" +html_static_path = ["_static"] diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..908c372 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,20 @@ +.. safe-pfl-distance documentation master file, created by + sphinx-quickstart on Sat Dec 21 00:30:53 2024. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to the safe-pfl-distance Documentation +============================ + +.. toctree:: + :maxdepth: 2 + :caption: Contents: + + modules + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` \ No newline at end of file diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..32bb245 --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=. +set BUILDDIR=_build + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "" goto help + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/docs/modules.rst b/docs/modules.rst new file mode 100644 index 0000000..6c18307 --- /dev/null +++ b/docs/modules.rst @@ -0,0 +1,8 @@ +Modules +======= + +.. toctree:: + :maxdepth: 2 + + safe_pfl_distance + utils \ No newline at end of file diff --git a/docs/safe_pfl_distance.rst b/docs/safe_pfl_distance.rst new file mode 100644 index 0000000..6940172 --- /dev/null +++ b/docs/safe_pfl_distance.rst @@ -0,0 +1,7 @@ +Safe PFL Distance Module +========================= + +.. automodule:: safe_pfl_distance + :members: + :undoc-members: + :show-inheritance: \ No newline at end of file diff --git a/docs/utils.rst b/docs/utils.rst new file mode 100644 index 0000000..c2fd32f --- /dev/null +++ b/docs/utils.rst @@ -0,0 +1,4 @@ +Utils Module +============ + +.. autofunction:: safe_pfl_distance.utils.model_loader.load_model \ No newline at end of file diff --git a/safe-pfl-distance/__init__.py b/safe_pfl_distance/__init__.py similarity index 100% rename from safe-pfl-distance/__init__.py rename to safe_pfl_distance/__init__.py diff --git a/safe_pfl_distance/cosine.py b/safe_pfl_distance/cosine.py new file mode 100644 index 0000000..3a0ee69 --- /dev/null +++ b/safe_pfl_distance/cosine.py @@ -0,0 +1,6 @@ +from scipy.spatial.distance import cosine + + +def cosine_distance(u, v): + distance = cosine(u, v) + return format(distance, ".2f") diff --git a/safe_pfl_distance/euclidean.py b/safe_pfl_distance/euclidean.py new file mode 100644 index 0000000..e0c3e19 --- /dev/null +++ b/safe_pfl_distance/euclidean.py @@ -0,0 +1,7 @@ +import numpy as np + + +def euclidean_distance(u, v): + distance = np.linalg.norm(u - v) + distance = distance * 10**4 + return format(distance, ".2f") diff --git a/safe_pfl_distance/jensen_shannon.py b/safe_pfl_distance/jensen_shannon.py new file mode 100644 index 0000000..f1f287e --- /dev/null +++ b/safe_pfl_distance/jensen_shannon.py @@ -0,0 +1,15 @@ +import numpy as np +from scipy.spatial.distance import jensenshannon + + +def jensen_shannon_distance_func(u, v): + u = np.abs(u) + v = np.abs(v) + u /= np.sum(u) + v /= np.sum(v) + epsilon = 1e-12 + u = np.clip(u, epsilon, None) + v = np.clip(v, epsilon, None) + distance = jensenshannon(u, v) + distance = distance * 10 + return round(distance, 2) diff --git a/safe_pfl_distance/safe_pfl_distance.py b/safe_pfl_distance/safe_pfl_distance.py new file mode 100644 index 0000000..e69de29 diff --git a/safe_pfl_distance/utils/model_loader.py b/safe_pfl_distance/utils/model_loader.py new file mode 100644 index 0000000..fa47275 --- /dev/null +++ b/safe_pfl_distance/utils/model_loader.py @@ -0,0 +1,26 @@ +import torch + + +def load_model(node_id: int, model_type: str, model_path_prefix: str = "./models"): + """ + Loads a PyTorch model from a specified file path. + + Args: + node_id (int): Identifier for the specific model node. + model_type (str): Type of the model (e.g., "classification", "regression"). + model_path_prefix (str, optional): Base directory where model files are stored. Defaults to "./models". + + Returns: + torch.nn.Module or None: The loaded PyTorch model if successful, otherwise None. + + Raises: + ValueError: If input parameters are invalid. + """ + try: + model_path = f"{model_path_prefix}/{model_type}/node_{node_id}.pth" + model = torch.load(model_path) + print(f"Model loaded successfully from {model_path}") + return model + except Exception as e: + print(f"Error loading model at node {node_id}: {e}") + return None diff --git a/safe_pfl_distance/wasserstein.py b/safe_pfl_distance/wasserstein.py new file mode 100644 index 0000000..4682aca --- /dev/null +++ b/safe_pfl_distance/wasserstein.py @@ -0,0 +1,7 @@ +from scipy.stats import wasserstein_distance + + +def wasserstein_distance_func(u, v): + distance = wasserstein_distance(u, v) + distance = distance * 10 + return round(distance, 2)