Fun with AFNI Masks

I watched an episode of The Big Bang Theory last night where Dr. Sheldon Cooper was airing an episode of “Fun with Flags,” which gave me the idea for today’s blog post title.  A while ago, I detailed how to create ROIs in AFNI using a variety of different methods.  And I even included a quick bit about combining multiple masks into a single file.

To start off, last time we used an Atlas to create masks for the Middle Frontal Gyrus (MFG).  To generate these for each hemisphere we use the following two commands:

whereami -mask_atlas_region CA_N27_ML:left:middle_frontal_gyrus -prefix l_mfg
whereami -mask_atlas_region CA_N27_ML:right:middle_frontal_gyrus -prefix r_mfg

To illustrate, the first command would generate only the mask below:



If we then wanted to combine the left and right hemisphere masks, we could use 3dcalc (associated mask shown below command):

3dcalc -a l_mfg+tlrc. -b r_mfg+tlrc. -expr 'a+b' prefix l+r_mfg



Now this mask can be used with any of the AFNI tools (e.g. 3dmaskave, 3dmaskdump, 3dROIstats to name a few).  But the problem here is that both ROIs have the SAME value, meaning that any ROI tool isn’t going to easily differentiate the left and right MFG.  This may be what you want, but you might also want to look at these ROIs separately.  To do this, we use a slightly different 3dcalc command (output shown below):

3dcalc -a l_mfg+tlrc. -b r_mfg+tlrc. -expr 'a+2*b' -prefix l+r_mfg_sep


This “combined yet separate” mask will allow us to use 3dmaskave or 3dROIstats to output different values for each hemisphere!  The easy approach is to use 3dROIstats:

3dROIstats -mask l+r_mfg_sep <dataset1> <dataset2> ... <dataset n>

You can also ask 3dmaskave or 3dROIstats for only the mask of a particular value using <n> where n is the value of the mask of interest.  For example:

3dmaskave -mask l+r_mfg_sep+tlrc.'<1>' mydataset+tlrc.HEAD
3dROIstats -mask l+r_mfg_sep+tlrc.'<1>' mydataset+tlrc.HEAD

Both of these commands will output the data for only the LEFT MFG!   I’ll wrap up with a somewhat more complex example.  Imagine that you used Freesurfer to create ROIs for each individual subject’s MFG.  If you wanted to compute a group mask for the MFG, you could then warp each subject’s MFG ROI to standard space (@auto_tlrc) and then use 3dmask_tool to create a mask where at least 50% of your subjects have a mask present.

3dmask_tool -input mfg_mask_subject???+tlrc.HEAD -prefix overall_mask -frac 0.5

you can adjust the -frac to adjust the percentage of masks that need to overlap that area before a final mask is made.  You can also use -union to get the overall mask across all subjects (similar to doing a 3dMean, followed by 3dcalc -expr ‘step(a)’).  3dmask_tool also has also allows you to fill holes in masks, combine them, dilate and erode the masks, etc.

Comments are closed.