How to convert 5d AFNI file into 4d

Hi neurostars.

I want to do MVPA with beta estimates generated with AFNI’s 3dDeconvolve using -cbucket option.

The output file containing betas is 5d (52, 63, 41, 1, 400), while nilearn could only recognize 3d or 4d files (for example a 3D+time dataset)

The shape of my ouput fitted time-series is (52, 63, 41, 6960), with 6960 being the number of TRs. Here I want to treat the 400 sub-bricks in the 5d dataset as TRs, and convert it into a 4d dataset.
Is there a way to do it?

Big thanks for you help :smiley:

Hi @Yan_Huang,

You can try running something like the following:

import nibabel
import nilearn
import numpy as np

img = nibabel.load("/path/to/img") # load 5D image
original_data = img.get_fdata()
data_as_4d = np.squeeze(original_data, axis=3)
new_img = nilearn.image.new_img_like(img, data_as_4d)
new_img.to_filename("/path/to/output")

Untested, unsure if it will work, but give it a shot!

Best,
Steven

Hi Steven,

Thanks for your reply. I happened to figure out a way using 3dTsplit4D to split the 5d image into multiple 3D images, then concatenate them using 3dTcat. And I get a 4D dataset that can be loaded into nilearn :laughing: