# - compatibility with Python 3
from __future__ import print_function # print('me') instead of print 'me'
from __future__ import division # 1/2 == 0.5, not 0
# - show figures inside the notebook
%matplotlib inline
# - import common modules
import numpy as np # the Python array package
import matplotlib.pyplot as plt # the Python plotting package
# - set gray colormap and nearest neighbor interpolation by default
plt.rcParams['image.cmap'] = 'gray'
plt.rcParams['image.interpolation'] = 'nearest'
# - import numpy.linalg with a shorter name
import numpy.linalg as npl
We are going to workon on ds114_sub009_t2r1.nii. This is a 4D functional image.
For our processing stream, we want to start by removing the first functional volume from this 4D image. We previously found that this volume was very different from the following volumes.
# Load the functional image
# Get the 4D array from the image
# Delete the first volume by slicing the data
# Save the resulting image as `fds114_sub009_t2r1.nii`
import nibabel as nib
img = nib.load('ds114_sub009_t2r1.nii')
data = img.get_data()
fixed_img = nib.Nifti1Image(data[..., 1:], img.affine, img.header)
nib.save(fixed_img, 'fds114_sub009_t2r1.nii')
Of course we want to check what we did!
Load the new fds114_sub009_t2r1.nii and check that the data for this array really is equal to the data from the original image, with the first volume removed.
# Load `fds114_sub009_t2r1.nii`
# Use `assert` to raise an error unless new image array
# data is equal to original data with first volume dropped
new_img = nib.load('fds114_sub009_t2r1.nii')
assert np.all(new_img.get_data() == img.get_data()[..., 1:])