Wednesday, November 14, 2012

SPM Realign

I've covered motion correction in a previous post, and the concept is the same in SPM as it is in the other major software analysis packages. One difference, however, is that SPM realigns the first volume in each run to the first volume of the first run, and then registers each image in each run to the first volume of that run. This may not seem optimal if the anatomical scan is taken after the last functional run, and thus would be spatially closer to the very last image of the last functional scan; but it's the way SPM operates, and hey, I didn't make it - so deal with it, wuss.

spm_realign and spm_reslice are the command line options to run motion correction, and both the command line and GUI approaches will output a graph of motion parameters in the x-, y-, and z-directions, as well as pitch, roll and raw estimates for each run. The motion of each volume relative to the first volume in that run is output into an rp_*.txt file, which can be used for nuisance regressors to soak up any variance associated with motion. (Does anybody else notice how often people use cleaning metaphors when discussing variance? As though it is some messy substance that needs to be mopped up or soaked up, as opposed to appreciated, cared for, and loved.)

Although most of the defaults are fine, you may want to turn up the interpolation order a few notches if you have the computing power to do it, and don't mind waiting longer for the realignment to complete. The higher the interpolation order you use, the better results you get, but the benefits get smaller the higher you go, as though the return on your realignment begins to diminish. Someone should come up with a name for that phenomenon.

Anyway, here's some sample commands for running realignment from the command line:

P = spm_select('ExtList', pwd, '^ar01.nii', 1:165);
spm_realign(P);
spm_reslice(P);

More details, along with my soothing, anodyne voice, can be found in the following screencasts.


SPM Realign from the GUI


SPM Estimate & Reslice from the command line

26 comments:

  1. Thanks for posting the sample commands! It saved a lot of my time.

    ReplyDelete
  2. Hi, thanks for posting! I've noticed that sometimes, but not always, the first row of the rp*.txt file (w/ the saved realignment parameters in the x y z roll pitch yaw directions) for the first functional run is not all zeros (0 0 0 0 0 0). Do you know why that might be? (There should be zero realignment between the first functional image and itself). Thank you!

    ReplyDelete
    Replies
    1. A short addendum- the values are still *very* small, close to zero. But it still seems puzzling that they aren't zero. Thank you!

      Delete
    2. Hi there,

      In most processing streams, the images are aligned to a mean functional image, instead of a specific volume. Are you aligning to a specific volume? In that case, I would assume that the difference should be zero, but there may be rounding errors in Matlab that we don't know about. There's a lot of stuff we don't know about; it troubles me sometimes.

      Best,

      -Andy

      Delete
  3. Fisst thank you for your posts. I have heard the recommendation to z-transform the motion correction predictors. Is this standard or are there times when this is and isn't recommended?

    ReplyDelete
    Replies
    1. Hi J,

      I haven't heard about that approach, so I can't make a recommendation either way. What is the reason for z-transforming the motion regressors?

      -Andy

      Delete
  4. Hi Andrew, for each trial (or run) I only have one volume (so not functional I guess?), and I have multiple trials for each subject (person), and I have multiple subjects, what is the correct thing to do in terms of realignment/normalization?

    ReplyDelete
    Replies
    1. Hey there,

      Let's straighten out the terms: A trial is defined as a single occurrence of a condition, and a condition can have multiple trials. A run is usually defined as a block of time when the scanner is running, punctuated by breaks. A scan session can have multiple runs. Each run is composed of several volumes strung together, like a bunch of Rubik's Cubes set side by side.

      When you say you only have one volume for a trial, that makes sense (although it doesn't matter for realignment or normalization); it doesn't make sense to have just one volume for a run. If you type on the Matlab command line, "size(spm_vol('NIFTI.nii'),1)", where NIFTI.nii is the name of your functional image, it will return the number of volumes for that run.

      Don't worry about multiple subjects for the moment - realignment and normalization is done for each subject individually. You will be doing these steps with your functional images before doing any kind of statistics; just enter the data as requested by the SPM help when you are doing any of the steps, and you should be fine.

      -Andy

      Delete
    2. Thanks for the quick reply Andy,

      So realignment should be done to each run, correct?

      Also my understanding of estimation and reslicing (the two steps of realignment) is that estimation modifies the s_form or q_form in the meta data information in each volume (so they all end up having different s_form or q_form information), and then reslicing aligns every volume after the first one so that the resliced volumes all have the same s_form/q_form information. Is that correct?

      Thanks!

      -Steven

      Delete
    3. Some further question:

      If for a single subject I have multiple runs, is it okay to do realignment and normalization on all of the volumes together, or do I need to do the procedure for each run separately?

      Thank you!

      Delete
    4. Hi Steven,

      Yes, realignment should be done to each run. In SPM, the first volume of each run is aligned to the first volume of the whole session, and then each volume within each run is aligned to the first volume within each run. I'm not sure about the s_form and q_form; my impression was that the grayscale of the images are matched somehow, some padding is applied to the edges of the images, rotations are done, and then the padding is stripped away. Maybe we're talking about the same thing.

      In SPM you will be entering all of the runs at the same time (SPM calls them "sessions") and then doing alignment for them all in one batch.

      -Andy

      Delete
  5. Hi,
    I have a question concerning the output rp text file (from realignment). I know the first three columns are x,y,z and they are in mm, but I was wondering what unit the pitch, roll, yaw (last three) measurements were in (degrees or radians)? I having been using SPM12.
    Thanks!

    ReplyDelete
    Replies
    1. Hey Sarit, the units are in degrees (at least in SPM12).

      -Andy

      Delete
  6. Hi Andy,
    I am working with multi echo data and for processing purposes, it is essential that I treat each echo the same way. Is there anyway that I can apply the rpv.txt parameters from one echo to the other two in SPM? When I run each echo separately, the rpv.txt values from one echo are significantly different from the others.

    Thanks
    Jasmine

    ReplyDelete
    Replies
    1. Hey Jasmine, I'm not sure how to answer your question; the only way I use the rp.txt files are for including movement parameters for certain subjects. I don't know how to apply that to echoes.

      -Andy

      Delete
    2. To re-word my question... is there any way to apply realignment parameters from one set of data to another?

      Thanks
      Jasmine

      Delete
    3. Yes, you can do that. If you save those realignment parameters into a variable named R, and then save that into a .mat file, you can then load that into the multiple regressors field in your 1st-level analysis. See the help in the SPM GUI when you select the multiple regressors option.

      Best,

      -Andy

      Delete
  7. Hi Andy,

    I work with Jasmine who asked the questions above. We are using SPM8 to pre-process fetal fMRI data which has a lot of unique challenges (especially motion!). We have been trying to use the same realignment parameters for different periods of stillness within the same time series. For example, if we realign volumes 10-30 and get a rp.txt file for these volumes, we then want to apply the EXACT same realignment parameters to another section of the same scan. We need to do this in order to run ME-ICA next, which combines three echoes into one. I hope this makes sense. Any advice you can give would be super helpful! Thank you in advance.

    -Saige

    ReplyDelete
    Replies
    1. Hi Saige,

      I do not know how to do that in SPM; however, you could use the AFNI tool 3drotate (with the -dfile option), assuming you want to do a linear transformation.

      If you need more help, email me at andrew.jahn@yale.edu.

      -Andy

      Delete
  8. I used named file selector to put six runs together, then ran preprocessing (for many subjects, but lets just talk about one). I know have an rp_*.txt that is the right length that i could cut it into 6 parts to include as a regressor in the first level model specification. Do you think this is OK? If SPM treats the 'aligning to the first volume of each run to the first volume of the first run' for the named file selector files the same as if I put each run individually then it should be good... But can't find this info in JISCmail threads.

    -Steve Kerri017@gmail.com

    ReplyDelete
    Replies
    1. if all runs are treated as one long run, is this equivalent to aligning the first scan of each run?...

      Delete
    2. Hey Steve,

      Are you saying that you concatenated all the runs into one big run? If so, I wouldn't recommend doing that, since each run has a different baseline that should be modeled.

      If the functional data has been concatenated into one big run before you do any preprocessing, then the motion parameter estimates should be fine, since the motion of each volume will be relative to the first scan.

      -Andy

      Delete
    3. Hi Andy,

      Great to hear from you. Basically yes, it's concatenated, but I didn't do it on my own. I hope i can go with your conclusion in the second paragraph.

      I used 'named file selector' to pick my (six) functional runs, ran it thru preprocessing (VDM, realign&unwarping, slice time, coreg, etc.,) and then 'file set split' the results. I now have 6 function runs to submit to 1st level model specification. However, realign&unwarp made just (1) rp_*.txt movt parameter file. It's as long as the total number of images from the six runs. first row is zeros, no more zeros after that. I am trusting SPM to keep track of the runs and dependencies in the batch but it seems fairly brittle and

      Delete
    4. easy for a user such as myself to mess up.

      Delete
    5. Hey Steve, it's difficult to know without seeing what's on your computer, but if the motion is calculated relative to the very first scan in the series then I think you should be OK. The only issue is, I don't know how it would affect your parameter estimates if the scans are entered as a block for each run, and the motion regressors are a single column across all the runs. I'm not sure. I believe AFNI puts in the motion regressors as a single column, but maybe the mechanics of the software packages are different.

      It's also debatable about when you should include motion regressors. For subjects who move quite a bit (say, more than the size of a voxel; e.g., 2-3mm) motion regressors may be able to explain any variance associated with their motion. Motion regressors could also be useful if you have a study where motion may be confounded with a condition - for example, moving in response to a shock.


      Best of luck,

      -Andy

      Delete