0001 function [p] = eeg_contours_engine(p)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 if ~exist('p','var'),
0045 fprintf('Setting default parameters.\n');
0046 [p] = eeg_toolbox_defaults('create');
0047 end
0048
0049
0050
0051
0052
0053 if isempty(p.elec.data),[p] = elec_open(p); end
0054
0055
0056 X = p.elec.data.x;
0057 Y = p.elec.data.y;
0058 Z = p.elec.data.z;
0059
0060 Xrad = p.elec.data.R(1);
0061 Yrad = p.elec.data.R(2);
0062 Zrad = p.elec.data.R(3);
0063
0064 elecNumElectrodes = length(p.elec.data.x);
0065
0066
0067 switch p.elec.shape
0068 case 'ellipse'
0069 Xp = p.elec.data.Xel;
0070 Yp = p.elec.data.Yel;
0071 Zp = p.elec.data.Zel;
0072 case 'sphere'
0073 Xp = p.elec.data.Xsp;
0074 Yp = p.elec.data.Ysp;
0075 Zp = p.elec.data.Zsp;
0076 otherwise
0077 Xp = p.elec.data.x;
0078 Yp = p.elec.data.y;
0079 Zp = p.elec.data.z;
0080 end
0081
0082
0083
0084
0085
0086 if isempty(p.volt.data),[p] = eeg_open(p); end
0087
0088 [voltNumTimePoints, voltNumElectrodes] = size(p.volt.data);
0089
0090
0091
0092 if (voltNumElectrodes ~= elecNumElectrodes)
0093 fprintf('\nWarning: # electrodes, data file %d, electrode file %d\n', voltNumElectrodes, elecNumElectrodes);
0094 if (voltNumTimePoints == elecNumElectrodes)
0095
0096 fprintf('Continuing with data file rotated\n');
0097 p.volt.data = p.volt.data'; [voltNumTimePoints, voltNumElectrodes] = size(p.volt.data);
0098 else
0099 fprintf('\nError: Cannot reconcile electrodes with voltage data.\n');
0100 return
0101 end
0102 end
0103
0104
0105
0106
0107 if or(isequal(p.clickTimePoint, 1),isempty(p.volt.samplePoint))
0108 figure('NumberTitle','off','Name','MouseClick the TimePoint')
0109 plot(p.volt.data);
0110 [Xt,Yt] = ginput(1);
0111 p.volt.samplePoint = round(Xt);
0112 close
0113 end
0114 if (p.volt.samplePoint > voltNumTimePoints)
0115 msg = sprintf(' Selected timepoint exceeds data points of %d\n\n', voltNumTimePoints);
0116 warning(msg);
0117 p.volt.samplePoint = voltNumTimePoints;
0118 end
0119 if ~isempty(p.volt.timeArray),
0120 p.volt.sampleTime = p.volt.timeArray(p.volt.samplePoint);
0121 end
0122
0123 V = p.volt.data(p.volt.samplePoint,:)';
0124
0125
0126
0127
0128
0129 switch p.rangeMethod
0130 case 'minmaxall',
0131 p.maximumIntensity = max(max(p.volt.data));
0132 p.minimumIntensity = min(min(p.volt.data));
0133 case 'minmaxone',
0134 p.maximumIntensity = max(max(V));
0135 p.minimumIntensity = min(min(V));
0136 case 'minmaxabs',
0137 absmax = max(max(abs(V)));
0138 p.maximumIntensity = absmax;
0139 p.minimumIntensity = -absmax;
0140 otherwise
0141
0142 if isempty(p.maximumIntensity),
0143 p.maximumIntensity = max(max(V)); end
0144 if isempty(p.minimumIntensity),
0145 p.minimumIntensity = min(min(V)); end
0146 end
0147
0148 switch p.contour.stepMethod
0149 case 0
0150 p.contour.levels = eeg_contour_levels( p.contour.stepSize, V );
0151 p.contour.Nsteps = length(p.contour.levels);
0152 otherwise
0153 p.contour.stepSize = abs(p.maximumIntensity - p.minimumIntensity) / p.contour.Nsteps;
0154 p.contour.levels = eeg_contour_levels( p.contour.stepSize, V );
0155 end
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 m = max(abs([Xp;Yp]));
0172 m = ceil(m);
0173 if (p.grid.method == 2), xg = -m:p.grid.res:m;
0174 else, xg = linspace(-m,m,p.grid.size);
0175 end
0176 yg = xg';
0177 [Xi,Yi,Zi] = griddata(Xp,Yp,Zp,xg,yg,p.interpMethod);
0178 Vi = griddata(Xp,Yp,V ,xg,yg,p.interpMethod);
0179
0180
0181
0182
0183
0184
0185
0186
0187 if p.mesh.plotSurf,
0188
0189
0190 [p.mesh.current,meshExists] = mesh_check(p,'scalp');
0191
0192 if isempty(p.mesh.current),
0193 fprintf('...no scalp mesh, using electrode surface.\n');
0194 p.mesh.plotSurf = 0;
0195 p.elec.plotSurf = 1;
0196 else
0197
0198
0199 if ~isfield(p.mesh.data,'Cdata'),
0200 [p] = mesh_scalp_interp(p);
0201 end
0202
0203 if length(p.mesh.data.Cdata) < p.mesh.current,
0204 [p] = mesh_scalp_interp(p);
0205 end
0206 if isempty(p.mesh.data.Cdata{p.mesh.current}),
0207 [p] = mesh_scalp_interp(p);
0208 end
0209
0210
0211
0212
0213
0214 TMP = p.mesh.data.Cdata{p.mesh.current};
0215 Nelec = size(p.volt.data,2);
0216 if ~isequal(p.volt.data',TMP(1:Nelec,:)),
0217
0218
0219 msg = sprintf('scalp time series not consistent with electrode time series - recalculating.\n');
0220 warning(msg);
0221 p.mesh.data.Cdata{p.mesh.current} = [];
0222 [p] = mesh_scalp_interp(p);
0223 TMP = p.mesh.data.Cdata{p.mesh.current};
0224 if ~isequal(p.volt.data',TMP(1:Nelec,:)),
0225 msg = sprintf('scalp timeseries not consistent with electrode timeseries.\n');
0226 error(msg);
0227 end
0228 end
0229 clear TMP;
0230 end
0231 end
0232
0233
0234
0235
0236
0237 if or(p.elec.plotSurf,p.mesh.plotSurf),
0238
0239 if ~isequal(p.volt.sampleTime,0),
0240 name = sprintf('Surface: %s @ %8.2f msec',p.volt.file, p.volt.sampleTime);
0241 else
0242 name = sprintf('Surface: %s',p.volt.file);
0243 end
0244
0245 if p.elec.plotSurf,
0246
0247 [p.mesh.current,meshExists] = mesh_check(p,'elec');
0248
0249 if ~meshExists,
0250 p.mesh.data.meshtype{p.mesh.current} = 'elec';
0251 p.mesh.data.vertices{p.mesh.current} = [Xp,Yp,Zp];
0252 p.mesh.data.faces{p.mesh.current} = convhulln([Xp,Yp,Zp]);
0253 p.mesh.data.lapmat{p.mesh.current} = [];
0254 p.mesh.data.lapint{p.mesh.current} = [];
0255 p.mesh.data.timeseries{p.mesh.current} = p.volt.timeArray(:,1);
0256 p.mesh.data.Cdata{p.mesh.current} = p.volt.data';
0257 end
0258
0259
0260
0261
0262 H.gui = figure('NumberTitle','off','Name',name,...
0263 'PaperType','A4','PaperUnits','centimeters');
0264
0265 set(gca,'Projection','perspective')
0266 set(gca,'DataAspectRatio',[1 1 1]);
0267
0268 p.elec.patch = patch('vertices',[Xp,Yp,Zp],'faces',convhulln([Xp,Yp,Zp]),...
0269 'facevertexCdata',V,'facecolor','interp','EdgeColor','none',...
0270 'FaceLighting','phong');
0271
0272
0273
0274 set(gca,'Projection','perspective')
0275 set(gca,'DataAspectRatio',[1 1 1]);
0276 axis off tight vis3d
0277
0278 lighting phong
0279 set(H.gui,'Renderer','zbuffer')
0280
0281
0282 H.light = light('style','infinite','position',[0 0 1000]);
0283
0284
0285
0286
0287
0288
0289
0290 p.reflect{1} = 0.9;
0291 p.reflect{2} = 0.1;
0292 p.reflect{3} = 0.0;
0293 p.reflect{4} = 500;
0294 p.reflect{5} = 0;
0295 set(p.elec.patch,'AmbientStrength',p.reflect{1});
0296 set(p.elec.patch,'DiffuseStrength',p.reflect{2});
0297 set(p.elec.patch,'SpecularStrength',p.reflect{3});
0298 set(p.elec.patch,'SpecularExponent',p.reflect{4});
0299 set(p.elec.patch,'SpecularColorReflectance',p.reflect{5});
0300
0301 if p.elec.plot,
0302 hold on, plot3(Xp,Yp,Zp,'k.');
0303 end
0304
0305 set(gca,'Visible','off');
0306 colormap(p.colorMap.map);
0307 caxis([p.minimumIntensity p.maximumIntensity]);
0308 colorbar
0309
0310 H.p = p;
0311 H.p.mesh.plotSurf = 0;
0312 set(H.gui,'userdata',H);
0313
0314 SaveGraphics(H.gui,'_3Delec_',p);
0315 if isequal(exist('mouse_rotate'),2),
0316 mouse_rotate;
0317 else
0318 rotate3D;
0319 end
0320 if isequal(exist('gui_topo_animate'),2),
0321 gui_topo_animate('init',p);
0322 end
0323 end
0324
0325
0326
0327
0328 if p.mesh.plotSurf,
0329
0330 [p.mesh.current,meshExists] = mesh_check(p,'scalp');
0331
0332 [p] = eeg_plot_surf(p);
0333
0334 return
0335 end
0336 end
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347 if isequal(p.contour.raw2D,1),
0348
0349 if ~isequal(p.volt.sampleTime,0),
0350 name = sprintf('2-D Contour: %s @ %8.2f msec',p.volt.file, p.volt.sampleTime);
0351 else
0352 name = sprintf('2-D Contour: %s',p.volt.file);
0353 end
0354 fig = figure('NumberTitle','off','Name',name); colormap(p.colorMap.map);
0355 set(gca,'Projection','perspective')
0356
0357 set(gca,'DataAspectRatio',[1 1 1]);
0358
0359
0360
0361
0362
0363 [c,ch,cf] = contourf(Xi,Yi,Vi,p.contour.levels);
0364 absmax = max(max(abs(Vi)));caxis([-absmax absmax]);
0365
0366
0367
0368
0369
0370
0371
0372 for i=1:size(ch),
0373 height = get(ch(i),'UserData');
0374
0375 if (height > 0),
0376 set(ch(i),'LineStyle', '-');
0377 set(ch(i),'EdgeColor',[1 1 1])
0378 elseif (height < 0),
0379 set(ch(i),'LineStyle', ':');
0380 else
0381 set(ch(i),'LineStyle', '-');
0382 set(ch(i),'LineWidth',2);
0383 end
0384 end
0385
0386 set(gca,'Visible','off');
0387 colorbar
0388
0389 SaveGraphics(fig,'_2D_',p);
0390 if isequal(exist('mouse_rotate'),2),
0391 mouse_rotate;
0392 else
0393 rotate3D;
0394 end
0395 end
0396
0397
0398
0399
0400
0401 if isequal(p.contour.plot2D,1)
0402
0403
0404 [X2p,Y2p] = elec_3d_2d(Xp,Yp,Zp,Zrad);
0405 m = max(abs([X2p;Y2p]));
0406 m = ceil(m);
0407 if (p.grid.method == 2)
0408
0409 X2g = -m:p.grid.res:m;
0410 else
0411
0412 X2g = linspace( -m, m, p.grid.size );
0413 end
0414 Y2g = X2g';
0415 [X2i Y2i V2i] = griddata(X2p,Y2p,V,X2g,Y2g,p.interpMethod);
0416
0417 if ~isequal(p.volt.sampleTime,0),
0418 name = sprintf('Projected 2D Contours: %s @ %8.2f msec',p.volt.file, p.volt.sampleTime);
0419 else
0420 name = sprintf('Projected 2D Contours: %s',p.volt.file);
0421 end
0422 fig = figure('NumberTitle','off','Name',name);
0423 set(gca,'Projection','perspective')
0424
0425 set(gca,'DataAspectRatio',[1 1 1]);
0426
0427 colormap(p.colorMap.map);
0428 contourf(X2i,Y2i,V2i, p.contour.Nsteps);
0429 absmax = max(max(abs(V2i)));caxis([-absmax absmax]);colorbar
0430 xlabel('X'); ylabel('Y');
0431
0432 SaveGraphics(fig,'_proj2D_',p);
0433 end
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444 if (p.contour.plot3D == 1)
0445
0446
0447
0448 [X2p,Y2p] = elec_3d_2d(Xp,Yp,Zp,Zrad);
0449 m = max(abs([X2p;Y2p]));
0450 m = ceil(m);
0451 if isequal(p.grid.method,2)
0452
0453 X2g = -m:p.grid.res:m;
0454 else
0455
0456 X2g = linspace( -m, m, p.grid.size );
0457 end
0458 Y2g = X2g';
0459
0460 [X2i Y2i V3i] = griddata(Xp, Yp, V,X2g,Y2g,p.interpMethod);
0461
0462
0463 C = contourc(X2g, Y2g, V3i, p.contour.Nsteps);
0464
0465 if ~isequal(p.volt.sampleTime,0),
0466 name = sprintf('3D Contours: %s @ %8.2f msec',p.volt.file, p.volt.sampleTime);
0467 else
0468 name = sprintf('3D Contours: %s',p.volt.file);
0469 end
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506 fig = figure('NumberTitle','off','Name',name); hold on
0507 set(gca,'Projection','perspective')
0508
0509 set(gca,'DataAspectRatio',[1 1 1]);
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520 i = 1; j = 0;
0521 limit = size(C,2);
0522 while (i < limit),
0523
0524 j = j + 1; contourValues(j) = C(1,i);
0525
0526 numberPoints = C(2,i);
0527
0528 endContourSet = i + numberPoints;
0529
0530 Xc = [ C(1,i+1:endContourSet) ]';
0531 Yc = [ C(2,i+1:endContourSet) ]';
0532
0533
0534
0535
0536 [X3,Y3,Z3] = elec_2d_3d(Xc,Yc,Xrad,Yrad,Zrad);
0537
0538 if (contourValues(j) < 0)
0539
0540 if isequal(p.colorMap.style,'Gray')
0541 line(X3,Y3,Z3,'LineStyle',':','LineWidth',0.5,'Color','black');
0542 else
0543 line(X3,Y3,Z3,'LineStyle',':','LineWidth',0.5,'Color','blue');
0544 end
0545 elseif (contourValues(j) > 0)
0546
0547 if isequal(p.colorMap.style,'Gray')
0548 line(X3,Y3,Z3,'LineStyle','-','LineWidth',0.5,'Color','black');
0549 else
0550 line(X3,Y3,Z3,'LineStyle','-','LineWidth',0.5,'Color','red');
0551 end
0552 else
0553
0554 line(X3,Y3,Z3,'LineStyle','-','LineWidth',0.75,'Color','black');
0555 end
0556 i = endContourSet + 1;
0557 end
0558
0559 view(0,90), axis tight, rotate3d
0560 set(gca,'Visible','off');
0561
0562
0563
0564 SaveGraphics(fig,'_3D_',p);
0565 end
0566
0567 return
0568
0569
0570
0571
0572 function DrawHead(Hradius, Hcolor)
0573
0574 switch Hcolor
0575 case 'black'
0576 HCOLOR = [0 0 0];
0577 case 'white'
0578 HCOLOR = [1 1 1];
0579 otherwise
0580 HCOLOR = [0 0 0];
0581 end
0582 HLINEWIDTH = 30;
0583
0584 rmax = 0.95 * Hradius;
0585
0586
0587
0588 theta1 = linspace(0,2*pi,50);
0589 thetad = (theta1(2) - theta1(1))/2;
0590 theta = [theta1, theta1 + thetad];
0591
0592 plot(cos(theta).*rmax,sin(theta).*rmax,'color',HCOLOR,'LineWidth',HLINEWIDTH);
0593
0594
0595 width = rmax * 0.1;
0596 tip = rmax * 1.075;
0597 base = rmax * 1.005;
0598
0599 plot([width;0;-width],[rmax;tip;rmax],'Color',HCOLOR,'LineWidth',HLINEWIDTH);
0600 return
0601
0602
0603 function SaveGraphics(F,type,p)
0604 if ~isequal(p.saveGraphics,'No Save Plots'),
0605
0606 [path,file,ext] = fileparts(strcat(p.volt.path, filesep, p.volt.file));
0607 file = strcat(file, type, num2str(p.volt.samplePoint),'.',p.saveGraphics);
0608 file = fullfile(path,file);
0609 saveas(F,file);
0610 end
0611 return