<html><head></head><body><div class="ydp3d59803dyahoo-style-wrap" style="font-family: bookman old style, new york, times, serif; font-size: 16px;"><div></div>
<div dir="ltr" data-setdir="false">Dear Eelke,</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">Thank you for your reply and yes you are right. Now I solved source grandaveraging issue if I include a line "source.pos= sourcemodel.grid.pos" after doing source analysis (<span><a href="https://mailman.science.ru.nl/pipermail/fieldtrip/2015-October/022580.html." style="color: rgb(51, 143, 233); text-decoration-line: underline; font-family: Helvetica Neue, Helvetica, Arial, sans-serif;" rel="nofollow" target="_blank">https://mailman.science.ru.nl/pipermail/fieldtrip/2015-October/022580.html. </a>)</span>. I have some more questions which you might help:</div><div dir="ltr" data-setdir="false">1) I tried to understand why do we need to include source.pos=sourcemodel.grid.pos after ft_sourceanalysis. If I excluded this line ft_sourcegrandaveraging throws an error (<span><span style="color: rgb(38, 40, 42); font-family: Helvetica Neue, Helvetica, Arial, sans-serif;">“Error using ft_sourcegrandaverage (line 116) the input sources vary in the field inside”) </span></span>?</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false">2) When should I do ft_sourceinterpolation and ft_volumenormalise? Should it be on the individual subject or on the source grandaverage (average subject)? It seems that after including <span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;">"source.pos= sourcemodel.grid.pos" and do <span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;">ft_sourceinterpolation and ft_volumenormalise on individual subject, no source activation anymore after ft_sourceplot of source grandaverage data. However, if I do not include the <span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;">"source.pos= sourcemodel.grid.pos" , then interpolate and volume normalize individual subject, there was source activation.</span></span> I am not sure this problem was due to<span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;">"source.pos= sourcemodel.grid.pos" or the way I did ft_interpolation and ft_volumenormalise. Hope you can suggest something.</span></span> <span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;">Here is the script:</span></span></span></span></span></span></div><div dir="ltr" data-setdir="false"><span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;"><span><span style="color: rgb(0, 0, 0); font-family: bookman old style, new york, times, serif; font-size: 16px;">************************************************************ </span></span></span></span></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><div><div> cfg = [];</div><div> cfg.channel = datall_avg.label;</div><div> cfg.elec = elec_aligned.elec_aligned;</div><div> cfg.method = 'lcmv';</div><div> cfg.headmodel = headmodel.hdm;</div><div> cfg.sourcemodel.grid = leadfield; </div><div> cfg.grid.resolution = 5;</div><div> cfg.lcmv.keepfilter = 'yes';</div><div> cfg.lcmv.fixedori = 'yes';</div><div> cfg.lcmv.projectnoise = 'yes';</div><div> cfg.lcmv.weightnorm = 'nai';</div><div> cfg.lcmv.lambda = '5%';</div><div> </div><div> eegsrc_all = ft_sourceanalysis(cfg, datall_avg); %source data in all time window</div><div><br></div><div dir="ltr" data-setdir="false"><div><div> % apply common filters to pre and post stimulus data</div><div> cfg.grid.filter = eegsrc_all.avg.filter;</div><div> </div><div> % Compute source for baseline and activty time window</div><div> eegsrc_basl = ft_sourceanalysis(cfg, datbasl_avg); %source data for baseline time window</div><div> eegsrc_act = ft_sourceanalysis(cfg, datact_avg); %source data for activity time window</div><div> </div><div> % contrast post stimulus onset activity with respect to baseline</div><div> eegsrc_act_basnrm = eegsrc_act;</div><div> eegsrc_act_basnrm.avg.pow = (eegsrc_act.avg.pow - eegsrc_basl.avg.pow)./ eegsrc_basl.avg.pow;</div><div dir="ltr" data-setdir="false"><div><div><br></div><div> %% Choose time of interest</div><div> </div><div> % average across time the dipole moments within the N20 latency range</div><div> ind = find(eegsrc_act_basnrm.time>=0.016 & eegsrc_act_basnrm.time<=0.024); %at 16ms-24ms</div><div> timecourse = eegsrc_act_basnrm.avg.mom(eegsrc_act_basnrm.inside);</div><div> tc_toi = zeros(size(eegsrc_act_basnrm.avg.pow(eegsrc_act_basnrm.inside)));</div><div> </div><div> for ii = 1:length(timecourse)</div><div> tc_toi(ii) = mean(abs(timecourse{ii}(ind)));</div><div> end</div><div> </div><div> source = eegsrc_act_basnrm;</div><div> source.avg.pow(eegsrc_act_basnrm.inside) = tc_toi;</div><div><br></div></div><br></div><div dir="ltr" data-setdir="false"><div> <font color="#cd232c"> <b style="background-color: rgb(255, 255, 255);">source.pos = sourcemodel.grid.pos %% <<<I Include here??</b></font></div><div><br></div><div dir="ltr" data-setdir="false"><div><div> % interpolate and normalize to individual mri</div><div> %first, interpolate</div><div> </div><div> cfg = [];</div><div> cfg.parameter = 'pow';</div><div dir="ltr" data-setdir="false"> <b><font color="#cd232c"> eeg_source_interp = ft_sourceinterpolate(cfg, source, mri); %%< interpolate on individual source??</font></b></div><div dir="ltr" data-setdir="false"><b><font color="#cd232c"><br></font></b></div><div> % second, normalise</div><div> cfg = [];</div><div> cfg.nonlinear = 'no';</div><div> eeg_source_interp_norm = ft_volumenormalise(cfg, eeg_source_interp);</div><div><br></div><div><br></div><div dir="ltr" data-setdir="false">Thank you.</div><div dir="ltr" data-setdir="false">Faizal</div><div><br></div></div><br></div></div><div><br></div></div><br></div><div><br></div></div><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><br></div><div><br></div>
</div><div id="ydp87a53958yahoo_quoted_8693760620" class="ydp87a53958yahoo_quoted">
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;color:#26282a;">
<div>
On Friday, 1 May 2020, 04:43:20 pm GMT+2, Eelke Spaak <e.spaak@donders.ru.nl> wrote:
</div>
<div><br></div>
<div><br></div>
<div><div dir="ltr">Dear Faizal,<br></div><div dir="ltr"><br></div><div dir="ltr">I notice that you are creating a sourcemodel called 'grid' in your<br></div><div dir="ltr">code, but later on in the call to ft_prepare_leadfield you are not<br></div><div dir="ltr">using that sourcemodel, instead 'cfg.grid.pos = sourcemodel.pos;',<br></div><div dir="ltr">possibly using the (non-warped) template positions. This might cause<br></div><div dir="ltr">issues. I would try to make sure you are using the correct source<br></div><div dir="ltr">model/leadfield variables throughout the code, and see whether the<br></div><div dir="ltr">problem persists.<br></div><div dir="ltr"><br></div><div dir="ltr">Best,<br></div><div dir="ltr">Eelke<br></div><div dir="ltr"><br></div><div dir="ltr">On Wed, 29 Apr 2020 at 13:14, Mohd Faizal Mohd Zulkifly<br></div><div dir="ltr"><<a href="mailto:mfzl_mzly@yahoo.com" rel="nofollow" target="_blank">mfzl_mzly@yahoo.com</a>> wrote:<br></div><div dir="ltr">><br></div><div dir="ltr">> Dear all,<br></div><div dir="ltr">><br></div><div dir="ltr">> I am doing source analysis based on subject-specific MRI and currently having an issue to do source grandaveraging. I tried to use different version of fieldtrips (i.e. Fieldtrip-20181122, Fieldtrip-20191127 and Fieltrip-20200109), and it still ended up with the same error: “Error using ft_sourcegrandaverage (line 116) the input sources vary in the field inside”. I also aware that this issue was discussed previously <a href="https://mailman.science.ru.nl/pipermail/fieldtrip/2015-October/022580.html. " rel="nofollow" target="_blank">https://mailman.science.ru.nl/pipermail/fieldtrip/2015-October/022580.html. </a>Unfortunately, I still facing the same error after specifying source.pos = template_grid.pos. It would be grateful if some of you can help and let me know my mistakes in the scripts below. It is a long script and I was thinking it might be helpful to provide a full script for others to help. Hope to hear from you guys soon. Thank you.<br></div><div dir="ltr">><br></div><div dir="ltr">> ***********************************Forward model************************************************************<br></div><div dir="ltr">><br></div><div dir="ltr">> subject_data= {'A’, ‘B’};<br></div><div dir="ltr">><br></div><div dir="ltr">> subj={'A’, ‘B’};<br></div><div dir="ltr">><br></div><div dir="ltr">> nsubj = numel(subject_data);<br></div><div dir="ltr">><br></div><div dir="ltr">> % MRI Segmentation<br></div><div dir="ltr">><br></div><div dir="ltr">> % if do_mri_seg<br></div><div dir="ltr">><br></div><div dir="ltr">> % read the *mgz files from FreeSurfer<br></div><div dir="ltr">><br></div><div dir="ltr">> fprintf(['starting forward model for: ', subj{s},'\n '])<br></div><div dir="ltr">><br></div><div dir="ltr">> mri = ft_read_mri([subject_mri, subj{s},'/','mri/','T1.mgz']);<br></div><div dir="ltr">><br></div><div dir="ltr">> mri.coordsys = 'ctf';<br></div><div dir="ltr">><br></div><div dir="ltr">> % interactively set fiducials to align MRI space to EEG space<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.method = 'interactive';<br></div><div dir="ltr">><br></div><div dir="ltr">> mri = ft_volumerealign(cfg, mri);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.output = {'brain','skull','scalp'};<br></div><div dir="ltr">><br></div><div dir="ltr">> segmentedmri = ft_volumesegment(cfg, mri);<br></div><div dir="ltr">><br></div><div dir="ltr">> % save segmentedmri<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %%<br></div><div dir="ltr">><br></div><div dir="ltr">> % Head mesh<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %if do_head_mesh<br></div><div dir="ltr">><br></div><div dir="ltr">> addpath 'XXXXXX/fieldtrip-20181122/external/spm12'<br></div><div dir="ltr">><br></div><div dir="ltr">> segmentedmri.dim = segmentedmri.dim;<br></div><div dir="ltr">><br></div><div dir="ltr">> segmentedmri.coordsys = segmentedmri.coordsys;<br></div><div dir="ltr">><br></div><div dir="ltr">> segmentedmri.unit = segmentedmri.unit;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.tissue = {'brain','skull','scalp'};<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.method = 'projectmesh';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.numvertices = [3000 2000 1000];<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd.transform = segmentedmri.transform;<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd.brain = segmentedmri.brain;<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> braindil = imdilate(segmentedmri.brain, strel_bol(6));<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd.skull = braindil > 0.5;<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd.scalp = segmentedmri.scalp;<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd.dim = segmentedmri.dim;<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd.coordsys = segmentedmri.coordsys;<br></div><div dir="ltr">><br></div><div dir="ltr">> mri_bnd.unit = segmentedmri.unit;<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> src = ft_prepare_mesh(cfg, mri_bnd);<br></div><div dir="ltr">><br></div><div dir="ltr">> % save src<br></div><div dir="ltr">><br></div><div dir="ltr">> %%<br></div><div dir="ltr">><br></div><div dir="ltr">> % COMPONENT 1: Headmodel<br></div><div dir="ltr">><br></div><div dir="ltr">> addpath 'XXX/fieldtrip-20181122/external/dipoli'<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.method = 'dipoli';<br></div><div dir="ltr">><br></div><div dir="ltr">> hdm = ft_prepare_headmodel(cfg, src);<br></div><div dir="ltr">><br></div><div dir="ltr">> % save hdm<br></div><div dir="ltr">><br></div><div dir="ltr">> disp(hdm)<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %%<br></div><div dir="ltr">><br></div><div dir="ltr">> % COMPONENT 2: Sensor adjustment % getting electrodes in the right position<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %if do_sensor_alignment<br></div><div dir="ltr">><br></div><div dir="ltr">> sens = ft_read_sens([XXX, subj{s}, '.sfp']); % GEOSCAN<br></div><div dir="ltr">><br></div><div dir="ltr">> sens = ft_convert_units(sens, 'mm');<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> nas = mri.cfg.fiducial.nas;<br></div><div dir="ltr">><br></div><div dir="ltr">> lpa = mri.cfg.fiducial.lpa;<br></div><div dir="ltr">><br></div><div dir="ltr">> rpa = mri.cfg.fiducial.rpa;<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % warp NAS LPA and RPA<br></div><div dir="ltr">><br></div><div dir="ltr">> nas = ft_warp_apply(mri.transform, nas, 'homogenous');<br></div><div dir="ltr">><br></div><div dir="ltr">> lpa = ft_warp_apply(mri.transform, lpa, 'homogenous');<br></div><div dir="ltr">><br></div><div dir="ltr">> rpa = ft_warp_apply(mri.transform, rpa, 'homogenous');<br></div><div dir="ltr">><br></div><div dir="ltr">> fid = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> fid.elecpos = [nas; lpa; rpa]; % ctf-coordinates of fiducials<br></div><div dir="ltr">><br></div><div dir="ltr">> fid.chanpos = [nas; lpa; rpa]; % ctf-coordinates of fiducials<br></div><div dir="ltr">><br></div><div dir="ltr">> fid.label = {char(sens.label(1)); ...<br></div><div dir="ltr">><br></div><div dir="ltr">> char(sens.label(2)); ...<br></div><div dir="ltr">><br></div><div dir="ltr">> char(sens.label(3))}; % same labels as in elec<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % alignment<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.method = 'fiducial';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.target = fid; % see above<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.elec = sens;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.fiducial = {char(sens.label(1)); ...<br></div><div dir="ltr">><br></div><div dir="ltr">> char(sens.label(2)); ...<br></div><div dir="ltr">><br></div><div dir="ltr">> char(sens.label(3))}; % labels of fiducials in fid and in elec<br></div><div dir="ltr">><br></div><div dir="ltr">> elec_aligned = ft_electroderealign(cfg);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.method = 'project';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.elec = elec_aligned;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.headshape = src(3);<br></div><div dir="ltr">><br></div><div dir="ltr">> elec_aligned = ft_electroderealign(cfg);<br></div><div dir="ltr">><br></div><div dir="ltr">> % save elec_aligned<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %%<br></div><div dir="ltr">><br></div><div dir="ltr">> %COMPONENT 3: Source model<br></div><div dir="ltr">><br></div><div dir="ltr">> % % load a template grid<br></div><div dir="ltr">><br></div><div dir="ltr">> % NOTE: the path to the template file is user-specific<br></div><div dir="ltr">><br></div><div dir="ltr">> pathin ='XXX/matlab/toolbox/';<br></div><div dir="ltr">><br></div><div dir="ltr">> load(fullfile(pathin, 'fieldtrip-20200109/template/sourcemodel/standard_sourcemodel3d5mm'));<br></div><div dir="ltr">><br></div><div dir="ltr">> template = sourcemodel;<br></div><div dir="ltr">><br></div><div dir="ltr">> clear sourcemodel;<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % create the subject specific grid, using the template grid that has just been created<br></div><div dir="ltr">><br></div><div dir="ltr">> % inverse-warp the template grid to subject specific coordinates<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.warpmni = 'yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.template = template;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.nonlinear = 'yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.mri = mri; %mri - not working properly/ showing scalp<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.unit = 'mm';<br></div><div dir="ltr">><br></div><div dir="ltr">> grid = ft_prepare_sourcemodel(cfg);<br></div><div dir="ltr">><br></div><div dir="ltr">> %save grid<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %%<br></div><div dir="ltr">><br></div><div dir="ltr">> % COMPONENT 4: Leadfield<br></div><div dir="ltr">><br></div><div dir="ltr">> fname_preproc_data = fullfile(XXX,subject_data{s}]);<br></div><div dir="ltr">><br></div><div dir="ltr">> preproc_data = load(fname_preproc_data);<br></div><div dir="ltr">><br></div><div dir="ltr">> EEG_name = cell2mat(fieldnames(preproc_data));<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.elec = elec_aligned;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.channel = preproc_data.conv_data_rpost.label; % ensure that rejected electrodes are not present<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.headmodel = hdm;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.grid.pos = sourcemodel.pos;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.normalize = 'no'; %normalize 'yes' it when don't have a contrast to avaoid activity at the center (NAI tutorial)<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.normalizeparam = 0.5;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.grid.unit = 'mm';<br></div><div dir="ltr">><br></div><div dir="ltr">> leadfield = ft_prepare_leadfield(cfg);<br></div><div dir="ltr">><br></div><div dir="ltr">> %save leadfield<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % the data consists of fewer channels than the precomputed<br></div><div dir="ltr">><br></div><div dir="ltr">> % leadfields, the following chunk of code takes care of this<br></div><div dir="ltr">><br></div><div dir="ltr">> [a,b] = match_str(preproc_data.conv_data_rpost.label, leadfield.label);<br></div><div dir="ltr">><br></div><div dir="ltr">> for k = 1:numel(leadfield.leadfield)<br></div><div dir="ltr">><br></div><div dir="ltr">> if ~isempty(leadfield.leadfield{k})<br></div><div dir="ltr">><br></div><div dir="ltr">> tmp = leadfield.leadfield{k};<br></div><div dir="ltr">><br></div><div dir="ltr">> tmp = tmp(b,:);<br></div><div dir="ltr">><br></div><div dir="ltr">> tmp = tmp-repmat(mean(tmp,1),[size(tmp,1) 1]); % average re-ref<br></div><div dir="ltr">><br></div><div dir="ltr">> leadfield.leadfield{k} = tmp;<br></div><div dir="ltr">><br></div><div dir="ltr">> end<br></div><div dir="ltr">><br></div><div dir="ltr">> end<br></div><div dir="ltr">><br></div><div dir="ltr">> leadfield.label = leadfield.label(b);<br></div><div dir="ltr">><br></div><div dir="ltr">> %save leadfield<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> *****************************************************source_analysis**************************************<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> subject_rpost_data= {‘A’, ‘B’};<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> subj={‘A’, ‘B’ };<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> nsubj = numel(subj);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % Loop over subjects<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> for s=1:nsubj<br></div><div dir="ltr">><br></div><div dir="ltr">> fprintf(['starting source analysis for: ', subj{s},'\n '])<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> load(fname_preproc_data);<br></div><div dir="ltr">><br></div><div dir="ltr">> load(fname_sourcemodel);<br></div><div dir="ltr">><br></div><div dir="ltr">> load(fname_leadfield);<br></div><div dir="ltr">><br></div><div dir="ltr">> load(fname_headmodel);<br></div><div dir="ltr">><br></div><div dir="ltr">> load(fname_elec);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> mri = ft_read_mri(fname_seg_mri);<br></div><div dir="ltr">><br></div><div dir="ltr">> mri.coordsys = 'ctf';<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> load(fname_smri )<br></div><div dir="ltr">><br></div><div dir="ltr">> % transform original mri to segemented mri transformation info<br></div><div dir="ltr">><br></div><div dir="ltr">> trns = segmentedmri.transform / mri.transform;<br></div><div dir="ltr">><br></div><div dir="ltr">> clear segmentedmri<br></div><div dir="ltr">><br></div><div dir="ltr">> % reslice mri<br></div><div dir="ltr">><br></div><div dir="ltr">> mri.coordsys = 'ctf';<br></div><div dir="ltr">><br></div><div dir="ltr">> mri = ft_volumereslice([], mri);<br></div><div dir="ltr">><br></div><div dir="ltr">> % transform mri to coordinates of leadfield<br></div><div dir="ltr">><br></div><div dir="ltr">> mri = ft_transform_geometry(trns, mri);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % Bring data as in time domain<br></div><div dir="ltr">><br></div><div dir="ltr">> % Cut Trial data in baseline and evoked time segments<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.latency = [-1.0 0]; % baseline % choose baseline as i did with sensor analysis (100ms)<br></div><div dir="ltr">><br></div><div dir="ltr">> datbasl = ft_selectdata(cfg, preproc_data.(EEG_name));<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.latency = [0 3]; % evoked activity<br></div><div dir="ltr">><br></div><div dir="ltr">> datact = ft_selectdata(cfg,preproc_data.(EEG_name));<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.latency = [-1 3];<br></div><div dir="ltr">><br></div><div dir="ltr">> datall = ft_selectdata(cfg,preproc_data.(EEG_name));<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %%<br></div><div dir="ltr">><br></div><div dir="ltr">> % Compute covariance matrix<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.covariance = 'yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.keeptrials = 'no'; % 'no' creates averaged trial<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.removemean = 'yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.channel = {'all', '-E257'};<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> datbasl_avg = ft_timelockanalysis(cfg, datbasl); %Baseline data (in time domain)<br></div><div dir="ltr">><br></div><div dir="ltr">> datact_avg = ft_timelockanalysis(cfg, datact); %Activity data<br></div><div dir="ltr">><br></div><div dir="ltr">> datall_avg = ft_timelockanalysis(cfg, datall); %Data with baseline and activity<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %% Source analysis<br></div><div dir="ltr">><br></div><div dir="ltr">> %<br></div><div dir="ltr">><br></div><div dir="ltr">> % adapt leadfield channels to channel of current eeg data (199 instead<br></div><div dir="ltr">><br></div><div dir="ltr">> % of 256)<br></div><div dir="ltr">><br></div><div dir="ltr">> [~,b] = match_str(datbasl_avg.label, leadfield.label);<br></div><div dir="ltr">><br></div><div dir="ltr">> for k = 1:numel(leadfield.leadfield)<br></div><div dir="ltr">><br></div><div dir="ltr">> if ~isempty(leadfield.leadfield{k})<br></div><div dir="ltr">><br></div><div dir="ltr">> tmp = leadfield.leadfield{k};<br></div><div dir="ltr">><br></div><div dir="ltr">> tmp = tmp(b,:);<br></div><div dir="ltr">><br></div><div dir="ltr">> tmp = tmp-repmat(mean(tmp,1),[size(tmp,1) 1]); % average re-ref<br></div><div dir="ltr">><br></div><div dir="ltr">> leadfield.leadfield{k} = tmp;<br></div><div dir="ltr">><br></div><div dir="ltr">> end<br></div><div dir="ltr">><br></div><div dir="ltr">> end<br></div><div dir="ltr">><br></div><div dir="ltr">> leadfield.label = leadfield.label(b);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % Compute common spatial filter<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.channel = datall_avg.label;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.elec = elec_aligned.elec_aligned;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.method = 'lcmv';<br></div><div dir="ltr">><br></div><div dir="ltr">> % cfg.grid = leadfield;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.headmodel = headmodel.hdm;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.sourcemodel.grid.pos = leadfield.pos; % bei FT wird hier der output von ft_leadfield angegeben<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.grid.resolution = 5;<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.lcmv.keepfilter = 'yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.lcmv.fixedori = 'yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.lcmv.projectnoise = 'yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.lcmv.weightnorm = 'nai';<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.lcmv.lambda = '5%';<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> eegsrc_all = ft_sourceanalysis(cfg, datall_avg); %source data in all time window<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % apply common filters to pre and post stimulus data<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.grid.filter = eegsrc_all.avg.filter;<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % Compute source for baseline and activty time window<br></div><div dir="ltr">><br></div><div dir="ltr">> eegsrc_basl = ft_sourceanalysis(cfg, datbasl_avg); %source data for baseline time window<br></div><div dir="ltr">><br></div><div dir="ltr">> eegsrc_act = ft_sourceanalysis(cfg, datact_avg); %source data for activity time window<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % contrast post stimulus onset activity with respect to baseline<br></div><div dir="ltr">><br></div><div dir="ltr">> eegsrc_act_basnrm = eegsrc_act;<br></div><div dir="ltr">><br></div><div dir="ltr">> eegsrc_act_basnrm.avg.pow = (eegsrc_act.avg.pow - eegsrc_basl.avg.pow)./ eegsrc_basl.avg.pow;<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %% Choose time of interest<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % average across time the dipole moments within the N1 latency range<br></div><div dir="ltr">><br></div><div dir="ltr">> ind = find(eegsrc_act_basnrm.time>=0.1 & eegsrc_act_basnrm.time<=0.5); %at 18ms-24ms<br></div><div dir="ltr">><br></div><div dir="ltr">> timecourse = eegsrc_act_basnrm.avg.mom(eegsrc_act_basnrm.inside);<br></div><div dir="ltr">><br></div><div dir="ltr">> tc_toi = zeros(size(eegsrc_act_basnrm.avg.pow(eegsrc_act_basnrm.inside)));<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> for ii = 1:length(timecourse)<br></div><div dir="ltr">><br></div><div dir="ltr">> tc_toi(ii) = mean(abs(timecourse{ii}(ind)));<br></div><div dir="ltr">><br></div><div dir="ltr">> end<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> source = eegsrc_act_basnrm;<br></div><div dir="ltr">><br></div><div dir="ltr">> source.avg.pow(eegsrc_act_basnrm.inside) = tc_toi;<br></div><div dir="ltr">><br></div><div dir="ltr">> % source.cfg = rmfield(source.cfg, {'headmodel' 'callinfo'}); % this is removed because it takes up a lot of memory<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % Save source<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % interpolate and normalize to individual mri<br></div><div dir="ltr">><br></div><div dir="ltr">> %first, interpolate<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.parameter = 'pow';<br></div><div dir="ltr">><br></div><div dir="ltr">> eeg_source_interp = ft_sourceinterpolate(cfg, source, mri);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % second, normalise<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.nonlinear = 'no';<br></div><div dir="ltr">><br></div><div dir="ltr">> eeg_source_interp_norm = ft_volumenormalise(cfg, eeg_source_interp);<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % save the interpolate and normalised data<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> %%<br></div><div dir="ltr">><br></div><div dir="ltr">> % load source reconstructed data<br></div><div dir="ltr">><br></div><div dir="ltr">> % keep subjects path_data nsubj path_ft<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> allsource_rpost = cell(1,nsubj);<br></div><div dir="ltr">><br></div><div dir="ltr">> for s = 1:nsubj<br></div><div dir="ltr">><br></div><div dir="ltr">> datapath = strcat(fname_eeg_source_interp_norm);<br></div><div dir="ltr">><br></div><div dir="ltr">> % datafile = fullfile(datapath, [subj{s}, '_source.mat']); % take _source data?<br></div><div dir="ltr">><br></div><div dir="ltr">> datafile = fullfile(datapath, [subj{s}, '_eeg_source_interp_norm.mat']);<br></div><div dir="ltr">><br></div><div dir="ltr">> load(datafile)<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> allsource_rpost{s} = eeg_source_interp_norm;<br></div><div dir="ltr">><br></div><div dir="ltr">> % allsource_rpost{s} = source;<br></div><div dir="ltr">><br></div><div dir="ltr">> end<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> % grand average<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg = [];<br></div><div dir="ltr">><br></div><div dir="ltr">> cfg.parameter = 'pow';<br></div><div dir="ltr">><br></div><div dir="ltr">> % cfg.keepindividual='yes';<br></div><div dir="ltr">><br></div><div dir="ltr">> gasource_rpost = ft_sourcegrandaverage(cfg, allsource_rpost{:});<br></div><div dir="ltr">><br></div><div dir="ltr">> %save gasource_rpost<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> With best wishes,<br></div><div dir="ltr">><br></div><div dir="ltr">> Faizal Zulkifly<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> --------------------------------------------------------<br></div><div dir="ltr">><br></div><div dir="ltr">> Mohd Faizal Mohd Zulkifly<br></div><div dir="ltr">><br></div><div dir="ltr">> University Medical Center Göttingen<br></div><div dir="ltr">><br></div><div dir="ltr">> University of Göttingen<br></div><div dir="ltr">><br></div><div dir="ltr">> Clinic for Clinical Neurophysiology<br></div><div dir="ltr">><br></div><div dir="ltr">> Robert-Koch-Straße 40<br></div><div dir="ltr">><br></div><div dir="ltr">> 37075 Göttingen<br></div><div dir="ltr">><br></div><div dir="ltr">> Germany<br></div><div dir="ltr">><br></div><div dir="ltr">> Tel: +49 551 39-8457<br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">><br></div><div dir="ltr">> _______________________________________________<br></div><div dir="ltr">> fieldtrip mailing list<br></div><div dir="ltr">> <a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" rel="nofollow" target="_blank">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br></div><div dir="ltr">> <a href="https://doi.org/10.1371/journal.pcbi.1002202" rel="nofollow" target="_blank">https://doi.org/10.1371/journal.pcbi.1002202</a><br></div><div dir="ltr"><br></div><div dir="ltr">_______________________________________________<br></div><div dir="ltr">fieldtrip mailing list<br></div><div dir="ltr"><a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" rel="nofollow" target="_blank">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br></div><div dir="ltr"><a href="https://doi.org/10.1371/journal.pcbi.1002202" rel="nofollow" target="_blank">https://doi.org/10.1371/journal.pcbi.1002202</a><br></div></div>
</div>
</div></body></html>