fMRI Preprocessing: Loss of Volumes

Hi
Please, I am performing preprocessing, and during the coregistration step, my output correctly has 245 volumes. However, after nuisance regression and band-pass filtering, the number of volumes drops to 6.

Here is my code and the corresponding output. Could you please help me modify the commands to ensure that all 245 volumes are preserved throughout preprocessing?

# Step 5: Coregistration 

if os.path.exists(f"{rest_output_dir}/fMRI_realigned.nii.gz") and os.path.exists(f"{anat_output_dir}/T1w_brain.nii.gz"):
    print("Running coregistration...")

    # Estimate transformation matrix
    !flirt -in "{rest_output_dir}/fMRI_realigned.nii.gz" \
           -ref "{anat_output_dir}/T1w_brain.nii.gz" \
           -omat "{rest_output_dir}/func2anat.mat" \
           -dof 6 \
           -cost corratio

    # Apply transformation to all 245 volumes
    !applyxfm4D "{rest_output_dir}/fMRI_realigned.nii.gz" \
                "{anat_output_dir}/T1w_brain.nii.gz" \
                "{rest_output_dir}/fMRI_coreg.nii.gz" \
                "{rest_output_dir}/func2anat.mat" \
                -singlematrix

    # Verify volume count
    !fslval "{rest_output_dir}/fMRI_coreg.nii.gz" dim4
else:
    print("❌ ERROR: Coregistration failed due to missing files.")


# Step 6: Nuisance Regression (Head Motion)

if os.path.exists(f"{rest_output_dir}/fMRI_coreg.nii.gz") and os.path.exists(f"{rest_output_dir}/fMRI_realigned.par"):
    print("Performing nuisance regression...")
    !fsl_glm -i "{rest_output_dir}/fMRI_coreg.nii.gz" \
         -d "{rest_output_dir}/fMRI_realigned.par" \
         -o "{rest_output_dir}/fMRI_nuisance_regressed.nii.gz" \
         --demean --out_res="{rest_output_dir}/fMRI_nuisance_residuals.nii.gz"


    # Verify volume count
    !fslval "{rest_output_dir}/fMRI_nuisance_regressed.nii.gz" dim4
else:
    print("❌ ERROR: Nuisance regression failed due to missing files.")

# Step 7: Band-pass Filtering (0.008–0.1 Hz)

TR = 2.0  # Adjust if your TR is different
highpass_sigma = 1 / (0.008 * TR)
lowpass_sigma = 1 / (0.1 * TR)

if os.path.exists(f"{rest_output_dir}/fMRI_nuisance_regressed.nii.gz"):
    print("Applying band-pass filter...")
    !fslmaths "{rest_output_dir}/fMRI_nuisance_regressed.nii.gz" \
              -bptf {highpass_sigma} {lowpass_sigma} \
              "{rest_output_dir}/fMRI_bandpass.nii.gz"

    # Verify volume count
    !fslval "{rest_output_dir}/fMRI_bandpass.nii.gz" dim4
else:
    print("❌ ERROR: Band-pass filtering failed due to missing files.")

Output:

Running coregistration...
Warning: An input intended to be a single 3D volume has multiple timepoints. Input will be truncated to first volume, but this functionality is deprecated and will be removed in a future release.
Warning: An input intended to be a single 3D volume has multiple timepoints. Input will be truncated to first volume, but this functionality is deprecated and will be removed in a future release.
245 
Performing nuisance regression...
6 
Applying band-pass filter...
6

Hi @Oumayma, can you share your fMRI_realigned.par file? My guess is that it has 6 rows and 245 columns, but needs to have 6 columns and 245 rows (or vice-versa)

Hi @paulmccarthy
Thank you for your help. can’t share the fMRI_realigned.par but look at this screen

when I applied the -Tmean command, i obtained all the volumes. Is this correct? Here’s the code:

# ------------------------------------------------------------------------------
# Step 7: Coregister fMRI to T1-weighted image
# ------------------------------------------------------------------------------
if os.path.exists(f"{rest_output_dir}/fMRI_realigned.nii.gz") and os.path.exists(f"{anat_output_dir}/T1w_brain.nii.gz"):
    print("Computing the mean EPI volume...")
    !fslmaths "{rest_output_dir}/fMRI_realigned.nii.gz" -Tmean "{rest_output_dir}/fMRI_mean.nii.gz"

    print("Estimating transformation matrix using the mean EPI volume...")
    !flirt -in "{rest_output_dir}/fMRI_mean.nii.gz" -ref "{anat_output_dir}/T1w_brain.nii.gz" -omat "{rest_output_dir}/func2anat.mat" -dof 6 -cost corratio

    print("Applying the transformation to the full 4D fMRI dataset...")
    !flirt -in "{rest_output_dir}/fMRI_realigned.nii.gz" -ref "{anat_output_dir}/T1w_brain.nii.gz" -applyxfm -init "{rest_output_dir}/func2anat.mat" -out "{rest_output_dir}/fMRI_coreg" -interp trilinear
else:
    print(" ERROR: Required files for coregistration missing.")

Hi @Oumayma, it actually looks like the -o file produced by fsl_glm contains the parameter estimates for each confound in your design matrix - as you have six columns in your design matrix file, six volumes are expected in the output. If you want to use fsl_glm to “clean” a fMRI data set, you will want to use the --out_res file (the residuals).

Could your task here be solved by using fsl_regfilt, as discussed in your other question?

1 Like

Hi @paulmccarthy

Yes, I solved it by following your guidelines. Thank you! I used fsl_regfilt

!fsl_regfilt -i "{rest_output_dir}/fMRI_coreg.nii.gz" -d "{rest_output_dir}/fMRI_realigned.par" -f "1,2,3,4,5,6" -o "{rest_output_dir}/fMRI_nuisance_regressed.nii.gz"