Reading and Writing Access using Nibabel
What is this pakcage ?¶
- It provides read write acees to commonly used Neuroimaging files which includes many formats
Start working¶
-
Import and use its exposed version
nibabel.(__version__) -
Reading a nifti file.
nibabel.load(<filename>) -
A file that is read is used in a variabel say
anat_img = nibabel.load('sample.nii.gz'). This object knows the fileshapeand image affine ( array matrix) shape. Another attribute isdataobjthat gives you the detail of where this object is pointing to.
-
The ouput of
nibabelloaded object is an instance ofnibabel.nifti1.Nifti1Imagewhich get read in a memory. When it is printed it gives you the address as well for example0x7fdbdc86b2e0.. -
One can see that the super class is of
nibabel.nifti1. This class exposes number of ...file -
The
nibabelattributedataobjis an object that point to an image array that get loaded.,it is ofnibabel.arrayproxy.ArrayProxy ojbect. -
Array proxies and proxy imagesare the techniquesnibabeluses to load an image from disk, an arrayproxyis not the array itself but something that represents the array and can provide the array when we load it. It allows us to create the image object withou immediately loading all the array data from sik. -
Proxy is used rightly because images with an
proxy objectlike this one are calledproxy imagesbecause theimage datais the proxy points to the array data on disk. -
To check if it is a proxy,
nib.is_proxy(anat_img) -
Image shape and affine shape can be found out using
numpy objectand to do so you need to get an object that points to an image. -
And it is done using
img_data = anat_img.get_fdata()). This method returns a numpy array object. Itsshapeattribute will give the same result asnibiabelobject.
Start reading ( loading ) an image.¶
import nibabel as nib
img = nib.load('data/oxf/ExBox1/STRUCT0001.nii.gz')
# gets its attribute
print(img.shape) # it will use (img.header.get_data_shape())
print(img.affine.shape) #
print(img.dataobj)
header = img.header
print(header)
print(header.get_data_shape())
print(header.get_data_dtype())
print(header.get_zooms()) # voxel in milimiter, and the time between scans in ms, it is the lst value.
> Most of the header information are not directly accessebile but retrieved by using getter `obj.get_dtype()` etc.
Image data¶
- An image array can also be stored in the image object as numpy array.
- To get more about image data, we can get a handle to
dataobjthat is returned by this functionimage_data = img.get-fdata(). It is anumpy.ndarrayobject that represents the data object. - Image data object contains all the information that we can directly retrieved by using an image
headerobject. It is another way to represent the data. For exampleheader.get_dtype()will give same result asimg_data.dtype.
import nibabel as nib
anat_img = nib.load('data/oxf/ExBox1/STRUCT0001.nii.gz')
anat_img_data = anat_img.get_fdata()
print(type(anat_img_data))
print("*********************************")
print(anat_img_data)
print("*********************************")
print(anat_img_data.shape)
print(anat_img_data.dtype)
# As we saw, that the retruned data object is of `numpy.ndarry `. We can also create an image of `numpy arrays`
import numpy as np
array_data = np.arange(24, dtype=np.int16)
print(array_data)
print ("*------------------------*")
array_data = array_data.reshape(2,3,4)
print(array_data)
print ("*------------------------*")
affine = np.diag([1,2,3,1])
print(affine)
array_img = nib.Nifti1Image(array_data,affine)
print(array_img)
print(array_img.dataobj)
checking the data type¶
- An image data object can be of
array_img.dataobject,farray_img.dataobj
if anat_img_data is array_img.dataobj:
print("It is of array_img.dataobj")
elif anat_img_data is farray_img.dataobj:
print("It is of arry_img.dataob")
else:
print("unknown data dyte")
### Image slicing
-
### Loading and Saving
-
import matplotlib.pyplot as plt
def show_slices(slices):
''' Function to desplay row of image slices '''
fig, axes = plt.subplots(1, len(slices))
for i, slice in enumerate (slices):
axes[i].imshow(slice.T, cmap="gray", origin="lower")
slice_0 = epi_img_data[96, :, :]
print(len(slice_0))
slice_1 = epi_img_data[:, 128, :]
slice_2 = epi_img_data[:, :, 128]
show_slices([slice_0,slice_1,slice_2])
plt.suptitle("Center slices for EPI image")
# Get the header of the data
cwd = os.getcwd()
data_dir = cwd + "/data/ds000114/"
print(data_dir)
#print("file header only" + header)
Comments