Hi Gio,<div><br></div><div>Thank you for sharing your script. cfg.output = 'tpm' for ft_volumesegment seems to make a big difference, and now I could go all the way to actually running beamformer on my data! Of course, I had to add a few things. For example, in order to clean up segmented images, I added lines as</div>
<div><br></div><div><font style="background-color:rgb(255,255,255)" color="#330099">seg2.brain(:,:,1:4)=0; </font></div><p class="p1"><font style="background-color:rgb(255,255,255)" color="#330099">seg2.skull(:,:,1:2)=0; </font></p>
<p class="p1">to make sure scalp, skull and brain do not intersept at neck. Also did something like below for each tissue type (otherwise the resulting beamformer was too small on the brain)</p>







<p class="p1"><font color="#330099">newsbrain = imdilate(seg2.brain,strel_bol(1));</font></p>
<p class="p1"><font color="#330099">seg2.brain = </font><span style="color:rgb(51,0,153)">newsbrain</span><font color="#330099">; </font> </p><p class="p1"><br></p><p class="p1">Fieldtrip developers, are there documentations for "cfg.output = 'tpm' " for segmentation? ft_segmentation help page simply says it's a default option.   </p>
<p class="p1">Thanks for your help! Akiko</p><div><br><div class="gmail_quote">On Thu, Feb 16, 2012 at 5:52 AM, Gio Piantoni <span dir="ltr"><<a href="mailto:g.piantoni@nin.knaw.nl">g.piantoni@nin.knaw.nl</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Akiko,<br>
<br>
What do you mean "problematic" exactly? From my understanding, there<br>
are 4 steps to get from the MRI to the forward model.<br>
1. from MRI to segmentation<br>
2. from segmentation to mesh (bnd)<br>
3. from mesh to headmodel<br>
4. from headmodel to forward model<br>
<br>
Here is the code I use. It seems to work fine, but experts might want<br>
to chip in and improve it.<br>
1. from MRI to segmentation<br>
<br>
%-----------------%<br>
%-read MRI<br>
mrifile = '/path/to/your/mri.nii.gz';<br>
mri = ft_read_mri(mrifile);<br>
%-----------------%<br>
<br>
%-----------------%<br>
%-segmenting the volume, Tissue Probability Maps<br>
% It takes time to run 'tpm'<br>
cfg1 = [];<br>
cfg1.threshold = [];<br>
cfg1.output = 'tpm';<br>
cfg1.coordsys = 'spm';<br>
tpm = ft_volumesegment(cfg1, mri);<br>
tpm.anatomy = mri.anatomy;<br>
%-----------------%<br>
<br>
%-----------------%<br>
%-segmenting the volume<br>
% this is faster after tpm<br>
cfg1 = [];<br>
cfg1.threshold = 1; % 1 is default, my MRI scalps were noisy, so I had<br>
to make it lower.<br>
cfg1.output = 'scalp';<br>
cfg1.coordsys = 'spm';<br>
segscalp = ft_volumesegment(cfg1, tpm);<br>
<br>
cfg1 = [];<br>
cfg1.threshold = [];<br>
cfg1.output = {'skull' 'brain'};<br>
cfg1.coordsys = 'spm';<br>
segment = ft_volumesegment(cfg1, tpm);<br>
segment.scalp = segscalp.scalp;<br>
%-----------------%<br>
<br>
Check the output with ft_sourceplot<br>
<br>
2. from segmentation to mesh (bnd)<br>
<br>
%-----------------%<br>
%-prepare mesh for skull and brain (easy)<br>
cfg2 = [];<br>
cfg2.tissue = {'skull', 'brain'};<br>
cfg2.numvertices = [1200 1000];<br>
cfg2.transform = segment.transform;<br>
bnd = ft_prepare_mesh_new(cfg2, segment);<br>
%-----------------%<br>
<br>
%-----------------%<br>
%-prepare mesh for scalp (we need to be more liberal with threshold,<br>
bc of poor quality of MRI)<br>
cfg2 = [];<br>
cfg2.tissue = {'scalp'};<br>
cfg2.numvertices = 2500;<br>
cfg2.thresholdseg = 0.1;<br>
cfg2.transform = segment.transform;<br>
scalp = ft_prepare_mesh_new(cfg2, segment);<br>
%-----------------%<br>
<br>
%-----------------%<br>
%-combine scalp and bnd<br>
bnd = [scalp bnd];<br>
<div class="im">ft_plot_mesh(bnd(1), 'facealpha', .5)<br>
hold on<br>
</div>ft_plot_mesh(bnd(2), 'facecolor', 'red')<br>
%-----------------%<br>
<br>
If bnd is good, no intersection between meshes and the outline looks<br>
nice, the following should run without problems. Please, do check your<br>
bnd.<br>
Segmentation and ft_prepare_mesh_new work really well. Another<br>
approach to get nice meshes is to skip step 1 and 2 and use<br>
Freesurfer/MNE. You can read the meshes in fieldtrip<br>
(ft_read_headshape, I think). The meshes are in the "bem" folder of<br>
Freesurfer, but I don't remember off the top of my head which files<br>
you should read ("outer_skull, inner_skull", I think).<br>
These meshes have a very very high resolution, you can scale them down<br>
with "reducepatch" in Matlab.<br>
<br>
3. from mesh to headmodel<br>
%-----------------%<br>
%-headmodel<br>
cfg3 = [];<br>
cfg3.method = 'bem_dipoli';<br>
cfg3.conductivity = [0.33 0.0042 0.33];<br>
vol = ft_prepare_headmodel(cfg3, bnd);<br>
%-----------------%<br>
<br>
You can really pick your favorite here: dipoli, openmeeg, bemcp. This<br>
will take time (the more time, the more points on your meshes).<br>
<br>
4. from headmodel to forward model<br>
You last problem is to get electrodes and headmodel in the same space.<br>
The template in SPM is already in MNI space, your individual MRIs<br>
probably not. You can realign them to MNI space before step 1 using<br>
SPM or FSL.<br>
<br>
elec = ft_read_sens(sensfile);<br>
elec = ft_convert_units(elec, 'mm');<br>
<br>
%-------%<br>
%-simple transformation (based on visual realignment)<br>
% elecM is an affine matrix that you can get from ft_electroderealign<br>
% electrodes do not need to match the scalp perfectly, but they should<br>
be pretty close by.<br>
elec.chanpos = warp_apply(elecM, elec.chanpos);<br>
elec.elecpos = warp_apply(elecM, elec.elecpos);<br>
%-------%<br>
<br>
[vol, elec] = ft_prepare_vol_sens(vol, elec);<br>
% now you should check that the electrodes are on the scalp.<br>
%-----------------%<br>
<br>
%-----------------%<br>
%-prepare leadfield<br>
cfg4 = [];<br>
cfg4.elec = elec;<br>
cfg4.vol = vol;<br>
cfg4.grid.xgrid = -70:10:70;<br>
cfg4.grid.ygrid = -110:10:80;<br>
cfg4.grid.zgrid = -60:10:90;<br>
cfg4.inwardshift = 1; % to avoid dipoles on the border of bnd(3),<br>
which are very instable<br>
cfg4.grid.tight = 'no';<br>
cfg4.feedback = 'none';<br>
lead = ft_prepare_leadfield(cfg4, []);<br>
% use ft_plot_topo3d and ft_plot_dipole to check whether the<br>
projection of your dipole onto the scalp makes sense.<br>
%-----------------%<br>
<br>
"lead" should be your subject-specific, electrode-specific leadfield.<br>
There are quite a few parameters that you can change, but a clean<br>
segmentation and non-intersecting, smooth and realistic-looking meshes<br>
are a must.<br>
Then, using dipoli, openmeeg or bemcp should not change your forward<br>
model too much.<br>
Sorry if some points are not clear, but you should really look at your<br>
MRI/segmentation/meshes/electrodes/forward model as much as possible.<br>
<br>
Hope this will get you started. I appreciate any feedback on improving<br>
the code, this is just my personal attempt.<br>
<br>
Cheers<br>
<span class="HOEnZb"><font color="#888888"><br>
Gio<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Wed, Feb 15, 2012 at 17:45, Akiko Ikkai <<a href="mailto:akiko.ikkai@gmail.com">akiko.ikkai@gmail.com</a>> wrote:<br>
> Dear Fieldtrip users,<br>
><br>
> Thank you Patrick, Nathan & Gio for your inputs. My MRI anatomical images<br>
> need extensive cleaning (enhance contrasts, get rid of non-zero voxels<br>
> outside the brain, etc...) before segmentation, as well as adjusting<br>
> cfg.threshold for each tissue type during segmentation and each subject. At<br>
> least testing with SPM standard MRI, segmentation and forward modeling works<br>
> OK now.<br>
><br>
> Individual subject's MRI, though, is still problematic, and I have a<br>
> question for Fieldtrip developers: could you let me know whether "Create BEM<br>
> headmodel for EEG (<br>
>  <a href="http://fieldtrip.fcdonders.nl/example/create_bem_headmodel_for_eeg" target="_blank">http://fieldtrip.fcdonders.nl/example/create_bem_headmodel_for_eeg</a> )"<br>
> routine is what you recommend, or it's integrated into a newer function with<br>
> newer cfg options? Is there a page I missed on the web?<br>
><br>
> Thank you so much for your time!<br>
> Akiko<br>
><br>
> On Fri, Feb 10, 2012 at 4:36 AM, Gio Piantoni <<a href="mailto:g.piantoni@nin.knaw.nl">g.piantoni@nin.knaw.nl</a>><br>
> wrote:<br>
>><br>
>> Hi Akiki and Patrick,<br>
>><br>
>> I agree with Nathan about segmentation problems. I had the same<br>
>> problem when two of the three meshes intersected each other. Before<br>
>> you run the forward model, you should check the segmentation (bnd)<br>
>> using ft_plot_mesh.<br>
>> ft_plot_mesh(bnd(1), 'facealpha', .5)<br>
>> hold on<br>
>> ft_plot_mesh(bnd(2), 'facecolor', 'r')<br>
>> You will see that the two meshes intersect with each other. Depending<br>
>> on how you created the meshes, you should fix the parameters in such a<br>
>> way that they don't intersect. For example, ft_volumesegment has a<br>
>> cfg.threshold option (you can check how the segmentation went with<br>
>> ft_sourceplot) and ft_prepare_mesh_new has cfg.thresholdseg and<br>
>> cfg.smoothseg.<br>
>><br>
>> I think that the meshes in SPM are computed on an (affine-transformed)<br>
>> template and not on the anatomically derived meshes. The template is<br>
>> more robust and that's why the two surfaces do not intersect.<br>
>><br>
>> In general, the segmentation depends on the quality of your MRIs and<br>
>> you should really visually check the segmentation and meshes. When<br>
>> those look ok, the BEM forward model usually works. Hope this helps.<br>
>><br>
>> Cheers,<br>
>><br>
>> Gio<br>
>><br>
>> On Fri, Feb 10, 2012 at 09:00, Nathan Weisz <<a href="mailto:nathanweisz@mac.com">nathanweisz@mac.com</a>> wrote:<br>
>> > Hi Akiki,<br>
>> ><br>
>> > I am not a headmodel expert.<br>
>> > but looking at the output message, it seems your segementation did not<br>
>> > go<br>
>> > too well (see the "intersect" message). my intuition is then then the<br>
>> > next<br>
>> > temporary file *.ama is not created and ft_prepare_bemmodel then simply<br>
>> > says<br>
>> > ciao.<br>
>> ><br>
>> > perhaps you want to check / redo the segmentation?<br>
>> ><br>
>> > good luck,<br>
>> > nathan<br>
>> ><br>
>> ><br>
>> > On 09.02.2012, at 23:04, Akiko Ikkai wrote:<br>
>> ><br>
>> > Hi,<br>
>> ><br>
>> > I'm trying to create a volume conduction model for my EEG study based on<br>
>> > anatomical MRI, and having trouble with ft_prepare_bemmodel<br>
>> ><br>
>> > when I run<br>
>> > cfg                = [];<br>
>> > cfg.tissue         = [7 4 1]; % value for brain, skull, and scalp<br>
>> > cfg.conductivity   = [1 1/80 1]*.33; % after standard_BEM.zip data<br>
>> > cfg.isolatedsource = true;<br>
>> > cfg.method         = 'dipoli';<br>
>> > cfg.sourceunits = 'mm';<br>
>> > cfg.mriunits = 'mm';<br>
>> > vol = ft_prepare_bemmodel(cfg, seg4bem);<br>
>> ><br>
>> > I get the following error message while ft_prepare_bemmodel is running:<br>
>> ><br>
>> > Fatal error in dipoli:  interface<br>
>> > /private/tmp/tpd8a04422_e19e_4978_9b9b_56d1f2696492.tri and<br>
>> > /private/tmp/tp313c9a32_1991_4df6_a404_8efc41b6302e.tri intersect<br>
>> >  at vertex 1402 of<br>
>> > /private/tmp/tpd8a04422_e19e_4978_9b9b_56d1f2696492.tri<br>
>> ><br>
>> > Warning: an error ocurred while running dipoli<br>
>> >> In dipoli at 94<br>
>> >   In ft_prepare_bemmodel at 112<br>
>> > Error using ==> fread<br>
>> > Invalid file identifier.  Use fopen to generate a valid file identifier.<br>
>> > Warning: File '/private/tmp/tp0c0779e3_a657_4ca5_b9e1_845f41e9574a.ama'<br>
>> > not<br>
>> > found.<br>
>> >> In dipoli at 102<br>
>> >   In ft_prepare_bemmodel at 112<br>
>> ><br>
>> ><br>
>> > and the output vol is missing .mat, which causes an error<br>
>> > during ft_sourceanalysis.<br>
>> >>> vol<br>
>> > vol =<br>
>> >             cond: [0.3300 0.0041 0.3300]<br>
>> >              bnd: [1x3 struct]<br>
>> >           source: 3<br>
>> >     skin_surface: 3<br>
>> >             skin: 1<br>
>> >             type: 'dipoli'<br>
>> ><br>
>> > Does anyone know how to correct for "fatal error" so<br>
>> > that ft_prepare_bemmodel creates the correct volume conduction model? I<br>
>> > opened .tri file and removed line 1402, but got the same results.<br>
>> ><br>
>> > Thanks in advance! Akiko<br>
>> ><br>
>> > --<br>
>> > Akiko Ikkai, Ph.D.<br>
>> > Postdoctoral Fellow<br>
>> > Department of Psychological and Brain Sciences<br>
>> > Johns Hopkins University<br>
>> > Ames Hall, 3400 N. Charles St.<br>
>> > Baltimore, MD 21218<br>
>> ><br>
>> ><br>
>> > _______________________________________________<br>
>> > fieldtrip mailing list<br>
>> > <a href="mailto:fieldtrip@donders.ru.nl">fieldtrip@donders.ru.nl</a><br>
>> > <a href="http://mailman.science.ru.nl/mailman/listinfo/fieldtrip" target="_blank">http://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
>> ><br>
>> ><br>
>> ><br>
>> > _______________________________________________<br>
>> > fieldtrip mailing list<br>
>> > <a href="mailto:fieldtrip@donders.ru.nl">fieldtrip@donders.ru.nl</a><br>
>> > <a href="http://mailman.science.ru.nl/mailman/listinfo/fieldtrip" target="_blank">http://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
>><br>
>> _______________________________________________<br>
>> fieldtrip mailing list<br>
>> <a href="mailto:fieldtrip@donders.ru.nl">fieldtrip@donders.ru.nl</a><br>
>> <a href="http://mailman.science.ru.nl/mailman/listinfo/fieldtrip" target="_blank">http://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
><br>
><br>
><br>
><br>
> --<br>
> Akiko Ikkai, Ph.D.<br>
> Postdoctoral Fellow<br>
> Department of Psychological and Brain Sciences<br>
> Johns Hopkins University<br>
> Ames Hall, 3400 N. Charles St.<br>
> Baltimore, MD 21218<br>
><br>
><br>
><br>
> _______________________________________________<br>
> fieldtrip mailing list<br>
> <a href="mailto:fieldtrip@donders.ru.nl">fieldtrip@donders.ru.nl</a><br>
> <a href="http://mailman.science.ru.nl/mailman/listinfo/fieldtrip" target="_blank">http://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
<br>
_______________________________________________<br>
fieldtrip mailing list<br>
<a href="mailto:fieldtrip@donders.ru.nl">fieldtrip@donders.ru.nl</a><br>
<a href="http://mailman.science.ru.nl/mailman/listinfo/fieldtrip" target="_blank">http://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><font><span style="font-family:arial,helvetica,sans-serif">Akiko Ikkai, Ph.D. <br>
Postdoctoral Fellow<br style="font-family:arial,helvetica,sans-serif"></span></font><font style="font-family:arial,helvetica,sans-serif" face="'PrimaSans BT,Verdana,sans-serif'" size="2">Department of 
Psychological and Brain Sciences<br>Johns Hopkins University<br>Ames 
Hall, 3400 N. Charles St.<br>Baltimore, MD 21218</font><br style="font-family:arial,helvetica,sans-serif"><br><br>
</div>