Basic nilearn example: manipulating and looking at data#
A simple example showing how to load an existing Nifti file and use basic nilearn functionalities.
Path to MNI152 template: '/home/remi/github/nilearn/nilearn/env/lib/python3.11/site-packages/nilearn/datasets/data/mni_icbm152_t1_tal_nlin_sym_09a_converted.nii.gz'
A first step: looking at our data#
Let’s quickly plot this file:
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fb3b9f7f390>
This is not a very pretty plot. We just used the simplest possible code. There is a whole section of the documentation on making prettier code.
Exercise: Try plotting one of your own files. In the above, MNI152_FILE_PATH is nothing more than a string with a path pointing to a nifti image. You can replace it with a string pointing to a file on your disk. Note that it should be a 3D volume, and not a 4D volume.
Simple image manipulation: smoothing#
Let’s use an image-smoothing function from nilearn:
Functions containing ‘img’ can take either a filename or an image as input.
Here we give as inputs the image filename and the smoothing value in mm
<nibabel.nifti1.Nifti1Image object at 0x7fb3921d1e10>
This is an in-memory object. We can pass it to nilearn function, for instance to look at it
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fb3963ca1d0>
We could also pass it to the smoothing function
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fb3b1468c50>
Saving results to a file#
We can save any in-memory object as follows:
Output will be saved to: /home/remi/github/nilearn/nilearn/examples/00_tutorials/results/plot_nilearn_101
Finally, calling plotting.show() is necessary to display the figure when running as a script outside IPython
To recap, all the nilearn tools can take data as filenames or in-memory objects, and return brain volumes as in-memory objects. These can be passed on to other nilearn tools, or saved to disk.
Total running time of the script: (0 minutes 6.076 seconds)
Estimated memory usage: 199 MB