Skip to content

Commit 561b6f7

Browse files
authored
Merge pull request #206 from lucasimi/generate-notebook-output
Generate notebook output
2 parents ecbe895 + d2ed6d8 commit 561b6f7

File tree

7 files changed

+240
-556
lines changed

7 files changed

+240
-556
lines changed

.readthedocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ build:
1414
# nodejs: "19"
1515
# rust: "1.64"
1616
# golang: "1.19"
17+
apt_packages:
18+
- pandoc
19+
1720

1821
# Build documentation in the "docs/" directory with Sphinx
1922
sphinx:

docs/requirements.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
sphinx_rtd_theme
2-
nbsphinx
2+
nbsphinx
3+
jupyter
4+
pandoc
5+
jupytext
6+
pandas
7+
scikit-learn<1.6.0
8+
tda-mapper

docs/source/conf.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,71 @@
33
# For the full list of built-in configuration values, see the documentation:
44
# https://www.sphinx-doc.org/en/master/usage/configuration.html
55

6+
# Notebook preprocessing
7+
import glob
8+
import os
9+
import subprocess
10+
11+
NOTEBOOK_DIR = os.path.join(os.path.dirname(__file__), "notebooks")
12+
13+
14+
def convert_py_to_ipynb():
15+
py_files = glob.glob(os.path.join(NOTEBOOK_DIR, "*.py"))
16+
for py_file in py_files:
17+
ipynb_file = py_file.replace(".py", ".ipynb")
18+
subprocess.run(
19+
["jupytext", "--to", "ipynb", "--output", ipynb_file, py_file], check=True
20+
)
21+
subprocess.run(
22+
[
23+
"jupyter",
24+
"nbconvert",
25+
"--to",
26+
"notebook",
27+
"--execute",
28+
"--inplace",
29+
ipynb_file,
30+
],
31+
check=True,
32+
)
33+
34+
35+
convert_py_to_ipynb()
36+
637
# -- Project information -----------------------------------------------------
738
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
839

9-
project = 'tda-mapper'
10-
copyright = '2024, Luca Simi'
11-
author = 'Luca Simi'
40+
project = "tda-mapper"
41+
copyright = "2024, Luca Simi"
42+
author = "Luca Simi"
1243

1344
# -- General configuration ---------------------------------------------------
1445
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
1546

16-
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx_rtd_theme', 'nbsphinx']
47+
extensions = [
48+
"sphinx.ext.autodoc",
49+
"sphinx.ext.viewcode",
50+
"sphinx_rtd_theme",
51+
"nbsphinx",
52+
]
1753

18-
templates_path = ['_templates']
54+
templates_path = ["_templates"]
1955
exclude_patterns = []
2056

2157

2258
# -- Options for HTML output -------------------------------------------------
2359
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
2460

25-
html_theme = 'sphinx_rtd_theme'
26-
html_logo = 'logos/tda-mapper-logo-horizontal.png'
61+
html_theme = "sphinx_rtd_theme"
62+
html_logo = "logos/tda-mapper-logo-horizontal.png"
2763
html_theme_options = {
28-
'sticky_navigation': True,
29-
'vcs_pageview_mode': 'blob',
64+
"sticky_navigation": True,
65+
"vcs_pageview_mode": "blob",
3066
}
3167
html_context = {
32-
'display_github': True,
33-
'github_user': 'lucasimi',
34-
'github_repo': 'tda-mapper-python',
35-
'github_version': 'main',
36-
'conf_py_path': '/docs/source/',
68+
"display_github": True,
69+
"github_user": "lucasimi",
70+
"github_repo": "tda-mapper-python",
71+
"github_version": "main",
72+
"conf_py_path": "/docs/source/",
3773
}

docs/source/notebooks/circles_online.ipynb

Lines changed: 0 additions & 275 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.17.0
9+
# kernelspec:
10+
# display_name: default
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %% [markdown]
16+
# # Circles dataset
17+
18+
# %%
19+
import numpy as np
20+
21+
from matplotlib import pyplot as plt
22+
23+
from sklearn.datasets import make_circles
24+
from sklearn.decomposition import PCA
25+
from sklearn.cluster import DBSCAN
26+
27+
from tdamapper.learn import MapperAlgorithm
28+
from tdamapper.cover import CubicalCover
29+
from tdamapper.plot import MapperPlot
30+
31+
X, y = make_circles( # load a labelled dataset
32+
n_samples=5000,
33+
noise=0.05,
34+
factor=0.3,
35+
random_state=42
36+
)
37+
lens = PCA(2, random_state=42).fit_transform(X)
38+
39+
plt.scatter(lens[:, 0], lens[:, 1], c=y, cmap='jet')
40+
41+
# %% [markdown]
42+
# ### Build Mapper graph
43+
44+
# %%
45+
mapper_algo = MapperAlgorithm(
46+
cover=CubicalCover(
47+
n_intervals=10,
48+
overlap_frac=0.3
49+
),
50+
clustering=DBSCAN()
51+
)
52+
53+
mapper_graph = mapper_algo.fit_transform(X, lens)
54+
55+
# %% [markdown]
56+
# ### Plot Mapper graph with mean
57+
58+
# %%
59+
mapper_plot = MapperPlot(
60+
mapper_graph,
61+
dim=2,
62+
iterations=60,
63+
seed=42
64+
)
65+
66+
fig = mapper_plot.plot_plotly(
67+
colors=y, # color according to categorical values
68+
cmap='jet', # Jet colormap, for classes
69+
agg=np.nanmean, # aggregate on nodes according to mean
70+
width=600,
71+
height=600
72+
)
73+
74+
fig.show(
75+
renderer='notebook_connected',
76+
config={'scrollZoom': True}
77+
)
78+
79+
# %%
80+
mapper_plot.plot_plotly_update(
81+
fig, # update the old figure
82+
colors=y,
83+
cmap='viridis', # viridis colormap, for ranges
84+
agg=np.nanstd # aggregate on nodes according to std
85+
)
86+
87+
fig.show(
88+
renderer='notebook_connected',
89+
config={'scrollZoom': True}
90+
)

docs/source/notebooks/digits_online.ipynb

Lines changed: 0 additions & 266 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.17.0
9+
# kernelspec:
10+
# display_name: Python 3 (ipykernel)
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %% [markdown]
16+
# # Digits dataset
17+
18+
# %%
19+
import numpy as np
20+
21+
from sklearn.datasets import load_digits
22+
from sklearn.cluster import AgglomerativeClustering
23+
from sklearn.decomposition import PCA
24+
25+
from tdamapper.learn import MapperAlgorithm
26+
from tdamapper.cover import CubicalCover
27+
from tdamapper.clustering import FailSafeClustering
28+
from tdamapper.plot import MapperPlot
29+
30+
31+
X, y = load_digits(return_X_y=True) # We load a labelled dataset
32+
lens = PCA(2, random_state=42).fit_transform(X) # We compute the lens values
33+
34+
# %% [markdown]
35+
# ### Build Mapper graph
36+
37+
# %%
38+
mapper_algo = MapperAlgorithm(
39+
cover=CubicalCover(
40+
n_intervals=10,
41+
overlap_frac=0.65
42+
),
43+
clustering=AgglomerativeClustering(10),
44+
verbose=False
45+
)
46+
47+
mapper_graph = mapper_algo.fit_transform(X, lens)
48+
49+
# %% [markdown]
50+
# ### Plot Mapper graph with mean
51+
52+
# %%
53+
mapper_plot = MapperPlot(
54+
mapper_graph,
55+
dim=2,
56+
iterations=400,
57+
seed=42
58+
)
59+
60+
fig = mapper_plot.plot_plotly(
61+
colors=y, # We color according to digit values
62+
cmap='jet', # Jet colormap, used for classes
63+
agg=np.nanmean, # We aggregate on graph nodes according to mean
64+
title='digit (mean)',
65+
width=600,
66+
height=600
67+
)
68+
69+
fig.show(
70+
renderer='notebook_connected',
71+
config={'scrollZoom': True}
72+
)
73+
74+
# %% [markdown]
75+
# ### Plot Mapper graph with standard deviation
76+
77+
# %%
78+
fig = mapper_plot.plot_plotly(
79+
colors=y,
80+
cmap='viridis', # Viridis colormap, used for ranges
81+
agg=np.nanstd, # We aggregate on graph nodes according to std
82+
title='digit (std)',
83+
width=600,
84+
height=600
85+
)
86+
87+
fig.show(
88+
renderer='notebook_connected',
89+
config={'scrollZoom': True}
90+
)

0 commit comments

Comments
 (0)