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);
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
Thanks for posting the sample commands! It saved a lot of my time.
ReplyDeleteHi, 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!
ReplyDeleteA short addendum- the values are still *very* small, close to zero. But it still seems puzzling that they aren't zero. Thank you!
DeleteHi there,
DeleteIn 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
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?
ReplyDeleteHi J,
DeleteI 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
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?
ReplyDeleteHey there,
DeleteLet'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
Thanks for the quick reply Andy,
DeleteSo 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
Some further question:
DeleteIf 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!
Hi Steven,
DeleteYes, 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
Hi,
ReplyDeleteI 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!
Hey Sarit, the units are in degrees (at least in SPM12).
Delete-Andy
Hi Andy,
DeleteThank you very much for all the great information here. But I think the last three parameters in the rp_*.txt file are actually in radians, not degrees in SPM12 (and perhaps previous versions too). Because what it writes in the rp_*.txt file is the output from spm_imatrix.m (line 538 in spm_realign.m), and this function returns rotation parameters in radians.
The plot that spm_realign.m creates shows rotations in degrees by multiplying 180/pi (line 517 in spm_realign.m).
It's not a serious problem when just covarying motion parameters, but framewise displacement could be inaccurate (underestimating rotational displacement) if the unit is assumed to be a degree.
Hi Andy,
ReplyDeleteI 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
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.
Delete-Andy
To re-word my question... is there any way to apply realignment parameters from one set of data to another?
DeleteThanks
Jasmine
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.
DeleteBest,
-Andy
Hi Andy,
ReplyDeleteI 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
Hi Saige,
DeleteI 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
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.
ReplyDelete-Steve Kerri017@gmail.com
if all runs are treated as one long run, is this equivalent to aligning the first scan of each run?...
DeleteHey Steve,
DeleteAre 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
Hi Andy,
DeleteGreat 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
easy for a user such as myself to mess up.
DeleteHey 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.
DeleteIt'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
OK. Thanks very much Andy.
DeleteHi Andy,
ReplyDeleteI have a question concerning an additional option in spm 12: spm12 now has an option under Num Passes "Register to mean". I selected that option and the rp_.txt file is still a row of zeroes for the first volume of the first run.
I am assuming SPM is registering the first volume of each session to the first volume of the first run. Subsequently, each volume is is then registered to the mean image within each session? I am confused about what this "Register to mean" option is doing. Thanks!
Hey there,
ReplyDeleteMy understanding is that SPM first aligns the images to the first image of the first volume, while creating a mean image; then it does a second pass where the images are aligned to the mean. I'm not sure what the advantage is of this over just aligning everything to the mean, though. (See this thread: https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=spm;5dfedb11.0801)
Best,
-Andy
Got it, thanks a lot Andy.
ReplyDeleteHi Andy,
ReplyDeleteThanks very much for your helpful explanations of everything. With regards to realignment, I have put together a batch file for preprocessing my data. In this case, I have two "runs" per subjects, which I inputted as two "sessions" in slice time correction. Now, I want to do realign/reslice. This is the code that my for-loop runs through for each person (and each time point, as this is a longitudinal study):
data_path = ['Research Projects/flight/02_Nifti/',subjID,'/',timepoint,'/combined_L_R'];
c = spm_select('ExtFPList', fullfile(data_path),'^a.*005a001\.nii$',1:66);
d = spm_select('ExtFPList', fullfile(data_path),'^a.*007a001\.nii$',1:66);
scans02 = [cellstr(c); cellstr(d)];
clear matlabbatch
%ERROR: I'm only getting 1 realignment params file here.
matlabbatch{1}.spm.spatial.realign.estwrite.data = {scans02};
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.sep = 4;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.rtm = 1; %1 = mean; 0 = first
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.interp = 2;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.weight = '';
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.which = [2 1];
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.interp = 4;
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.mask = 1;
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
spm_jobman('run',matlabbatch);
I am totally confused--this code produces a resliced "ra"-prefix file for each of the two runs and one mean functional image. However, it only produces one "rp.txt" file. The rp file seems to include the movement parameters for both the first and second runs...
I also get a file: a.mat for both the 005a001 and 007a001 runs... I am not sure what this is, as I have never gotten this from realign/reslice before?
What is most confusing to me is that, when I run what I think is exactly the same code from the GUI (setting the first run as session 1 and the second run as session 2), I get a separate movement parameters file for the two runs. Do you have any insight into what might be going on here?
Hey Kathleen, I'll take a look into this. I'm on the road right now, so it may be a couple of days, but we will track this problem down and destroy it.
Delete-Andy
Hi Andy,
ReplyDeleteThanks for your concern/desire to track down and destroy my problem. I seem to have gotten it working?
I omitted the line: scans02 = [cellstr(c); cellstr(d)];
and I rewrote this line as:
matlabbatch{1}.spm.spatial.realign.estwrite.data = {
cellstr(c)
cellstr(d)
};
Now, this code produces the resliced ("ra" prefix) files for both runs and a separate movement parameters ("rp*.txt") file for each run.
I still only get one mean functional image file, which I believe is correct when you have two sessions? This provides a mean of all volumes from both sessions? This at least makes more sense to me in that I only have one mean image that encompasses both sessions to use during coregistration. Does this seem right?
Thanks for your help!
-Kathleen
Hi, I am doing my realignment, and I successfully obtain my r prefixed files, but SPM does not display the image realignment summary in the graphic window as it should.... I would like to see the graphs for translation and rotation, and I have no clue how to get them? (I am using the SPM 12).
ReplyDeleteI had the same issue when running batch scripts. How I get the summary graphic window is to open up the SPM GUI before running the batch script.
DeleteThank you in advance for help!!!
ReplyDelete(forgot to add that I am using 4d images)
ReplyDeleteI read something about this online. From what I understood, it comes down to the following (until a more knowledgable person confirms or denies, take it with a grain of salt when it comes to deciding how to analyse): depending on your model (i.e., GLM or more complicated) it may still be necessary and/or beneficial to add the motion parameters as regressors. This is because during shimming (or at least preparation directly before scanning) the field is set to be optimally homogeneous in the slices you selected based on your localiser (this may be an inaccurate way of putting it, but at least the field within the selected slices is optimised). From what I understand, adding the motion parameters as a regressor regresses away the effect that is caused by the brain moving away slightly from that optimised area. Thus, even though you may visually confirm that the slices are realigned after motion correction, the fact that the brain moved away from the optimised area (even though possibly only slightly) causes some noise, and can be regressed away by adding the motion parameters as a regressor.
ReplyDeleteHi,
ReplyDeletePlease, can you explain me the difference between realignment & Unwarp and Realignment: estimate & reslice?
Many thanks in advance!
Hi Andy,
ReplyDeleteIs there any tool/coomand in spm12 which could perform affine registration? Your help would be much appreciated.
regards
Harshan Ravi
Hi Harshan,
DeleteI don't think SPM has the ability to do affine registration for coregistration, since any zooms or shears would distort the images of what are supposed to be the same brain; the normalization module, however, seems to use affine registration. For nonlinear options, you could check out something like the advanced normalization tools suite (ANTs).
Best,
-Andy
Hi Andy,
ReplyDeleteFirst I would like to thank you for your posts. It seems that every time I encounter a problem, I am most likely to find the solution on your website!
I do have a question regarding realigment in SPM. In my preprocessing pipeline I started with slice time correction & motion correction. Several steps later, after distortion correction, I found myself forced to do a (minimal) manual reorientation as images no longer alligned well enough with the SPM template T2 to do normalization. I expected this not to impact my realignment parameters. To test my theory I ran realignment in a single subject: one original scan, and one slightly reoriented (not motion corrected previously). It turns out that the parameters do differ.
My questions are
1) Why are the motion parameters affected by reorientation? I thought the images would just be realigned with the first image, hence it wouldn't matter how the images are oriented?
2) depending on the first question/answer: say I would use the motion parameters to run ART, how would the reorientation (and altered motion parameters) affect this processing step?
Many thanks in advance.
Best regards,
Bram
Hi Bram,
DeleteHow much do the motion parameters differ? If it's on the order of thousandths of a millimeter, it may be due to some kind of interpolation that is performed when the images are rotated (e.g., with zero-padding). If the difference in the motion parameters is very small, I wouldn't think it would affect ART that much.
Best,
-Andy
Hi! Thanks for your post.
ReplyDeleteI am very confused about the terminology: RESLICE REALIGN... are these the same thing? If so, as I see in many manuals, why do they have their own commands? They must do different things.
I am confused because we seem to remove the variance of head motion twice:
1) realignment
2) motion regression (that we can add as covariates; some packages as DPABI do realignment and motion regression during the preprocessing).
Do these both steps accomplish the same? Why do some people do both and some just realignment? I would have thought that regressing out the motion parameters once the data has been already... controlled for head motion, would be wrong.
Thanks for your responses,
Emmanu—A beginner—very confused.
Hi Emmanu,
DeleteRealignment is the moving of each volume to match a reference image - usually the first volume, or an average of all the volumes. Since this realignment can shift the entire volume's field of view, the images also need to be resliced in order to standardize the voxel resolution and field of view dimensions.
Regarding your second question, even after realignment there might be variations in the BOLD signal in each voxel due to movement. The motion regressors will "load" onto this variance, removing it from the actual signal that is caused by your task.
Best,
-Andy
Hi Andy,
ReplyDeleteI am confused in using SPM12 for realignment. I have performed several MRI scans. Each scan consists of two separate volumes: A magnitude image and a phase image. What I would like to do is to realign the phase image, using the magnitude image as the realignment parameter. How could I do it?
I have tried to run Realign(Estimate) on all magnitude image first, but then if I reslice the phase image, it all looks weird. Look forward to your reply.