Neuroimaging data and file structures in Python

Neuroimaging data and file structures in Python

"Learning the basics of neuroimaging file format with Nibabel."


The estimated time to complete this training module is 1h30m for the basic content and 1h for the optional advanced content.

  • Basic - Be able to load and save different types of neuroimaging files in Nibabel. Know how to work with volumetric data.
  • Advanced - Understand the difference between array and array proxy images.
  • Optional - Acquire a passing familiarity with the structures of surface images, CIFTI-2 files and tractogram.

The prerequisites to take this module are:

If you have any questions regarding the module content please ask them in the relevant module channel on the school Discord server. If you do not have access to the server and would like to join, please send us an email at school [dot] brainhack [at] gmail [dot] com.


We will watch the presentation NiBabel: Neuroimaging data and file structures in Python from Neurohackademy 2020 by Chris Markiewicz.

The original presentation and the practical notebook can be found here. In this exercise, we will use the notebook in the brainhack school repository. Follow the instructions below to run both.

Downlad data and jupyter notebook

You will need to install the example dataset with DataLad.

# download open neuro dataset
mkdir ~/brainhackschool_nibabel
cd ~/brainhackschool_nibabel
mkdir data
cd data
datalad install ///openneuro
cd openneuro
datalad get -r ds000114/sub-01

cd ~/brainhackschool_nibabel/data
datalad install
cd ds000228-fmriprep
datalad get -r sourcedata/freesurfer/sub-pixar001/surf
datalad get -r sourcedata/freesurfer/fsaverage5

datalad get sub-pixar001/anat/
datalad get sub-pixar001/func/sub-pixar001_task-pixar_hemi-L_space-fsaverage5_bold.func.gii
datalad get sub-pixar001/func/sub-pixar001_task-pixar_space-fsLR_den-91k_bold.dtseries.nii

# get the surface data from midnight scan club
cd ~/brainhackschool_nibabel/data
wget -c 

# get the tutorial
cd ~/brainhackschool_nibabel
wget -c
wget -c

# make a temporary directory to dump things
mkdir ~/brainhackschool_nibabel/data/tmp

Set up environment

You will need to create the environment with the requirements.txt file.

pip install virtualenv

cd ~/brainhackschool_nibabel/
virtualenv env
source env/bin/activate
pip install -r requirements.txt  # install the environment

From here you can launch jupyter notebook and navigate to the presentaion, or launch the notebook directly using:

jupyter notebook


Follow the notebook along with the presentation.

The time stamps of this video are as follows. You need to watch all the basic content to complete the excercise. You can skip the optional sections.

  • 0:00 Introduction
  • 0:44 Brief introduction to NiBabel / some Neurohackademy related business
  • 2:01 Start of the talk
  • 3:01 Installation of NiBabel
  • 6:01 Learning objective of the original presentation
  • 7:32 Basic file input/output (I/O) (basic)
  • 12:03 Data array of spatial image (basic)
  • 20:11 Affine (basic)
  • 32:13 Header (basic)
  • 36:55 Create an image (basic)
  • 39:57 Break
  • 47:27 Array and array proxy (advanced)
  • 1:06:52 Slicing image (basic)
  • 1:13:13 Question time
  • 1:14:17 Surface image (optional)
  • 1:25:36 CIFTI-2 image (optional)
  • 1:30:22 Tractogram (optional)
  • 1:34:38 Summary and ending

The video presentation is available below:


  • Watch the video presentation by Chris Markiewicz and go over the notebook.

  • In a separate notebook, complete the following exercises:

    • Use the nilearn library function fetch_atlas_difumo to get the 64 parcellation image. As we learned in the video, the data in this image is just a number array and you can maniputate it with numpy. Please extract the 16th region, binarize it, and save it as a new nifti image.

    • Use the slicer object to view the new nifti file we created in the three different views.

    • Bonus: in 200 words, describe the conceptual differences between array and array proxy images.

  • Follow up with your local TA(s) to validate you completed the exercise correctly.

  • 🎉 🎉 🎉 you completed this training module! 🎉 🎉 🎉

More resources

Nilearn has some high level wrappers for nibabel I/O function, we encourage you to have a look:

Here are all the links mentioned in the presentations: