{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualisation of spatial resources with _TemplateFlow_\n",
"\n",
"This notebook showcases how _TemplateFlow_ can facilitate visualisation of spatially standardised data, including template and atlas resources. We begin by importing the _TemplateFlow_ API, along with `nibabel` for reading neuroimaging data files, `matplotlib` for plotting, and a number of plotting functions from the `nilearn` and `niworkflows` packages."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:30: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" method='lar', copy_X=True, eps=np.finfo(np.float).eps,\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:167: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" method='lar', copy_X=True, eps=np.finfo(np.float).eps,\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:284: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" eps=np.finfo(np.float).eps, copy_Gram=True, verbose=0,\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:862: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" eps=np.finfo(np.float).eps, copy_X=True, fit_path=True,\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:1101: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" eps=np.finfo(np.float).eps, copy_X=True, fit_path=True,\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:1127: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" eps=np.finfo(np.float).eps, positive=False):\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:1362: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" max_n_alphas=1000, n_jobs=None, eps=np.finfo(np.float).eps,\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:1602: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" max_n_alphas=1000, n_jobs=None, eps=np.finfo(np.float).eps,\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/least_angle.py:1738: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" eps=np.finfo(np.float).eps, copy_X=True, positive=False):\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/decomposition/online_lda.py:29: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" EPS = np.finfo(np.float).eps\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/sklearn/feature_extraction/image.py:167: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" dtype=np.int):\n",
"/usr/local/anaconda3/lib/python3.7/site-packages/nilearn/datasets/__init__.py:89: FutureWarning: Fetchers from the nilearn.datasets module will be updated in version 0.9 to return python strings instead of bytes and Pandas dataframes instead of Numpy arrays.\n",
" \"Numpy arrays.\", FutureWarning)\n"
]
}
],
"source": [
"import templateflow.api as tflow\n",
"import nibabel as nb\n",
"import matplotlib.pyplot as plt\n",
"from niworkflows.viz.utils import (\n",
" plot_registration,\n",
" plot_segs,\n",
" compose_view,\n",
" cuts_from_bbox\n",
")\n",
"from IPython.display import SVG, display"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualising a template\n",
"\n",
"In addition to numerous human template resources, _TemplateFlow_ includes the `Fischer344` rodent template for use with preclinical imaging workflows. Let's start by creating a mosaic plot of this template. To do this, we begin by using the _TemplateFlow_ client to retrieve the T2-weighted image (`suffix='T2w'`) of the Fischer template. We also retrieve a brain mask (`desc='brain', suffix='mask'`); this will be used to determine the sections of the brain to be visualised.\n",
"\n",
"_Note:_ Some templates might have several different resolutions; in this case, a `resolution` argument should be used to specify the desired resolution. (Resolutions are defined in the `template_description.json` metadata file.)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"uid = 'Fischer344'\n",
"nii = nb.load(tflow.get(uid, desc=None, suffix='T2w'))\n",
"mask_nii = nb.load(tflow.get(uid, hemi=None, desc='brain', suffix='mask'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we use the `plot_registration` function from `niworkflows` to create a mosaic SVG plot of the `Fischer344` brain. This plot will include axial, sagittal, and coronal slices. The number of slices is set by `n_cuts`, and the positions of the slices is determined by the `niworkflows` utility function `cuts_from_bbox`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"n_cuts = 7\n",
"cuts = cuts_from_bbox(mask_nii, cuts=n_cuts)\n",
"figs = plot_registration(\n",
" nii,\n",
" 'fixed-image',\n",
" estimate_brightness=True,\n",
" cuts=cuts,\n",
" label=uid,\n",
" contour=mask_nii,\n",
" compress='auto',\n",
" dismiss_affine=False,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for i, fig in enumerate(figs):\n",
" fig.save(f'/tmp/tpl-{uid}_view{i}.svg')\n",
" display(SVG(filename=f'/tmp/tpl-{uid}_view{i}.svg'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"### Segmentation overlay\n",
"\n",
"Most templates in _TemplateFlow_ are distributed with segmentations. Discrete segmentations (`suffix='dseg'`) are voxelwise annotations that assign each voxel of the brain to a single region or tissue class. Probabilistic segmentations (`suffix='probseg'`) instead assign each voxel to a categorical distribution over tissue classes or regions.\n",
"\n",
"Let's visualise the tissue classes present in the `Fischer344` brain. To do this, we'll use a 4-way probabilistic segmentation into grey matter, white matter, cerebrospinal fluid, and a catch-all 'other' category to delineate boundary contours of each tissue class. We begin by retrieving the probabilistic segmentations using the _TemplateFlow_ API. Now, we use another plotting function from `niworkflows` designed for this purpose, `plot_segs`, again selecting the slices to be plotted with guidance from the mask (`bbox_nii` argument)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"probseg_nii = [str(path) for path in tflow.get('Fischer344', suffix='probseg')]\n",
"\n",
"figs = plot_segs(\n",
" image_nii=str(tflow.get(uid, desc=None, suffix='T2w')),\n",
" seg_niis=probseg_nii,\n",
" bbox_nii=str(tflow.get(uid, hemi=None, desc='brain', suffix='mask')),\n",
" out_file=f'/tmp/tpl-{uid}_seg.svg'\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
"