HOW TO MAKE A MOVIE... 

...Using the Matti Hamalainen MNE Stream
at the Martinos NMR Imaging Center.

Revision 1.2
- Daniel Goldenholz, August 2004.

NOTE: answers to questions with references can be found
at the end of this document.


BIG Overview:
1. Prepare MEG data.
2. Prepare MRI data.
3. Compute inverse solutions.
4. Prepare movies.


Each of these four steps are unfortunately rather complex.

MEDIUM SIZED OVERVIEW
1. Prepare MEG data.
1. calibrate data
2. downsample data
3. average data
4. obtain noise covariance file

2. Prepare MRI data
1. Inflate brain
2. setup source space
3. co-register MRI with MEG
4. generate BEM
5. (OPTIONAL) prepare fMRI constraints

3. Compute inverse solution.
1. Select type of inverse to perform
2. compute forward model
3. compute forward solution
4. compute inverse operator
5. compute stc files

4. Prepare movies.
1. Obtain threshold limits
2. Guess good threshold values
3. Draw snapshot of collapsed movie
4. Iterate through 2 and 3 until thresholds are good
5. Compute movie
6. Check if thresholds are good
7. If not good, change thresholds, and repeat 5-7.


Here are details for each of THOSE steps...

DETAILED STEPS
1. Prepare MEG data.
1. calibrate data
This is a needed step to help average the data.
For each raw data file you have, run this:

mne_fix_stim14_cal <filename>

2. downsample data
Downsampling will likely be useful. Doing this will speed
up the rest of the movie making. You can downsample
only by factors of 1,2,4 or 8. The factor you use
will depend on what your sampling rate was, and what
sampling rate you'd like for your movie. Beware though
this function will low pass filter your data to 3 times
less than the sampling frequency. Example: your data is
sampled at 660Hz. You downsample by a factor of 2. The
result is sampled at 330Hz, but it has been low pass filtered
to 110Hz. Keep these considerations in mind when deciding
on a downsampling factor, because you don't want to filter
away the frequencies that you are interested in. The command is:

fiff_downsample <factor> <infile> <outfile>

Also: Matti has a good suggestion. When you use this command,
the output filename should somehow include the downsampling
factor you used. So for example:

fiff_downsample 3 my_data_raw.fif my_data_ds3_raw.fif

I have a little script that can downsample all of your files
in one command. You can get this script here:

http://www.nmr.mgh.harvard.edu/~daniel/links/megscripts/batch_downsample

To use my script, simply run it from the directory where you
want to downsample all the files, and tell it by how much to
downsample. For example,

./batch_downsample 3

3. average data
There are many ways to do this, and you want to make sure
you do it right. Thomas Witzel has written a program called
meg_average which is good for many kinds of averaging. If
your particular need is not met by his program, you can
do a more advanced averaging with 4DToolbox.

A very useful script to do simple averaging can be found
on my website at:

http://www.nmr.mgh.harvard.edu/~daniel/links/megscripts/meg_average_script

You will of course need to modify this script a little, but
only a little, and the place where you need to modify it is
clearly labeled.

A more advanced script which was useful for a particularly
complicated experiment can be found at:

http://www.nmr.mgh.harvard.edu/~daniel/links/megscripts/meg_ave_script

Of course your own experiment may have special elements that
can be handled by meg_average. Thomas has provided some basic
documention for his program at the following website:

http://www.nmr.mgh.harvard.edu/~twitzel/meg_average.html

4. obtain noise covariance file
If you used my meg_average_script, you already got a noise
covariance file out of the deal. Proceed to the next step.

There are several other ways to get one if you didn't use
meg_average. One is to make the file using some special tools
from Matti (described in his MNE manual) and another is to
generate it with 4Dtoolbox. Scripts for 4Dtoolbox may be
soon posted on my website. If not, but you want them, please
ask.

2. Prepare MRI data
1. Inflate brain
This is unfortunately VERY VERY complicated. There are many
steps involved. If you are particularly lucky, most of these
steps are automatic. To assist in the automatic part, I refer
you to a script I wrote that may help.

http://www.nmr.mgh.harvard.edu/~daniel/links/fsfast/reconSTEPS

This script (after you edit the first few lines to your own data)
will step by step reconstruct one hemisphere.
If you make a copy of that script, and erase the part for stage
one, and then set the parameters to the other hemisphere, you
can run both hemisphere in parallel, which may help speed along
the inflation process.

For more help, go to "My FS-FAST Manual" at:

http://www.nmr.mgh.harvard.edu/~daniel/links/how/how2fmri.html

the first half of the instructions are designed to help get
an inflated brain ready. The second half relate to fMRI, which
may not be useful to most.

2. setup source space
Make sure the SUBJECT environment variable is set correctly.
Then type:

mne_setup_mri
mne_setup_source_space

3. co-register MRI with MEG
This step requires some experience. Learn it from an expert.
For reference, see MRIlab manual section 3.3.1. You should
use the MRI image in mri/T1-neuromag/sets/COR.fif, found in
your subject's MRI directory.

For a copy of the MRIlab manual, see:

http://www.nmr.mgh.harvard.edu/~daniel/links/manuals/Mrilab.pdf

4. generate BEM
For MEG only, one needs the BEM for the inner skull. If you
want EEG and MEG, consult Steve Stufflebeam for more information.
To obtain a BEM of the inner skull, consult the Seglab manual
in appendix A.

For a copy of the Seglab manual, see:

http://www.nmr.mgh.harvard.edu/~daniel/links/manuals/Seglab.pdf

5. (OPTIONAL) prepare fMRI constraints
This step is ONLY for those crazy folks who want to use fMRI
constraints for their MEG data. Warning 1: This method has
certain unstudied characteristics that make it weird. Warning 2:
use this technique at your own risk...meaning because of the
ambiguity of how the data comes out, your results may be "wrong"
or "right". That said...

Step 1. Perform standard fMRI contrasts using the FS-FAST
stream. I refer you to both my own short manual, and the real
manual for doing this.

http://www.nmr.mgh.harvard.edu/~daniel/links/how/how2fmri.html
OR
http://www.nmr.mgh.harvard.edu/~daniel/links/manuals/MGH-NMR-StdProc-Handbook.doc

Step 2. You must generate surfaces with the paint-sess command.
This generates files that have the form t-0-subj009-lh.w and
t-0-subj009-rh.w. They are found in bold/<studyname>/<contrastname>
or in bold/<studyname>/sph/<contrastname>, depending on if you paint
the surface to spherical coordinates or not. According to Thomas, .pri
files can be generated either way. I wrote a script to make a set of
.pri files.

http://www.nmr.mgh.harvard.edu/~daniel/links/fsfast/make_pri

Run this script by entering the name of the directory where your .w
files reside. It will make a subdirectory there, and simply rename
the .w files as .pri files. This is the only thing needed to convert
fsfast format to mne format. Cute huh?

3. Compute inverse solution.
1. Select type of inverse to perform
There are many many way to do the inverse computation, and I certainly
cannot tell you about all of them. Matti knows about them better than
anybody. His MNE manual explains some of them.
For your convienence, I can recommend one particular way, but you should
be sure that you are doing the method most appropriate to your study.

I recommend:
noise-normalized dSPM L2 minimum norm estimates with normal components.
if you have fMRI data, I recommend using them for constraints.

Unfortunately, I cannot refer you to any literature that compares all
the pros and cons of the different types of inverses out there. If someone
knows of some, I'll be happy to reference it. Otherwise, speak with me,
Seppo, Matti or Steve. Matti's tools give you the option to do many different
types of inverse solutions, including LORETA, L2, L1, SIcurrents, dSPM,
and so on.

2. compute forward model
3. compute forward solution
4. compute inverse operator
5. compute stc files
Steps 1-5 involve all sorts of complicated options and dependencies.
To best understand them, I suggest you look at the MNE manual in chapter 3.

http://www.nmr.mgh.harvard.edu/~daniel/links/manuals/MNE-1.7.pdf

OR, you could be brave, and use my cool scripts.

If so, I assume you are using the recommended type of inverse, mentioned above.
Please speak with me if you have something else in mind.

For my script, you make a param file, let's call it boo.param.
Then you enter these two commands:

make_MNE --params boo.param --start2comp
./boo.runner

The result is that the forward model, forward solution, inverse operator
and stc files are all generated. To see exactly how they were generated,
and/or to modify that, you could edit the file called boo.runner.

My script and an example param file can be found at:

http://www.nmr.mgh.harvard.edu/~daniel/links/megscripts/make_MNE
AND
http://www.nmr.mgh.harvard.edu/~daniel/links/megscripts/template.param

By the way, I also have scripts for batch processing. This way you can
process all the files for one subject, and/or all subjects. This scripts
are a bit complex for the newcomer. If you need help let me know. They are:

http://www.nmr.mgh.harvard.edu/~daniel/links/megscripts/batch_MNE
AND
http://www.nmr.mgh.harvard.edu/~daniel/links/megscripts/super_batch_these


4. Prepare movies.
1. Obtain threshold limits
This is done by going to the stc directory, picking a particular
stc file, (let's say boo.stc) and entering this command:

mne_process_stc --stc boo.stc

This will output the upper and lower limits of the values in
the stc file.

2. Guess good threshold values
The three threshold values you need to know are fthresh, fmid and
fslope. fthresh is the lowest value you would consider displaying.
fmid is the midrange value, and the maximum value is computed by
fmid + 1/fslope. Midrange values are red, and max or above max values
are yellow.

For instance, suppose your limits on your stc file was 0...7.332.
I might try as a first guess fthresh = 3, fmid = 5, fslope = 1.
That means anything below 3 won't show up. Anything around 5 will be
red, and anything 6 or above will be yellow.

If you are using my make_MNE script, you should input your guess
for appropriate threshold values into the param file.

3. Draw snapshot of collapsed movie
Thomas Witzel wrote a program called meg_snapshot_om which will do
this for you. As I write this, I have no knowledge of documentation
for this program. However, it is very very useful for two things. One,
it can make a collapsed image of the entire MEG movie. In this way, you
can see if your thresholds were good or bad much faster than generating
a movie every time you change the thresholds. The second thing it is
good for is making an image for publication. To obtain documentation for
this great tool, ask Thomas.

Fa-Hsuan Lin also developed code for making snapshots, but he uses matlab.
The code is not yet user friendly, so only the matlab saavy should try this.

For normal uses, I recommend using my make_MNE script, which calls Thomas's
program according to options which are particularly useful for this stage
of the analysis. As I mentioned, I don't know of any documentation, so I am
only using the options for meg_snapshot_om that I have found out about.
Assuming you used the script up to this point, all you need to do is enter:

make_MNE --params boo.param --colap
./boo.runner

4. Iterate through 2 and 3 until thresholds are good
You will want to look at the pictures that you generated. If you used my script,
they will be in a directory called "pix".

To view the pictures, use an image viewing program. A very nice one is:

gqview &

That command will show you a list of all the pictures in your directory, and
if you click on any one of them, they are displayed.

If the snapshots have too much bright activity all over the brain, you should
consider lowering your thresholds. On the other hand, if they have not enough
activity in the brain, you should consider raising your thresholds.

Making thresholds which "look good" is a fine art. I wish you luck.

This is one special note. If you are using dSPM, as I recommended, then
you can take advantage of the fact that dSPM has a sort of p value associated
with each statistical value. That means you can set your thresholds based
on some statistical values, like p<0.05 and so on. To do this, you must
look up the p values in a F-Statistics table, or use matlab to compute the
p values. But there is a catch. It isn't like real statistics, and any
real statistician would fall off his chair laughing if you told them exactly
how the p values are obtained. Therefore, because it is more like a pseudo-
statistical value, I only mention this for those who are statistically minded.

5. Compute movie
Thomas Witzel wrote a cool program called EasyMeg2, which as I write this,
has no documentation that I am aware of. As in the case of the snapshots,
I have included scripts to do what I consider the useful stuff based on
the options that I know about. The only existing documentation of how to
use this program is in the MNE-1.7.pdf manual referred to above. But it really
isn't very helpful in my opinion. If you want to really understand how
to use EasyMeg2 to it's potential, you must speak with Thomas.

Using make_MNE, all you need to do is:

make_MNE --params boo.param --movie --noplay
./boo.runner

OR, if you want to make the movie AND watch it right away,
then don't use the --noplay option.

6. Check if thresholds are good
Watch the movies. If you made them with my scripts, they will be in a mov
directory.
To watch a movie in linux, type

xmovie <movie_name> &

For those who didn't use the --noplay option in make_MNE, this
command was called automatically.

Does the movie look good to you? Again, this is an art, not a science.

7. If not good, change thresholds, and repeat 5-7.
Good luck!







--------------------------------------

Upon request, I can provide additional detail for any steps listed above which require clarification.

--------------------------------

QUESTIONS ANSWERED HERE...

I need general MEG background...
What is the MNE Stream actually computing?
What is the meaning of dSPM, noise normalization, and fMRI constraints?
How to perform specific MNE computations without my scripts
How to average data?
Where can I find other cool tools?


I need general MEG background...
----------------------------------
Cohen, D and Halgren, E (2003). Magnetoencephalography (Neuromagnetism). Encyclopedia
 of Neuroscience, 3rd Edition, 1-7.

What is the MNE Stream actually computing?
------------------------------------------
Hamalainen, M, Hari, R, Ilmoniemi, R, Knuutila, J, Lounasmaa, O (1993).
Magnetoencephalography--theory, instrumentation, and applications to noninvasive
studies of the working human brain. Rev Mod Phys, 65: 1-93.

What is the meaning of dSPM, noise normalization, and fMRI constraints?
----------------------------------------------------------------------
Dale AM, Liu AK, Fischl B, Lewine JD, Buckner RL, Belliveau JW, Halgren E (2000)
Dynamic statistical parameter mapping: combining fMRI and MEG to produce high
resolution imaging of cortical activity. Neuron, 26: 55-67.

How to perform specific MNE computations without my scripts
------------------------------------------------------------
http://www.nmr.mgh.harvard.edu/~daniel/links/manuals/MNE-1.7.pdf

How to average data?
--------------------
No good reference is available online. Tools such as meg_average from Thomas Witzel
provide certain types of averaging. For more advanced needs, I suggest 4DToolbox for matlab.
http://www.nmr.mgh.harvard.edu/~twitzel/meg_average.html
http://boojum.hut.fi/~ojensen/4Dtools/


Where can I find other cool tools?
--------------------------------
Check my website. I keep a "what's new" section, so you can periodically check and see
if anything has been changed or updated. I have useful scripts for MEG, UNIX, fMRI, and
lots of useful manuals and even some useful links to other sites.
http://www.nmr.mgh.harvard.edu/~daniel