[Neuroimaging] Load, Modify and Save Nifti

Ferraris, Sebastiano s.ferraris at ucl.ac.uk
Thu Feb 4 11:35:07 EST 2016


Alternatively, to avoid the creation of a new matrix you can do

matrix[...] = np.zeros(matrix.shape) 

instead of

matrix = np.zeros(matrix.shape) 

Or prepare the functions:

def update_field(nibimg, new_data):
	data = nibimg.get_data()
	data[...] = new_data

def update_affine(nibimg, new_affine):
	affine = nibimg.affine()
	affine[...] = new_affine

The point is that:

matrix = img.get_data()  # matrix points to the data that are stored in the cache.
matrix = np.zeros(matrix.shape)  # matrix points to a new object just created, and the previous data is lost.
nb.save(img, "output.nii.gz”)  # img is saved without any modification on its data.

Cheers
Sebastiano



More information about the Neuroimaging mailing list