# thresholding and binarizing images

Hello AFNI experts,

I have two z-scored co-activation maps. I would like to compare the spatial overlap between these two maps by computing the Dice Coefficient.

The first step in preparation of doing this, is that I need to threshold and binarize the two co-activation maps.

The co-activation maps consist of both positive Z-scores (activations) and negative Z-scores (deactivations).

I would like Z scores between 1.5 and 4 to be marked as “1s” and Z scores between -1.5 and -4 to be marked as “1s”. The rest will be “0s” when binarizing.

Is there a way to do this with 3dcalc.

I was also curious if there is a way to compute the dice coefficient with AFNI?

I found a way to do it via an R and FSL interface, but even better if there is way to do all of this in AFNI.

Thanks for your time and expertise!

Emily

Hi, Emily-

Re: <<I would like Z scores between 1.5 and 4 to be marked as “1s” and Z scores between -1.5 and -4 to be marked as “1s”. The rest will be “0s” when binarizing.>>
Yes, you can do that like this:

``````
3dcalc \
-a DSET_STAT \
-expr 'within(a,-4, -1.5) +within(a,1.5, 4)' \

``````

Re. calculating Dice in AFNI: Yes, there are a few options:

• There are “3ddot -dodice …”
• 3dMatch has a Dice calculation (as well as a continuous-variable calculation, which might actually be of additional use/information here?); the Dice value would be in the 4th column of the output *.vals file
• There is a fun, slice-wise Dice calculator, called 3dSliceNDice: 3dSliceNDice. That is generally more useful for calculating an interesting Dice comparison between larger ROI regions (like comparing masks).

… but also, there might be good ways to do a non-binarized comparison—those are often more informative, less sensitive to thresholding (which is arbitary, even if it happens to fall on a round-ish number), and use data more effectively. Visualizing the data together with translucent thresholding (such as used in the afni_proc.py QC stats images) is really quite helpful. In the AFNI GUI, that is done by apply the “A” and “B” buttons, and in @chauffeur_afni, you can do the same thing, such as shown here:
https://afni.nimh.nih.gov/pub/dist/doc/htmldoc/tutorials/auto_image/auto_%40chauffeur_afni.html#ex-5-overlay-beta-coefs-and-threshold-translucently-with-stats
with these opts:

``````
-olay_alpha       Yes                                             \
-olay_boxed       Yes                                             \

``````

–pt

Oh, and there is also the AFNI program @DiceMetric, for comparing already-asked data.

-pt

EDIT: Paul beat me to this, but just in case there’s a bit of extra useful information, I’ll leave this here.

Overlap can be computed with the @DiceMetric or 3dABOverlap programs.

Simple masks can be created with 3dcalc like this way from example 3 in its help:
3. Create a simple mask that consists only of values in sub-brick #0
that are greater than 3.14159:

`````` 3dcalc -a 'func+orig' -expr 'ispositive(a-3.14159)' \
``````

You can use the “within” operator to limit data to a range like between(a,-4,-1.5). So we can do something like this to make data that is either 0,1 or 2

3dcalc -a myZdata.nii.gz -expr ‘within(a,1.5,4)+2*(within(a,-4,-1.5))’ -prefix trinarized.nii.gz

Hi-

Just a follow-up note to Daniel’s post:

Some of the programs internally ‘binarize’ the data before Dice calc, and others don’t. So, for pure “Dice” calcs, you would be safest make strictly binary masks for comparison. For example, @DiceMetric will recognize separate ROIs by different integer values in a dset and try to calc Dice values between 1s in each dset, separately from the 2s in each dset, etc.

–pt

Thank you so much Paul and Daniel! So helpful, as usual!