<div dir="ltr"><div>Hi all,<br><br></div>When I plot the sourcemodel and the headmodel, the sourcemodel is many magnitudes larger than the headmodel. Not sure if it has anything to do with my code so attaching that below. Also attaching the jpgs. <br><br>%% read the parameters<br><br>%% Parameter 2.2<br><br>mrifile='Siemens_AF8-MPRAGE_20141022_1.nii';<br>mritype='MNI';<br>realign='realigned';<br>volumereslicing='volumeresliced';<br>brainthresh=0.5;<br>scalpthresh=0.10;<br>scalpsmooth='scalp_smooth';<br>scalp_smooth.parameter=9;<br>scalpvertices=1000;<br>skullvertices=1000;<br>brainvertices=1000;<br>meshmethod='projectmesh';<br>headmodel_method='dipoli';<br>subjnum='915';<br>time='pre';<br>timetype='hassan';<br>timewindow=[0 .1];<br>gridtype='template10';<br>beamformer='lcmv';<br>sourcemodeltype= 'templatebased';<br>identifier='Parameter2-2';<br>===================================<br>%%read in mri<br><br>switch mritype<br>    case 'MNI'<br>        [mri]               = ft_read_mri(mrifile);<br>        mri.coordsys        ='MNI';<br>    case 'Analyze'<br>        [mri]               = ft_read_mri(mrifile);<br>        mri.coordsys        ='Analyze';<br>    case 'SPM'<br>        [mri]               = ft_read_mri(mrifile);<br>        mri.coordsys        = 'SPM';<br>end<br>save(strcat(mrifile,'_',identifier,'_','mri.mat'),'mri')<br>cfg=[];<br>ft_sourceplot(cfg,mri)<br>==============================<br><br>%% realigning<br>switch realign<br>    case 'realigned'<br>        cfg=[];<br>        cfg.method='fiducial';<br>        cfg.fiducial.nas    = [6.60688      6.30230     -2.94229];<br>        cfg.fiducial.lpa    = [6.04082      7.65872     0.350950];<br>        cfg.fiducial.rpa    = [4.41106      8.71481      3.50199];<br>        mri_realigned=ft_volumerealign(cfg,mri);<br>        ft_sourceplot(cfg,mri_realigned)<br>        save(strcat(mrifile,'_',identifier,'_','mri_realigned.mat'),'mri_realigned')<br>    case 'notrealigned'<br>        %do nothing<br>end<br><br>=========================<br>%% volume reslicing<br>switch volumereslicing<br>    case 'volumeresliced'<br>    cfg = [];<br>    cfg.resolution = 1;<br>    mri_resliced = ft_volumereslice(cfg, mri);<br>    save(strcat(mrifile,'_',identifier,'_','mri_resliced.mat'),'mri_resliced')<br>    case 'notresliced'<br>        %do nothing<br>end<br>ft_sourceplot(cfg,mri_resliced)<br>==============================<br><br>%% segmentation parameters<br>switch scalpsmooth<br>    case 'noscalpsmooth'<br>        cfg                 = [];<br>        cfg.brainthreshold  = brainthresh;<br>        cfg.scalpthreshold  = scalpthresh;<br>        cfg.downsample      = 1; %no downsampling<br>        cfg.output          = {'brain' 'skull' 'scalp'};<br>        seg                 = ft_volumesegment(cfg, mri);<br>    case 'scalp_smooth'<br>        cfg                 = [];<br>        cfg.brainthreshold  = brainthresh;<br>        cfg.scalpthreshold  = scalpthresh;<br>        cfg.scalpsmooth     = scalp_smooth.parameter;<br>        cfg.downsample      = 1; %no downsampling<br>        cfg.output          = {'brain' 'skull' 'scalp'};<br>        seg                 = ft_volumesegment(cfg, mri); <br>end<br><br>seg.anatomy         = mri.anatomy;<br>cfg                 = [];<br>cfg.funparameter    = 'trishells';<br>cfg.funcolormap     = [1 0 0;0 1 0;0 0 1];<br>cfg.maskparameter=cfg.funparameter;<br>ft_sourceplot(cfg,seg)<br><br>save(strcat(mrifile,scalpsmooth,'_',identifier,'_','seg.mat'),'seg')<br>=================================<br>%% prepare mesh <br><br>cfg                 = [];<br>cfg.method= meshmethod;<br>cfg.tissue          = {'scalp', 'skull', 'brain'};<br>cfg.numvertices     = [scalpvertices skullvertices brainvertices];<br>bnd                 = ft_prepare_mesh(cfg, seg);<br>% i enlarged / reduced the boundary mesh a little bit, because they were causing errors, probably due to overlapping<br>bnd(1).pnt          = bnd(1).pnt.*1.001;<br>bnd(2).pnt          = bnd(2).pnt.*1;<br>bnd(3).pnt          = bnd(3).pnt.*0.999;<br><br>figure;<br>ft_plot_mesh(bnd(1))%scalp<br>figure;<br>ft_plot_mesh(bnd(2))%skull<br>figure;<br>ft_plot_mesh(bnd(3))%brain<br><br>%% save mesh<br>save(strcat(mrifile,cfg.method,num2str(scalpvertices),num2str(skullvertices),num2str(brainvertices),'_',identifier,'_','bnd.mat'),'bnd')<br>=============================================<br><br>%% build headmodel<br><br>switch headmodel_method<br>    case 'dipoli'<br>        cfg                 = [];<br>        cfg.method          = 'dipoli';<br>    case 'bemcp'<br>        cfg                 = [];<br>        cfg.method          = 'bemcp';<br>end<br><br>headmodel           = ft_prepare_headmodel(cfg, bnd);<br>headmodel           = ft_convert_units(headmodel,'cm');<br><br>save(strcat(mrifile,cfg.method,'_',identifier,'_','headmodel.mat'),'headmodel');<br><br>====================================<br>%% adding electrodes to headmodel<br>elec=ft_read_sens('Hydrocel_GSN_128_1.0_TRIM_mod.sfp');    %load electrode file                     <br>% visualize head surface (scalp)<br>figure;<br>ft_plot_mesh(headmodel.bnd(1), 'edgecolor','none','facealpha',0.8,'facecolor',[0.6 0.6 0.8]); <br>hold on;<br>% plot electrodes<br>ft_plot_sens(elec,'style', 'sk')<br>=================================<br>%most likely they are not aligned, so for aligning electrodes<br>cfg           = [];<br>cfg.method    = 'interactive';%can try the 'fiducial' method but is very <br>%likely not to fix it<br>cfg.elec      = elec;<br>% nas=mri_realigned.cfg.fiducial.nas;<br>% lpa=mri_realigned.cfg.fiducial.lpa;<br>% rpa=mri_realigned.cfg.fiducial.rpa;<br>cfg.headshape = headmodel.bnd(1);%scalp<br>elec_aligned  = ft_electroderealign(cfg);%use the gui to manually align the electrodes<br><br>save(strcat(mrifile,'_',identifier,'_','elec_aligned.mat'),'elec_aligned')<br>==================================<br>%% load EEG data<br>load(strcat('subj',subjnum,'_',time,'_FT_bothStim_rejChan500hz0.1hz-30hz_fromContinuous.mat'), 'dataM6w')<br>load(strcat('subj',subjnum,'_',time,'_FT_bothStim_rejChan500hz0.1hz-30hz_fromContinuous.mat'), 'dataM0')<br>load(strcat('subj',subjnum,'_',time,'_FT_bothStim_rejChan500hz0.1hz-30hz_fromContinuous.mat'), 'dataM3W')<br>load(strcat('subj',subjnum,'_',time,'_FT_bothStim_rejChan500hz0.1hz-30hz_fromContinuous.mat'), 'dataM3Bw')<br>load(strcat('subj',subjnum,'_',time,'_FT_bothStim_rejChan500hz0.1hz-30hz_fromContinuous.mat'), 'dataM3Bb')<br>load(strcat('subj',subjnum,'_',time,'_FT_bothStim_rejChan500hz0.1hz-30hz_fromContinuous.mat'), 'dataM6b')<br>load(strcat('subj',subjnum,'_',time,'_FT_bothStim_rejChan500hz0.1hz-30hz_fromContinuous.mat'), 'dataM6')<br>====================================<br>%% timelock data<br><br>cfg = [];<br>cfg.refef= 'yes'; %added at 2-5<br><br>    <br>    same = dataM0;<br>    same.trial = [dataM0.trial dataM3W.trial];<br>    same.time = [dataM0.time dataM3W.time];<br>    <br>    diff = dataM6;<br>    diff.trial = [dataM3Bb.trial dataM6.trial];<br>    diff.time = [dataM3Bb.time dataM6.time];<br>    <br> switch timetype<br>     case 'simple'<br>        timelock_m0 = ft_timelockanalysis(cfg, dataM0);<br>        timelock_m6 = ft_timelockanalysis(cfg, dataM6);<br>        timelock_m3w = ft_timelockanalysis(cfg, dataM3W);<br>        timelock_m3b = ft_timelockanalysis(cfg, dataM3Bb);<br>        timelock_diff = ft_timelockanalysis(cfg, diff);<br>        timelock_same = ft_timelockanalysis(cfg, same);<br>     case 'hassan'<br>       cfg.vartrllength = 0; %was changed to 0 from 2<br>       cfg.covariance = 'yes';<br>       cfg.covariancewindow = timewindow;<br>       cfg.keeptrials = 'yes';<br>       timelock_m0 = ft_timelockanalysis(cfg, dataM0);<br>       timelock_m6 = ft_timelockanalysis(cfg, dataM6);<br>       timelock_m3w = ft_timelockanalysis(cfg, dataM3W);<br>       timelock_m3b = ft_timelockanalysis(cfg, dataM3Bb);<br>       timelock_diff = ft_timelockanalysis(cfg, diff);<br>       timelock_same = ft_timelockanalysis(cfg, same);<br><br> end<br> <br>save(strcat(mrifile,timetype,time,'_',identifier,'_','timelock_datam0.mat'),'timelock_m0')<br>save(strcat(mrifile,timetype,time,'_',identifier,'_','timelock_datam6.mat'),'timelock_m6')<br>save(strcat(mrifile,timetype,time,'_',identifier,'_','timelock_datam3w.mat'),'timelock_m3w')<br>save(strcat(mrifile,timetype,time,'_',identifier,'_','timelock_data3b.mat'),'timelock_m3b')<br>save(strcat(mrifile,timetype,time,'_',identifier,'_','timelock_diff.mat'),'timelock_diff')<br>save(strcat(mrifile,timetype,time,'_',identifier,'_','timelock_same.mat'),'timelock_same')<br>    <br>============================<br>%% make sourcemodel<br><br>switch sourcemodeltype<br>    case 'templatebased'<br>        template=load('standard_sourcemodel3d10mm.mat');<br>        elec=ft_read_sens('Hydrocel_GSN_128_1.0_TRIM_mod.sfp');    %load electrode file        <br>        cfg = [];<br>        cfg.headmodel = headmodel;<br>        cfg.mri=mri; <br>        cfg.grid.warpmni = 'yes';<br>        cfg.grid.template = template.sourcemodel;<br>        cfg.grid.nonlinear = 'yes';<br>        cfg.resolution = 2; % resolution of the template grid in mm<br>        cfg.moveinward = 1; % actually uses vol mesh<br>        cfg.grid.unit      ='mm';<br>        cfg.inwardshift = 0; % needs to be expressed to work with moveinward<br>        cfg.elec = elec;<br>        % cfg.headshape=('cortex_5124.surf.gii');<br>        mysourcemodel = ft_prepare_sourcemodel(cfg);<br><br>    case 'individual'<br>        load('standard_sourcemodel3d5mm.mat');<br>        cfg = [];<br>        cfg.grid.warpmni = 'yes';<br>        cfg.resolution = 5; % resolution of the template grid in mm<br>        cfg.grid.nonlinear = 'yes'; % use non linear normalization<br>        cfg.mri = mri; % use subject's mri in mni coordinates<br>        cfg.grid.unit = 'mm';<br>        mysourcemodel = ft_prepare_sourcemodel(cfg);<br>    case 'template' <br>        mysourcemodel=load('standard_sourcemodel3d10mm.mat');%doensn't work<br>end <br><br><br><br>save(strcat(mrifile,'resolution',num2str(cfg.resolution),'_',subjnum,'_',identifier,'_','sourcemodel.mat'),'mysourcemodel')<br>=======================<br>%% Plot the single subject'e head model and grid positions<br><br>figure<br>hold on<br>ft_plot_vol(headmodel,'edgecolor','none','facealpha',0.3);<br>ft_plot_mesh(mysourcemodel.pos(mysourcemodel.inside,:));<br>========================<br>%% Double check that source model is aligned to the segmented volume and to the electrodes<br><b>HERE IS WHERE I SEE THE ERROR</b><br>figure<br>hold on<br>plot3(mysourcemodel.pos(mysourcemodel.inside,1),mysourcemodel.pos(mysourcemodel.inside,2),mysourcemodel.pos(mysourcemodel.inside,3),'.');<br>ft_plot_vol(headmodel,'facecolor','brain','edgecolor','none','facealpha',0.3);<br>ft_plot_sens(elec_aligned,'label','label','style','y*','Markersize',10);<br>========================================<br>%% make leadfield<br><br><br>switch gridtype<br>    case 'individual'<br>        cfg            = [];<br>        cfg.elec       = elec;<br>        cfg.headmodel  = headmodel;<br>        cfg.normalize  = 'yes';<br>        cfg.reducerank= 3;<br>         cfg.grid.unit = 'cm';% same unit as above, i.e. in cm<br>        [grid] = ft_prepare_leadfield(cfg);<br>    case 'templatebased'<br>        load('standard_sourcemodel3d10mm.mat')<br>        cfg                 = [];<br>        cfg.elec            = elec;<br>        cfg.template = sourcemodel.cfg.grid;<br>        cfg.grid=mysourcemodel.cfg.grid;<br>        cfg.headmodel             = headmodel;<br>        cfg.reducerank      = 3; % default is 3 for EEG, 2 for MEG<br>        cfg.channel  = 'all';<br>        cfg.normalize= 'yes';<br>        cfg.grid.unit       = 'cm';<br>        cfg.grid.tight      = 'yes';<br>        [grid] = ft_prepare_leadfield(cfg);<br>    case 'template10'<br>        load('standard_sourcemodel3d10mm.mat')<br>        cfg.normalize='yes';<br>        [grid]=sourcemodel.cfg.grid;<br>end<br><br><br>save(strcat(mrifile,gridtype,'_',identifier,'_','leadfield.mat'),'grid')<br>===========================<br>%% Interpolate<br>cfg = [];<br>cfg.interpmethod = 'linear';<br>cfg.parameter = 'pow';<br>sourceinterp_diff = ft_sourceinterpolate(cfg, sourceanalysis_diff, mri);<br>sourceinterp_same = ft_sourceinterpolate(cfg, sourceanalysis_same, mri);<br>sourceinterp_m0 = ft_sourceinterpolate(cfg, sourceanalysis_m0, mri);<br>sourceinterp_m6 = ft_sourceinterpolate(cfg, sourceanalysis_m6, mri);<br>sourceinterp_m3w = ft_sourceinterpolate(cfg, sourceanalysis_m3w, mri);<br>sourceinterp_m3b = ft_sourceinterpolate(cfg, sourceanalysis_m3b, mri);<br><br><br>% save(strcat(mrifile,'_',identifier,'_','sourceinterp_diff.mat'),'sourceinterp_diff')<br>% save(strcat(mrifile,'_',identifier,'_','sourceinterp_m0.mat'),'sourceinterp_m0')<br>% save(strcat(mrifile,'_',identifier,'_','sourceinterp_m6.mat'),'sourceinterp_m6')<br>% save(strcat(mrifile,'_',identifier,'_','sourceinterp_m3w.mat'),'sourceinterp_m3w')<br>% save(strcat(mrifile,'_',identifier,'_','sourceinterp_m3b.mat'),'sourceinterp_m3b')<br>% save(strcat(mrifile,'_',identifier,'_','sourceinterp_same.mat'),'sourceinterp_same')<br>=================================<br><br>%%beamforming<br><br>cfg = [];<br>cfg.method            = 'slice';<br>% cfg.slicedim= 1;<br>cfg.funcolormap = 'jet';<br>% cfg.method        = 'ortho';<br>% cfg.funcolorlim   = 'zeromax';<br>% cfg.funcolorlim   = [0.01 0.2];<br>cfg.funparameter= 'pow';<br><br><br>test2=sourceinterp_diff;<br>test2.pow=((sourceinterp_diff.pow-sourceinterp_same.pow)./(sourceinterp_diff.pow+sourceinterp_same.pow));<br>test=sourceinterp_diff;<br>test.pow=sourceinterp_diff.pow-sourceinterp_same.pow;<br>ft_sourceplot(cfg,test)<br>ft_sourceplot(cfg,test2)<br>ft_sourceplot(cfg,sourceinterp_same);<br>ft_sourceplot(cfg,sourceinterp_diff);<br>ft_sourceplot(cfg,sourceinterp_m0);<br>ft_sourceplot(cfg,sourceinterp_m6);<br>ft_sourceplot(cfg,sourceinterp_m3w);<br>ft_sourceplot(cfg,sourceinterp_m3b);<br></div>