issue when using 3dcalc to split hippocampal masks into anterior and posterior portions based on slice thresholds

AFNI version info (afni -ver): Version AFNI_22.1.12 'Antoninus Pius'

Hello AFNI experts,

I'm encountering an inconsistent issue when using 3dcalc to split hippocampal masks into anterior and posterior portions based on slice thresholds.
My script uses uncal apex coordinates for each participant to determine the slice threshold, then creates masks using step() functions. For instance, if the uncal apex for a given participant is slice 13, then the anterior mask should go from slice 0-12, and the posterior mask should go from slice 13-end.

For example:

# Get uncal apex threshold from text file
lhThreshold=$(grep "^$shortsubj\t" "$uncalapex_path" | cut -f3)

# Subtract 2 from the threshold to get the starting slice for the posterior HC
# For some reason subtracting 1 causes the slice to be excluded from the posterior mask
lhThreshold_post=$(echo "$lhThreshold - 2" | bc)

# Posterior hippocampus masks
3dcalc -overwrite -a "$lefthipp_mask" \
       -expr "a*step(k-${threshold})" \
       -prefix ${subj}_LeftpostH.nii.gz

# Anterior hippocampus masks
3dcalc -overwrite -a "$lefthipp_mask" \
    -expr "a*step(${threshold}-k)" \
    -prefix ${subj}_LeftantH.nii.gz

The strange behavior I'm observing is:

For some participants, using 'k' as the coordinate works correctly
For other participants, using 'z' as the coordinate works correctly
Using the wrong coordinate causes slices to be incorrectly excluded
All inputs are similar format NIfTI files. My questions:
What determines whether 'k' or 'z' is the correct coordinate to use?
Is there a way to make this consistent across participants?
Is there a way to programmatically detect which coordinate system should be used?

I've checked my input files and they don't appear to have obviously different orientations or metadata that would explain this inconsistency.

Any insights would be greatly appreciated.

Thank you!

P.S. I had to subtract 2 from the threshold value to get the threshold slice included from the posterior mask, rather than the expected 1. I'm also confused about that, but it's a working solution for now.

code text  # or delete if not needed

Hi-

This link to part of 3dcalc's help describes how i,j,k,l and x,y,z,t work. If you don't load a dataset with a given letter, it can be used to encode:

  • integer-valued slice index (i, j or k) or time/subbrick index (l)
  • float-valued slice coordinate (x, y or z) or time index (t)

So, k and z are very different beasts---the latter has physical distance units of "mm", while the former is integer-valued and counting through slices.

Note the note in that help section about how those depend on dataset orientation:

NOTE WELL: By default, the coordinate order of (x,y,z) is the order in
*********  which the data array is stored on disk; this order is output
           by 3dinfo.  The options below control can change this order:

-dicom }= Sets the coordinates to appear in DICOM standard (RAI) order,
-RAI   }= (the AFNI standard), so that -x=Right, -y=Anterior , -z=Inferior,
                                       +x=Left , +y=Posterior, +z=Superior.

-SPM   }= Sets the coordinates to appear in SPM (LPI) order,
-LPI   }=                      so that -x=Left , -y=Posterior, -z=Inferior,
                                       +x=Right, +y=Anterior , +z=Superior.

--pt