<div dir="ltr">Hello,<div><br></div><div>I am currently source-localizing some MEG data in such a way that I am inverse-warping a template of MNI coordinates to each individual's anatomical MRI. I am running the following code:</div><div><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">mri = ft_read_mri(mri_filename);</span></div><div><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px"><br></span></div><div><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg = [];
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.method = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'interactive';
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.coordsys = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'acpc'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">; </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(0,128,19)">% the desired coordinate system</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">mri_realigned = ft_volumerealign(cfg, mri);
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
cfg = [];
</span>cfg.sourcemodel.pos<span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px"> = positions;
</span><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="white-space:pre">template = ft_prepare_sourcemodel(cfg);
</span></font><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace">
cfg                = [];
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.method         = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'basedonmni'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.template  = ft_convert_units(</span>template.sourcemodel<span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">, </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'mm'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">);</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.nonlinear      = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'yes'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">; </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(0,128,19)">% use non-linear normalization</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.mri            = mri_realigned;</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">sourcemodel        = ft_prepare_sourcemodel(cfg);</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
<font color="#008013">% Prepare head model using single-shell method
</font></span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg = [];
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.output = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'brain'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span>segmentedmri<span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px"> = ft_volumesegment(cfg, mri_realigned);
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg = [];
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.method = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'singleshell'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">headmodel = ft_prepare_headmodel(cfg, segmentedmri);
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
<font color="#008013">% Calculate means and covariances for source analysis</font></span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg                   = [];
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.covariance        = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'yes'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.covariancewindow  = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'all'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.channel           = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'MEG'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.keeptrial         = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'yes'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">tlock                 = ft_timelockanalysis(cfg, data); % data is preprocessed MEG data
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
<font color="#008013">% Prepare leadfield model
</font></span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg                 = [];
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.grad            = tlock.grad;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.headmodel       = headmodel;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.reducerank      = 2;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.channel         = {</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'MEG'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">};
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.sourcemodel     = sourcemodel;  </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(0,128,19)">% Inverse-warped template</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.normalize       = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'yes'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.normalizeparam  = 1;</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">[lf]                = ft_prepare_leadfield(cfg);
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
<font color="#008013">% Run source analysis on the subject-specific inverse-warped template positions</font></span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg                    = [];
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.method             = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'lcmv'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.sourcemodel        = lf;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.headmodel          = headmodel;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.lcmv.keepfilter    = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'yes'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.lcmv.lambda        = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'5%'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">cfg.lcmv.fixedori      = </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(167,9,245)">'yes'</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">;</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">source  = ft_sourceanalysis(cfg, tlock);</span></div><div><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
nb_sources = length(source.avg.filter);
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">nb_trials = length(data.trial);
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">nb_samples = size(data.trial{1}, 2);</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">vs = zeros(nb_sources,nb_trials,nb_samples);
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">
<font color="#0e00ff">for </font></span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">i=1:nb_sources
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">   flt = source.avg.filter{i};
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(14,0,255)">   if </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">isempty(flt)
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">      vs(i,:,:) = NaN(nb_trials,nb_samples); % source-reconstructed timeseries
</span><span style="color:rgb(14,0,255);white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">   else
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px;color:rgb(14,0,255)">      for </span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">t=1:nb_trials
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">         v = flt*data.trial{t};
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">         vs(i,t,:) = v;
</span><span style="color:rgb(14,0,255);white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">      end
</span><span style="color:rgb(14,0,255);white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px">   end
</span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px"><font color="#0e00ff">end
</font></span><span style="white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px"><font color="#0e00ff">
</font>vs = squeeze(vs);</span><span style="color:rgb(14,0,255);white-space:pre;font-family:Menlo,Monaco,Consolas,"Courier New",monospace;font-size:13px"><br></span><span style="white-space:pre;font-size:13px"><font face="Menlo, Monaco, Consolas, Courier New, monospace">
</font><font face="arial, sans-serif"><b>I have three questions:
</b><ol><li><b>Is it necessary to run ft_volumerealign before running ft_prepare_sourcemodel? The origin of the anatomical MRI
does not appear to be the anterior commissure; see figure 1 below, which was produced through ft_determine_coordsys.</b></li><li><b>source.avg.pow contains very small values, on the order of 10^-47, even though the MEG data is on the order of 
10^-14.</b></li><li><b>When I try to visualize the source activity, it seems totally misaligned with the anatomical MRI. See figure 2 and the
code that was used to produce it below.</b></li></ol></font></span><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="white-space:pre">
<b>Figure 1</b></span></font></div><div><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="white-space:pre"><br></span></font><img src="cid:ii_m2j9e9f81" alt="Screenshot 2024-10-21 at 20.56.51.png" width="542" height="525"><br><br><b>Figure 2</b><br><br></div><div><img src="cid:ii_m2j8u6jb0" alt="Screenshot 2024-10-21 at 20.41.13.png" width="542" height="487"><br><div><br></div><span class="gmail_signature_prefix"><font face="monospace">% Interpolate the source onto the MRI for visualization<br>cfg = [];<br>cfg.parameter = 'pow';  % Or 'avg.pow' depending on your data<br>cfg.interpmethod = 'nearest';<br>source_interp = ft_sourceinterpolate(cfg, source_placebo, mri_realigned);<br>% Plot the source on the MRI<br>cfg = [];<br>cfg.method = 'ortho';<br>cfg.funparameter = 'pow';<br>ft_sourceplot(cfg, source_interp);<br></font><br>Many thanks for reading this long email! I look forward to hearing from you.</span></div><div><span class="gmail_signature_prefix"><br></span></div><div><span class="gmail_signature_prefix">Best regards,</span></div><div><span class="gmail_signature_prefix">Kenneth Shinozuka<br><br>-- </span><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Kenneth Shinozuka</div><div>Harvard College '20 | BA in Neuroscience</div><div>Oxford University '24 | PhD Candidate in Psychiatry</div><div>+1 9493028589 (US, WhatsApp)</div><div>+44 07572876618 (UK)</div></div></div></div></div></div></div>