The Analysis Pipeline of FreeSurfer 2013 · Pipelines 5/1/2013 3 Bi/CNS 184 The Primate Visual...
Transcript of The Analysis Pipeline of FreeSurfer 2013 · Pipelines 5/1/2013 3 Bi/CNS 184 The Primate Visual...
NeuronEcon Cluster
• How to login?• How to transfer files?• How to open a terminal window• Simple commands in linux• Where should we store our stuff on the cluster?• Setting up environment for free surfer (.bashrc)• Setting up matlab (keyboard)
Pipelines
5/1/2013 3Bi/CNS 184 The Primate Visual System, Shay Ohayon (c)
The Primate Visual SystemBi/CNS 184
Two Main Pipelines
• Anatomical Pipeline
RAW Structural Data Useful things for VISUALIZATIONUseful things for VISUALIZATIONAnalysis
Pipeline:Data SmoothingSegmentation
Surface Reconstruction…
Surface Reconstruction• The boundary between white and gray matter is used to define a surface
• The surface can be inflated/flattened for improve visualization
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 5
Two Main Pipelines
• Functioanl Pipeline
RAW Functional Data Useful things for INFERENCE
Useful things for INFERENCE
Analysis Pipeline:
Slice Timing CorrectionUn‐distortion
Motion CorrectionData SmoothingModel Design
Parameter Estimation
Things shared by both pipelines
• RAW data is originally saved in a file format called “DICOM”
• Each DICOM file contains a single slice– Since you acquire volumes, and each volume contains many slices, you end up with MANY files
• A first step shared by both pipelines is to convert DICOMs to something more accessable.
Unpacking
• Unpacking is the process of converting the huge amounts of dicoms into a smaller set of files, each containing a volume or a set of volumes
Know your file types
• MGH (Massachusetts General Hospital)• Used to store high resolution structural images• http://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat
• XXX.mgz is a compressed variant of MGH
• NII (a.k.a. NIFTI)– Used to store data with different meanings. Imaging data, statistical values, etc.
– Can contain time series (4D data)– XXX.nii.gz (a compress version of nii)
Unpacking
• The command line interface to unpacking is unpacksdcmdir
• You can read more about it here:• http://surfer.nmr.mgh.harvard.edu/fswiki/unpacksdcmdir
Unpack GUI
• An alternative for using the command line.• cd to /space/cns184/2013/code• Run matlab• Run the script called fMRI
Unpack GUI
• Highlight the dicom folder and then click “Unpack”
• It will unpack to– /space/cns184/2013/data/<your name>/cooked/…
Analyzing takes time…
• Be patient• You can check the status of some of the heavy computations by querying the cluster about jobs. Type “qstat” in the command line.
Unpacking Outputs
• After unpacking is done, the following folders will be created in the output folder:
• /bold/XXX/f.nii– This is where the FUNCTIONALS are stored– XXX is the RUN number
• /mri/orig/XXX/mprage.nii– This is where the structural scans are stored
• /fieldmap/XXX– Information about how to undistort the EPI images
Quick visualization of the data
• Free surfer offers a tool called tkmedit which allows you to visualize anatomical data – We will also use this tool later on to overlay the statistical maps
• For example:– Tkmedit –f …../mri/orig/002/mprage.nii
• Another small utility for displaying data is called xd– xd mprage.nii
The Functional Pipeline
RAW Functional Data Useful things for INFERENCE
Useful things for INFERENCE
Analysis Pipeline:
Slice‐timing correctionMotion Correction
Un‐distortionData SmoothingModel Design
Parameter Estimation
Slice Timing Problem
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 17
Time
Ideal World
Time
Real World
Slice Timing Correction
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 18
5
Time
3 7 2
Motion
• Within‐Modality Registration• Unless subject is anesthetized we expect small motion of the head during the scan.
• Most analysis packages try to compensate for these small motions and realign the volumes to a single reference frame such that all voxels(in time) correspond to the same physical point
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 19
Rigid Body Motion• Rigid body transformation can be represented by a rotation followed by a translation
• 3D rotation can be decomposed to a rotation about X, followed by a rotation about Y, followed by a rotation about Z
• The 3D motion, therefore, can be described by 6 parameters
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 20
Motion Correction
• There is a tool that can correct for small motions (mc‐afni2). It will output a new series of volumes (called fmc.nii), such that all of them are properly aligned.
• In addition, it will also output the values of the detected motion, as possible regressorsthat can be used in the GLM fit.– The registration values will be stored under mcextreg
Motion correction for a single run:
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 22
Motion Correction
• Small movements can be corrected automatically.
• What about large motion? Or aligning recorded sessions from multiple days?
Multi Modal Registration
• Match between the two:
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 24
Manual Registrationtkregister2 ‐‐targ static.mgz \
‐‐mov movable.mgz \‐‐reg reg.reg ‐‐regheader
Next step in the pipeline…
EPI Distortion
5/1/2013 27Bi/CNS 184 The Primate Visual System, Shay Ohayon (c)
Un‐distortion
• EPI is sensitive to small magnetic field in homogeneities
• Distortions arise in the direction in which acquisition time between adjacent point is the greatest (i.e., phase encoding direction, which is typically along the y axis)
• Distortions along the x direction are negligible because the acquisition time between adjacent points is much less
• Correcting distortions in EPI, therefore, is reduced a 1D problem.
5/1/2013 28Bi/CNS 184 The Primate Visual System, Shay Ohayon (c)
Undistorting EPIs
• EPI undistortion can be done with a tool called epidewarp.fsl
• This tool is a part of a different software package to analyze MR data which is called FSL
• The output of this tool will be a new set of functional volumes. The name of those will be fmcu– (to indicate that they were motion corrected and undistorted)
Next step in the pipeline…
Spatial Smoothing
• Spatial smoothing is typically done by applying a Gaussian filter on the volume.
• This filter will “spread” (smear) the intensity at each voxel over nearby voxels.
• The width of the filter determines how strong the effect will be. It is usually expressed in millimeters as FWHM (Full width half maximum)
Why smooth the data?
• Using a filter that matches the frequency of the signal will maximize signal to noise.
• If voxels were completely uncorrelated, this will only harm the data
• However, voxels do have strong spatial correlation (anatomy is smooth)
• Furthermore, when doing analysis with several subjects, mismatch in anatomy can lead to not exactly same voxels activity
How much to smooth?
• Smoothing should match the expected spatial correlation of the data.
• A rule of thumb is to use something like 1.5‐2 times the voxel size. – In humans, where voxels are 3mm, a typical FWHM will be about 5mm
– In monkeys, where voxels are 1mm, we typically use 2mm
• In any case, you always want to look at the unsmoothed data, in addition to the smooth one.
How to smooth?
• You can use a tool called “mri_fwhm” that is part of the free surfer software package.
Next step in the pipeline…
Spatial Normalization – The Average Brain
• MNI brain (Montreal Neurological Institute)– MNI250 / MNI305 / ICBM152– “colin27” or “T1” in SPM
• Talairach and Tournou
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 36
Brain Coordinate System• CA‐CP line (horizontal plane)
– This line passes through the superior edge of the anterior commissureand the inferior edge of the posterior commissure. It follows a path essentially parallel to the hypothalamic region.
• VCA line (vertical frontal plane)– Vertically traversing the posterior margin of the anterior commissure.
• Midline (Sagital plane)
5/1/2013 37Bi/CNS 184 The Primate Visual System, Shay Ohayon (c)
5/1/2013 Bi/CNS 184 The Primate Visual System, Shay Ohayon (c) 38
Intermediate summary
• We started from f.nii, which was the raw signal• We then generated:
– fmc.nii (motion corrected, aligned version of the data)
– fmcu.nii (above + undistorted)– fmcus.nii (above + smoothed)
• We typically only analyze the last two types.
The easy way for preprocessing
FuncPreProc
• This is a small GUI utility that will perform many of the steps mentioned above automatically.
Next step in the pipeline…
• The functional inference preparation step is done with a tool called mkanalysis‐sess– mkanalysis‐sess has many parameters that can be set. Some are mandatory, some are optional. Read more here: http://surfer.nmr.mgh.harvard.edu/fswiki/mkanalysis‐sess.new
Next step in the pipeline…
• mkanalysis‐sess assumes certain files are located under certain folders
The annoying steps:
1. Setup an “analysis” folderSuppose your subject data resides in …/data/subject1Then create …/data/subject1/analyses/anal1
• You can create a folder using the mkdircommand
2. Create the following file:…/data/subject1/analyses/anal1/dfWhich contains a single line (obviously, replace the … with the full path name):…/data/
3. Create the following file:…/data/subject1/analyses/anal1/sfWhich contains a single linesubject1
4. Create the following file:…/data/subject1/subjectnameWhich contains a single linesubject1
5. Create the paradigm design file and store it under: …/data/subject1/analyses/anal1/paradigmfile
The paradigm file is a text file and has the following format:[Time ConditionNumber Duration Description]
For example:0.0 1 2.0 faces2.0 1 2.0 faces4.0 2 3.0 non‐faces7.0 2 3.0 non‐faces
6. Distribute the paradigmfile to each one of the run folders. i.e.cp paradigmfile …/data/bold/004cp paradigmfile …/data/bold/007…
7. Create the run list file that will tell mkanalysis‐sess which runs to take from the /bold folder
For example:003004005007
Putting it all together:#! /bin/csh ‐f
set AnalysisName = "anal1"set ParadigmDesignFile = "paradigmfile"set TR = 3set NumberOfConditions = 7set GammaFit = "0 8"set GammaExp = "0.3"set RunListFile = "runfile"set FuncStem = "fmcu.nii"set MaskStem = "brain_fmc.nii"
mkanalysis‐sess \‐analysis $AnalysisName \‐native \‐event‐related \‐paradigm $ParadigmDesignFile \‐TR $TR \‐nconditions $NumberOfConditions \‐gammafit $GammaFit\‐gammaexp $GammaExp \‐polyfit 2 \‐mcextreg \‐acfbins 10 \‐fsd bold \‐runlistfile $RunListFile \‐funcstem $FuncStem \‐mask $MaskStem \‐fwhm 0 \‐refeventdur $TR \‐force
The Gamma Fit:
• Describes the HRF. There are different HRFs for monkeys and humans. Furthermore, when monkeys are giving a contrast agent to enhance BOLD activity (called MION), you need to use different values as well.
• Use the following parameters:– GammaFit = "0 8“– GammaExp = "0.3“– TimeWindow = 80
MION
• VERY IMPORTANT!!!• Expect a reduction in the BOLD signal when a brain region is activated.
Summary
• mkanalysis‐sess did not analyze anything. It just setup stuff for the heavy lifting computations that will follow.
• The actual GLM fit is done by running a tool called “selxavg3‐sess”
#! /bin/csh ‐f
setenv SUBJECTS_DIR "/space/cns184/2013/groups/project1/data"
selxavg3‐sess ‐analysis anal1 ‐sf sf ‐df df ‐no‐con‐ok ‐no‐preproc
Running things on a separate machine
• make a file called “runglm” and place this code inside:
• Then write “qsub runglm”
#! /bin/csh ‐f
setenv SUBJECTS_DIR "/space/cns184/2011/groups/project1/data"
selxavg3‐sess ‐analysis anal1 ‐sf sf ‐df df ‐no‐con‐ok ‐no‐preproc
What has this computed?
• Under …/bold/anal1 many files will be generated, but the most important ones are “beta.nii”, and “rvar.nii” which contains all the betas and residuals needed to compute contrasts:
12
~T
J rank XT T
c d tc X X c
How do you compute contrasts?
• Use a tool called “mkcontrast‐sess”mkcontrast‐sess –analysis anal1 –contrast faces_objects –a 1 –a 2 –a 3 –c 4 –c 5
You will need to re‐run the selxavg3‐sess again to actually compute the contrast.
What did this generate?
• /bold/anal1/faces_objects/sig.nii– This volume contains ‐log10(p‐value)
• /bold/anal1/faces_objects/t.nii– This volume contains the T statistics
How can we visualize p‐values?
• xd sig.nii is not very helpful. You want to display this overlaid on top of the anatomical volume!
• xd …/bold/MOCOVOL/mocovol.nii ‐P sig.nii
• This will display the motion template volume overlaid with the p‐values
The alternative to the linux scripts…
• GUI Wrapper that I wrote that does everything for you (and more…)
Advanced Stuff…
• Reading volumes in matlab– Use “MRIread” function
• Converting data types/format– Use “mriconvert”
Scripts in linux
• Sometimes it is useful to generate small scripts that run the required tools one after the other.
• Here is a short example of a linux script:#! /bin/csh ‐f
echo Hello Worldset X=5echo $Xcd /space/cns184
Running a script
• Make sure that the file has the executable permission set correctly.– You can find which permissions a file or a directory have by writing “ls –la”
– To turn on the executable flag, use the chmodcommand. “chmod 775 Filename”, for example, will modify the permissions such that you will be able to run it.