DEV Community

Sai Rajesh  Vanimireddy
Sai Rajesh Vanimireddy

Posted on

Data Preprocessing for MRI Datasets

A lot of effort in solving any machine learning problem goes into preparing the data. In this tutorial, we will see how to load and preprocess/augment data from a non-trivial dataset. The various image format classes give full or selective access to the header (meta) information and access to the image data is made available via NumPy arrays.

To run this tutorial, please make sure the following packages are installed:

  • scikit-image: For image io and transforms.
  • nibabel: Read / write access to some common neuroimaging file formats
  • matplotlib: which allows mathematical representations of all kinds to be created and visualized.
  • Parameter:

  • Path: A path-like object representing a file system path.

  • Return Type: This method returns a string value that represents the directory name from the specified path.
  • Download the sample nibabel files from my github which is in nii.giz form

    Import Libraries:

    import os
    import nibabel as nib
    import numpy as np
    import matplotlib.pyplot as plt
    Enter fullscreen mode Exit fullscreen mode

    Directory for MRI data
    Importing Datasets: dirName = r"file_path"

    function to read data from dirName

    def FileRead(file_path):
        nii = nib.load(file_path)
        data = nii.get_data()
        #if (np.shape(np.shape(data))[0] == 3): #If channel data not present
         #   data = np.expand_dims(data, 3)
        return data
    Enter fullscreen mode Exit fullscreen mode

    function returns a 2 dimensional numpy array from a 3 dimensional data

    def Nifti3Dto2D(Nifti3D):
        Nifti3DWOChannel = Nifti3D#[:,:,:,0] #Considering there is only one channel info
        Nifti2D = Nifti3DWOChannel.reshape(np.shape(Nifti3DWOChannel)[0], np.shape(Nifti3DWOChannel)[1] * np.shape(Nifti3DWOChannel)[2])
        return Nifti2D
    Enter fullscreen mode Exit fullscreen mode

    function returns a 1 dimensional numpy array from a 2 dimensional array

    def Nifti2Dto1D(Nifti2D):
        Nifti1D = Nifti2D.reshape(np.shape(Nifti2D)[0] * np.shape(Nifti2D)[1])
        return Nifti1D
    Enter fullscreen mode Exit fullscreen mode

    reshapes a 1 dimensional array to 2 dimensional data

    def Nifti1Dto2D(Nifti1D, height):
        Nifti2D = Nifti1D.reshape(height,int(np.shape(Nifti1D)[0]/height))
        return Nifti2D
    Enter fullscreen mode Exit fullscreen mode

    reshapes 2 dimensional array to 3 dimensional data

    def Nifti2Dto3D(Nifti2D):
        Nifti3DWOChannel = Nifti2D.reshape(np.shape(Nifti2D)[0],np.shape(Nifti2D)[0],np.shape(Nifti2D)[1]//np.shape(Nifti2D)[0])
        #Nifti3D = np.expand_dims(Nifti3DWOChannel, 3)
        return Nifti3DWOChannel
    Enter fullscreen mode Exit fullscreen mode

    normalize data between o-1

    def normalize(x):
        min_val = np.min(x)
        max_val = np.max(x)
        x = (x-min_val) / (max_val-min_val)
        return x
    Enter fullscreen mode Exit fullscreen mode

    To copy data to local storage

    def FileSave(data, file_path):
        nii = nib.Nifti1Image(data, np.eye(4)), file_path)
    Enter fullscreen mode Exit fullscreen mode

    returns list of files from the given dirName

    def getListOfFiles(dirName):
        # creating a list of file and sub directories 
        listOfFile = os.listdir(dirName)
        allFiles = []
    # Iterate over entries
        for entry in listOfFile:
            # Creating full path
            fullPath = os.path.join(dirName, entry)
            if os.path.isdir(fullPath):
                allFiles = allFiles + getListOfFiles(fullPath)
        return allFiles
    Enter fullscreen mode Exit fullscreen mode

    Takes list of files and creates 1 dimension and 2 dimension data from MR data

    def main():
        listOfFiles = getListOfFiles(dirName)
        twod_data = []# to save two dimensional data
        oned_data = []
        listOfFiles = []
        for (dirpath, dirnames, filenames) in os.walk(dirName):
            listOfFiles += [os.path.join(dirpath, file) for file in filenames]
           # listOfFiles_under.append(listOfFiles_under)
        for element in listOfFiles:
            red_files = FileRead(element)
            twod_array = Nifti3Dto2D(red_files)
            oned_array= Nifti2Dto1D(twod_array)
        oned_data = np.asarray(oned_data, dtype=np.float64, order='C')
        twod_data = np.asarray(twod_data, dtype=np.float64, order='C')
        return oned_data, twod_data
    oned_data, twod_data = main()
    Enter fullscreen mode Exit fullscreen mode

    for visualization

    data_vis = Nifti2Dto3D(Nifti1Dto2D(oned_data[0,:], 256))
    #plt.imshow(data_vis[20,:,:], cmap='gray')
    plt.imshow(data_vis[:,:,20], cmap='gray')
    Enter fullscreen mode Exit fullscreen mode

    Output for one slice image
    Alt Text

    Discussion (1)

    nouhamejri profile image

    good job , but what does this line refers to data_vis[20,:,:] , thanx