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