-
Notifications
You must be signed in to change notification settings - Fork 8
/
dipfit_gridsearch.m
115 lines (103 loc) · 4.45 KB
/
dipfit_gridsearch.m
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
% dipfit_gridsearch() - do initial batch-like dipole scan and fit to all
% data components and return a dipole model with a
% single dipole for each component.
%
% Usage:
% >> EEGOUT = dipfit_gridsearch( EEGIN, varargin)
%
% Inputs:
% ...
%
% Optional inputs:
% 'component' - vector with integers, ICA components to scan
% 'xgrid' - vector with floats, grid positions along x-axis
% 'ygrid' - vector with floats, grid positions along y-axis
% 'zgrid' - vector with floats, grid positions along z-axis
%
% Output:
% ...
%
% Author: Robert Oostenveld, SMI/FCDC, Nijmegen 2003, load/save by
% Arnaud Delorme
% 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 [email protected]
%
% 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_gridsearch(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_gridsearch
return
end
% specify the FieldTrip DIPOLEFITTING configuration
cfg.model = 'moving';
cfg.gridsearch = 'yes';
cfg.nonlinear = 'no';
% 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
% 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 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isfield(cfg, 'component')
% default is to scan all components
cfg.component = 1:size(comp.topo,2);
end
% for each component scan the whole brain with dipoles using FIELDTRIPs
% dipolefitting function
source = ft_dipolefitting(cfg, comp);
% reformat the output dipole sources into EEGLABs data structure
for i=1:length(cfg.component)
EEG.dipfit.model(cfg.component(i)).posxyz = source.dip(i).pos;
EEG.dipfit.model(cfg.component(i)).momxyz = reshape(source.dip(i).mom, 3, length(source.dip(i).mom)/3)';
EEG.dipfit.model(cfg.component(i)).rv = source.dip(i).rv;
end
EEGOUT = EEG;