Skip to content
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
43 changes: 32 additions & 11 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application
name: Tests and coverage

on:
push:
branches: [master]
pull_request:
branches: [master]
workflow_dispatch:

jobs:
lint_and_format_check_with_ruff:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v5

- name: Set up Python 3.12
uses: actions/setup-python@v6
with:
python-version: "3.12"

- uses: astral-sh/ruff-action@v3
with:
args: "format --check"

- uses: astral-sh/ruff-action@v3
with:
args: "check"

test:
runs-on: ubuntu-latest
strategy:
Expand All @@ -22,20 +42,21 @@ jobs:
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
pip install -e ".[testing]"
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

- name: Test with pytest
run: |
pytest --cov persim
pytest --cov=persim --cov-report=xml

- name: Upload coverage results
run: |
bash <(curl -s https://codecov.io/bash)
if: matrix.python-version == '3.12'
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: persim
verbose: true
fail_ci_if_error: true
6 changes: 3 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# -*- coding: utf-8 -*-
import os
import sys
from importlib import metadata

sys.path.insert(0, os.path.abspath("."))
from sktda_docs_config import *

from persim import __version__

project = "Persim"
copyright = "2019, Nathaniel Saul"
author = "Nathaniel Saul"

version = __version__
release = __version__
version = metadata.version("persim")
release = metadata.version("persim")

language = "en"

Expand Down
39 changes: 23 additions & 16 deletions docs/notebooks/Classification with persistence images.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"import matplotlib.pyplot as plt\n",
"\n",
"from ripser import Rips\n",
"from persim import PersImage\n",
"from persim import PersistenceImager"
]
},
Expand All @@ -51,17 +50,23 @@
"N_per_class = int(N / 2)\n",
"N_in_class = 400\n",
"\n",
"\n",
"def noise(N, scale):\n",
" return scale * np.random.random((N, 2))\n",
"\n",
"\n",
"def circle(N, scale, offset):\n",
" return offset + scale * datasets.make_circles(n_samples=N, factor=0.4, noise=0.05)[0]\n",
" \n",
" return (\n",
" offset + scale * datasets.make_circles(n_samples=N, factor=0.4, noise=0.05)[0]\n",
" )\n",
"\n",
"\n",
"just_noise = [noise(N_in_class, 150) for _ in range(N_per_class)]\n",
"\n",
"half = int(N_in_class / 2)\n",
"with_circle = [np.concatenate((circle(half, 50, 70), noise(half, 150)))\n",
" for _ in range(N_per_class)]\n",
"with_circle = [\n",
" np.concatenate((circle(half, 50, 70), noise(half, 150))) for _ in range(N_per_class)\n",
"]\n",
"\n",
"datas = []\n",
"datas.extend(just_noise)\n",
Expand Down Expand Up @@ -93,17 +98,17 @@
"source": [
"# Visualize the data\n",
"fig, axs = plt.subplots(1, 2)\n",
"fig.set_size_inches(10,5)\n",
"fig.set_size_inches(10, 5)\n",
"\n",
"xs, ys = just_noise[0][:,0], just_noise[0][:,1]\n",
"xs, ys = just_noise[0][:, 0], just_noise[0][:, 1]\n",
"axs[0].scatter(xs, ys)\n",
"axs[0].set_title(\"Example noise dataset\")\n",
"axs[0].set_aspect('equal', 'box')\n",
"axs[0].set_aspect(\"equal\", \"box\")\n",
"\n",
"xs_, ys_ = with_circle[0][:,0], with_circle[0][:,1]\n",
"xs_, ys_ = with_circle[0][:, 0], with_circle[0][:, 1]\n",
"axs[1].scatter(xs_, ys_)\n",
"axs[1].set_title(\"Example noise with circle dataset\")\n",
"axs[1].set_aspect('equal', 'box')\n",
"axs[1].set_aspect(\"equal\", \"box\")\n",
"\n",
"fig.tight_layout()"
]
Expand Down Expand Up @@ -155,7 +160,7 @@
}
],
"source": [
"plt.figure(figsize=(12,6))\n",
"plt.figure(figsize=(12, 6))\n",
"plt.subplot(121)\n",
"\n",
"rips.plot(diagrams_h1[0], show=False)\n",
Expand Down Expand Up @@ -234,16 +239,16 @@
}
],
"source": [
"plt.figure(figsize=(15,7.5))\n",
"plt.figure(figsize=(15, 7.5))\n",
"\n",
"for i in range(4):\n",
" ax = plt.subplot(240+i+1)\n",
" ax = plt.subplot(240 + i + 1)\n",
" pimgr.plot_image(imgs[i], ax)\n",
" plt.title(\"PI of $H_1$ for noise\")\n",
"\n",
"for i in range(4):\n",
" ax = plt.subplot(240+i+5)\n",
" pimgr.plot_image(imgs[-(i+1)], ax)\n",
" ax = plt.subplot(240 + i + 5)\n",
" pimgr.plot_image(imgs[-(i + 1)], ax)\n",
" plt.title(\"PI of $H_1$ for circle w/ noise\")"
]
},
Expand All @@ -260,7 +265,9 @@
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(imgs_array, labels, test_size=0.40, random_state=42)"
"X_train, X_test, y_train, y_test = train_test_split(\n",
" imgs_array, labels, test_size=0.40, random_state=42\n",
")"
]
},
{
Expand Down
76 changes: 44 additions & 32 deletions docs/notebooks/Differentiation with Persistence Landscapes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import random\n",
"\n",
"from ripser import ripser\n",
"from persim.landscapes import (\n",
" PersLandscapeApprox, \n",
" average_approx, \n",
" snap_pl, \n",
" plot_landscape, \n",
" plot_landscape_simple\n",
" PersLandscapeApprox,\n",
" average_approx,\n",
" snap_pl,\n",
" plot_landscape,\n",
" plot_landscape_simple,\n",
")\n",
"from tadasets import dsphere"
]
Expand Down Expand Up @@ -100,15 +99,19 @@
"sph3_pl1 = []\n",
"\n",
"for _ in range(num_runs):\n",
" sph2 = dsphere(n=num_pts, d=2)/1.3333 # sample points, scaling appropriately\n",
" sph2_dgm = ripser(sph2, maxdim=2)['dgms'] # compute PH0, PH1, PH2\n",
" sph2_pl = PersLandscapeApprox(dgms=sph2_dgm, hom_deg=1) # compute persistence landscape\n",
" sph2 = dsphere(n=num_pts, d=2) / 1.3333 # sample points, scaling appropriately\n",
" sph2_dgm = ripser(sph2, maxdim=2)[\"dgms\"] # compute PH0, PH1, PH2\n",
" sph2_pl = PersLandscapeApprox(\n",
" dgms=sph2_dgm, hom_deg=1\n",
" ) # compute persistence landscape\n",
" sph2_pl1.append(sph2_pl)\n",
" \n",
" sph3 = dsphere(n=num_pts, d=3)/1.3581 # sample points, scaling appropriately\n",
" sph3_dgm = ripser(sph3, maxdim=2)['dgms'] # compute PH0, PH1, PH2\n",
" sph3_pl = PersLandscapeApprox(dgms=sph3_dgm, hom_deg=1) # compute persistence landscape\n",
" sph3_pl1.append(sph3_pl)\n"
"\n",
" sph3 = dsphere(n=num_pts, d=3) / 1.3581 # sample points, scaling appropriately\n",
" sph3_dgm = ripser(sph3, maxdim=2)[\"dgms\"] # compute PH0, PH1, PH2\n",
" sph3_pl = PersLandscapeApprox(\n",
" dgms=sph3_dgm, hom_deg=1\n",
" ) # compute persistence landscape\n",
" sph3_pl1.append(sph3_pl)"
]
},
{
Expand Down Expand Up @@ -138,7 +141,7 @@
"source": [
"avg_sph2 = average_approx(sph2_pl1)\n",
"avg_sph3 = average_approx(sph3_pl1)\n",
"print(avg_sph2, '\\n') \n",
"print(avg_sph2, \"\\n\")\n",
"print(avg_sph3)"
]
},
Expand Down Expand Up @@ -169,7 +172,7 @@
"true_diff_pl = avg_sph2_snapped - avg_sph3_snapped\n",
"significance = true_diff_pl.sup_norm()\n",
"\n",
"print(f'The threshold for significance is {significance}.')"
"print(f\"The threshold for significance is {significance}.\")"
]
},
{
Expand Down Expand Up @@ -201,9 +204,11 @@
],
"source": [
"# view figures in notebook\n",
"# %matplotlib inline \n",
"# %matplotlib inline\n",
"\n",
"plot_landscape_simple(avg_sph2_snapped,title='Average $PL_1$ for $S^2$.',depth_range=range(13))"
"plot_landscape_simple(\n",
" avg_sph2_snapped, title=\"Average $PL_1$ for $S^2$.\", depth_range=range(13)\n",
")"
]
},
{
Expand All @@ -226,7 +231,9 @@
}
],
"source": [
"plot_landscape_simple(avg_sph3_snapped,title='Average $PL_1$ for $S^2$.',depth_range=range(13))"
"plot_landscape_simple(\n",
" avg_sph3_snapped, title=\"Average $PL_1$ for $S^2$.\", depth_range=range(13)\n",
")"
]
},
{
Expand All @@ -249,7 +256,9 @@
}
],
"source": [
"plot_landscape_simple(true_diff_pl,title='Difference of average PLs',depth_range=range(13))"
"plot_landscape_simple(\n",
" true_diff_pl, title=\"Difference of average PLs\", depth_range=range(13)\n",
")"
]
},
{
Expand Down Expand Up @@ -280,7 +289,7 @@
}
],
"source": [
"plot_landscape(true_diff_pl,title='Difference of average PLs')"
"plot_landscape(true_diff_pl, title=\"Difference of average PLs\")"
]
},
{
Expand Down Expand Up @@ -310,24 +319,27 @@
"sig_count = 0\n",
"\n",
"for shuffle in range(num_perms):\n",
" A_indices = random.sample(range(2*num_runs),num_runs)\n",
" B_indices = [_ for _ in range(2*num_runs) if _ not in A_indices]\n",
" \n",
" A_indices = random.sample(range(2 * num_runs), num_runs)\n",
" B_indices = [_ for _ in range(2 * num_runs) if _ not in A_indices]\n",
"\n",
" A_pl = [comb_pl[i] for i in A_indices]\n",
" B_pl = [comb_pl[j] for j in B_indices]\n",
" \n",
"\n",
" A_avg = average_approx(A_pl)\n",
" B_avg = average_approx(B_pl)\n",
" [A_avg_sn, B_avg_sn] = snap_pl([A_avg,B_avg])\n",
" \n",
" [A_avg_sn, B_avg_sn] = snap_pl([A_avg, B_avg])\n",
"\n",
" shuff_diff = A_avg_sn - B_avg_sn\n",
" if (shuff_diff.sup_norm() >= significance): sig_count += 1\n",
" if shuff_diff.sup_norm() >= significance:\n",
" sig_count += 1\n",
"\n",
"pval = sig_count/num_perms\n",
"pval = sig_count / num_perms\n",
"\n",
"print(f'There were {sig_count} shuffles out of {num_perms} that',\n",
" 'were more significant than the true labelling. Thus, the',\n",
" f'p-value is {pval}.')"
"print(\n",
" f\"There were {sig_count} shuffles out of {num_perms} that\",\n",
" \"were more significant than the true labelling. Thus, the\",\n",
" f\"p-value is {pval}.\",\n",
")"
]
},
{
Expand Down
Loading