Note that if work from the path where all the downloadable parts are downloaded to, you don’t need to change the paths
(leave them as is, but do evaluate the sections)
The paths simply serve as an example of how you can set up your analysis structure, which is especially useful if you have more than one subject
Change these to appropriate paths for your operating system and setup
%% paths
addpath /home/lau/matlab/fieldtrip-20170613_workshop_natmeg/
ft_defaults
raw_meg_path = '/archive/20067_workshop_source_reconstruction/MEG/';
meg_path = '/home/share/workshop_source_reconstruction/data/MEG/';
mri_path = '/home/share/workshop_source_reconstruction/data/MRI/';
set(0, 'DefaultAxesFontWeight', 'bold', 'defaultaxesfontsize', 20, 'defaultlinelinewidth', 2);
Make subject and recording specific paths (the cell array “subjects_and_dates” can be expanded)
%% subjects and dates
subjects_and_dates = ...
{
'NatMEG_0177/170424/'
};
output_path = fullfile(meg_path, subjects_and_dates{1});
events = [1 2 4 8 16]; %% right little, right ring, right middle, right index, right thumb
We are loading three different files here:
(For now leave the tissue_based_headmodel on as ‘yes’, later we will play around with it)
%% go to relevant path and load data
tissue_based_headmodel = 'yes';
% cd(output_path) %% comment this in if you want to set the output path
disp 'Loading timelockeds and headmodel'
load timelockeds.mat
load headmodel_meg.mat
load headmodel_eeg.mat
load headmodel_eeg_fourspheres_elec.mat
load headmodel_meg_singlesphere_headshape.mat
disp Done
if ~strcmp(tissue_based_headmodel, 'yes');
headmodel_eeg = headmodel_eeg_fourspheres_elec;
headmodel_eeg.type = 'concentricspheres';
headmodel_meg = headmodel_meg_singlesphere_headshape;
end
%% identify components of interest
close all
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
% multiplot over all magnetometers
cfg = [];
cfg.layout = 'neuromag306mag.lay'; % layour for magnetometers
ft_multiplotER(cfg, timelockeds{4}); %% explore this by yourself
% combine planar gradiometers (makes them interpretable)
cfg = [];
cmb = ft_combineplanar(cfg, timelockeds{4});
% plot channels "over" SI
cfg = [];
cfg.channel = {'MEG0412+0413' 'MEG0422+0423' 'MEG0432+0433' 'MEG0442+0443'}; %% channels "over" SI
cfg.layout = 'neuromag306cmb.lay'; %% layout for combined gradiometers
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
ft_singleplotER(cfg, cmb);
xlabel('Time (s)')
ylabel('Root mean squared activity')
% plot topoplot for early activity
cfg = [];
cfg.layout = 'neuromag306cmb.lay';
cfg.xlim = [0.045 0.065]; % s
cfg.comment = 'no';
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
ft_topoplotER(cfg, cmb);
% plot topoplot for later activity
cfg.xlim = [0.115 0.155]; % s
cfg.zlim = [0 6e-12]; % scale so both peaks can be seen
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
ft_topoplotER(cfg, cmb);
Notice the peaks around 55 msec and 135 msec
Create a grid around the brain and estimate the leadfield for each of the grid points in the brain
We define the leadfields for each of the three kinds of data here (magnetometers, gradiometers and electrodes)
%% make leadfields eeg and meg
cfg = [];
cfg.headmodel = headmodel_eeg;
cfg.elec = timelockeds{1}.elec;
cfg.senstype = 'eeg';
cfg.grid.resolution = 1;
cfg.grid.unit = 'cm';
leadfield_eeg = ft_prepare_leadfield(cfg, timelockeds{1});
cfg.senstype = 'meg';
cfg.grad = timelockeds{1}.grad;
cfg.headmodel = headmodel_meg;
cfg.channel = 'megmag';
leadfield_mag = ft_prepare_leadfield(cfg, timelockeds{1});
cfg.channel = 'meggrad';
leadfield_grad = ft_prepare_leadfield(cfg, timelockeds{1});
%% plot grid and headmodel
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
hold on
ft_plot_mesh(ft_convert_units(leadfield_mag, 'mm'));
ft_plot_vol(headmodel_meg);
view([-45 20]) %% try to rotate it yourself with the plot tools
%% dipole fits
n_events = length(events); % the events to be looped through
% the six cell arrays below are preparation for the fits
dipoles_mag_early = cell(1, n_events);
dipoles_grad_early = cell(1, n_events);
dipoles_eeg_early = cell(1, n_events);
dipoles_mag_late = cell(1, n_events);
dipoles_grad_late = cell(1, n_events);
dipoles_eeg_late = cell(1, n_events);
early_latency = [0.045 0.065]; % s
late_latency = [0.115 0.155]; % s
for event_index = 1:n_events
cfg = [];
cfg.gridsearch = 'yes'; %% search the grid for an optimal starting point
cfg.grid = leadfield_mag; %% supply the grid/leadfield
cfg.headmodel = headmodel_meg; %% supply the headmodel
cfg.dipfit.metric = 'rv'; %% the metric to minimize (the relative residual variance: proportion of variance left unexplained by the dipole model)
cfg.model = 'regional'; %% we assume that the dipole has a fixed position during the time points in the latency range
cfg.senstype = 'meg'; %% sensor type
cfg.channel = 'megmag'; %% which channels to use
cfg.nonlinear = 'yes'; %% do a non-linear search
% magnetometer fits
cfg.latency = early_latency; %% specify the latency
cfg.numdipoles = 1; %% we only expect contralateral activity
cfg.symmetry = []; %% empty for single dipole fit
dipoles_mag_early{event_index} = ft_dipolefitting(cfg, timelockeds{event_index});
cfg.latency = late_latency;
cfg.numdipoles = 2; %% we expect bilateral activity
cfg.symmetry = 'x'; %% we expect it to be symmetrical in the x-direction (ear-to-ear)
dipoles_mag_late{event_index} = ft_dipolefitting(cfg, timelockeds{event_index});
% gradiometer fits
cfg.channel = 'meggrad';
cfg.grid = leadfield_grad;
cfg.latency = early_latency;
cfg.numdipoles = 1;
cfg.symmetry = [];
dipoles_grad_early{event_index} = ft_dipolefitting(cfg, timelockeds{event_index});
cfg.latency = late_latency;
cfg.numdipoles = 2;
cfg.symmetry = 'x';
dipoles_grad_late{event_index} = ft_dipolefitting(cfg, timelockeds{event_index});
%% eeg fits
cfg.senstype = 'eeg';
cfg.channel = 'eeg';
cfg.grid = leadfield_eeg;
cfg.headmodel = headmodel_eeg;
cfg.latency = early_latency;
cfg.numdipoles = 1;
cfg.symmetry = [];
dipoles_eeg_early{event_index} = ft_dipolefitting(cfg, timelockeds{event_index});
cfg.latency = late_latency;
cfg.numdipoles = 2;
cfg.symmetry = 'x';
dipoles_eeg_late{event_index} = ft_dipolefitting(cfg, timelockeds{event_index});
end
disp('Done')
Try to rotate these with the normal plot tools to get a feeling of where the dipole is located
%% plot dipoles_early on brain
close all
load mri_resliced_cm.mat
all_dipoles = {dipoles_mag_early dipoles_grad_early dipoles_eeg_early};
colours = {'r' 'g' 'y' 'b' 'm'};
fingers = {'right little finger' 'right ring finger' 'right middle finger' 'right index finger' 'right thumb'};
% figure('units', 'normalized', 'outerposition', [0 0 1 1]);
for dipole_type_index = 1:length(all_dipoles)
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
for event_index = 1:n_events
subplot(2, 3, event_index)
dipole = all_dipoles{dipole_type_index}{event_index};
hold on
ft_plot_dipole(dipole.dip.pos(1, :), mean(dipole.dip.mom(1:3, :), 2), 'color', colours{event_index});
pos = mean(dipole.dip.pos, 1);
ft_plot_slice(mri_resliced_cm.anatomy, 'transform', mri_resliced_cm.transform, 'location', pos, 'orientation', [1 0 0], 'resolution', 0.1);
ft_plot_slice(mri_resliced_cm.anatomy, 'transform', mri_resliced_cm.transform, 'location', pos, 'orientation', [0 1 0], 'resolution', 0.1);
ft_plot_slice(mri_resliced_cm.anatomy, 'transform', mri_resliced_cm.transform, 'location', pos, 'orientation', [0 0 1], 'resolution', 0.1);
title_text = sprintf([fingers{event_index} ' at coordinates (cm):\nx: ' num2str(pos(1)) '; y: ' num2str(pos(2)) '; z: ' num2str(pos(3))]);
title(title_text, 'fontsize', 24)
axis tight
axis off
end
end
Try to rotate these with the normal plot tools to get a feeling of where the dipoles are located
%% plot dipoles_late on brain
close all
load mri_resliced_cm.mat
% load dipoles_late.mat
all_dipoles = {dipoles_mag_late dipoles_grad_late dipoles_eeg_late};
colours = {'r' 'g' 'y' 'b' 'm'};
fingers = {'right little finger' 'right ring finger' 'right middle finger' 'right index finger' 'right thumb'};
for dipole_type_index = 1:length(all_dipoles)
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
for event_index = 1:n_events
subplot(2, 3, event_index)
dipole = all_dipoles{dipole_type_index}{event_index};
hold on
ft_plot_dipole(dipole.dip.pos(1, :), mean(dipole.dip.mom(1:3, :), 2), 'color', colours{event_index});
ft_plot_dipole(dipole.dip.pos(2, :), mean(dipole.dip.mom(4:6, :), 2), 'color', colours{event_index});
pos = mean(dipole.dip.pos, 1);
ft_plot_slice(mri_resliced_cm.anatomy, 'transform', mri_resliced_cm.transform, 'location', pos, 'orientation', [1 0 0], 'resolution', 0.1);
ft_plot_slice(mri_resliced_cm.anatomy, 'transform', mri_resliced_cm.transform, 'location', pos, 'orientation', [0 1 0], 'resolution', 0.1);
ft_plot_slice(mri_resliced_cm.anatomy, 'transform', mri_resliced_cm.transform, 'location', pos, 'orientation', [0 0 1], 'resolution', 0.1);
title_text = sprintf([fingers{event_index} ' at coordinates (cm):\nx: ' num2str(pos(1)) '; y: ' num2str(pos(2)) '; z: ' num2str(pos(3))]);
title(title_text, 'fontsize', 24)
axis tight
axis off
end
end
We get a good fit to the SI
We get a good fit to the SI
We get a bad fit to the SI
We get a bad fit to the SII
We get a good fit to the SII
We get a bad fit to the SII
We get the best fits from the gradiometers. In contrast the electrodes seem way off.
It consists of a single sphere, which model the brain (the magnetic field does not have different conductivities through the different media)
We get a good fit to the SI
We get a good fit to the SI
Now We get a good bad fit to the SI
We get a bad fit to the SII
We get a good fit to the SII
Now we get a good fit to the SII