<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
Dear Manon,
<div class=""><br class="">
</div>
<div class="">Just as a pre-preliiminary: you write that you want to compute EMG-MEG coherence. Did you consider to use DICS for this? If you know which frequency (range) you want to look at, I think that this would be the most straightforward. There’s plenty
 of documentation on the website for this.</div>
<div class=""><br class="">
</div>
<div class="">Then, as a second preliminary: you write that you want to use a ‘beamformer technique in the time domain’, but in your code you use ‘mne’ as a method, which is a different algorithm altogether. Yet, it is not really relevant to the problem you
 are facing, so for now I’ll let it slip :). As a side note, the option ‘rawtrial’, for ft_sourceanalysis, and as far as I know, does not have any funcionality for the mne method, and only (historically) was functional for beamformers. These days, this option
 is explicitly DISrecommended. (nobody has found the time yet to cleanly deprecate this functionality altogether)</div>
<div class=""><br class="">
</div>
<div class="">OK, after these preliminaries, let’s think about the conceptual challenges first:</div>
<div class=""><br class="">
</div>
<div class="">What you have looked into, and already understand quite well, is the way in which FieldTrip does a lot of bookkeeping in order to try and ensure that all data objects that implicitly have a channel dimension are well matched w.r.t. the assumed
 order of the channels.</div>
<div class=""><br class="">
</div>
<div class="">Specifically, in the context of forward and inverse modelling, different types of data ‘come together’. There’s leadfieds (either or not precomputed), spatial filters, and data (covariances and time series data). In order for the mathematical
 operations to make sense, the identity and the order of the channels in the data objects need to match. This assumed equivalence of channel order is not always guaranteed, particularly if you have a quirky naming (and ordering) scheme as the la Timone MEG
 system, but perhaps the other JM (=JeanMichel) would disagree with me calling your system quirky. Typically, the channel order in raw data from 4D-neuroimaging/Magnes/BTI systems is NOT in alphanumeric order, neither in the time series, nor in the description
 of the magnetometer array. Sooo, the long story short is that books need to be kept regarding the channel ordering. Yet, another thing to keep in mind in addition, is that any (linear) operations applied to the time series data (e.g. removal of spatial topographies
 by using ICA followed by backprojection, or applying denoising using the reference channels) need to be represented in the leadfields as well. In other words, if you precomputed the leadfields on an unbalanced grad-structure (fieldtrip speak for a sensor representation
 that has not been processed with the same processing pipeline that was used to preprocess the actual data) then this is not good.</div>
<div class=""><br class="">
</div>
<div class="">Long story short, if you are to precompute your leadfields in a separate step (which I’d recommend), I would do it as follows:</div>
<div class=""><br class="">
</div>
<div class="">cfg = [];</div>
<div class="">cfg.headmodel = <headmodel></div>
<div class="">cfg.sourcemodel = <sourcemodel></div>
<div class="">cfg. < some other options ></div>
<div class="">cfg.channel = data.label (assuming that the data does not contain non-MEG channels)</div>
<div class="">leadfield = ft_prepare_leadfield(cfg, data)</div>
<div class=""><br class="">
</div>
<div class="">where data is the data structure that you later want to subject to ft_sourceanalysis. This would result in leadfields that not only have a more or less consistent order in the channels (i.e. consistent with the data), but also the leadfields are
 properly ‘balanced’. Note, however that a lot of the stuff you described in your e-mail related to the code that you identified in FT tries to ensure a correct channel order between different data objects.</div>
<div class=""><br class="">
</div>
<div class="">Now, concretely, if you want to use a beamformer for source reconstruction, you should specify cfg.method = ‘lcmv’, rather than ‘mne’. If you on the other hand want to compute an mne-solution, you should NOT input the full data covariance matrix,
 but rather use an estimate of the noise (e.g. using an empty room measurement). In the code that you pasted in the e-mail you seem to have mixed these two things up.</div>
<div class=""><br class="">
</div>
<div class="">Then, finally getting to what you want to achieve, the question is how to efficiently obtain single trial source level time series data. Indeed, I’d recommend a two-step approach, where you first obtain a set of spatial filters, and then subject
 the channel data to the spatial filters. I would assume that if you use precomputed leadfields as per the above suggestion, the order of the channels will match the order of the channels in the data (again provided the data does not contain non-MEG) channels.
 Now the question is whether you will have enough computer RAM to begin with to fit all the dipoles’ single trial time courses. </div>
<div class=""><br class="">
</div>
<div class="">Perhaps we could follow up on this in a next set of e-mails in this thread - also to give other people the opportunity to chime in -, but also because 1) the above might already give you ample food for thought for now, and 2) my risotto is almost
 cooked, so I have to summon the troups at home to the dinner table.</div>
<div class=""><br class="">
</div>
<div class="">Keep up the good work and critical thinking,</div>
<div class=""><br class="">
</div>
<div class="">JM</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 25 Oct 2021, at 19:02, CHATEAUX Manon via fieldtrip <<a href="mailto:fieldtrip@science.ru.nl" class="">fieldtrip@science.ru.nl</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Dear FieldTrip community,<o:p class=""></o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">I want to compute coherence between brain sources and an EMG channel that I selected. Here is the pipeline I am using :<o:p class=""></o:p></span></div>
<ul type="disc" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-GB" class="">Preprocess MEG and EMG data<o:p class=""></o:p></span></li><li class="MsoListParagraph" style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-GB" class="">Process single subject MRI data to get (a) the head model, (b) the source model with leadfields<o:p class=""></o:p></span></li><li class="MsoListParagraph" style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-GB" class="">Reconstruct MEG data using beamformer techniques in the time domain.<o:p class=""></o:p></span></li><li class="MsoListParagraph" style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-GB" class="">Compute coherence between source data and EMG data<o:p class=""></o:p></span></li></ul>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">To be able to compute coherence, I need to reconstruct data for all trials and not as an average of trials. But I encountered a problem at this level.<o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">Here is the code I used first :<o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">% Compute noise-covariance matrix</span><span style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 10pt; font-family: "Courier New";" class="">    </span><span style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 10pt; font-family: "Courier New";" class="">    cfg            = [];</span><span style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 10pt; font-family: "Courier New";" class="">   <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">cfg.covariance =<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'yes'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">;<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">%default
 takes the whole time window to estimate the noise covariance matrix</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.keeptrials =<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'yes'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">;</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">   </span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    tlcontr_MEG    = ft_timelockanalysis(cfg, MEG_contr);</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">   </span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">% Compute inverse modelling with mne : source data averaged over</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">% trails</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg                = [];</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.method         =<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'mne'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">;</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.sourcemodel    = leadfield;</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.headmodel      = headmodel;</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.mne.lambda     = 3;<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">%%????</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.mne.keepfilter =<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'yes'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">;</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.rawtrial       =<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'no'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">;</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">   </span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    source_MEG_contr   = ft_sourceanalysis(cfg,tlcontr_MEG);</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">% Get source data for all trials : project trials by hand</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    FILTER = source_MEG_contr.avg.filter(cellfun(@(a) ~isempty(a),source_MEG_contr.avg.filter));</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">   </span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: blue;" class="">for</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class=""><span class="Apple-converted-space"> </span>triali
 = 1 : length(MEG_contr.trial)</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">        source_MEG_contr_trials.pow{triali} = cell2mat(cellfun(@(a) sum((a*MEG_contr.trial{triali}).^2,1),FILTER,</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'UniformOutput'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">,false)');</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">        disp([</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'Trial n° '</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class=""><span class="Apple-converted-space"> </span>num2str(triali)<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'
 out of '</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class=""><span class="Apple-converted-space"> </span>num2str(length(MEG_contr.trial))])</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">       </span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    </span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: blue;" class="">end</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">source_MEG_contr_trials</span><span lang="EN-GB" class=""><span class="Apple-converted-space"> </span> is to be the structure that contains source data for all trials. As you can
 see I computed it by hand, following a thread that I found on the website.<span class="Apple-converted-space"> </span><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">Then I found it was possible to use ft_sourceanalysis again, to project all trials through the filter computed at the first use of the function. So I replaced the last paragraph of code with the following :<o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">   <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">% project all trials
 through common spatial filter to get source data</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">   <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">% for separate trials</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg=[];</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.method             =<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'mne'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">;</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.sourcemodel        = leadfield;      <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">%
 previously computed grid</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.headmodel          = headmodel;       <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">%
 previously computed volume conduction model</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.rawtrial           =<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(160, 32, 240);" class="">'yes'</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">;     <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">%
 project each single trial through the filter to reconstruct single trial data</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">   </span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    cfg.sourcemodel.filter = source_MEG_contr.avg.filter;<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">%
 use the common filter computed in the previous step!</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">    source_MEG_contr_trials      = ft_sourceanalysis(cfg, tlcontr_MEG);<span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: rgb(52, 192, 60);" class="">%
 contains the source estimates for all trials/both conditions</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">I preferred this “automatic” way rather than the manual one because it allows me to keep the FieldTrip shape of structures more easily and display results using FieldTrip functions.<o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">However, I realized that it returned completely different results. After trying to understand the steps performed in the second use of ft_sourceanalysis, I realized that filters used to compute trial source data from trial channel
 data (l. 295 in ft_inverse_mne of fieldtrip-20210921 :<span class="Apple-converted-space"> </span><b class="">estimate.mom{i} = sourcemodel.filter{i}*dat</b>) were different from filters in cfg.sourcemodel.filter.  <br class="">
In fact, channel labels are taken from the leadfield structure. Lines (channel dimension) in filter matrices (in cfg.sourcemodel.filter) are reorganized so that it matches channel order from data.label (= tlcontr_MEG.label).<span class="Apple-converted-space"> </span><br class="">
See l.404-410 in ft_sourceanalysis of fieldtrip-20210921 :<span class="Apple-converted-space"> </span><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">[dum, chansel] = match_str(data.label, sourcemodel.label);</span><span lang="EN-GB" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 10pt; font-family: "Courier New";" class="">sourcemodel.label = sourcemodel.label(chansel);<o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: blue;" class="">for</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class=""><span class="Apple-converted-space"> </span>i=1:numel(sourcemodel.filter)</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">  <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: blue;" class="">if</span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class=""><span class="Apple-converted-space"> </span>~isempty(sourcemodel.filter{i})</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">     sourcemodel.filter{i} = sourcemodel.filter{i}(:, chansel);</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New";" class="">  <span class="Apple-converted-space"> </span></span><span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: blue;" class="">end</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 10pt; font-family: "Courier New"; color: blue;" class="">end</span><span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" style="font-size: 12pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">Thus, if I understand it correctly, this step assumes that lines in filter matrices (which come from source_MEG_contr structure) are organized in the same way as channels in leadfields. However, this is not actually the case here
 because<o:p class=""></o:p></span></div>
<ol start="1" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-GB" class="">source_MEG_contr.avg.label and data.label (= tlcontr_MEG.label) are equal.  <o:p class=""></o:p></span></li><li class="MsoListParagraph" style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-GB" class="">I don’t understand why but leadfields.label is different from the original alphanumerical channel organization – A1 A2 A3 etc…- found in raw data (as well as in source_MEG_contr, and in tlcontr_MEG).<o:p class=""></o:p></span></li></ol>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">So I think that in my case, lines in filter matrices should not be reorganized (when they are not, I get the same results as by hand) but as leadfield structure is also used when I compute source_MEG_contr from tlcontr_MEG, I am
 not sure I can trust source_MEG_contr either.<span class="Apple-converted-space"> </span><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">I would truly appreciate if someone could proofread and pinpoint some problems in this reasoning to help me solve this annoying problem. In the meantime I will try to recompute the leadfield structure so that channel order is the
 same everywhere.<span class="Apple-converted-space"> </span><o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class="">Thank you in advance for your help and the time you will spend reading through this message!<o:p class=""></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="EN-GB" class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Manon Châteaux<o:p class=""></o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
phD Student at Aix-Marseille University<o:p class=""></o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">_______________________________________________</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">fieldtrip
 mailing list</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">
<a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" style="color: rgb(149, 79, 114); text-decoration: underline; font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">
<a href="https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!sfGi3q4aOeMCoe7W0_ghPeD-viaovjeAzPnJRvx05cVOf7UZKHz_GIupVsUV7rEW0MzP5TGOqE2mnKs$" style="color: rgb(149, 79, 114); text-decoration: underline; font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!sfGi3q4aOeMCoe7W0_ghPeD-viaovjeAzPnJRvx05cVOf7UZKHz_GIupVsUV7rEW0MzP5TGOqE2mnKs$</a><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class=""></span></div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>