|
| 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 }} |
0 commit comments