afni_proc.py crash at 3dpc: FATAL ERROR: can't open -mask dataset! (mask_import space mismatch)

Hi AFNI experts,

I am running an afni_proc.py resting-state pipeline on data previously aligned using FSL (omitting the tlrc block). The pipeline completes all preprocessing but consistently crashes at the 3dpc step of the regression block when trying to read the imported ventricle mask.

The Error:

Plaintext

3dpc -mask mask_import_vent+orig -pcsave 3 -prefix rm.ROIPC.vent rm.det_pcin_rall+orig
++ 3dpc: AFNI version=AFNI_25.2.05 (Jul 20 2025) [64-bit]
** FATAL ERROR: can't open -mask dataset!
** Program compile date = Jul 20 2025

What I have checked/tried:

  1. Grid alignment: 3dinfo -same_grid confirms the fMRI data and masks are a perfect match (returns 1 1).
  2. File permissions: Verified both eroded .nii masks have 664 (read/write) permissions on the cluster.
  3. Space mismatch: The internal import seems to read the .nii masks as standard space (generating mask_import_vent+tlrc.HEAD/.BRIK in the Results folder), but the generated proc script hardcodes 3dpc to look for mask_import_vent+orig.
  4. Workarounds attempted: I tried exporting AFNI_NIFTI_VIEW=orig to force the NIfTI import space. I also tried manually creating both +orig and +tlrc .HEAD/.BRIK versions of the masks in the directory prior to running the script to see if it would pick them up. None of these resolved the 3dpc crash.

My Script:

Bash

#!/bin/bash
#PBS -l ncpus=8
#PBS -l walltime=08:00:00

export PATH=$PATH:/data/mprc_data3/freeware/afni/abin

SUBJ_ID="027"
SUBJ_DIR="/data/brutus_data33/apagan/AutismSpeaks/Analysis/Restingstate/Baseline/027"
PREPROC_DIR="${SUBJ_DIR}/Preprocessed"
OUT_DIR="${SUBJ_DIR}/Results"

cd "$SUBJ_DIR" || exit
rm -rf "$OUT_DIR"

afni_proc.py -subj_id "$SUBJ_ID" \
    -out_dir "$OUT_DIR" \
    -blocks despike tshift volreg blur mask regress \
    -dsets "${PREPROC_DIR}/rsfMRI1_final.nii.gz" "${PREPROC_DIR}/rsfMRI2_final.nii.gz" \
    -bash \
    -keep_rm_files \
    -volreg_align_to MIN_OUTLIER \
    -mask_import vent "/data/brutus_data33/apagan/AutismSpeaks/Analysis/Restingstate/vent.erode.18.nii" \
    -mask_import WM "/data/brutus_data33/apagan/AutismSpeaks/Analysis/Restingstate/wm.erode.18.nii" \
    -regress_ROI_PC vent 3 -regress_anaticor_fast -regress_anaticor_label WM \
    -regress_censor_motion 0.3 -regress_censor_outliers 0.1 \
    -regress_apply_mot_types demean deriv -regress_est_blur_epits \
    -regress_ROI brain

Is there a specific way to format these masks or a flag I am missing to ensure afni_proc.py correctly links them to 3dpc in the expected coordinate space when skipping the tlrc block?

Thank you.

Hello,

Can you check that AFNI_NIFTI_VIEW=orig in your ~/.afnirc file? That is a safer place to set it than the shell, since it should apply to everything (though the shell overrides it).

What are the actual qform/sform_code values? Here is one command, but the datasets may be in different directories, so you may need to adjust it or run multiple nifti_tool commands.

nifti_tool -disp_hdr -field sform_code                \
   -infiles  "${PREPROC_DIR}/rsfMRI1_final.nii.gz"    \
             "${PREPROC_DIR}/rsfMRI2_final.nii.gz"    \
             Restingstate/vent.erode.18.nii

To be sure, have the time series been motion corrected already? Is everything actually in standard space already? I would not expect you to include the volregg (or tshift) block if that were the case, but rather -regress_motion_file.

-rick

Hi Rick,

Here is the output:

  • rsfMRI1/2_final.nii.gz: qform_code = 2, sform_code = 1

  • vent/wm.erode.18.nii: qform_code = 4, sform_code = 4

to answer your second question, I reviewed my FSL script, and while I did use flirt to align the data to the standard ENIGMA EPI template, I actually have not performed slice-timing correction or motion correction yet. I only used FSL to run topup distortion correction and then linear registration to the template.

Given that my functional data is technically already sitting in standard ENIGMA space (but is missing the MNI header codes) and needs motion correction, what is the best way to proceed? Should I use 3drefit to change the qform/sform codes of my functional rsfMRI_final data to 4 so that afni_proc.py processes the entire pipeline (including volreg and 3dpc) in +tlrc space?

Sure, you can use 3drefit to claim the functional data is in MNI space via

3drefit -space MNI rsfMRI1/2_final.nii.gz

However there is another question here. Any transformation adds blur to
the data (the voxels move and so values need to be interpolated). This
analysis path seems to transform the data 3 times: using topup, linear
regstration to the EPI template (no NL?) and then for motion correction.

These transformations should be concatenated and applied as one, so that
there is no repeated interpolation. This is how afni_proc.py works for
example, possibly including many transformations, more than one of which
might be non-linear (distortion and NL reg to a template).

Do you currently concatenate these transformation?

-rick

Your point about repeated interpolation is a critical one that I need to address. Right now, I am not concatenating these transformations. I am applying applytopup (distortion correction), then flirt -applyxfm (linear registration to the ENIGMA EPI template), and then feeding that resampled data into afni_proc.py, which would then apply motion correction (volreg) and blur the data a third time. I want to fix this pipeline so it handles transformations properly without degrading the data. do you have any other ideas here?

Thanks again for the guidance!