-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdipfit_nonlinear.m
More file actions
126 lines (112 loc) · 4.89 KB
/
Copy pathdipfit_nonlinear.m
File metadata and controls
126 lines (112 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
% dipfit_nonlinear() - perform nonlinear dipole fit on one of the components
% to improve the initial dipole model. Only selected dipoles
% will be fitted.
%
% Usage:
% >> EEGOUT = dipfit_nonlinear( EEGIN, optarg)
%
% Author: Robert Oostenveld, SMI/FCDC, Nijmegen 2003
% Thanks to Nicolas Robitaille for his help on the CTF MEG
% implementation
% SMI, University Aalborg, Denmark http://www.smi.auc.dk/
% FC Donders Centre, University Nijmegen, the Netherlands http://www.fcdonders.kun.nl
% Copyright (C) 2003 Robert Oostenveld, SMI/FCDC roberto@smi.auc.dk
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [EEGOUT] = dipfit_nonlinear( EEG, varargin )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% convert the optional arguments into a configuration structure that can be
% understood by FIELDTRIPs dipolefitting function
if nargin>2
cfg = struct(varargin{:});
else
help dipfit_nonlinear
return
end
% specify the FieldTrip DIPOLEFITTING configuration
cfg.model = 'moving';
cfg.gridsearch = 'no';
if ~isfield(cfg, 'nonlinear')
% if this flag is set to 'no', only the dipole moment will be fitted
cfg.nonlinear = 'yes';
end
% add some additional settings from EEGLAB to the configuration
tmpchanlocs = EEG.chanlocs;
cfg.channel = { tmpchanlocs(EEG.dipfit.chansel).labels };
if isfield(EEG.dipfit, 'vol')
cfg.vol = EEG.dipfit.vol;
elseif isfield(EEG.dipfit, 'hdmfile')
cfg.hdmfile = EEG.dipfit.hdmfile;
else
error('no head model in EEG.dipfit')
end
if isfield(EEG.dipfit, 'elecfile') & ~isempty(EEG.dipfit.elecfile)
cfg.elecfile = EEG.dipfit.elecfile;
end
if isfield(EEG.dipfit, 'gradfile') & ~isempty(EEG.dipfit.gradfile)
cfg.gradfile = EEG.dipfit.gradfile;
end
% set up the initial dipole model based on the one in the EEG structure
cfg.dip.pos = EEG.dipfit.model(cfg.component).posxyz;
cfg.dip.mom = EEG.dipfit.model(cfg.component).momxyz';
cfg.dip.mom = cfg.dip.mom(:);
% convert the EEGLAB data structure into a structure that looks as if it
% was computed using FIELDTRIPs componentanalysis function
comp = eeglab2fieldtrip(EEG, 'componentanalysis', 'dipfit');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Added code to handle CTF data with multipleSphere head model %
% This code is copy-pasted in dipfit_gridSearch, dipfit_nonlinear %
% The flag .isMultiSphere is used by dipplot %
% Nicolas Robitaille, January 2007. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Do some trick to force fieldtrip to use the multiple sphere model
if strcmpi(EEG.dipfit.coordformat, 'CTF') && ~isstruct(EEG.dipfit.chanfile)
cfg = rmfield(cfg, 'channel');
comp = rmfield(comp, 'elec');
cfg.gradfile = EEG.dipfit.chanfile;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fit the dipoles to the ICA component(s) of interest using FIELDTRIPs
% dipolefitting function
currentPath = pwd;
ptmp = which('ft_prepare_vol_sens');
ptmp = fileparts(ptmp);
if isempty(ptmp), error('Path to "forward" folder of Fieldtrip missing'); end;
cd(fullfile(ptmp, 'private'));
try,
source = ft_dipolefitting(cfg, comp);
catch,
cd(currentPath);
lasterr
error(lasterr);
end;
cd(currentPath);
% reformat the output dipole sources into EEGLABs data structure
EEG.dipfit.model(cfg.component).posxyz = source.dip.pos;
EEG.dipfit.model(cfg.component).momxyz = reshape(source.dip.mom, 3, length(source.dip.mom)/3)';
EEG.dipfit.model(cfg.component).diffmap = source.Vmodel - source.Vdata;
EEG.dipfit.model(cfg.component).sourcepot = source.Vmodel;
EEG.dipfit.model(cfg.component).datapot = source.Vdata;
EEG.dipfit.model(cfg.component).rv = source.dip.rv;
%EEG.dipfit.model(cfg.component).rv = sum((source.Vdata - source.Vmodel).^2) / sum( source.Vdata.^2 );
try
EEG = eeg_compatlas(EEG, 'components', cfg.component);
catch
disp('Fail to look up brain areas');
end
EEGOUT = EEG;