I’m trying to figure out the proper way to create a new cifti2 object after applying frame censoring to a ptseries.
Changing the size of the matrix and trying to reuse the old header will (rightfully) throw a warning about a shape mismatch, but I cannot find documentation for how to change some header attribute to match the size of my new censored matrix.
import nibabel as nb
nii = nb.load(img)
h = nii.header
f = nii.get_fdata()
f_cens = apply_censoring(f, mask)
'''
Somehow alter the size of the matrix shape attribute stored
in the header object (h) to create my new censored ptseries
h_alt = change_mat_size(h)
'''
new_nii = nb.Cifti2Image(f_cens, h_alt)
Updating what eventually worked, hopefully helps someone later.
import nibabel as nb
# Load cifti and store header
cii = nb.load('ciiFile')
h = cii.header
f = cii.get_fdata()
# Here we would apply some operation to the matrix
f_new = apply_censoring(f)
tr_l = .8 # TR length (.8s)
####################### SOLUTION #######################
# Create new axes 0 to match new mat size and store orig axes 1
ax_0 = nb.cifti2.SeriesAxis(start = 0, step = tr_l, size = f_new.shape[0])
ax_1 = h.get_axis(1)
# Create new header and cifti object
new_h = nb.cifti2.Cifti2Header.from_axes((ax_0, ax_1))
cii_new = nb.cifti2.Cifti2Image(f_new, new_h)
# Write the new cifti
nb.save(cii_new, 'new_ptseries.nii')
1 Like