We can’t start to learn how to use Nipype before we don’t have any data to work on. The fact that you are interested in using Nipype implies that you probably already have access to an MRI dataset. You are of course free to do your first steps with Nipype directly on your own dataset. But if you’re new to the topic or want to be sure that you get the same results like I do, I recommend to use a tutorial dataset which we can find over on OpenfMRI.
- The Dicom image (or file) is a 2D image even though it has a volume but in reality it is a 2D image because it has only one slice of a MRI, CT, TEP. Instead of that, the Nifti file can contain a set of these Dicom images. To give you a quick example that I am sure you will understand, a video is a set of frames so when we put all the frames.
- DCM2NII attempts to convert images from the proprietary scanner format to the NIfTI format used by FSL, SPM5, MRIcron and many other brain imaging tools. NIfTI is a modern incarnation of the Analyze format, but includes important information like the orientation of the image. DCM2NII is a stand-alone program that is distributed with MRIcron.
Analyze (img) Nifti (nii. Fslswapdim fslview hdr Hippocampus img Linux Mac OS X mgz mri MRI scanner mriconvert MRIcron multi instances NIfTI nii nii.gz.
Download the tutorial dataset¶
OpenfMRI is an awesome new online database where you can upload ans share your fMRI dataset or download datasets from other researchers. Such an open approach to science allows you and other researchers to profit from each other and helps the field as a whole to progress faster. A list of all datasets available on OpenfMRI can be found on openfmri.org.
Download the dataset from OpenfMRI¶
I chose to use the dataset DS102: Flanker task (event-related) as the tutorial dataset for this beginner’s guide because the dataset is complete, in good quality, the experiment is representative for most fMRI experiments and the dataset as a whole is with its 1.8GB not too big to download. To download the dataset go to the button of the DS102: Flanker task (event-related) page and click on the link: Raw data on AWS.
Note
If you are on a Linux system, you can also download the tutorial dataset directly to your Downloads
folder with the following command:
Unpack and prepare the folder structure¶
The file you’ve just downloaded contains amongst others one anatomical and two functional MRI scans and the behavioral response and onsets of stimuli during those functional scans. The whole dataset consists of 26 subjects. Information about gender and age of each subject is stored in the demographics.txt file, also found in downloaded file. To reduce the total time of computation, this tutorial will only analyze the first 10 subjects of the whole dataset. Now let’s get ready.
What we want is an experiment folder called nipype_tutorial
that contains a folder called data
where we save the raw data of the first 10 subjects into. In the end, the structure should be something like this
This can either be done manually or with the following code:
Hint
You can download this code as a script here: tutorial_1_create_dataset.sh
Acquire scan and experiment parameters¶
One of the most important things when analyzing any data is to know your data. What does it consist of, how was it recorded, what are its characteristics and most importantly, what does it look like. The DS102: Flanker task (event-related) page helps us already to answer many of those parameters:
Scan parameters¶
Magnetic field: 3T [Tesla]
Head coil: Siemens standard, which probably means 32-channel
- Information about functional acquisition
Acquisition type: contiguous echo planar imaging (EPI)
Number of volumes per session: 146
Number of slices per volume: 40
Slice order: unknown
Repetition time (TR): 2000ms
Field of view (FOV): 64x64
Voxel size: 3x3x4mm
- Information about anatomical acquisition
Acquisition type: magnetization prepared gradient echo sequence (MPRAGE)
Number of slices: 176
Repetition time (TR): 2500ms
Field of view (FOV): 256mm
Experiment parameters¶
Task: On each trial, participants used one of two buttons on a response pad to indicate the direction of a central arrow in an array of 5 arrows. In congruent trials the flanking arrows pointed in the same direction as the central arrow (e.g., < < < < <), while in more demanding incongruent trials the flanking arrows pointed in the opposite direction (e.g., < < > < <).
Condition: congruent and incongruent
Scan sessions: Subjects performed two 5-minute blocks, each containing 12 congruent and 12 incongruent trials, presented in a pseudo-random order.
So what do we know?¶
We know that we have two functional scans per subjects, in our case called run001 and run002. Each functional scan represents 5min of scan time and consists of 146 volumes. Each of those volume consists 40 slices (with a thickness of 4mm) and each slice consists of 64x64 voxel with the size of 3x3mm. The TR of each volume is 2000ms.
So far we don’t know what the slice order of the functional acquisition is. But a closer look at the provided references tells us that the data was acquired with an interleaved slice to slice order. The fact that it is not stated if it is ascending or descending interleaved means that the acquisition is most certainly ascending.
We know that we have one anatomical scan per subject, in our case called struct and that this anatomical scan consists of 176 slices with each having a FOV of 256mm. This hints to an isometric voxel resolution of 1x1x1mm.
We know the task of the experiment and that it consists of two conditions, congruent and incongruent. With each condition being presented 12 times per session.
We know from the onset file found in the subject folder what the actual onset of the two conditions are, but we have no clear information about the duration of each stimulation. The description of the design on OpenfMRI.org tells us that the inter-trial interval varies between 8 and 14s with a mean of 12s. A closer look at the references tells us that the subjects first see a fixation cross for 500ms, followed by the congruent or incongruent condition (i.e. the arrows) for 1500ms, followed by a blank screen for 8000-12000ms.
Check the data¶
It is always important to look at your data and verify that it actually is recorded the way it should be. To take a look at the data, I usually use FreeSurfer’s freeview
. For example, if you want to load the anatomical scan of all ten subjects use the following code:
To verify the information about the scan parameters we can use fslinfo
. fslinfo
allows us to read the header information of a NIfTI file and therefore get information about voxel resolution and TR. For example, reading the header of the anatomical scan of subject2 with the command fslinfo~/nipype_tutorial/data/sub002/struct.nii.gz
gives us following output:
This output tells us, that the anatomical volume consists of 256x256x176 voxels with each having 1x1x1mm resolution.
Using the same command on a functional scan of subject 2 gives as following output:
This output tells us that this functional scan consists of 146 volumes, of which each consists of 64x64x40 voxels with a resolution of 3x3x4mm. pixdim4 gives us additionally information about the TR of this functional scan.
Freesurfer Dicom To Nifti
Note
Just as a side note: The data_type of a NIfTI file tells you the amount of bits used to store the value of each voxel. INT16 stands for signed short (16 bits/voxel), INT32 stands for signed int (32 bits/voxel) and FLOAT64 stands for float (64 bits/voxel). The more bits used for storing a voxel value the bigger the whole NIfTI file.
If you want to change the data type of your image, either use Nipype’s function ChangeDataType
found in the nipype.interfaces.fsl.maths
package (read more here) or use fslmaths
directly to change the data_type to INT32 with the following command:
For those who use their own dataset¶
If you want to use your own dataset, make sure that you know the following parameters:
Number of volumes, number of slices per volume, slice order and TR of the functional scan.
Number of conditions during a session, as well as onset and duration of stimulation during each condition.
Important
Make sure that the layout of your data is similar to the one stated above, so that further code is also applicable for your case.
Make the dataset ready for Nipype¶
Convert your data into NIfTI format¶
You don’t have to do this step if you’re using the tutorial dataset. But chances are that you soon want to analyze your own recorded dataset. And most often, the images coming directly from the scanner are not in the common NIfTI
format, but rather in a scanner specific format (e.g. DICOM
, PAR/REC
, etc.). This means you first have to convert your data from this specific scanner format to the standard NIfTI format.
Probably the most common scanner format is DICOM. Therefore, the following section will cover how you can convert your files from DICOM to NIfTI. There are many different tools that you can use to convert your files. For example, if you like to have a nice GUI to convert your files, use MRICron’s MRIConvert. But for this Beginner’s Guide we will use FreeSurfer’s mri_convert
function, as it is rather easy to use and doesn’t require many steps.
But first, as always, be aware of your folder structure. So let’s assume that we’ve stored our dicoms in a folder called raw_data
and that the folder structure looks something like this:
This means, that we have one folder per subject with each containing another folder, one for the structural T1 weighted image and 2 for the functional T2 weighted images. The conversion of the dicom files in those folders is rather easy. If you use FreeSurfer’s mri_convert
function, the command is as as follows: mri_convert<involume><outvolume>
. You have to replace <involume>
by the actual path to any one dicom file in the folder and <outvolume>
with the name for your outputfile.
So, to accomplish this with some few terminal command, we first have to tell the system the path and names of the folders that we later want to feed to the mri_convert
function. This is done by the following variables (line 1 to 6). If this is done, we only have to run the loop (line 8 to 17) to actually run mri_convert
for each subject and each scanner image.
Run FreeSurfer’s recon-all¶
Not mandatory but highly recommended is to run FreeSurfer’s recon-all
process on the anatomical scans of your subject. recon-all
is FreeSurfer’s cortical reconstruction process that automatically creates a parcellation of cortical and a segmentation of subcortical regions. A more detailed description about the recon-all
process can be found on the official homepage.
As I said, you don’t have to use FreeSurfer’s recon-all
process, but you want to! Because many of FreeSurfer’s other algorithms require the output of recon-all
. The only negative point about recon-all
is that it takes rather long to process a single subject. My average times are between 12-24h, but it is also possible that the process takes up to 40h. All of it depends on the system you are using. So far, recon-all
can’t be run in parallel. Luckily, if you have an 8 core processor with enough memory, you should be able to process 8 subjects in parallel.
Run recon-all on the tutorial dataset (terminal version)¶
Mri_convert Dicom To Nifti
The code to run recon-all
on a single subject is rather simple, i.e. recon-all-all-subjidsub001
. The only thing that you need to keep in mind is to tell your system the path to the freesurfer folder by specifying the variable SUBJECTS_DIR
and that each subject you want to run the process on has a according anatomical scan in this freesurfer folder under SUBJECTS_DIR
.
To run recon-all
on the 10 subjects of the tutorial dataset you can run the following code:
This code will run the subjects in sequential order. If you want to process the 10 subjects in (manual) parallel order, delete line 12 - recon-all-all-subjidsub0$id
- from the code above, run it and than run the following code, each line in its own terminal:
Note
Nrrd To Nifti
If your MRI data was recorded on a 3T scanner, I highly recommend to use the -nuintensitycor-3T
flag on the recon-all
command, e.g. recon-all-all-subjidsub0$id-nuintensitycor-3T
. This flag was created specifically for 3T scans and improves the brain segmentation accuracy by optimizing non-uniformity correction using N3.
Hint
You can download this code as a script here: tutorial_2_recon_shell.sh
Run recon-all on the tutorial dataset (Nipype version)¶
If you run recon-all
only by itself, I recommend you to use the terminal version shown above. But of course, you can also create a pipeline and use Nipype to do the same steps. This might be better if you want to make better use of the parallelization implemented in Nipype or if you want to put recon-all
in a bigger workflow.
I won’t explain to much how this workflow actually works, as the structure and creation of a common pipeline is covered in more detail in the next section. But to use Nipype to run FreeSurfer’s recon-all
process do as follows:
After this script has run, all important outputs will be stored directly under ~/nipype_tutorial/freesurfer
. But the running of the reconflow
pipeline also created some temporary files. As defined by the script above, those files were stored under ~/nipype_tutorial/workingdir_reconflow
. Now that the script has run you can delete this folder again. Either do this manually, use the shell command rm-rf~/nipype_tutorial/workingdir_reconflow
or add the following lines to the end of the python script above:
Note
In the code above, if we don’t create the freesurfer
output folder on line 19, we would get following error:
Also, if your data was recorded on a 3T scanner and you want to use the mentioned -nuintensitycor-3T
flag, just uncomment line 32, i.e. delete the #
sign before flags='-nuintensitycor-3T'
on line 32.
Hint
You can download this code as a script here: tutorial_2_recon_python.py
Resulting Folder Structure¶
After we’ve prepared our data and run the recon-all
process the folder structure of our experiment folder should look as follows:
Ferraris, S; Shakir, DI; Van Der Merwe, J; Gsell, W; Deprest, J; Vercauteren, T; (2017) Bruker2nifti: Magnetic Resonance Images converter from Bruker ParaVision to Nifti format.Journal Of Open Source Software, 2 (16) p. 354. 10.21105/joss.00354.
Text Ferraris_10.21105.joss.00354.pdf - Published version Download (122kB) | Preview |
Abstract
In clinical and pre-clinical research involving medical images, the first step following a Magnetic Resonance Imaging dataset acquisition, usually entails the conversion of image data from the native scanner format to a format suitable for the intended analysis. The proprietary [Bruker ParaVision](https://www.bruker.com/products/mr/preclinical-mri/software/service-support.html) software currently does not provide the tools for conversion of the data to suitable and open formats for research, such as nifti [@cox2004sort], for which most of the available tools for medical image analysis are implemented. For this purpose we have designed and developed [bruker2nifti](https://github.com/SebastianoF/bruker2nifti), a pip-installable Python tool provided with a Graphical User Interface to convert from the native MRI Bruker format to the nifti format, without any intermediate step through the DICOM standard formats [@Mildenberger2002]. Bruker2nifti is intended to be a tool to access the data structure and to parse all parameter files of the Bruker ParaVision format into python dictionaries, to select the relevant information to fill the Nifti header and data volume. Lastly it is meant to be a starting point where to integrate possible future variations in Bruker hardware and ParaVision software future releases.
Type: | Article |
---|---|
Title: | Bruker2nifti: Magnetic Resonance Images converter from Bruker ParaVision to Nifti format |
Open access status: | An open access version is available from UCL Discovery |
DOI: | 10.21105/joss.00354 |
Publisher version: | http://dx.doi.org/10.21105/joss.00354 |
Language: | English |
Additional information: | Authors of JOSS papers retain copyright and release the work under a Creative Commons Attribution 4.0 International License (CC-BY) (http://creativecommons.org/licenses/by/4.0/). |
UCL classification: | UCL UCL > Provost and Vice Provost Offices UCL > Provost and Vice Provost Offices > UCL BEAMS UCL > Provost and Vice Provost Offices > UCL BEAMS > Faculty of Engineering Science UCL > Provost and Vice Provost Offices > UCL BEAMS > Faculty of Engineering Science > Dept of Med Phys and Biomedical Eng |
URI: | https://discovery.ucl.ac.uk/id/eprint/1571768 |
Archive Staff Only
View Item |