-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheeglab2loreta.m
161 lines (145 loc) · 6.66 KB
/
eeglab2loreta.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
% eeglab2loreta() - export chanlocs EEGLAB channel structure
% to Tailarach coordinates that can be read using
% the LORETA software.
% Usage:
% eeglab2loreta( chanlocs, data, 'key', 'val', ... );
%
% Inputs:
% chanlocs - EEGLAB channel location structure using MNI
% coordinates (if not use parameter 'transform').
% data - [chan x time] or ICA inverse matrix (EEG.icawinv)
% containing component to export. Enter an empty array
% to export only the channel location file. Note that
% exporting [ chan x time ] requires to set the 'exporterp'
% flag.
%
% Optional parameter:
% 'fileloc' - [string] file name for channel locations. Extension
% '.xyz' (exporting coordinates) or '.txt' (exporting
% labels only) will be added automatically. Default is
% 'loreta_chanlocs'.
% 'filecomp' - [string] base file name for components. Default is
% 'comp' so files are written as 'compX.txt' (X representing
% the component number) or 'erp.txt' when exporting ERP.
% 'exporterp' - ['on'|'off'] export ERP instead of
% 'compnum' - [integer array] only export these components.
% 'labelonly' - ['on'|'off'] only export channel labels and have LORETA
% lookup talairach position for them. Default: 'off'.
% 'transform' - [4x4 matrix] optional homogenous transfromation matrix
% to convert channel electrode location to MNI
% coordinates.
% 'excludechan' - [integer] list of channel to omit.
% 'lowchanlim' - [float] lower z limit (in MNI space) for exporting
% channel. For instance 0 will only export channel above
% the midline.
% 'polinv' - [-1 and 1s] invert polarity of some components.
% Default is no inversion.
%
% Output files:
% location_file - contain channel labels or 3-D coordinates in Talairach
% space that can be directly read by LORETA
% component_files - contains 10 rows containing 10 identical component
% scalp maps.
%
% Author: Arnaud Delorme, SCCN, INC, UCSD, 2005
%
% See also: eeglab()
% Copyright (C) 2005 Arnaud Delorme, SCCN, INC, UCSD, 2005 [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 eeglab2loreta( chanlocs, winv, varargin )
if nargin < 2
help eeglab2loreta;
return;
end;
g = finputcheck( varargin, { 'fileloc' 'string' [] 'loreta_chanlocs';
'filecomp' 'string' [] 'comp';
'compnum' 'integer' [1 Inf] [];
'transform' 'real' [] [];
'labelonly' 'string' { 'on' 'off' } 'off';
'exporterp' 'string' { 'on' 'off' } 'off';
'excludechan' 'integer' [1 Inf] [];
'polinv' 'integer' [-1 1] [];
'lowchanlim' 'float' [] NaN });
if isstr(g), error(g); end
if isempty(g.compnum), g.compnum = [1:size(winv,2)]; end
if isempty(g.polinv) g.polinv = ones(1, length(g.compnum)); end
if length(g.compnum) ~= length(g.polinv), error('Length of polarity inversion option must match the number of components'); end
% remove channels
% ---------------
inds = find(cellfun('isempty', { chanlocs.X }));
g.excludechan = union(g.excludechan, inds);
if ~isempty(g.excludechan)
chanlocs(g.excludechan) = [];
winv(g.excludechan,:) = [];
end
if strcmpi(g.labelonly, 'off')
% convert to MNI coordinates
% --------------------------
XYZ = [ [chanlocs.X]' [chanlocs.Y]' [chanlocs.Z]' ];
if ~isempty(g.transform)
XYZ = g.transform*[ XYZ ones(size(XYZ,1),1) ]';
XYZ = XYZ(1:3,:)';
end
% remove all channels below limit
% -------------------------------
if ~isnan(g.lowchanlim)
rmelec = find(XYZ(:,3) < g.lowchanlim);
chanlocs(rmelec) = [];
XYZ(rmelec,:) = [];
winv(rmelec,:) = [];
end
% convert to tailairach coordinates
% ---------------------------------
talXYZ = mni2tal(XYZ);
% writing file
% ------------
fid = fopen( [ g.fileloc '.xyz' ], 'w');
fprintf(fid, '%d\n', length(chanlocs));
for index = 1:length(chanlocs)
fprintf(fid, '%e %e %e %s\n', talXYZ(index,1), -talXYZ(index,2), talXYZ(index,3),chanlocs(index).labels);
end
fclose(fid);
fprintf('File %s saved\n', [ g.fileloc '.xyz' ]);
else
% export labels only
% ------------------
fid = fopen( [ g.fileloc '.txt' ], 'w');
for index = 1:length(chanlocs)
fprintf(fid, '%s\n', chanlocs(index).labels);
end
fclose(fid);
fprintf('File %s saved\n', [ g.fileloc '.xyz' ]);
end
% export components
% -----------------
if ~isempty(winv)
if strcmpi(g.exporterp, 'off')
for iComp=1:length(g.compnum)
toLORETA = winv(:,g.compnum(iComp))'*g.polinv(iComp);
toLORETA(2:10,:) = ones(9,1)*toLORETA(1,:);
filename = sprintf( [ g.filecomp '%d.txt' ], g.compnum(iComp));
save('-ascii', filename, 'toLORETA');
fprintf('File %s saved\n', filename);
end
else
if strcmpi(g.filecomp, 'comp'), g.filecomp = 'erp'; end
toLORETA = double(winv)';
filename = g.filecomp;
if ~strcmpi(g.filecomp(end-2:end), 'txt'), filename = [ filename '.txt' ]; end
save('-ascii', filename, 'toLORETA');
fprintf('File %s saved\n', filename);
end
end