"The [nibabel](http://nipy.org/nibabel/) module is used to read and write NIfTI images and also some other medical imaging formats (e.g., ANALYZE, GIFTI, MINC, MGH). This module is included within the FSL python environment.\n",
"The [`nibabel`](http://nipy.org/nibabel/) module is used to read and write NIfTI\n",
"images and also some other medical imaging formats (e.g., ANALYZE, GIFTI,\n",
"MINC, MGH). `nibabel` is included within the FSL python environment.\n",
"> We use the expandvars() function above to insert the FSLDIR\n",
">environmental variable into our string. This function is\n",
">discussed more fully in the file management practical.\n",
"\n",
">environmental variable into our string. This function is\n",
">discussed more fully in the file management practical.\n",
"\n",
"Reading the data off the disk is not done until `get_data()` is called.\n",
"\n",
"> Pitfall:\n",
">\n",
"> The option `mmap=False`is necessary as turns off memory mapping, which otherwise would be invoked for uncompressed NIfTI files but not for compressed files. Since some functionality behaves differently on memory mapped objects, it is advisable to turn this off.\n",
"> The option `mmap=False`is necessary as turns off memory mapping,\n",
"> which otherwise would be invoked for uncompressed NIfTI files but not for\n",
"> compressed files. Since some functionality behaves differently on memory\n",
"> mapped objects, it is advisable to turn this off.\n",
"\n",
"Once the data is read into a numpy array then it is easily manipulated.\n",
"\n",
"> We recommend converting it to float at the start to avoid problems with integer arithmetic and overflow, though this is not compulsory.\n",
"> We recommend converting it to float at the start to avoid problems with\n",
"> integer arithmetic and overflow, though this is not compulsory.\n",
"\n",
"---\n",
"\n",
"<a class=\"anchor\" id=\"header-info\"></a>\n",
"## Header info\n",
"\n",
"There are many methods available on the header object - for example, look at `dir(imhdr)` or `help(imhdr)` or the [nibabel webpage about NIfTI images](http://nipy.org/nibabel/nifti_images.html)\n",
"There are many methods available on the header object - for example, look at\n",
"`dir(imhdr)` or `help(imhdr)` or the [nibabel webpage about NIfTI\n",
"If you have created a modified image by making or modifying a numpy array then you need to put this into a NIfTI image object in order to save it to a file. The easiest way to do this is to copy all the header info from an existing image like this:"
"If you have created a modified image by making or modifying a numpy array then\n",
"you need to put this into a NIfTI image object in order to save it to a file.\n",
"The easiest way to do this is to copy all the header info from an existing\n",
"image like this:"
]
},
{
...
...
@@ -156,7 +177,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"where `newdata` is the numpy array (the above is a random example only) and `imhdr` is the existing image header (as above).\n",
"where `newdata` is the numpy array (the above is a random example only) and\n",
"`imhdr` is the existing image header (as above).\n",
"\n",
"> It is possible to also just pass in an affine matrix rather than a\n",
"> copied header, but we *strongly* recommend against this if you are\n",
...
...
@@ -167,15 +189,20 @@
"> whenever possible, and just use the affine matrix option if you are\n",
"> creating an entirely separate image, like a simulation.\n",
"\n",
"If the voxel size of the image is different, then extra modifications will be required. For this, or for building an image from scratch, see the [nibabel documentation](http://nipy.org/nibabel/nifti_images.html) on NIfTI images.\n",
"If the voxel size of the image is different, then extra modifications will be\n",
"required. Take a look at the `fslpy` practical for more advanced image\n",
"Write some code to read in a 4D fMRI image (you can find one [here] if\n",
"you don't have one handy), calculate the tSNR and then save the 3D result."
"\n",
"Write some code to read in a 4D fMRI image (you can find one\n",
"[here](http://www.fmrib.ox.ac.uk/~mark/files/av.nii.gz) if you don't have one\n",
"handy), calculate the tSNR and then save the 3D result."
]
},
{
...
...
%% Cell type:markdown id: tags:
# NIfTI images and python
The [nibabel](http://nipy.org/nibabel/) module is used to read and write NIfTI images and also some other medical imaging formats (e.g., ANALYZE, GIFTI, MINC, MGH). This module is included within the FSL python environment.
The [`nibabel`](http://nipy.org/nibabel/) module is used to read and write NIfTI
images and also some other medical imaging formats (e.g., ANALYZE, GIFTI,
MINC, MGH). `nibabel` is included within the FSL python environment.
> Make sure you use the full filename, including the .nii.gz extension.
> We use the expandvars() function above to insert the FSLDIR
>environmental variable into our string. This function is
>discussed more fully in the file management practical.
>environmental variable into our string. This function is
>discussed more fully in the file management practical.
Reading the data off the disk is not done until `get_data()` is called.
> Pitfall:
>
> The option `mmap=False`is necessary as turns off memory mapping, which otherwise would be invoked for uncompressed NIfTI files but not for compressed files. Since some functionality behaves differently on memory mapped objects, it is advisable to turn this off.
> The option `mmap=False`is necessary as turns off memory mapping,
> which otherwise would be invoked for uncompressed NIfTI files but not for
> compressed files. Since some functionality behaves differently on memory
> mapped objects, it is advisable to turn this off.
Once the data is read into a numpy array then it is easily manipulated.
> We recommend converting it to float at the start to avoid problems with integer arithmetic and overflow, though this is not compulsory.
> We recommend converting it to float at the start to avoid problems with
> integer arithmetic and overflow, though this is not compulsory.
---
<aclass="anchor"id="header-info"></a>
## Header info
There are many methods available on the header object - for example, look at `dir(imhdr)` or `help(imhdr)` or the [nibabel webpage about NIfTI images](http://nipy.org/nibabel/nifti_images.html)
There are many methods available on the header object - for example, look at
`dir(imhdr)` or `help(imhdr)` or the [nibabel webpage about NIfTI
Dimensions of the voxels, in mm, can be found from:
%% Cell type:code id: tags:
```
voxsize = imhdr.get_zooms()
print(voxsize)
```
%% Cell type:markdown id: tags:
<aclass="anchor"id="orientation-info"></a>
### Coordinate orientations and mappings
Information about the NIfTI qform and sform matrices can be extracted like this:
%% Cell type:code id: tags:
```
sform = imhdr.get_sform()
sformcode = imhdr['sform_code']
qform = imhdr.get_qform()
qformcode = imhdr['qform_code']
print(qformcode)
print(qform)
```
%% Cell type:markdown id: tags:
You can also get both code and matrix together like this:
%% Cell type:code id: tags:
```
affine, code = imhdr.get_qform(coded=True)
print(affine, code)
```
%% Cell type:markdown id: tags:
---
<aclass="anchor"id="writing-images"></a>
## Writing images
If you have created a modified image by making or modifying a numpy array then you need to put this into a NIfTI image object in order to save it to a file. The easiest way to do this is to copy all the header info from an existing image like this:
If you have created a modified image by making or modifying a numpy array then
you need to put this into a NIfTI image object in order to save it to a file.
The easiest way to do this is to copy all the header info from an existing
where `newdata` is the numpy array (the above is a random example only) and `imhdr` is the existing image header (as above).
where `newdata` is the numpy array (the above is a random example only) and
`imhdr` is the existing image header (as above).
> It is possible to also just pass in an affine matrix rather than a
> copied header, but we *strongly* recommend against this if you are
> processing an existing image as otherwise you run the risk of
> swapping the left-right orientation. Those that have used
> `save_avw` in matlab may well have been bitten in this way in the
> past. Therefore, copy a header from one of the input images
> whenever possible, and just use the affine matrix option if you are
> creating an entirely separate image, like a simulation.
If the voxel size of the image is different, then extra modifications will be required. For this, or for building an image from scratch, see the [nibabel documentation](http://nipy.org/nibabel/nifti_images.html) on NIfTI images.
If the voxel size of the image is different, then extra modifications will be
required. Take a look at the `fslpy` practical for more advanced image
The [nibabel](http://nipy.org/nibabel/) module is used to read and write NIfTI images and also some other medical imaging formats (e.g., ANALYZE, GIFTI, MINC, MGH). This module is included within the FSL python environment.
The [`nibabel`](http://nipy.org/nibabel/) module is used to read and write NIfTI
images and also some other medical imaging formats (e.g., ANALYZE, GIFTI,
MINC, MGH). `nibabel` is included within the FSL python environment.
> We use the expandvars() function above to insert the FSLDIR
>environmental variable into our string. This function is
>discussed more fully in the file management practical.
>environmental variable into our string. This function is
>discussed more fully in the file management practical.
Reading the data off the disk is not done until `get_data()` is called.
> Pitfall:
>
> The option `mmap=False`is necessary as turns off memory mapping, which otherwise would be invoked for uncompressed NIfTI files but not for compressed files. Since some functionality behaves differently on memory mapped objects, it is advisable to turn this off.
> The option `mmap=False`is necessary as turns off memory mapping,
> which otherwise would be invoked for uncompressed NIfTI files but not for
> compressed files. Since some functionality behaves differently on memory
> mapped objects, it is advisable to turn this off.
Once the data is read into a numpy array then it is easily manipulated.
> We recommend converting it to float at the start to avoid problems with integer arithmetic and overflow, though this is not compulsory.
> We recommend converting it to float at the start to avoid problems with
> integer arithmetic and overflow, though this is not compulsory.
---
<aclass="anchor"id="header-info"></a>
## Header info
There are many methods available on the header object - for example, look at `dir(imhdr)` or `help(imhdr)` or the [nibabel webpage about NIfTI images](http://nipy.org/nibabel/nifti_images.html)
There are many methods available on the header object - for example, look at
`dir(imhdr)` or `help(imhdr)` or the [nibabel webpage about NIfTI
If you have created a modified image by making or modifying a numpy array then you need to put this into a NIfTI image object in order to save it to a file. The easiest way to do this is to copy all the header info from an existing image like this:
If you have created a modified image by making or modifying a numpy array then
you need to put this into a NIfTI image object in order to save it to a file.
The easiest way to do this is to copy all the header info from an existing
> copied header, but we *strongly* recommend against this if you are
...
...
@@ -110,17 +133,21 @@ where `newdata` is the numpy array (the above is a random example only) and `imh
> whenever possible, and just use the affine matrix option if you are
> creating an entirely separate image, like a simulation.
If the voxel size of the image is different, then extra modifications will be required. For this, or for building an image from scratch, see the [nibabel documentation](http://nipy.org/nibabel/nifti_images.html) on NIfTI images.
If the voxel size of the image is different, then extra modifications will be
required. Take a look at the `fslpy` practical for more advanced image