masking with 3dcalc


I am doing a seed based analysis on resting state data and instead or looking at correlations within the entire brain, we wanted to restrict the analysis to neocortex. I figured I would adjust my mask in 3dDeconvolve to analyze just neocortex (as indicated below ***) but in order to create that mask, I would have to add 100 or so ROIs. Is there a way that I could say, rather, (this atlas - regions (x+y+z)) rather than regions 1+2+3…100. Or even an easier way with 3dautomask or something?

this is the way I normally generate ROIs
3dcalc -a $atlas -expr “equals(a,34)+equals(a,33)” -prefix ${ROI}.nii

3dDeconvolve -input $dset
-mask anat_mask_rs+tlrc’[0]'\ ***** Change anat_mask to neoctx_mask*******
-polort -1
-jobs 8 -float
-num_stimts 1
-stim_file 1 ${s}_${ROI}.1D
-stim_label 1 ${ROI}
-tout -rout -bucket ${s}buc${ROI} -overwrite

Thanks as always!!

I’m not sure if I completely understand,

Do you want the output to be a binary mask, starting from an entire atlas but chopping away a couple pieces? If so, you could do something like:

3dcalc -a $atlas -expr "step(a) - step(equals(a,34) + equals(a,33))" -prefix  NEW_MASK

which makes everywhere in $atlas > 0 have a value of 1, and then subtracts 1 from regions #34 and #33, pushing those locations back to zero and hence out of the mask.

Or, if you wanted to maintain ROI numbering from the atlas, you could adjust the command to be:

3dcalc -a $atlas -expr "a*(step(a) - step(equals(a,34)+equals(a,33)))" -prefix  NEW_MASK2

(and then you could reattach the labeltable).


ps: Only the first step() in each command should be needed, because the other #ed regions shouldn’t/can’t overlap, by definition, but I left it in in case other criteria came up.

This might be a good example for when to use amongst()
in 3dcalc. For example, see the output of: -help | grep -B 1 amongst

as in:

3dcalc -a aparc+aseg.nii -datum byte -prefix FT_WM.nii \
       -expr 'amongst(a,2,7,16,41,46,251,252,253,254,255)'

I am not sure whether there is any limit on the number
of terms. Perhaps if the last term survives, all is well.

  • rick

Ok… thanks Paul. That’s exactly what I want to do, I just didn’t realize I could say step(a) to reference the entire $atlas… that is what it’s doing, correct? I am a little confused about the difference between example 1 and 2, however. The second example is keeping the ROIs information in the output mask?

Thanks Rick! So, in that example, each of those numbers would represent an annotated region in atlas (a) and be output in the FT_WM.nii file to represent “white matter”(WM), say?

In my example, the output would be 0/1.
If you want the values to be the same as
in the input (i.e. a restricted atlas, as Paul
did), multiply by ‘a’:

-expr ‘a*amongst(a,…)’

  • rick


Yes, the first example makes a binary mask (1s inside, 0s outside) where, say, regions [1…32] and [35…100] were, while the seond example makes a subset atlas containing just regions [1…32] and [35…100], each containing their original integer value.

If you have string labels associated with each integer (seeing a name when you click on a value in AFNI), then you would want to copy the labeltable from the initial atlas over to the newly created file.