Nipype introduction
Starting with nipype¶
-
Nipypeprobably pronouncec asnipee..yipeeis an abbreviation for Neuroimaging in Python pipleline and interfaces - It is a Toolbox for anylysing data coming from neuroimaging modalaties. It is written in Python.
- Installation
- Check your installation
Beginners Guide¶
- It is year 2017 guide, it can be tested and re written for others
- The main purpose of this toolbox is to provide an easy way to build a workflow termed as a pipleline, to facilitate the existing technologies used in neuroimaging analyis. All popular technololgies such as
SPM, FreeSurfer, FSL etccan be used. - It allows to combine these techonolgies in an specifed workflow, it is what you decided to use which technology for which prupose.
- For example
- The whole idea is to provide an environment where reasearch can be reproduced with the same data by sharing with others.
Nipype architechture:¶
It consist of many components, important ones are interfaces,Workflow Engines and Execution Plugins.
-
Interfaces are the python programs (scripts) that are used to interface with existing technologies like
MATLAB,AFNI,ANTs etc. -
Workflow Engine is a part that deals with the complexities involve in executing diferent task by gluing eachother. It uses following terms interchanably.
- Node:An interface needs the information about the technologies it is dealing with and it is given in the form of node. A node describes these information.
- MapNode:It is similar to node and takes multiple inputs of same type. For example 10 patient of same data analysis is performed on them.
- Wordflow:It is graph a kind of directed acyclic graph or forest of grapsh that describes the dataflow interms of its Nodes, MapNodes or Workflows it self.
- Execution Plugins: They descirbe how to execute your workfow in a physical machine by leaverging the power of differnet cores.
A conventional way of neuro imaging¶
- Acqusation of MRI data: you need to know how it is taken what varibales and terms are used. Which series of MRI is uses commonly known as modalaties. There are many like ( DTI, fMRI etc).
- The format of resulted images: Different scanners uses different formats e.g.
DICOM , PAR or REC. To analyse these images they are to be converted into different format so that un necessary details can be removed. Initially data is kept inK-spaceand converted into different space. Mostly the format used is eitherniftiand now recetnly isgifti. - Design of the experiment: Which kind of experimental design is used what parameters to take into accunt etc.
- Preprocessing of data:
- Slice Timing Correction (fMRI images needes to be correcyted )
The below is taken verbatim from nipype Micheal tutorial.¶
Because functional MRI measurement sequences don’t acquire every slice in a volume at the same time we have to account for the time differences among the slices. For example, if you acquire a volume with 37 slices in ascending order, and each slice is acquired every 50ms, there is a difference of 1.8s between the first and the last slice acquired. You must know the order in which the slices were acquired to be able to apply the proper correction. Slices are typically acquired in one of three methods: descending order (top-down); ascending order (bottom-up); or interleaved (acquire every other slice in each direction), where the interleaving may start at the top or the bottom. (Left: ascending, Right: interleaved)
Slice Timing Correction is used to compensate for the time differences between the slice acquisitions by temporally interpolating the slices so that the resulting volume is close to equivalent to acquiring the whole brain image at a single time point. This temporal factor of acquisition especially has to be accounted for in fMRI models where timing is an important factor (e.g. for event related designs, where the type of stimulus changes from volume to volume).
Chec If nipype is working use
import nipypeorimport nipype. Run the cell if gets error it means it is not working otherwise it is present.
- Thouth the module has been imported successfully but it does not have any attribute or any function
help()it thorws an error. The above command succeeded, it means it is working. we have nipype on our path. To see where it is loaded from useshift + tab
Getting ready for dataset¶
- Make a directory
datain current folder that is underneate yournotebookfolder. Then usingdataladinstall dataset. Note the cell is used to executebashprograme, here it is refered asbashkernel. Other kernels can be used as well.
%%bash
mkdir -p data
cd data
datalad install -r ///workshops/nih-2017/ds000114
- Looking into the dataset.
ls data/ds000114/
# We have one anatomical image in every folder. Lets make sure it is there.
!ls data/ds000114/sub-01/ses-test/anat/
Using BET from fsl that we imorted in first step.¶
import os
from os.path import abspath
from nipype import Workflow, Node, MapNode, Function
from nipype.interfaces.fsl import BET, IsotropicSmooth, ApplyMask
from nilearn.plotting import plot_anat
%matplotlib inline
import matplotlib.pyplot as plt
from nipype.testing import example_data
# reading file in a variable
working_dir = os.getcwd()
data_dir = working_dir + "/data"
print(data_dir)
input_file = abspath(data_dir + "/ds000114/sub-01/ses-test/anat/sub-01_ses-test_T1w")
print(input_file)
fn = "./sub-01_ses-test_T1w.nii.gz"
bet = BET()
bet.inputs.in_file = input_file
bet.inputs.out_file = "T1.nii.gz"
res = bet.run()
Start working with nipype¶
- Importing few things
import os
from os.path import abspath
from os.path import relpath
from nipype import Workflow, Node, MapNode, Function
from nipype.interfaces.fsl import BET, IsotropicSmooth, ApplyMask
from nilearn.plotting import plot_anat
%matplotlib inline
import matplotlib.pyplot as plt
# will use a T1w from ds000114 dataset
input_file = abspath("/data/ds000114/sub-01/ses-test/anat/sub-01_ses-test_T1w.nii.gz")
bet = BET()
bet.inputs.in_file = abspath("data/sample-nifiti-file.nii")
bet.imputs.int_file= input_file
help(bet.inputs)
bet.inputs.out_file = "sample_bet.nii.gz"
res = bet.run()
res.outputs
plot_anat("sample_bet.nii.gz",
display_mode='ortho', dim=-1, draw_cross=False, annotate=False);
Comments