-
Notifications
You must be signed in to change notification settings - Fork 1
/
spm_eeg_plotScalpData_d.m
executable file
·446 lines (393 loc) · 11.8 KB
/
spm_eeg_plotScalpData_d.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
function [ZI,f,d] = spm_eeg_plotScalpData(Z,pos,ChanLabel,in)
% Display M/EEG interpolated sensor data on a scalp image
% FORMAT [ZI,f] = spm_eeg_plotScalpData(Z,pos,ChanLabel,in)
%
% INPUT:
% Z - the data matrix at the sensors
% pos - the positions of the sensors
% ChanLabel - the names of the sensors
% in - a structure containing some informations related to the
% main PRESELECTDATA window. This entry is not necessary
% OUTPUT:
% ZI - an image of interpolated data onto the scalp
% f - the handle of the figure which displays the interpolated
% data
%__________________________________________________________________________
%
% This function creates a figure whose purpose is to display an
% interpolation of the sensor data on the scalp (as an image).
%__________________________________________________________________________
% Copyright (C) 2008-2014 Wellcome Trust Centre for Neuroimaging
% Jean Daunizeau
% $Id: spm_eeg_plotScalpData.m 7221 2017-11-16 14:25:37Z vladimir $
ChanLabel = char(ChanLabel);
ParentAxes = [];
f = [];
clim = [min(Z(:))-( max(Z(:))-min(Z(:)) )/63 , max(Z(:))];
figName = 'Image Scalp data';
noButtons = 0;
if nargin < 4 || isempty(in)
in = [];
else
if isfield(in,'min') && ...
isfield(in,'max') && ...
isfield(in,'type')
clim = [in.min, in.max];
dc = abs(diff(clim))./63;
clim(1) = clim(1) - dc;
figName = ['Image Scalp data: ',in.type,' sensors'];
if isfield(in,'trN')
figName = [figName ', trial #',num2str(in.trN),'.'];
end
end
if isfield(in,'f')
f = in.f;
else
f = figure;
end
if isfield(in,'ParentAxes')
ParentAxes = in.ParentAxes;
else
ParentAxes = axes('parent',f);
end
if isfield(in,'noButtons')
noButtons = ~~in.noButtons;
end
end
if ~isfield(in,'cbar')
in.cbar = 1;
end
if ~isfield(in,'plotpos')
in.plotpos = 1;
end
if size(pos,2) ~= size(ChanLabel, 1)
pos = pos';
end
nD = size(pos,1);
if nD ~= 2
% get 2D positions from 3D positions
xyz = pos;
[pos] = get2Dfrom3D(xyz);
pos = pos';
end
% exclude channels ?
goodChannels = find(~isnan(pos(1,:)));
pos = pos(:,goodChannels);
Z = Z(goodChannels,:);
ChanLabel = ChanLabel(goodChannels, :);
if ~isempty(in) && isfield(in,'type') && strcmp(in.type, 'MEGPLANAR')
[cZ, cpos, cChanLabel] = combineplanar(Z, pos, ChanLabel);
else
cZ = Z;
cpos = pos;
cChanLabel = ChanLabel;
end
xmin = min(cpos(1,:));
xmax = max(cpos(1,:));
dx = (xmax-xmin)./100;
ymin = min(cpos(2,:));
ymax = max(cpos(2,:));
dy = (ymax-ymin)./100;
x = xmin:dx:xmax;
y = ymin:dy:ymax;
[XI,YI] = meshgrid(x,y);
ZI = griddata(cpos(1,:)',cpos(2,:)',full(double(cZ')),XI,YI);
try
figure(f)
catch
f = figure(...
'name', figName,...
'color', [1 1 1],...
'deleteFcn', @dFcn);
ParentAxes = axes('parent',f);
end
COLOR = get(f,'color');
d.hi = image(flipud(ZI),...
'CDataMapping','scaled',...
'Parent',ParentAxes);
set(ParentAxes,'nextPlot','add',...
'tag','spm_eeg_plotScalpData')
try
if length(unique(ZI)) ~= 1
[C,d.hc] = contour(ParentAxes,flipud(ZI),...
'linecolor',0.5.*ones(3,1));
end
end
caxis(ParentAxes,clim);
col = jet;
col(1,:) = COLOR;
colormap(ParentAxes,col)
if in.cbar
d.cbar = colorbar('peer',ParentAxes);
end
axis(ParentAxes,'off')
axis(ParentAxes,'equal')
axis(ParentAxes,'tight')
fpos = cpos;
fpos(1,:) = fpos(1,:) - xmin;
fpos(2,:) = fpos(2,:) - ymin;
fpos(1,:) = fpos(1,:)./(dx);
fpos(2,:) = fpos(2,:)./(dy);
fpos(2,:) = 100-fpos(2,:); % for display purposes (flipud imagesc)
figure(f);
if in.plotpos
d.hp = plot(ParentAxes,...
fpos(1,:),fpos(2,:),...
'ko');
end
d.ht = text(fpos(1,:),fpos(2,:),cChanLabel,...
'Parent',ParentAxes,...
'visible','off');
axis(ParentAxes,'image')
d.interp.XI = XI;
d.interp.YI = YI;
d.interp.pos = cpos;
d.f = f;
d.pos = fpos;
d.goodChannels = goodChannels;
d.ChanLabel = cChanLabel;
d.origChanLabel = ChanLabel;
d.origpos = pos;
d.ParentAxes = ParentAxes;
d.in = in;
if ~noButtons
d.hsp = uicontrol(f,...
'style','pushbutton',...
'callback',{@dosp},...
'BusyAction','cancel',...
'Interruptible','off',...
'position',[10 50 80 20],...
'string','channel pos');
d.hsn = uicontrol(f,...
'style','pushbutton',...
'callback',{@dosn},...
'BusyAction','cancel',...
'Interruptible','off',...
'position',[10 80 80 20],...
'string','channel names');
end
if ~isempty(in) && isfield(in,'handles')
nT = length(in.gridTime);
d.hti = uicontrol(f,...
'style','text',...
'BackgroundColor',COLOR,...
'string',[num2str(in.gridTime(in.x)),' (',in.unit,')'],...
'position',[10 10 120 20]);
d.hts = uicontrol(f,...
'style','slider',...
'Position',[130 10 250 20],...
'min',1,'max',nT,...
'value',in.x,'sliderstep',[1./(nT-1) 1./(nT-1)],...
'callback',{@doChangeTime},...
'BusyAction','cancel',...
'Interruptible','off');
set(d.hti,'userdata',d);
set(d.hts,'userdata',d);
end
if ~noButtons
set(d.hsp,'userdata',d);
set(d.hsn,'userdata',d);
end
set(d.ParentAxes,'userdata',d);
%==========================================================================
% dFcn
%==========================================================================
function dFcn(btn,evd)
hf = findobj('tag','Graphics');
D = get(hf,'userdata');
try delete(D.PSD.handles.hli); end
%==========================================================================
% dosp
%==========================================================================
function dosp(btn,evd)
d = get(btn,'userdata');
switch get(d.hp,'visible');
case 'on'
set(d.hp,'visible','off');
case 'off'
set(d.hp,'visible','on');
end
%==========================================================================
% dosn
%==========================================================================
function dosn(btn,evd)
d = get(btn,'userdata');
switch get(d.ht(1),'visible')
case 'on'
set(d.ht,'visible','off');
case 'off'
set(d.ht,'visible','on');
end
%==========================================================================
% doChangeTime
%==========================================================================
function doChangeTime(btn,evd)
d = get(btn,'userdata');
v = get(btn,'value');
% get data
if ishandle(d.in.handles.hfig)
D = get(d.in.handles.hfig,'userdata');
if ~isfield(d.in,'trN')
trN = 1;
else
trN = d.in.trN;
end
try
Z = D(d.in.ind,v,trN);
Z = Z(d.goodChannels);
if strcmp(d.in.type, 'MEGPLANAR')
Z = combineplanar(Z, d.origpos, d.origChanLabel);
end
clear ud;
% interpolate data
ZI = griddata(d.interp.pos(1,:),d.interp.pos(2,:),full(double(Z)),d.interp.XI,d.interp.YI);
% update data display
set(d.hi,'Cdata',flipud(ZI));
% update time index display
v = round(v);
set(d.hti,'string',[num2str(d.in.gridTime(v)), ' (', d.in.unit, ')']);
% update display marker position
try;set(d.in.hl,'xdata',[v;v]);end
set(d.ParentAxes,'nextPlot','add')
try
% delete current contour plot
delete(findobj(d.ParentAxes,'type','hggroup'));
delete(findobj(d.ParentAxes,'type','contour')); % R2014b
% create new one
[C,hc] = contour(d.ParentAxes,flipud(ZI),...
'linecolor',[0.5.*ones(3,1)]);
end
axis(d.ParentAxes,'image')
drawnow
catch
% else
error('Did not find the data!')
end
else
error('SPM Graphics Figure has been deleted!')
end
%==========================================================================
% get2Dfrom3D
%==========================================================================
function [xy] = get2Dfrom3D(xyz)
% function [xy] = get2Dfrom3D(xyz)
% This function is used to flatten 3D sensor positions onto the 2D plane
% using a modified spherical projection operation.
% It is used to visualize channel data.
% IN:
% - xyz: the cartesian sensor position in 3D space
% OUT:
% - xy: the (x,y) cartesian coordinates of the sensors after projection
% onto the best-fitting sphere
if size(xyz,2) ~= 3
xyz = xyz';
end
% exclude channels ?
badChannels = find(isnan(xyz(:,1)));
goodChannels = find(isnan(xyz(:,1))~=1);
xyz = xyz(goodChannels,:);
% Fit sphere to 3d sensors and center frame
C = fitSphere(xyz(:,1),xyz(:,2),xyz(:,3));
xyz = xyz - repmat(C,size(xyz,1),1);
% apply transformation using spherical coordinates
[TH,PHI,RAD] = cart2sph(xyz(:,1),xyz(:,2),xyz(:,3));
TH = TH - mean(TH);
[X,Y,Z] = sph2cart(TH,zeros(size(TH)),RAD.*(cos(PHI+pi./2)+1));
xy = [X(:),Y(:)];
%==========================================================================
% combineplanar
%==========================================================================
function [Z, pos, ChanLabel] = combineplanar(Z, pos, ChanLabel)
if ~iscell(ChanLabel)
ChanLabel = cellstr(ChanLabel);
end
chanind = zeros(1, numel(ChanLabel));
for i = 1:numel(ChanLabel)
chanind(i) = sscanf(ChanLabel{i}, 'MEG%d');
end
pairs = [];
unpaired = [];
paired = zeros(length(chanind));
for i = 1:length(chanind)
if ~paired(i)
cpair = find(abs(chanind - chanind(i))<2);
if length(cpair) == 1
unpaired = [unpaired cpair];
else
pairs = [pairs; cpair(:)'];
end
paired(cpair) = 1;
end
end
if ~isempty(unpaired)
warning(['Could not pair all channels. Ignoring ' num2str(length(unpaired)) ' unpaired channels.']);
end
Z = sqrt(Z(pairs(:, 1)).^2 + Z(pairs(:, 2)).^2);
pos = (pos(:, pairs(:, 1)) + pos(:, pairs(:, 2)))./2;
ChanLabel = {};
for i = 1:size(pairs,1)
ChanLabel{i} = ['MEG' num2str(min(pairs(i,:))) '+' num2str(max(pairs(i,:)))];
end
%==========================================================================
% fitSphere
%==========================================================================
function [C,R,out] = fitSphere(x,y,z)
% fitSphere Fit sphere.
% A = fitSphere(x,y,z) returns the parameters of the best-fit
% [C,R,out] = fitSphere(x,y,z) returns the center and radius
% sphere to data points in vectors (x,y,z) using Taubin's method.
% IN:
% - x/y/z: 3D carthesian ccordinates
% OUT:
% - C: the center of sphere coordinates
% - R: the radius of the sphere
% - out: an output structure devoted to graphical display of the best fit
% sphere
% Make sugary one and zero vectors
l = ones(length(x),1);
O = zeros(length(x),1);
% Make design mx
D = [(x.*x + y.*y + z.*z) x y z l];
Dx = [2*x l O O O];
Dy = [2*y O l O O];
Dz = [2*z O O l O];
% Create scatter matrices
M = D'*D;
N = Dx'*Dx + Dy'*Dy + Dz'*Dz;
% Extract eigensystem
[v, evalues] = eig(M);
evalues = diag(evalues);
Mrank = sum(evalues > eps*5*norm(M));
if (Mrank == 5)
% Full rank -- min ev corresponds to solution
Minverse = v'*diag(1./evalues)*v;
[v,evalues] = eig(inv(M)*N);
[dmin,dminindex] = max(diag(evalues));
pvec = v(:,dminindex(1))';
else
% Rank deficient -- just extract nullspace of M
pvec = null(M)';
[m,n] = size(pvec);
if m > 1
pvec = pvec(1,:);
end
end
% Convert to (R,C)
if nargout == 1
if pvec(1) < 0
pvec = -pvec;
end
C = pvec;
else
C = -0.5*pvec(2:4) / pvec(1);
R = sqrt(sum(C*C') - pvec(5)/pvec(1));
end
[X,Y,Z] = sphere;
[TH,PHI,R0] = cart2sph(X,Y,Z);
[X,Y,Z] = sph2cart(TH,PHI,R);
X = X + C(1);
Y = Y + C(2);
Z = Z + C(3);
out.X = X;
out.Y = Y;
out.Z = Z;