Skip to content

Commit 9beb711

Browse files
authored
docs: Add example notebooks. (#759)
1 parent 942c1e0 commit 9beb711

10 files changed

+191
-31
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,6 @@ sparse/_version.py
8282

8383
# Benchmark Results
8484
results/
85+
86+
# Notebooks converted to scripts.
87+
docs/examples_ipynb/

docs/css/mkdocstrings.css

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
:root {
2-
--md-primary-fg-color: #c96c08;
3-
--md-primary-fg-color--light: #94f2f7;
4-
--md-primary-fg-color--dark: #335365;
5-
}
1+
:root {
2+
--md-primary-fg-color: #c96c08;
3+
--md-primary-fg-color--light: #94f2f7;
4+
--md-primary-fg-color--dark: #335365;
5+
}
66

7-
.md-tabs__item {
8-
font-weight: bolder;
9-
}
7+
.md-tabs__item {
8+
font-weight: bolder;
9+
}
1010

11-
.grid {
12-
font-weight: bolder;
13-
font-size: 160%;
14-
font-family: Georgia, serif;
15-
}
11+
.grid {
12+
font-weight: bolder;
13+
font-size: 160%;
14+
font-family: Georgia, serif;
15+
}

docs/examples/dask_example.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: light
7+
# format_version: '1.5'
8+
# jupytext_version: 1.16.4
9+
# kernelspec:
10+
# display_name: sparse
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# # Using with Dask
16+
# ## Import
17+
18+
# +
19+
import sparse
20+
21+
import dask.array as da
22+
23+
import numpy as np
24+
25+
# -
26+
27+
# ## Create Arrays
28+
#
29+
# Here, we create two random sparse arrays and move them to Dask.
30+
31+
# +
32+
rng = np.random.default_rng(42)
33+
M, N = 10_000, 10_000
34+
DENSITY = 0.0001
35+
a = sparse.random((M, N), density=DENSITY)
36+
b = sparse.random((M, N), density=DENSITY)
37+
38+
a_dask = da.from_array(a, chunks=1000)
39+
b_dask = da.from_array(b, chunks=1000)
40+
# -
41+
42+
# As we can see in the "data type" section, each chunk of the Dask array is still sparse.
43+
44+
a_dask # noqa: B018
45+
46+
# # Compute and check results
47+
# As we can see, what we get out of Dask matches what we get out of `sparse`.
48+
49+
assert sparse.all(a + b == (a_dask + b_dask).compute())

docs/examples/formats_example.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: light
7+
# format_version: '1.5'
8+
# jupytext_version: 1.16.4
9+
# kernelspec:
10+
# display_name: sparse
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# # Multiple Formats
16+
# ## Import
17+
# Let's set the backend and import `sparse`.
18+
19+
# +
20+
import sparse
21+
22+
import numpy as np
23+
24+
# -
25+
26+
27+
# ## Perform Operations
28+
# Let's create two arrays.
29+
30+
rng = np.random.default_rng(42) # Seed for reproducibility
31+
a = sparse.random((3, 3), density=1 / 6, random_state=rng)
32+
b = sparse.random((3, 3), density=1 / 6, random_state=rng)
33+
34+
# Now let's matrix multiply them.
35+
36+
c = a @ b
37+
38+
# And view the result as a (dense) NumPy array.
39+
40+
c_dense = c.todense()
41+
42+
# Now let's do the same for other formats, and compare the results.
43+
44+
for format in ["coo", "csr", "csc"]:
45+
af = sparse.asarray(a, format=format)
46+
bf = sparse.asarray(b, format=format)
47+
cf = af @ bf
48+
np.testing.assert_array_equal(c_dense, cf.todense())

docs/examples/finch_example.py docs/examples/formats_example_finch.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
# extension: .py
66
# format_name: light
77
# format_version: '1.5'
8-
# jupytext_version: 1.16.3
8+
# jupytext_version: 1.16.4
99
# kernelspec:
1010
# display_name: sparse
1111
# language: python
1212
# name: python3
1313
# ---
1414

15+
# # Multiple Formats with Finch
1516
# ## Import
1617
# Let's set the backend and import `sparse`.
1718

@@ -26,6 +27,7 @@
2627

2728
# -
2829

30+
2931
# ## Perform Operations
3032
# Let's create two arrays.
3133

@@ -39,4 +41,12 @@
3941

4042
# And view the result as a (dense) NumPy array.
4143

42-
c.todense()
44+
c_dense = c.todense()
45+
46+
# Now let's do the same for other formats, and compare the results.
47+
48+
for format in ["coo", "csr", "csc", "dense"]:
49+
af = sparse.asarray(a, format=format)
50+
bf = sparse.asarray(b, format=format)
51+
cf = af @ bf
52+
np.testing.assert_array_equal(c_dense, cf.todense())

docs/examples/scipy_example.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: light
7+
# format_version: '1.5'
8+
# jupytext_version: 1.16.4
9+
# kernelspec:
10+
# display_name: sparse
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# # Using with SciPy
16+
# ## Import
17+
18+
# +
19+
import sparse
20+
21+
import numpy as np
22+
import scipy.sparse as sps
23+
24+
# -
25+
26+
# ## Create Arrays
27+
28+
rng = np.random.default_rng(42)
29+
M = 1_000
30+
DENSITY = 0.01
31+
a = sparse.random((M, M), density=DENSITY, format="csc")
32+
identity = sparse.eye(M, format="csc")
33+
34+
# ## Invert and verify matrix
35+
# This showcases the `scipy.sparse.linalg` integration.
36+
37+
a_inv = sps.linalg.spsolve(a, identity)
38+
np.testing.assert_array_almost_equal((a_inv @ a).todense(), identity.todense())
39+
40+
# ## Calculate the graph distances
41+
# This showcases the `scipy.sparse.csgraph` integration.
42+
43+
sps.csgraph.bellman_ford(sparse.eye(5, k=1) + sparse.eye(5, k=-1), return_predecessors=False)

docs/javascripts/katex.js

-10
This file was deleted.

docs/js/katex.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
document$.subscribe(({ body }) => {
2+
renderMathInElement(body, {
3+
delimiters: [
4+
{ left: "$$", right: "$$", display: true },
5+
{ left: "$", right: "$", display: false },
6+
{ left: "\\(", right: "\\)", display: false },
7+
{ left: "\\[", right: "\\]", display: true },
8+
],
9+
});
10+
});

mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ markdown_extensions:
3737
- md_in_html # Used for grids
3838

3939
extra_javascript:
40-
- javascripts/katex.js
40+
- js/katex.js
4141
- https://unpkg.com/katex@0/dist/katex.min.js
4242
- https://unpkg.com/katex@0/dist/contrib/auto-render.min.js
4343

pyproject.toml

+12-5
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@ docs = [
3535
"mkdocs-literate-nav",
3636
"mkdocs-section-index",
3737
"mkdocs-jupyter",
38-
"scipy",
39-
"sparse[finch]",
38+
"sparse[extras]",
4039
]
41-
tests = [
40+
extras = [
4241
"dask[array]",
42+
"sparse[finch]",
43+
"scipy",
44+
"scikit-learn",
45+
]
46+
tests = [
47+
"sparse[extras]",
4348
"pytest>=3.5",
4449
"pytest-cov",
4550
"pytest-xdist",
4651
"pre-commit",
47-
"scipy",
48-
"sparse[finch]",
4952
"pytest-codspeed",
5053
]
5154
tox = ["sparse[tests]", "tox"]
@@ -93,3 +96,7 @@ section-order = [
9396
"numpy",
9497
"local-folder",
9598
]
99+
100+
[tool.jupytext.formats]
101+
"docs/examples_ipynb/" = "ipynb"
102+
"docs/examples/" = "py:light"

0 commit comments

Comments
 (0)