{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# NiBabel\n", "\n", "
\n", "\n", "## Neuroimaging data and file structures in Python\n", "\n", "###### Christopher J Markiewicz\n", "\n", "###### NeuroHackademy 2020" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "The goal of this presentation is to familiarize you with some broad classes of neuroimaging data that can be interacted with in Python, using the NiBabel library.\n", "\n", "This document is intended to be viewed as a [RISE](https://rise.readthedocs.io/) presentation. It works fine as a notebook, but blocks with images may look strange because they are formatted to work as slides." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# NiBabel\n", "\n", "NiBabel is a low-level Python library that gives access to a variety of imaging formats, with a particular focus on providing a common interface to the various volumetric formats produced by scanners and used in common neuroimaging toolkits:\n", "\n", "| | | |\n", "|:---: |:---: |:---:|\n", "| NIfTI-1 | NIfTI-2 | MGH |\n", "| MINC 1.0 | MINC 2.0 | AFNI BRIK/HEAD |\n", "| ANALYZE | SPM99 ANALYZE | SPM2 ANALYZE |\n", "| DICOM | PAR/REC | ECAT | \n", "\n", "It also supports surface file formats:\n", "\n", "| | |\n", "|:--:|:--:|\n", "| GIFTI | FreeSurfer (FS) geometry |\n", "|FS labels | FS annotations |\n", "\n", "Tractography files:\n", "\n", "| | |\n", "|:--:|:--:|\n", "| TrackVis (TRK) | MRtrix (TCK) |\n", "\n", "As well as the CIFTI-2 format for composite volume/surface data." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Installation\n", "\n", "NiBabel is available on [PyPI](https://pypi.org/project/nibabel/):\n", "\n", "```Shell\n", "pip install nibabel\n", "```\n", "\n", "And [conda-forge](https://anaconda.org/conda-forge/nibabel):\n", "\n", "```Shell\n", "conda install -c conda-forge nibabel\n", "```\n", "\n", "*Note*: This notebook assumes NiBabel 3+, which requires a minimum Python version of 3.5." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1.1\n" ] } ], "source": [ "import nibabel as nb\n", "print(nb.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "# Some additional, useful imports\n", "from pathlib import Path # Combine path elements with /\n", "from pprint import pprint # Pretty-printing\n", "\n", "import numpy as np # Numeric Python\n", "from matplotlib import pyplot as plt # Matlab-ish plotting commands\n", "from nilearn import plotting as nlp # Nice neuroimage plotting\n", "import transforms3d # Work with affine algebra\n", "from scipy import ndimage as ndi # Operate on N-dimensional images\n", "import nibabel.testing # For fetching test data\n", "\n", "%pylab inline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Assume we're on the NeuroHackademy hub.\n", "data_dir = Path('/home/jovyan/data')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Learning objectives\n", "\n", "1. Be able to load and save different types of files in NiBabel\n", "1. Become familiar with the `SpatialImage` API and identify its components\n", "1. Understand the differences between array and proxy images\n", "1. Acquire a passing familiarity with the structures of surface images, CIFTI-2 files, and tractograms" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Basic I/O\n", "\n", "### Loading" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "t1w = nb.load(data_dir / 'openneuro/ds000114/sub-01/ses-test/anat/sub-01_ses-test_T1w.nii.gz')\n", "bold = nb.load(data_dir / 'openneuro/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "