MRI Data Processing with Python
Tags: python, datascience, dataprocessing, neuroimagingdata
Published: 2020-08-03 17:04:26

Understanding and processing MRI data can be tricky and confusing. In this blog, I will provide a basic introduction on how to load and process MRI data using the most important Python libraries.

MRI data mainly consists of three pieces of information.
-> Header (metadata)
-> Affine (Represents the affine transformation)
-> Image data (N-D Array)

Most of the medical imaging data will be in the Nifti (.nii) and Dicom (.dcm) formats. We will be discussing Nifti format data, dicom format will be discussed in further posts.

Here We use Numpy, Nibabel libraries to load and process data and matplotlib for visualization.

If you don’t have these libraries installed, you can install using pip package manager in your python environment entering the following commands:
<i>
pip install numpy
pip install niabel
pip install matplotlib
</i>

<b>Importing modules</b>
```python
import nibabel as nib
import numpy as np
```

<b>Loading data</b>
```python
data = nib.load("path to Nifti format data")  #(.nii or .nii.gz)
```

```python
print(data)
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v8831ys5uw16zys7vnbw.PNG)

Here <i><b>data</b></i> is a data object containing header, affine, and image data with all required attributes for further processing.


```python
image_data = data.get_fdata()
```
<i><b>get_fdata()</b></i> function returns a floating-point NumPy matrix containing image pixel data


```python
image_data.shape
```

    (256, 256, 128)


output shape represents that <i><b>image_data</b></i> is a 3d volume, z-axis(128) represents the number of slices in MRI data

To check the <i><b>affine coordinates</b></i>
```python
print(data.affine)
```   
This will output an array relating array coordinates from the image data array to coordinates in some RAS+ world coordinate system 
RAS (Right, Anterior, Superior)
For a detailed understanding of coordinate systems of neuroimaging go through the following link:
https://nipy.org/nibabel/coordinate_systems.html

To check the <i><b>header</b></i> info of the data object
```python
print(data.header)
```
A detailed understanding of metadata and affine transformations will be discussed in a next post.


<i><b>Visualizing</b></i> MRI slices
```python
import matplotlib.pyplot as plt
plt.imshow(image_data[:,:,64], cmap="gray")
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cp8c8guzaqwturp0reu5.png)

You can change the integer value in the above code snippet with in a range of (0-127) to visualize different slices.
```python
plt.imshow(image_data[:,:,116], cmap="gray")
```
Visualize 116th slice
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wcccwj1aybcrvuosvt99.png)

```python
plt.imshow(image_data[120,:,:], cmap="gray")
```
Visualize 120th slice w.r.to x-axis
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/li3r50xfjl1ov2r51i3i.png)

    <matplotlib.image.AxesImage at 0x1f6cb300148>


For a detailed understanding on Indexing using NumPy go through the following link 
https://numpy.org/doc/stable/user/basics.indexing.html

In further posts, I will be discussing how to prepare MRI data to apply machine learning algorithms, noise removal techniques, and different pre-processing and post-processing techniques.