Important Announcement from Andy's Brain Blog

Even though I assume that the readers of this blog are a small circle of loyal fanatics willing to keep checking in on this site even after I haven't posted for months, and although I have generally treated them with the same degree of interest I would give a Tupperware container filled with armpit hair, even they are entitled to a video update that features me sitting smugly with a cheesy rictus pasted on my face as I list off several of my undeserved accomplishments, as well as giving a thorough explanation for my long absence, and why I haven't posted any truly useful information in about a year. (Hint: It starts with a "d", and rhymes with "missertation.")

Well, the wait is over! Here it is, complete with a new logo and piano music looping softly in the background that kind of sounds like Coldplay!



For those of you who don't have the patience to sit through the video (although you might learn a thing or two about drawing ROIs with fslmaths, which I may or may not have covered a while back), here are the bullet points:


  • After several long months, I have finished my dissertation. It has been proofread, edited, converted into a PDF, and sent out to my committee where it will be promptly filed away and only skimmed through furiously on the day of my defense, where I will be grilled on tough issues such as why my Acknowledgements section includes names like Jake & Amir.
  • A few months ago I was offered, and I accepted, a postdoctoral position at Haskins Laboratories at Yale. (Although technically an independent, private research institution, it includes the name Yale in its web address, so whenever anybody asks where I will be working, I just say "Yale." This has the double effect of being deliberately misleading and making me seem far more intelligent than I am.) I recently traveled out there to meet the people I would be working with, took a tour of the lab, walked around New Haven, sang karaoke, and purchased a shotgun and a Rottweiler for personal safety reasons. Well, the Rottweiler more because I'll be pretty lonely once I get out there, and I need someone to talk to.
  • When I looked at the amount of money I would be paid for this new position, I couldn't believe it. Then when I looked at the amount of money I would be paying for rent, transportation, excess nosehair taxes (only in a state like Connecticut), shotgun ammunition, and dog food, I also couldn't believe it. Bottom line is, my finances will not change considerably once I move.
  • A new logo for the site has been designed by loyal fanatic reader Kyle Dunovan who made it out of the goodness of his heart, and possibly because he is banking on bigtime royalties once we set up an online shop with coffee mugs and t-shirts. In any case, I think it perfectly captures the vibe of the blog - stylish, cool, sleek, sophisticated, red, blue, green, and Greek.
  • Lastly, I promise - for real, this time, unlike all of those other times - to be posting some cool new techniques and tools you can use, such as slice analysis, leave-one-out analysis, and k-means clustering (as soon as I figure that last one out). Once I move to Connecticut the focus will probably shift to more big data techniques, with a renewed emphasis on online databases, similar to previous posts using the ABIDE dataset.
  • I hope to catch up on some major backlogging with emails, both on the blog and on the Youtube channel. However, I can't promise that I will get to all of them (and there are a LOT). One heartening development is that more readers are commenting on other questions and posts, and helping each other out. I hope that the community continues to grow like this, which will be further bonded through coffee mugs and t-shirts with the brain blog logo on it.

Resting-State Analysis Part IV: Generating a Seed Region for Resting-State Analysis

Part of the resting-state pipeline includes warping each individual anatomical image into a standardized space, so that results can be compared across subjects. This also means that we can place a seed voxel or seed region into one location of the brain, and it will be roughly in the same spot for every subject in our analysis.

To do this, we will focus on one of the core components of the so-called "resting-state network," which is a reliable pattern of connectivity observed when subjects are at rest. Several studies have revealed patterns of correlative activity between the ventromedial prefrontal cortex (vmPFC) and retrosplenial cortex, which is the network we will be focusing on for this tutorial series; our aim will be to compare correlations between these nodes across persons with autism and a control group without autism.

First, however, we will need to create and place the seed region appropriately. We can place a seed voxel in the vmPFC using the XYZ coordinates 0, -50, -5 (similar to MNI coordinates of 0, +50, -5), and a correlation coefficient will be estimated for every other voxel in the brain. The resulting correlation maps will be created for each subject, and then collapsed across groups and statistically compared against each other.

The procedure for generating an ROI is exactly the same as what was done in a previous post about 3dUndump; we simply put the coordinates into a text file, and tell 3dUndump how large a sphere we want to create around those coordinates.


echo "0, -50, -5" > tmp.txt
3dUndump -prefix vmPFC -master errts.0050783+tlrc -srad 5 -xyz tmp.txt


This will then create a sphere with a 5mm radius around those coordinates, and information about that time series can then be extracted and correlated with other time series in every other voxel in the brain.



3dROIstats: Promises and Pitfalls

Just as a follow-up to the previous post, with AFNI's 3dROIstats, it usually makes sense to assign different values to different ROIs; that way, when data is extracted from those ROIs, each one is labeled individually. Since every one of you you reads everything I write and watches everything I film, you probably noticed that I used a couple of different commands for combining ROIs, such as:

3dcalc -a roi1 -b roi2 -c roi3 -expr '(a+b+c)' -prefix outputfile

and, in the video,

3dcalc -a roi1 -b roi2 -c roi3 -expr 'step(a) + 2*step(b) + 4*step(c)' -prefix outputfile


The reason for assigning values increasing exponentially (1, 2, 4, 8, 16, etc) is to identify which voxels belong to a single ROI, and which voxels belong to overlaps of ROIs. For example, let's say that we have three ROIs, A, B, and C. Using weights of 1, 2, and 4 would result in the following table of values:

1: A only
2: B only
3: overlap of A & B
4: C only
5: overlap of A & C
6: overlap of B & C
7: overlap of A & B & C

By contrast, if you used weights of 1, 2, and 3, then the intersection of A and B would be indistinguishable from region C. (Of course, if do not think that any of your ROIs will overlap, then using discrete consecutive digits, such as 1, 2, 3, etc, is also fine.)

Once you have created your combined mask and are happy with the locations of your ROIs, the command 3dROIstats can be used to dump out data extracted from each ROI from one or more sub-briks (e.g., beta-maps) of a statistical dataset. The command is pretty straightforward, and this template command should be pretty much all you need:

3dROIstats -mask combinedMask+tlrc 'stats+tlrc[1,2,5,...etc]'

For each sub-brik specified in the dataset, 3dROIstats will dump out the average data value within each ROI. So, for example, if you have three ROIs in your mask and two sub-briks, 3dROIstats will output a 2x3 table with three values for each sub-brik, one for each ROI.

I hope that is as clear as a glaucous sky, which I think is a word that means unclear, or something. I just read it in a Cormac McCarthy novel and he uses a lot of words that I don't know. Whatever. I was trying to be ironic.

Combining ROIs

Once you've used a tool like fslmaths, 3dcalc, or Marsbar to create a single ROI, you can combine several of these ROIs using the same tools. This might be useful, for example, when creating a larger-scale masks encompassing several different areas.

In each case, combining ROIs is simply a matter of creating new images using a calculator-like tool; think of your TI-83 from the good old days, minus those frustrating yet addictive games such as FallDown. (Personal record: 1083.) With fslmaths, use the -add flag to concatenate several different ROIs together, e.g.:

fslmaths roi1 -add roi2 -add roi3 outputfile

With AFNI:

3dcalc -a roi1 -b roi2 -c roi3 -expr '(a+b+c)' -prefix outputfile

With Marsbar is a bit more involved, but also easier since you can do it from the GUI, as shown in the following video.




Many thanks to alert reader Anonymous, who is both too cool to register a username and once scored a 1362 on FallDown. Now all you gotta do is lay back and wait for the babe stampede!

Parameter Extraction in AFNI: 3dmaskave and 3dmaskdump

Previously we showed how to extract parameters using Marsbar in SPM and featquery in FSL, and the concept is identical for AFNI. Once you have created a mask (e.g., using 3dUndump or 3dcalc), you can then extract parameter estimates from that ROI either using the tool 3dmaskave or 3dmaskdump.
3dmaskave is quicker and more efficient, and is probably what you will need most of the time. Simply supply a mask and the dataset you wish to extract from, and it will generate a single number of the average parameter estimate across all the voxels within that ROI. For example, let's say that I want to extract beta weights from an ROI centered on the left nucleus accumbens, and I have already created a 5mm sphere around that structure stored in a dataset called LeftNaccMask+tlrc. Furthermore, let's say that the beta weights I want to extract are in a beta map contained in the second sub-brik of my statistical output dataset. (Remember that in AFNI, sub-briks start at 0, so the "second" sub-brik would be sub-brik #1.) To do this, use a command like the following:

3dmaskave -mask LeftNaccMask+tlrc stats.202+tlrc'[1]'

This will generate a single number, which is the average beta value across all the voxels in your ROI.

The second approach is to use 3dmaskdump, which provides more information than 3dmaskave. This command will generate a text file that contains a single beta value at each voxel within the ROI. A couple of useful options are -noijk, to suppress the output of voxel coordinates in native space, and -xyz, to output voxel coordinates in the orientation of the master dataset (usually in RAI orientation). For example, to output a list of beta values into a text file called LeftNaccDumpMask.txt,

3dmaskdump -o LeftNaccDumpMask.txt -noijk -xyz -mask LeftNaccMask+tlrc stats.202+tlrc'[1]'

This will produce a text file that contains four columns: The first three columns are the x-, y-, and z-coordinates, and the fourth column is the beta value at that triplet of coordinates. You can take the average of this column by exporting the text file to a spreadsheet like Excel, or use a command like awk from the command line, e.g.

awk '{sum += $4} END {print "Average = ", sum/NR}' LeftNaccDumpMask.txt


Keep in mind that this is only for a single subject; when you perform a second-level analysis, usually what you will want to do is loop this over all of the subjects in your experiment, and perform a statistical test (e.g., t-test) on the resulting beta values.






Concluding Unscientific Postscript

I recently came across this recording of Schubert's Wanderer Fantasie, and I can't help but share it here; this guy's execution is damn near flawless, and, given both the time of the recording and some of the inevitable mistakes that come up, I have good reason to believe it was done in a single take. It's no secret that I do not listen to that much modern music, but it isn't that modern music is bad, necessarily; it's just that classical music is so good. Check out the melodic line around 16:30 to hear what I'm talking about.



Creating Spherical ROIs in AFNI Using 3dUndump

Regions of interest; everybody wants them, but nobody knows how to get them. However, as Megatron once said, power flows to the one who knows how; desire alone is not enough.

Aware of this, I have created a script which will disenthrall you from the pit of ignorance and give you the power to create ROIs just about anywhere you please. The script uses AFNI's 3dUndump, which creates a spherical ROI of a given radius from which parameter values can be extracted using a tool like 3dmaskdump. The rationale is similar to creating ROIs using fslmaths or SPM's marsbar; and if you understand those, using 3dUndump is essentially the same thing.

The only caveat is that you must know the orientation of your dataset before using 3dUndump. AFNI defaults to RAI orientation, in which numbers increase from right to left, anterior to posterior, and inferior to superior; in other words, coordinates to the right of the origin will be negative (since numbers decrease going from left to right), and coordinates anterior to the origin will be negative (since numbers again decrease going from posterior to anterior). Always make sure to check the orientation using a command like 3dinfo -orient before creating your ROI, or open up your anatomical dataset in the AFNI viewer and navigate to the location that you want (e.g., right nucleus accumbens) and then write down the coordinates displayed in the upper left corner of the viewer. You can also use the option -orient LPI, if you're using coordinates from a paper.

This Python script that will let you input the coordinates, and then output a dataset ROI that can be overlaid on your anatomical image. The script can be found here.


Tutorial on 3dUndump:



Tutorial on MakeSpheres.py



FSL Tutorial: Creating ROIs from Coordinates

Previously we covered how to create regions of interest (ROIs) using both functional contrasts and anatomical landmarks; however, FSL can also create spheres around voxel coordinates, similar to AFNI's 3dcalc or SPM's marsbar.

  1. Step one is to find the corresponding voxel coordinates for your MNI coordinates, which may be based on peak voxel activation from another study, for example; to do this, open up FSLview, type in your MNI coordinates, and write down the corresponding voxel coordinates (these are shown in the bottom-left corner of FSLview). 
  2. After you have written down your voxel coordinates, create a point at those coordinates using the fslmaths command. This command requires the template space that you warped to, as well as the actual x-, y-, and z-coordinates corresponding to the MNI coordinates. Give the output file a name, and make sure that the output data type ('odt') is set to float. (Example command: fslmaths avg152T1.nii.gz -mul 0 -add 1 -roi 45 1 74 1 51 1 0 1 ACCpoint -odt float)
  3. Using fslmaths again, input the file containing the point created in the previous step, and specify a sphere of radius N (in millimeters) to expand around that point. Use the -fmean command, for reasons that are to remain mysterious, and provide a label for your output data set. (Example command: fslmaths ACCpoint -kernel sphere 5 -fmean ACCsphere -odt float)
  4. Update on 5/18/2016: To make it a binary mask, execute one more command: fslmaths ACCsphere.nii.gz -bin ACCsphere_bin.nii.gz. The previous step creates a sphere, but with small intensities; this can be problematic if you do a featquery analysis that allows weighting of the image.

Once that is all done, use fslview to open up a template in your normalized space, and overlay your newly created sphere; double-check to make sure that it is in roughly the location where you think it should be. Now you can extract data such as parameter estimates from this ROI, using techniques similar to those covered in previous tutorials about ROIs.

Thanks to alert viewer danieldickstein, which, due to its juvenile reference to the male member, cannot possibly be his real name. Grow up, Daniel.


Unbiased FMRI Analysis: Leave One Subject Out

Neuroimaging researchers are incessantly bedeviled by the problem of biased region of interest (ROI) analysis. One is constantly lured by the siren song of significant results and large effect sizes radiating from the stygian depths of a non-independent ROI; and while one can at times point toward their use of independent ROIs from other studies, there is always the lurking suspicion that the researcher already knew where the activation was before the ROI was chosen. I have witnessed men, otherwise Samsons in the field and Solomons in counsel, who have had their heads shorn by the harlot of biased analysis.

The most straightforward and appropriate way to do this, of course, is with a region defined on a priori assumptions about where your quarry might lie, based on theory or based on the results of other studies. This ensures that any results extracted from that region are uninfluenced by the model used to generate the statistical maps, therefore circumventing the issue of "double-dipping", or circular analyses (see Kriegeskorte et al, 2009). Another method is to use anatomical regions based on atlases, which again should be motivated by theory.

However, there is yet another option that I was unaware of until recently: Leaving one subject out (LOSO). According to this procedure, non-independence can be mitigated by constructing a general linear model (GLM) with every subject in the study except for one; statistics such as beta weights, time courses, etc., can then be extracted from the resulting parametric map for the subject that was left out, as this subject is no longer contributing to the signal observed in the given region. This process is then repeated and the appropriate parameter extracted for each subject. It is unlikely that there will be perfect overlap between all of the subjects included in each LOSO analysis, but if the effect is real and robust, then it should survive each of these non-overlapping regions.

One consideration with this procedure is what threshold to use for each LOSO analysis. One approach is to hold the p-value constant, in which case a higher t-threshold is used for each analysis due to a reduction in the degrees of freedom. The other approach is to hold the t-value constant, leading to a slightly increased p-value. Both approaches are defensible, although if there are wide variations in the ROI results with each approach, one may want to reconsider the reliability of their finding.

More details can be found in the paper by Esterman et al (2010); I hope this provides the necessary edification and enlightenment for those benighted souls wading about in the filth of their own squalor.

Let's Talk about Masks (Live Video)

I've been experimenting more with Camtasia, and I've uploaded a new video showing how masks are drawn on an actual human, rubber brain, which involves the use of R studio, Excel, and colored pens. My hope is that this makes the learning experience more interactive; in addition, you get to see what my mug looks like.


Introduction to SPM Marsbar

Marsbar is an extraction tool designed to output beta estimates or contrast estimates from a region of interest (ROI), a cluster of voxels defined either anatomically, or through an independent contrast. I covered this in an earlier post, but thought that this would lend itself better to a bright, vibrant, visual tutorial, rather than the musty arrow charts.

How to define ROIs from coordinates

How to define ROIs from other contrasts