Skip to content

Commit 26fb52b

Browse files
committed
Add documentation and examples.
1 parent bd288f5 commit 26fb52b

11 files changed

Lines changed: 622 additions & 0 deletions

docs/_config.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Book settings
2+
# Learn more at https://jupyterbook.org/customize/config.html
3+
4+
title: ""
5+
author: the brainplotlib developers and contributors
6+
logo: brainplotlib_logo.png
7+
8+
# Force re-execution of notebooks on each build.
9+
# See https://jupyterbook.org/content/execute.html
10+
execute:
11+
execute_notebooks: cache
12+
13+
# Define the name of the latex output file for PDF builds
14+
latex:
15+
latex_documents:
16+
targetname: book.tex
17+
18+
# Add a bibtex file so that we can create citations
19+
# bibtex_bibfiles:
20+
# - references.bib
21+
22+
# Information about where the book exists on the web
23+
repository:
24+
url: https://github.com/feilong/brainplotlib # Online location of your book
25+
path_to_book: docs # Optional path to your book, relative to the repository root
26+
branch: master # Which branch of the repository should be used when creating links (optional)
27+
28+
# Add GitHub buttons to your book
29+
# See https://jupyterbook.org/customize/config.html#add-a-link-to-your-repository
30+
html:
31+
use_issues_button: true
32+
use_repository_button: true
33+
34+
parse:
35+
myst_substitutions:
36+
gallery: |
37+
````{panels}
38+
:column: col-4
39+
40+
<a href="/examples/save_image.html">
41+
Save the high-res image
42+
^^^
43+
```{glue:} save_image
44+
```
45+
</a>
46+
47+
---
48+
49+
<a href="/examples/plot_colorbar.html">
50+
Plot a colorbar
51+
^^^
52+
```{glue:} with_colorbar
53+
```
54+
</a>
55+
56+
---
57+
58+
<a href="/examples/different_cmaps.html">
59+
Different colormaps
60+
^^^
61+
```{glue:} different_cmaps
62+
```
63+
</a>
64+
65+
````
66+
````{panels}
67+
:column: col-4
68+
69+
<a href="/examples/different_resolutions.html">
70+
Different data resolutions
71+
^^^
72+
```{glue:} different_resolutions
73+
```
74+
</a>
75+
76+
---
77+
78+
<a href="/examples/cortical_masks.html">
79+
Handling cortical masks
80+
^^^
81+
```{glue:} cortical_masks
82+
```
83+
</a>
84+
85+
---
86+
87+
<a href="/examples/different_surfaces.html">
88+
Alternative surface types
89+
^^^
90+
```{glue:} different_surfaces
91+
```
92+
</a>
93+
94+
````
95+
gallery_link : |
96+
---
97+
[**<< Go back to the gallery of examples**](/examples/index)

docs/_toc.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Table of contents
2+
# Learn more at https://jupyterbook.org/customize/toc.html
3+
4+
format: jb-book
5+
root: intro
6+
parts:
7+
- caption: Examples
8+
chapters:
9+
- file: examples/index
10+
- file: examples/save_image
11+
- file: examples/plot_colorbar
12+
- file: examples/different_cmaps
13+
- file: examples/different_resolutions
14+
- file: examples/cortical_masks
15+
- file: examples/different_surfaces

docs/brainplotlib_logo.png

22.1 KB
Loading

docs/examples/cortical_masks.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
jupytext:
3+
cell_metadata_filter: -all
4+
formats: md:myst
5+
text_representation:
6+
extension: .md
7+
format_name: myst
8+
format_version: 0.13
9+
jupytext_version: 1.11.5
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
---
15+
16+
# Handling cortical masks
17+
18+
Not every vertex on surface belongs to the cerebral cortex.
19+
There are approximately 8.5% of vertices around the medial wall that are non-cortical.
20+
When analyzing fMRI data on surface, we usually want to focus on vertices of the cerebral cortex and mask out non-cortical vertices.
21+
This masking step changes the number of vertices and the shape of the data.
22+
23+
```{margin} Different surface spaces
24+
If you use the cortical mask of the `fsaverage5` or `fsaverage6` surface instead of `fsaverage`, the number of cortical vertices would slightly differ.
25+
```
26+
For example, with the `icoorder5` resolution `fsaverage` surface there are 10242 vertices per hemisphere in total.
27+
After masking out the non-cortical vertices, there are 9372 and 9370 vertices for the left and right hemispheres, respectively.
28+
29+
The `brain_plot` function automatically detects whether the data has been masked or not and render it accordingly.
30+
31+
```{glue:} cortical_masks
32+
```
33+
34+
```{code-cell}python
35+
import numpy as np
36+
from brainplotlib import brain_plot
37+
import matplotlib.pyplot as plt
38+
39+
rng = np.random.default_rng(0)
40+
```
41+
42+
```{code-cell}python
43+
:tags: ["remove-output"]
44+
fig, axs = plt.subplots(2, 2, dpi=300, figsize=([_/300 + 1 for _ in [1728, 1560]]))
45+
for i in range(2):
46+
for j in range(2):
47+
if (i, j) == (0, 0):
48+
v = rng.random((588 + 587, ))
49+
title = 'masked icoorder3'
50+
elif (i, j) == (0, 1):
51+
v = rng.random((642 * 2, ))
52+
title = 'non-masked icoorder3'
53+
elif (i, j) == (1, 0):
54+
v = rng.random((9372 + 9370, ))
55+
title = 'masked icoorder5'
56+
elif (i, j) == (1, 1):
57+
v = rng.random((10242 * 2, ))
58+
title = 'non-masked icoorder5'
59+
60+
ax = axs[i][j]
61+
img = brain_plot(v, vmax=1, vmin=0)
62+
ax.imshow(img)
63+
ax.axis('off')
64+
ax.set_title(title)
65+
plt.show()
66+
```
67+
68+
```{code-cell}python
69+
:tags: ["remove-cell"]
70+
from myst_nb import glue
71+
glue('cortical_masks', fig, display=False)
72+
```
73+
74+
## Different forms of input
75+
76+
The `brain_plot` function also handles different forms of input. The values to be plotted on surface can be one of:
77+
- Two NumPy arrays for the left and right hemispheres, respectively.
78+
- A list or tuple of NumPy arrays with two elements.
79+
- A concatenated NumPy array comprising data from both left and right hemispheres (left hemisphere first).
80+
81+
The same image can be generated using different forms of input, as long as the actual data is the same.
82+
```{code-cell}python
83+
lh, rh = rng.random((588, )), rng.random((587, ))
84+
85+
img1 = brain_plot(lh, rh, vmax=1, vmin=0)
86+
img2 = brain_plot([lh, rh], vmax=1, vmin=0)
87+
img3 = brain_plot(np.concatenate([lh, rh], axis=0), vmax=1, vmin=0)
88+
89+
np.testing.assert_array_equal(img1, img2)
90+
np.testing.assert_array_equal(img1, img3)
91+
```
92+
93+
{{ gallery_link }}

docs/examples/different_cmaps.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
jupytext:
3+
cell_metadata_filter: -all
4+
formats: md:myst
5+
text_representation:
6+
extension: .md
7+
format_name: myst
8+
format_version: 0.13
9+
jupytext_version: 1.11.5
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
---
15+
16+
# Use different colormaps
17+
18+
This example shows how to use different colormaps.
19+
20+
```{glue:} different_cmaps
21+
```
22+
23+
```{code-cell}python
24+
import numpy as np
25+
from brainplotlib import brain_plot
26+
import matplotlib.pyplot as plt
27+
28+
rng = np.random.default_rng(0)
29+
v = rng.random((1175, ))
30+
```
31+
```{margin} Change the colormap
32+
The `cmap` parameter of `brain_plot` can use any `matplotlib` colormaps in a similar way as `plt.plot`.
33+
```
34+
```{code-cell}python
35+
:tags: ["remove-output"]
36+
fig, axs = plt.subplots(2, 2, dpi=300, figsize=([_/300 + 1 for _ in [1728, 1560]]))
37+
cmaps = ['viridis', 'jet', 'bwr', 'plasma']
38+
for i in range(2):
39+
for j in range(2):
40+
ax = axs[i][j]
41+
cmap = cmaps[i*2+j]
42+
img = brain_plot(v, vmax=1, vmin=0, cmap=cmap)
43+
ax.imshow(img)
44+
ax.axis('off')
45+
ax.set_title(cmap)
46+
plt.show()
47+
```
48+
```{code-cell}python
49+
:tags: ["remove-cell"]
50+
from myst_nb import glue
51+
glue('different_cmaps', fig, display=False)
52+
```
53+
54+
{{ gallery_link }}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
jupytext:
3+
cell_metadata_filter: -all
4+
formats: md:myst
5+
text_representation:
6+
extension: .md
7+
format_name: myst
8+
format_version: 0.13
9+
jupytext_version: 1.11.5
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
---
15+
16+
# Different data resolutions
17+
18+
This example is about different input data resolutions.
19+
20+
```{glue:} different_resolutions
21+
```
22+
23+
Neuroimaging data on cortical surface has different spatial resolutions:
24+
25+
| name | resolution | # of vertices per hemishere |
26+
| --- | --- | --- |
27+
| icoorder7 | 0.7 mm | 163842 |
28+
| icoorder6 | 1.5 mm | 40962 |
29+
| icoorder5 | 3 mm | 10242 |
30+
| icoorder4 | 6 mm | 2562 |
31+
| icoorder3 | 13 mm | 642 |
32+
33+
Typical the data resolution is between `icoorder7` (usually anatomical scans) and `icoorder5` (usually functional scans). `icoorder3` resolution is often used in defining connectivity targets for [connectivity hyperalignment](https://doi.org/10.1371/journal.pcbi.1006120) and in examples.
34+
35+
The number of cortical vertices **per hemisphere** can be computed based on icoorder: $ n_v = 4^{icoorder} \times 10 + 2 $, where $n_v$ is the number of vertices.
36+
37+
The `brain_plot` function automatically handles various data resolution between `icoorder7` and `icoorder3`. When the input data has a lower resolution than `icoorder7`, it's automatically upsampled to the `icoorder7` resolution in a nearest-neighbor manner based on the [Voronoi diagram](https://en.wikipedia.org/wiki/Voronoi_diagram). The data is always visualized using the `icoorder7` high-resolution surface (i.e., `fsaverage`).
38+
39+
```{code-cell}python
40+
import numpy as np
41+
from brainplotlib import brain_plot
42+
import matplotlib.pyplot as plt
43+
44+
rng = np.random.default_rng(0)
45+
```
46+
```{code-cell}python
47+
:tags: ["remove-output"]
48+
fig, axs = plt.subplots(2, 2, dpi=300, figsize=([_/300 + 1 for _ in [1728, 1560]]))
49+
icoorders = [3, 5, 6, 7]
50+
for i in range(2):
51+
for j in range(2):
52+
ax = axs[i][j]
53+
icoorder = icoorders[i*2+j]
54+
nv = 4**icoorder * 10 + 2
55+
v = rng.random((nv * 2, ))
56+
img = brain_plot(v, vmax=1, vmin=0, cmap='coolwarm')
57+
ax.imshow(img)
58+
ax.axis('off')
59+
ax.set_title(f'icoorder{icoorder}')
60+
plt.show()
61+
```
62+
```{code-cell}python
63+
:tags: ["remove-cell"]
64+
from myst_nb import glue
65+
glue('different_resolutions', fig, display=False)
66+
```
67+
68+
{{ gallery_link }}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
jupytext:
3+
cell_metadata_filter: -all
4+
formats: md:myst
5+
text_representation:
6+
extension: .md
7+
format_name: myst
8+
format_version: 0.13
9+
jupytext_version: 1.11.5
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
---
15+
16+
# Use different types of surface
17+
18+
By default `brain_plot` uses the `inflated` surface.
19+
This can be changed to the `pial`, `white`, or `midthickness` (average of `pial` and `white`) surface using the `surf_type` parameter.
20+
21+
```{glue:} different_surfaces
22+
```
23+
24+
```{code-cell}python
25+
import numpy as np
26+
from brainplotlib import brain_plot
27+
import matplotlib.pyplot as plt
28+
29+
rng = np.random.default_rng(0)
30+
v = rng.random((1175, ))
31+
```
32+
```{code-cell}python
33+
:tags: ["remove-output"]
34+
fig, axs = plt.subplots(2, 2, dpi=300, figsize=([_/300 + 1 for _ in [1728, 1560]]))
35+
surf_types = ['inflated', 'midthickness', 'pial', 'white']
36+
for i in range(2):
37+
for j in range(2):
38+
ax = axs[i][j]
39+
surf_type = surf_types[i*2+j]
40+
img = brain_plot(v, vmax=1, vmin=0, cmap='rainbow', surf_type=surf_type)
41+
ax.imshow(img)
42+
ax.axis('off')
43+
ax.set_title(f'{surf_type} surface')
44+
plt.show()
45+
```
46+
```{code-cell}python
47+
:tags: ["remove-cell"]
48+
from myst_nb import glue
49+
glue('different_surfaces', fig, display=False)
50+
```
51+
52+
{{ gallery_link }}

0 commit comments

Comments
 (0)