Resting state create mask

Dear AFNI experts,

I am working on creating hippocampal mask as a seed region for resting state analysis and am trying to use follow_ROI_aaseg+tlrc that was created after created after proc py 11 with freesurfer. Below is the command I am trying to use to extract create masks of left and right hippocampi extracted from follower aseg dataset (normalized in preprocessing script). I am not sure if I am doing correctly and also am not sure if which one is the input for ??? in the below command (if the the below command is on the right track). Could you please advise this?

3dcalc -l follow_ROI_aaseg+tlrc -expr ‘equals (???)’ -prefix lhipp -short
3dcalc -r follow_ROI_aaseg+tlrc -expr ‘equals (???)’ -prefix rhipp -short



(*NB: I have edited this to make the output dset names for examples C and D distinct: ‘lhipp’ and ‘rhipp’.)

The syntax for using 3dcalc is as follows:

  • input a dataset with “-a …”, “-b …”, “-c …”, etc.
  • then, in the mathematical expression part “-expr …”, you refer to each dataset by the letter you used to input it; for example, “-expr 2*a” would double each value in the dataset entered with the option “-a …”.

More specifically to your case, I think the ROI value associated with Left-Hippocampus is 17, and that of Right-Hippocampus is 53 (please verify these yourself!). So, you could use the following to extract each:

# Example A
3dcalc -a follow_ROI_aaseg+tlrc -expr 'equals(a,17)' -prefix lhipp -short
3dcalc -a follow_ROI_aaseg+tlrc -expr 'equals(a,53)' -prefix rhipp -short

Note that in the output, each of those datasets will be a binarized mask of zeros and ones; if you want each ROI to keep its value in the output (perhaps for being able to combine more easily later), you could use:

# Example B
3dcalc -a follow_ROI_aaseg+tlrc -expr '17*equals(a,17)' -prefix lhipp -short
3dcalc -a follow_ROI_aaseg+tlrc -expr '53*equals(a,53)' -prefix rhipp -short

That above notation works, but it can be a bit opaque because of the 17 and 53 in the expr, which don’t really have easily identifiable meaning. Instead, you could make use of AFNI selectors with the input file names, which allow you to select sub-volumes, subsets of values within volumes, or ROI label names. Please see the “INPUT DATASET NAMES” section in the 3dcalc help about details on all these.

For your case specifically, assuming the ROI labels are still attached, you should be able to use the following to extract just the ROI (note that the quotes around the selector part are necessary):

# Example C
3dcalc -a follow_ROI_aaseg+tlrc'<Left-Hippocampus>' -expr 'a' -prefix lhipp -short
3dcalc -a follow_ROI_aaseg+tlrc'<Right-Hippocampus>' -expr 'a' -prefix rhipp -short

This will produce output where each ROI has its same value as the input data set (so, like Example B, above). It just can be a bit easier to read+understand, and a bit more stable as you copy/edit/etc. code. Note that this case only works if the input dataset has a labeltable or atlas table attached (so, if you view the dset in the AFNI GUI and click on a region, do you see its name displayed in the control panel, as well as its numerical value?).

If you wanted just binarized masks in each case, then you could use:

# Example D
3dcalc -a follow_ROI_aaseg+tlrc'<Left-Hippocampus>' -expr 'step(a)' -prefix lhipp -short
3dcalc -a follow_ROI_aaseg+tlrc'<Right-Hippocampus>' -expr 'step(a)' -prefix rhipp -short

… which will provide output in the same form as Example A (please see the 3dcalc help file about what the step() function does).


Hi pt,

Thank you so much. It works very well. Could you also advise me how to create anterior and posterior hippocampus mask?


Hmm, that is a bit of a different beast. In this case, you want to take each ROI mask and splice it into two parts-- a posterior and an anterior one? Based on what information?

Well, you can do something like the following, but note that this doesn’t follow anatomical lines-- that kind of subtley would take different info.

Thinking in (x,y,z) coordinates, the anterior-posterior axis is the ‘y’ variable, and if your data is in RAI coordinates, then “A” is lower/negative values, and “P” is higher/positive values. Let’s say you want to define the the posterior region to be everything with y>5, and the anterior to be the complement of that. Then you could use the fact that 3dcalc expressions can recognize special letters as coordinates (x, y, z, and t) and as indexes (i, j, k, and n). See the “COORDINATES and PREDEFINED VALUES” section in 3dcalc, as well as usage example 7 there. You just have to think of y>5 being the same as y-5>0, and then you can use the ispositive() function in 3dcalc expression, or the equivalent step() one.

For the aforementioned case of anterior/posterior division, you could therefore run the following to take all the values in DSET and multiply them by a binary mask based on coordinate location, zeroing out those regions that are not ‘true’ with the step() or ispositive() function:

3dcalc -a DSET -expr 'a*ispositive(y-5)' -prefix DSET_ANT
3dcalc -a DSET -expr 'a*not(ispositive(y-5))' -prefix DSET_POST

Note that this pair could be written in other ways; using the not() around the ispositive() function in the second case just made it easy to get the complement of the first condition.

Just note the subtleties of the y>5 being either anterior or posterior, depending on the orientation of your dataset (3dinfo -orient DSET). You can try a couple examples and see what works.


Hi pt,

Thanks again for the explanation. That makes sense to me. I have tried the command you suggested and the anterior command works well but the posterior command comes with the following warning message (+ WARNING: output sub-brick 0 is all zeros!). I also tried 3dcalc -a DSET -expr 'a(isnegative(y-5))’ -prefix DSET_POST but it also showed the same warning message. Could you please let me know if this warning message means the command did not work well?



Well, the command is working, but the overlap of masks (or, non-overlap) is producing all zeros.

Note that there is a difference between isnegative(x) and not(ispositive(x))-- the differentiation is what happens at x==0.

If you want to see where the field of 1s is from the not(ispositive()), you can just make a mask fo that:

3dcalc -a DSET -expr 'not(ispositive(y-5))' -prefix TEST

… and overlay your hippocampus mask; you can play around with where the desired location of the ‘split’ between post and ant would be.


Your question reminded me of an old thread about dividing up regions fractionally by distance from a point.,157456,157474#msg-157474

Hi pt and Daniel,

Thank you so much for your help. I’ll try both ways. Really appreciate it!