Skip to content

Commit

Permalink
DOC[app]: github pages
Browse files Browse the repository at this point in the history
  • Loading branch information
mmRoshani committed Dec 20, 2024
1 parent 88f6f62 commit eb68c5d
Show file tree
Hide file tree
Showing 18 changed files with 243 additions and 1 deletion.
35 changes: 35 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -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/
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@ cython_debug/

# PyPI configuration file
.pypirc


.vscode/
.vscode/*

docs/_build/
docs/_static/
docs/_templates
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ download:
download-clean:
pip download --no-deps safe-pfl-distance


doc-generate:
@cd docs && make clean && make html
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -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"]
20 changes: 20 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -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`
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions docs/modules.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Modules
=======

.. toctree::
:maxdepth: 2

safe_pfl_distance
utils
7 changes: 7 additions & 0 deletions docs/safe_pfl_distance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Safe PFL Distance Module
=========================

.. automodule:: safe_pfl_distance
:members:
:undoc-members:
:show-inheritance:
4 changes: 4 additions & 0 deletions docs/utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Utils Module
============

.. autofunction:: safe_pfl_distance.utils.model_loader.load_model
File renamed without changes.
6 changes: 6 additions & 0 deletions safe_pfl_distance/cosine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from scipy.spatial.distance import cosine


def cosine_distance(u, v):
distance = cosine(u, v)
return format(distance, ".2f")
7 changes: 7 additions & 0 deletions safe_pfl_distance/euclidean.py
Original file line number Diff line number Diff line change
@@ -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")
15 changes: 15 additions & 0 deletions safe_pfl_distance/jensen_shannon.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
26 changes: 26 additions & 0 deletions safe_pfl_distance/utils/model_loader.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions safe_pfl_distance/wasserstein.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit eb68c5d

Please sign in to comment.