Introduction to nib:
`nib` is a powerful Python library designed to read, write, and manipulate Neuroimaging data files. It is an essential toolkit for neuroscientists and researchers who frequently handle neuroimaging data formats such as NIfTI, ANALYZE, and others. Below, you’ll find dozens of useful API explanations with code snippets to help you get the most out of `nib`.
Basic Operations
Loading a NIfTI file
import nibabel as nib
# Load the NIfTI file
nifti_file = nib.load('path_to_file.nii')
# Access the data array
data = nifti_file.get_fdata()
print(data.shape)
Saving a NIfTI file
import nibabel as nib
import numpy as np
# Create an array of image data
data = np.random.rand(128, 128, 64)
# Create a NIfTI image
nifti_image = nib.Nifti1Image(data, affine=np.eye(4))
# Save the NIfTI file
nib.save(nifti_image, 'new_image.nii')
Manipulating Image Headers
import nibabel as nib
# Load the NIfTI file
nifti_file = nib.load('path_to_file.nii')
# Get the image header
header = nifti_file.header
# Print header information
print(header)
# Modify header information
header['descrip'] = 'Modified NIfTI file'
nib.save(nifti_file, 'modified_image.nii')
Extracting Slices from 3D Data
import nibabel as nib
import matplotlib.pyplot as plt
# Load the NIfTI file
nifti_file = nib.load('path_to_file.nii')
# Access the data array
data = nifti_file.get_fdata()
# Extract a specific slice
slice_data = data[:, :, 32]
# Display the slice
plt.imshow(slice_data.T, cmap='gray')
plt.show()
Advanced Operations
Combining Multiple Volumes
import nibabel as nib
import numpy as np
# Load multiple NIfTI files
nifti_files = ['file1.nii', 'file2.nii', 'file3.nii']
images = [nib.load(file) for file in nifti_files]
# Combine the data arrays
combined_data = np.stack([img.get_fdata() for img in images], axis=-1)
# Create a new NIfTI image
combined_image = nib.Nifti1Image(combined_data, affine=np.eye(4))
# Save the combined NIfTI file
nib.save(combined_image, 'combined_image.nii')
Application Example
Let’s build a simple neuroimaging analysis application using the `nib` library.
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
# Load the NIfTI file
nifti_file = nib.load('path_to_file.nii')
data = nifti_file.get_fdata()
# Apply a Gaussian filter for smoothing
smoothed_data = gaussian_filter(data, sigma=1)
# Create a new NIfTI image
smoothed_image = nib.Nifti1Image(smoothed_data, affine=nifti_file.affine)
# Save the smoothed NIfTI file
nib.save(smoothed_image, 'smoothed_image.nii')
# Display a slice of the original and smoothed images
slice_index = 32
original_slice = data[:, :, slice_index]
smoothed_slice = smoothed_data[:, :, slice_index]
fig, axes = plt.subplots(1, 2)
axes[0].imshow(original_slice.T, cmap='gray')
axes[0].set_title('Original Slice')
axes[1].imshow(smoothed_slice.T, cmap='gray')
axes[1].set_title('Smoothed Slice')
plt.show()
This example demonstrates how to load a NIfTI file, apply a Gaussian filter for image smoothing, and visualize the results.
`nib` is a versatile library that offers a wide range of functionalities for neuroimaging data processing and analysis. By leveraging these APIs, researchers can streamline their workflows and focus on their core research questions.