top of page

Nipype and Nilearn

This blog post will be focusing on the applications, functions, and installation of the neuroimaging tools Nipype and Nilearn.

What is Nipype?

Nipype (Neuroimaging in Python - Pipelines and Interfaces) is a user-friendly, open-source software package developed by NiPy. This tool enables users to pipeline their neuroimaging workflow efficiently, as well as use the software packages and algorithms of their choice, independent of programming language.  Nipype provides an interface to several current neuroimaging processing and analysis tools, such as SPM, FreeSurfer, FSL, AFNI, ANTS, Camino, MRtrix, Slicer, and MNE. Nipype is fully written in python. This allows for the neuroimaging analysis using Nipype to be easily specified using python scripts. Nipype is easy to learn and allows software packages to be used to combine several processing steps. 

      Nipype allows users to:

  • Easily develop new workflows from previous ones

  • Combine processing steps from different software packages

  • Process data faster by running it in parallel

 

 

More information can be found at: https://nipype.readthedocs.io/en/latest

Screen Shot 2022-08-17 at 8.05.58 AM.png

Components of Nipype:

Although Nipype consists of many parts, the main components are the interfaces, workflow engine, and execution plugins. The interface wraps a program or function, the workflow engine represents the data flow, and plugins describe how a workflow should be executed. The diagram below describes examples of interfaces, execution plugins, and the workflow engine when using nipype.

Screen Shot 2022-08-17 at 8.09.08 AM.png

Benefits of Nipype:

​Nipype allows you to select your preferred algorithms from several software packages without being restricted to a single programming language or software package. The nipype tutorial provides an example of a project requiring the use of SPM for motion correction, ANTS for normalization, FreeSurfer for coregistration, and FSL for smoothing. Without nipype, this would require the use of several programming languages, software packages, and manual intervention. However Nipype allows this to be done efficiently, in parallel, and with less manual intervention as to the right.

Screen Shot 2022-08-17 at 8.11.53 AM.png

A Nipype workflow like this one can be created using code similar to this: 

 

# Import modules

from nipype.interfaces.freesurfer   import BBRegister

from nipype.interfaces.ants         import WarpTimeSeriesImageMultiTransform

from nipype.interfaces.fsl          import SUSAN

from nipype.interfaces.spm          import Realign

 

# Motion Correction (SPM)

realign = Realign(register_to_mean=True)

 

# Coregistration (FreeSurfer)

coreg = BBRegister()

 

# Normalization (ANTS)

normalize = WarpTimeSeriesImageMultiTransform()

# Smoothing (FSL)

smooth = SUSAN(fwhm=6.0)

 

# Where can the raw data be found?

grabber = nipype.DataGrabber()

grabber.inputs.base_directory = '~/experiment_folder/data'

grabber.inputs.subject_id = ['subject1', 'subject2', 'subject3']

 

# Where should the output data be stored at?

sink = nipype.DataSink()

sink.inputs.base_directory = '~/experiment_folder/output_folder'


* Note this code was obtained from the nipype tutorial

Installing Nipype

Nipype can be installed several ways including through docker, conda, pypi, ubuntu/debian, or miniconda. A tutorial for installing nipype can be found here 

 

The most recent release of nipype can be found here


Previous versions: https://github.com/nipy/nipype/tags

MacOS:

The easiest way to run nipype on Mac OS X is by installing the latest version of Miniconda, a free minimal installer for conda. You can find the tutorial for downloading Miniconda on Windows, macOS, and Linux here.  If you have a non-conda environment you can install nipype by typing:

 

pip install nipype

Running Nipype using docker:

To run nipype using docker start by downloading Docker. Docker desktop is a free, open source platform to easily create, deploy, and execute applications across many systems through the use of containers. The installation tutorial and requirements for downloading docker can be found here:

https://www.docker.com/products/docker-desktop/

 

Docker installation tutorial: https://www.youtube.com/watch?v=SGmFGYCuJK4

 

Troubleshooting: https://docs.docker.com/desktop/troubleshoot/overview/

 

Once you have downloaded docker, you may refer to the Nipype tutorial  or pull the nipype/nipype image from Docker hub:


docker pull nipype/nipype

NeuroDocker:

 

You may also use neurodocker, a command line program which allows users to generate custom docker containers which include neuroimaging software. In order to do this, you will need to have docker installed and a stable internet connection. Please refer to the neurodocker tutorial for more information. 

Introduction to Nilearn

Nilearn uses advanced machine learning, pattern recognition, and multivariate statistical techniques to perform statistical analysis on neuroimaging data. It has applications such as MVPA (Multi-Voxel Pattern Analysis), functional connectivity, predictive modeling, and many more.

Click here for more information

Screen Shot 2022-08-21 at 12.04.28 PM.png

Machine learning in neuroimaging

Statistical machine learning methods are used to analyze neuroimaging data. This allows for the modeling of high-dimensional datasets such as analysis of activation images or resting-state time series. Supervised learning can have applications in the analysis of brain images under behavioral or clinical observations. Other machine learning methods such as information mapping or transfer learning can be used in studying brain scans and determining both a diagnosis and prognosis for various brain disorders. 

    Data mining, the data driven exploration of brain images, has been used to extract major brain networks from resting-state data but also has plenty of other applications. For example, Independent Component Analysis is one approach to finding independent sources from fmri images. ICA, along with other techniques can be used to define regions or networks sharing similar BOLD signals across a period of time.

    To the right is an example of visualizing various components over the brain and plotting the map for different components separately.

Screen Shot 2022-08-21 at 12.23.20 PM.png

More information on extracting functional brain networks using Independent Component Analysis and interpreting components can be found here

Installing Nilearn

Nilearn requires a Python installation and the following dependencies: ipython, scipy, scikit-learn, joblib, matplotlib, nibabel, and pandas. Nilearn can be installed on Windows, Mac OS, and Linux. The information for this section was found here.

MacOS:

  1. Download and install 64 bit Anaconda

    • This will save you time as Anaconda meets all of the requirements of nilearn

    • You can download Anaconda for Mac OS here 

  2. Open terminal (navigate to /Applications/Utilities and double-click on Terminal)

    • Type the following line: 

      • pip install -U --user nilearn

    • Press “Enter"

 

 

 

 

 

 

 

 

 

 

 

 

 

Here is an example of the terminal display once steps 1 and two are completed correctly

    3. Open Ipython (open by writing “ipython” in terminal and pressing “Enter”)

  • ​Type  the following line: 

    • In [1]: import nilearn

  • Press “Enter” 

 

If no error occurs you have successfully installed Nilearn!

Screen Shot 2022-08-21 at 12.28.14 PM.png

Windows:

  1. Download and install 64 bit Anaconda

    • This will save you time as Anaconda meets all of the requirements of nilearn

    • You can download Anaconda for Windows here

  2. Open a command prompt (Press "Win-R", type "cmd" and press "Enter" to open the program cmd.exe which is the command prompt) 

    • Type in the following line: 

      • pip install -U --user nilearn

  3. Open Ipython (open by writing “ipython” in the command prompt and pressing “Enter”) 

    • Type  the following line: 

      • In [1]: import nilearn

    • Press “Enter”

 

If no error occurs you have successfully installed Nilearn!

Thank you for reading this blog on Nipype and Nilearn! If you would like more information on anything covered in this blog please refer to the Nipype Documentation or the Nilearn user guide.

bottom of page