-
Notifications
You must be signed in to change notification settings - Fork 20
/
eeg.m
682 lines (641 loc) · 33.2 KB
/
eeg.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
% Definition of the class eeg. This class defines analysis methods
% exclusively for EEG data.
%
% For more details visit: https://code.google.com/p/mobilab/
%
% Author: Alejandro Ojeda, SCCN, INC, UCSD, Apr-2011
classdef eeg < dataStream
properties
isReferenced % Boolean that reflects whether an EEG data set has been re-referenced of not.
reference % Cell array with the labels of the channels used to compute the reference.
channelSpace
fiducials
end
methods
function obj = eeg(header)
% Creates an eeg object.
%
% Input arguments:
% header: header file (string)
%
% Output arguments:
% obj: eeg object (handle)
%
% Usage:
% obj = eeg(header);
if nargin < 1, error('Not enough input arguments.');end
obj@dataStream(header);
end
%%
function channelSpace = get.channelSpace(obj)
stack = dbstack;
if any(strcmp({stack.name},'coreStreamObject.set.channelSpace')), label = obj.channelSpace;return;end
if isempty(obj.channelSpace), obj.channelSpace = retrieveProperty(obj,'channelSpace');end
channelSpace = obj.channelSpace;
end
function set.channelSpace(obj,channelSpace)
stack = dbstack;
if any(strcmp({stack.name},'eeg.get.channelSpace'))
obj.channelSpace = channelSpace;
return;
end
obj.channelSpace = channelSpace;
saveProperty(obj,'channelSpace',channelSpace);
end
%%
function isReferenced = get.isReferenced(obj)
stack = dbstack;
if any(strcmp({stack.name},'eeg.set.isReferenced'))
isReferenced = obj.isReferenced;
return;
end
if isempty(obj.isReferenced)
try obj.isReferenced = retrieveProperty(obj,'isReferenced');
catch
isReferenced = false;
save(obj.header,'-mat','-append','isReferenced')
obj.isReferenced = isReferenced;
end
end
isReferenced = obj.isReferenced;
end
function set.isReferenced(obj,isReferenced)
stack = dbstack;
if any(strcmp({stack.name},'eeg.get.isReferenced'))
obj.isReferenced = isReferenced;
return;
end
obj.isReferenced = isReferenced;
saveProperty(obj,'isReferenced',isReferenced)
end
%%
function reference = get.reference(obj)
stack = dbstack;
if any(strcmp({stack.name},'eeg.set.reference'))
reference = obj.reference;
return;
end
if isempty(obj.reference), obj.reference = retrieveProperty(obj,'reference');end
reference = obj.reference;
end
function set.reference(obj,reference)
stack = dbstack;
if any(strcmp({stack.name},'eeg.get.reference'))
obj.reference = reference;
return;
end
obj.reference = reference;
saveProperty(obj,'reference',reference);
end
%%
function jsonObj = serialize(obj)
metadata = saveobj(obj);
metadata.size = size(obj);
metadata.event = obj.event.uniqueLabel;
metadata.writable = double(metadata.writable);
metadata.history = obj.history;
metadata.sessionUUID = char(obj.sessionUUID);
metadata.uuid = char(obj.uuid);
if ~isempty(metadata.channelSpace), metadata.hasChannelSpace = 'yes'; else metadata.hasChannelSpace = 'no';end
metadata = rmfield(metadata,{'parentCommand' 'timeStamp' 'hardwareMetaData' 'channelSpace' 'fiducials'});
jsonObj = savejson('',metadata,'ForceRootName', false);
end
%%
function readMontage(obj,file)
% Reads a file containing the sensor positions and its labels.
% If recorder, it also extracts fiducial landmarks. If the number
% of channels in the file are less than the number of channels in
% the object, a new object is created with the common set. If more
% than one file is supplied the program creates separate objects
% containing the different set of channels selected by each file.
%
% Input argument:
% file: filename. The following formats are supported:
% BESA or EGI3D cartesian .sfp, Polhemus .elp,
% Matlab .xyz or .sph, EEGLAB .loc, and Neuroscan
% .asc or .dat.
if nargin < 2
[filename, pathname] = uigetfile2({'*.sfp','BESA or EGI 3-D cartesian files (*.sfp)';'*.elp','Polhemus native files (*.elp)';...
'*.xyz','Matlab xyz files (*.xyz)';'*.loc','EEGLAB polar files (*.loc)';'*.sph','Matlab spherical files (*.sph)';...
'*.asc','Neuroscan polar files (*.asc)';'*.dat','Neuroscan 3-D files (*.dat)';'*.*','All files (*.*)'},...
'Select a file containing sensor positions','MultiSelect', 'on');
if isnumeric(filename) || isnumeric(pathname), return;end
elseif ~ischar(file)
[filename, pathname] = uigetfile2({'*.sfp','BESA or EGI 3-D cartesian files (*.sfp)';'*.elp','Polhemus native files (*.elp)';...
'*.xyz','Matlab xyz files (*.xyz)';'*.loc','EEGLAB polar files (*.loc)';'*.sph','Matlab spherical files (*.sph)';...
'*.asc','Neuroscan polar files (*.asc)';'*.dat','Neuroscan 3-D files (*.dat)';'*.*','All files (*.*)'},...
'Select a file containing sensor positions','MultiSelect', 'on');
if isnumeric(filename) || isnumeric(pathname), return;end
else
[pathname,filename,ext] = fileparts(file);
filename = [filename,ext];
end
if iscellstr(filename)
N = length(filename);
newChannelSpace = cell(N,1);
newLabels = cell(N,1);
newEEGchannel = cell(N,1);
fiducials = cell(N,1);
tmpLabel = [];
indEEGchannels = false(obj.numberOfChannels,1);
for it=1:obj.numberOfChannels
ind = obj.label{it} == '_';
if any(ind)
ind = find(ind);
tmpLabel{end+1} = obj.label{it}(ind(end)+1:end); %#ok
indEEGchannels(it) = true;
end
end
indEEGchannels = find(indEEGchannels);
if isempty(indEEGchannels)
indEEGchannels = 1:obj.numberOfChannels;
tmpLabel = obj.label;
end
for it=1:N
file = fullfile(pathname,filename{it});
[eloc, labels] = readlocs( file);
eloc = [cell2mat({eloc.X}'), cell2mat({eloc.Y}'), cell2mat({eloc.Z}')];
Nl = length(labels);
elecIndices = false(Nl,1);
channelIndices = zeros(Nl,1);
for jt=1:Nl
if ~isempty(strfind(labels{jt},'fidnz')) || ~isempty(strfind(labels{jt},'nasion')) || ~isempty(strfind(labels{jt},'Nz'))
fiducials{it}.nasion = eloc(jt,:);
elseif ~isempty(strfind(labels{jt},'fidt9')) || ~isempty(strfind(labels{jt},'lpa'))
fiducials{it}.lpa = eloc(jt,:);
elseif ~isempty(strfind(labels{jt},'fidt10')) || ~isempty(strfind(labels{jt},'rpa'))
fiducials{it}.rpa = eloc(jt,:);
elseif ~isempty(strfind(labels{jt},'fidt10')) || ~isempty(strfind(labels{jt},'vertex'))
fiducials{it}.vertex = eloc(jt,:);
else
I = find(strcmpi(tmpLabel,labels{jt}));
if ~isempty(I)
elecIndices(jt) = true;
channelIndices(jt) = I(1);
end
end
end
channelIndices(channelIndices==0) = [];
newLabels{it} = labels(elecIndices);
newChannelSpace{it} = eloc(logical(elecIndices),:);
newEEGchannel{it} = indEEGchannels(channelIndices);
tmpLabel(channelIndices) = []; %#ok
indEEGchannels(channelIndices) = [];
end
% these two loops can be fused, however I've split them for debugging
for it=1:N
cobj = divideStreamObject(obj,newEEGchannel{it},newLabels{it},[obj.name '_' num2str(it)]);
cobj.channelSpace = newChannelSpace{it};
saveProperty(cobj,'channelSpace',cobj.channelSpace);
try
cobj.fiducials = fiducials{it};
saveProperty(cobj,'fiducials',cobj.fiducials);
catch ME
warning(ME.message);
warning('MoBILAB:noFiducials','Fiducial landmarks are missing.');
end
end
else
file = fullfile(pathname,filename);
[eloc, labels, theta, radius, indices] = readlocs( file); %#ok
eloc = [cell2mat({eloc.X}'), cell2mat({eloc.Y}'), cell2mat({eloc.Z}')];
rmThis = [];
I = strcmpi(labels,'fidnz') | strcmpi(labels,'nasion') | strcmpi(labels,'Nz');
if any(I), fiducials.nasion = eloc(I,:);rmThis(1) = find(I);end
I = strcmpi(labels,'fidt9') | strcmpi(labels,'lpa');
if any(I), fiducials.lpa = eloc(I,:);rmThis(2) = find(I);end
I = strcmpi(labels,'fidt10') | strcmpi(labels,'rpa');
if any(I), fiducials.rpa = eloc(I,:);rmThis(3) = find(I);end
I = strcmpi(labels,'vertex');
if any(I), fiducials.vertex = eloc(I,:);rmThis(4) = find(I);end
% fixing sccn old protocol bug
for it=1:length(labels), if ~isempty(strfind(labels{it},'EXG')), labels{it}(strfind(labels{it},'EXG'):3) = 'EXT';end;end
labels(rmThis) = [];
eloc(rmThis,:) = [];
if obj.numberOfChannels > length(labels)
ind = obj.container.getItemIndexFromItemNameSimilarTo(obj.name);
tmpObj = divideStreamObject(obj,1:length(labels) ,labels,[obj.name '_' num2str(length(ind))]);
else
tmpObj = obj;
end
tmpObj.label = labels;
tmpObj.channelSpace = eloc;
saveProperty(tmpObj,'channelSpace',tmpObj.channelSpace);
if exist('fiducials','var')
tmpObj.fiducials = fiducials;
saveProperty(tmpObj,'fiducials',tmpObj.fiducials);
else
warning('MoBILAB:noFiducials','Fiducial landmarks are missing.');
end
end
disp('Done!!!')
end
%%
function cobj = filter(obj,varargin)
if length(varargin) == 1 && iscell(varargin{1}), varargin = varargin{1};end
cobj = filter@dataStream(obj,varargin);
if isempty(cobj), return;end
cobj.mmfObj.Writable = true;
obj.initStatusbar(0,1,'Centering channels...');
cobj.mmfObj.Data.x = bsxfun(@minus,cobj.mmfObj.Data.x,mean(cobj.mmfObj.Data.x));
obj.statusbar(1);
cobj.mmfObj.Writable = false;
end
%%
function cobj = reReference(obj,channels2BeReferenced,channels2BeAveraged)
% Re-reference the data.
%
% Input arguments:
% channels2BeReferenced: cell array with the label of the
% channels to be referenced
% channels2BeAveraged: cell array with the label of the
% channels to be averaged
%
% Output arguments:
% cobj: handle to the new object
%
% Usage:
% eegObj = mobilab.allStreams.item{ eegItem };
% channels2BeReferenced = eegObj.label;
% channels2BeAveraged = eegObj.label;
% eegObjRef = eegObj.reReference(channels2BeReferenced,channels2BeAveraged);
if nargin < 2, error('Not enough input arguments.');end
dispCommand = false;
if isnumeric(channels2BeReferenced) && channels2BeReferenced==-1
cobj = [];
channels2BeReferenced = obj.label;
channels2BeAveraged = obj.label;
dispCommand = true;
end
if ~iscellstr(channels2BeReferenced), error('First argument must be a cell with the labels for the channels to be referenced.');end
if ~iscell(channels2BeAveraged), error('Second argument must be a cell with the labels for the channels to be averaged (The ones to be taken as a reference).');end
try
ind1 = ismember(obj.label,channels2BeReferenced);
ind2 = ismember(obj.label,channels2BeAveraged);
if sum(ind1)*sum(ind2)==0, error('Some of the labels you''ve entered don''t match the labels in the object.');end
ind1 = sort(find(ind1));
ind2 = sort(find(ind2));
commandHistory.commandName = 'reReference';
commandHistory.uuid = obj.uuid;
commandHistory.varargin{1} = obj.label(ind1);
commandHistory.varargin{2} = obj.label(ind2);
cobj = obj.copyobj(commandHistory);
if dispCommand
disp('Running:');
disp([' ' cobj.history]);
end
data = obj.mmfObj.Data.x;
ref = mean(data(:,ind2),2);
obj.initStatusbar(1,cobj.numberOfChannels,'Re-referencing...');
for it=1:cobj.numberOfChannels
cobj.mmfObj.Data.x(:,it) = data(:,ind1(it))-ref;
obj.statusbar(it);
end
cobj.isReferenced = true;
cobj.reference = channels2BeAveraged;
catch ME
if exist('cobj','var'), obj.container.deleteItem(obj.container.findItem(cobj.uuid));end
ME.rethrow;
end
end
%%
function EEG = EEGstructure(obj,ismmf, passData)
% Returns the equivalent EEG (EEGLAB) structure.
%
% Usage:
% eegObj = mobilab.allStreams.item{ eegItem };
% EEG = eegObj.EEGstructure;
if nargin < 2, ismmf = false;end
if nargin < 3, passData = true;end
s = dbstack;
isCalledFromGui = any(~cellfun(@isempty,strfind({s.name},'myDispatch')));
if isCalledFromGui
disp('Running:');
disp([' EEG = mobilab.allStreams.item{ ' num2str(obj.container.findItem(obj.uuid)) ' }.EEGstructure;']);
end
EEG = eeg_emptyset;
EEG.setname = obj.name;
EEG.times = obj.timeStamp*1e3; % from seconds to mili-seconds
EEG.nbchan = obj.numberOfChannels;
EEG.pnts = length(obj.timeStamp);
EEG.trials = 1;
EEG.srate = obj.samplingRate;
EEG.xmin = obj.timeStamp(1);
EEG.xmax = obj.timeStamp(end);
[path,name,ext] = fileparts(obj.binFile); %#ok
if passData
if ismmf
EEG.data = [path filesep name '.fdt'];
EEG.filepath = path;
EEG.filename = name;
copyMMF(EEG,obj);
else EEG.data = single(obj.mmfObj.Data.x)';
end
end
EEG.chaninfo.nosedir = '+X';
if isfield(obj.hardwareMetaData,'desc'), EEG.etc.desc = obj.hardwareMetaData.desc;end
chanlocs = repmat(struct('labels',[],'type',[],'X',[],'Y',[],'Z',[],'radius',[],'theta',[]),EEG.nbchan,1);
labels = obj.label;
if ~isempty(obj.channelSpace)
xyz = obj.channelSpace;
xyz = bsxfun(@minus,xyz,mean(xyz));
xyz = bsxfun(@rdivide,xyz,max(xyz))/2;
for it=1:obj.numberOfChannels
chanlocs(it).labels = labels{it};
try chanlocs(it).type = obj.hardwareMetaData.desc.channels.channel{it}.type;
catch chanlocs(it).type = 'EEG';
end
chanlocs(it).X = xyz(it,1);
chanlocs(it).Y = -xyz(it,2);
chanlocs(it).Z = xyz(it,3);
[chanlocs(it).theta,chanlocs(it).radius] = cart2pol(chanlocs(it).X, chanlocs(it).Y, chanlocs(it).Z);
chanlocs(it).theta = chanlocs(it).theta*180/pi;
end
EEG.chanlocs = chanlocs;
end
EEG.etc.mobi.sessionUUID = obj.sessionUUID;
try
ALLEEG = evalin('base','ALLEEG');
catch
ALLEEG = [];
end
if isempty(obj.event.label)
if ismmf, pop_saveset( EEG, [name '.set'],path);end
if isCalledFromGui
[ALLEEG,EEG,CURRENTSET] = eeg_store(ALLEEG, EEG);
assignin('base','ALLEEG',ALLEEG);
assignin('base','CURRENTSET',CURRENTSET);
assignin('base','EEG',EEG);
try
evalin('base','eeglab redraw');
catch ME
disp(ME.message)
end
end
return;
end
type = obj.event.label;
latency = obj.event.latencyInFrame;
loc = ismember(type,'boundary');
if sum(loc)
loc = find(loc);
[~,loc2] = unique(latency(loc));
[~,rmloc] = setdiff(loc,loc(loc2));
latency(loc(rmloc)) = [];
type(loc(rmloc)) = [];
end
if ~isempty(latency) && ~isempty(type)
[latency,loc] = sort(latency,'ascend');
type = type(loc);
EEG.event = repmat(struct('type','','latency',0,'duration',0,'urevent',1),1,length(latency));
for it=1:length(latency)
EEG.event(it).type = type{it};
EEG.event(it).latency = latency(it);
EEG.event(it).urevent = it;
if strcmp(type,'boundary'), EEG.event(it).duration = NaN;end
end
EEG.urevent = EEG.event;
end
if ismmf && passData, pop_saveset( EEG, [name '.set'],path);end
if ~isCalledFromGui
[ALLEEG,EEG,CURRENTSET] = eeg_store(ALLEEG, EEG);
assignin('base','ALLEEG',ALLEEG);
assignin('base','CURRENTSET',CURRENTSET);
assignin('base','EEG',EEG);
evalin('base','eeglab redraw');
end
end
%%
function epochObj = epoching(obj,eventLabelOrLatency, timeLimits, channels, condition,subjectID,preStimulusLatency)
% Creates epoch objects. Epoch objects don't result in a new addition to the tree.
% Even when the epoch object manages its data through a memory mapped file they are
% regarded as temporal variables, once the object is destroyed the associated binary
% file is deleted.
%
% Input arguments:
% eventLabelOrLatency: if is a string or a cell array of strings it is used as
% the event around to make the trials; if is a vector is
% interpreted as the sample number of the events around
% to make the trials.
% timeLimits: two elements vector specifying the size of the trial
% taking as reference the target latency. Ex: [-1 2]
% correspond to one second before and two seconds after
% the target event/latency.
% channels: indices of the channels to epoch
% condition: optional string specifying the name of the condition,
% default: unknown
% subjectID: unique identidier of the epoched data, obj.uuid or a
% combination of the former and obj.sessionUUID is recommended,
% default: obj.sessionUUID
% preStimulusLatency: two elements vector specifying the segment in the epoch
% considered pre-stimulus (or base line), default: [timeLimits(1) 0]
%
%
% Output argument:
% epochObject: handle to the epoched object
%
% Usage:
% eegObj = mobilab.allStreams.item{ eegItem };
% eventType = {'eventLabel_x'}; % for more than one type use: {'eventLabel_x', 'eventLabel_y', ...}
% timeLimits = [-2 2]; % two seconds before and after the event (it does not have to be a symmetric window)
% channels = 85; % channel to epoch (could be more than one). Observe that in case of ICA data,
% % channels correspond to independent components activation.
% condition = 'target1';
% preStimulusLatency = [-1.7 -1.2]; % base-line: between 1.7 and 1.2 seconds before the stimulus/response
%
% epObj = eegObj.epoching( eventType, timeLimits, channels, condition, eegObj.sessionUUID, preStimulusLatency);
% plot(epObj);
% [~,ersp,itc,frequency,time] = epObj.waveletTimeFrequencyAnalysis;
%
% See eegEpoch for more details
if nargin < 2, error('Not enough input arguments.');end
if nargin < 3, warning('MoBILAB:noTImeLimits','Undefined time limits, assuming [-1 1] seconds.'); timeLimits = [-1 1];end
if nargin < 4, warning('MoBILAB:noChannels','Undefined channels to epoch, epoching all.'); channels = 1:obj.numberOfChannels;end
if nargin < 5, condition = 'unknown';end
if nargin < 6, subjectID = obj.uuid;end
if nargin < 7, warning('MoBILAB:noChannels','Undefined preStimulusLatency, assuming half of the epoch.'); preStimulusLatency = [];end
[data,time,eventInterval] = epoching@coreStreamObject(obj,eventLabelOrLatency, timeLimits, channels);
if isempty(preStimulusLatency)
preStimulusLatency = [timeLimits(1) 0];
elseif any(preStimulusLatency < timeLimits(1)) || any(preStimulusLatency > timeLimits(end))
preStimulusLatency = [timeLimits(1) 0];
elseif length(preStimulusLatency) ~= 2
error('preStimulusLatency must be a two elements vector.');
end
[~,loc1] = min(abs(time - preStimulusLatency(1)));
[~,loc2] = min(abs(time - preStimulusLatency(2)));
preStimulusLatency = [loc1 loc2];
epochObj = eegEpoch(data,time,obj.label(channels),condition,eventInterval,subjectID,preStimulusLatency);
end
%%
function epochObj = epochingTW(obj,latency, channels, condition,subjectID)
% Creates epoch objects. Epoch objects don't result in new additions to the tree.
% Even when an epoch object manages its data through a memory mapped file they are
% regarded as temporal variables, once the object is destroyed its binary file is
% deleted. TW stands for time warping. This method makes trials between two set of
% events/latencies, because each trial can have a slightly different size the resulting
% trials are interpolated to a common time axis.
%
% Input argument:
% latency: two columns matrix with the set of start end latencies (in seconds)
% for each trial.
% channels: indices of the channels to epoch
% condition: optional string specifying the name of the condition, default: unknown
% subjectID: unique identidier of the epoched data, obj.uuid or a combination of the
% former and obj.sessionUUID is recommended, default: obj.sessionUUID
%
% Output arguments:
% epochObject: handle to the epoched object
%
% Usage:
% eegObj = mobilab.allStreams.item{ eegItem };
% latency_walk = eegObj.event.getLatencyForEventLabel('701'); % Go event marker
% latency_stop = eegObj.event.getLatencyForEventLabel('702'); % Stop event marker
% latency = [latency_walk(:) latency_stop(:)];
% channels = 85; % channel to epoch (could be more than one). Observe that in case of ICA data,
% % channels correspond to independent components activation.
% condition = 'walking';
%
% epObj = eegObj.epochingTW(latency, channels, condition, eegObj.sessionUUID);
% plot(epObj);
% [~,ersp,itc,frequency,time] = epObj.waveletTimeFrequencyAnalysis;
%
% See eegEpoch for more details
if nargin < 2, error('Not enough input arguments.');end
if nargin < 3, warning('MoBILAB:noChannels','Undefined channels to epoch, epoching all.'); channels = 1:obj.numberOfChannels;end
if nargin < 4, condition = 'unknown';end
if nargin < 5, subjectID = obj.uuid;end
[data,time,eventInterval] = epochingTW@coreStreamObject(obj,latency, channels);
epochObj = eegEpoch(data,time,obj.label(channels),condition,eventInterval,subjectID);
end
end
methods(Hidden = true)
function loadElectrodeWizard(obj,file)
warning('This method will be deprecated, instead use ''readMontage'' with the same input arguments.');
readMontage(obj,file);
end
%%
function newHeader = createHeader(obj,commandHistory)
if nargin < 2
commandHistory.commandName = 'copyobj';
commandHistory.uuid = obj.uuid;
end
newHeader = createHeader@dataStream(obj,commandHistory);
if ~isempty(newHeader), return;end
metadata = obj.saveobj;
metadata.writable = true;
metadata.parentCommand = commandHistory;
uuid = generateUUID;
metadata.uuid = uuid;
path = fileparts(obj.binFile);
switch commandHistory.commandName
case 'reReference'
prename = 'ref_';
metadata.name = [prename metadata.name];
metadata.binFile = fullfile(path,[metadata.name '_' metadata.uuid '_' metadata.sessionUUID '.bin']);
channels2BeReferenced = commandHistory.varargin{1};
ind = ismember(obj.label,channels2BeReferenced);
metadata.label = obj.label(ind);
metadata.numberOfChannels = length(metadata.label);
if ~isempty(obj.channelSpace)
metadata.channelSpace = metadata.channelSpace(ind,:);
end
allocateFile(metadata.binFile,obj.precision,[length(metadata.timeStamp) metadata.numberOfChannels]);
otherwise
error('Cannot make a copy of this object. Please provide a valid ''command history'' instruction.');
end
newHeader = metadata2headerFile(metadata);
end
%%
function disp(obj)
string = sprintf(' channelSpace: <%ix3 double>\n',size(obj.channelSpace,1));
disp@coreStreamObject(obj)
fprintf(string);
end
%%
function properyArray = getPropertyGridField(obj)
dim = size(obj.channelSpace,1);
properyArray = getPropertyGridField@coreStreamObject(obj);
properyArray{1}{end+1} = 'channelSpace';
properyArray{2}{end+1} = ['<' num2str(dim(1)) 'x3 ' obj.precision '>'];
end
%%
function jmenu = contextMenu(obj)
jmenu = javax.swing.JPopupMenu;
%--
menuItem = javax.swing.JMenuItem('Add sensor locations');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj,'readMontage',-1});
jmenu.add(menuItem);
%--
jmenu.addSeparator;
%---------
menuItem = javax.swing.JMenuItem('Re-reference');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj,'reReference',-1});
jmenu.add(menuItem);
%--
menuItem = javax.swing.JMenuItem('Filter');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj,'filter',-1});
jmenu.add(menuItem);
%---------
jmenu.addSeparator;
%---------
menuItem = javax.swing.JMenuItem('Plot');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj,'dataStreamBrowser',-1});
jmenu.add(menuItem);
%--
menuItem = javax.swing.JMenuItem('Plot spectrum');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj,'spectrum',-1});
jmenu.add(menuItem);
%---------
jmenu.addSeparator;
%---------
menuItem = javax.swing.JMenuItem('Inspect');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj,'inspect',-1});
jmenu.add(menuItem);
%--
menuItem = javax.swing.JMenuItem('Export to EEGLAB');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj,'EEGstructure',0});
jmenu.add(menuItem);
%--
menuItem = javax.swing.JMenuItem('Annotation');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@annotation_Callback,obj});
jmenu.add(menuItem);
%--
menuItem = javax.swing.JMenuItem('Generate batch script');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@generateBatch_Callback,obj});
jmenu.add(menuItem);
%--
menuItem = javax.swing.JMenuItem('<HTML><FONT color="maroon">Delete object</HTML>');
set(handle(menuItem,'CallbackProperties'), 'ActionPerformedCallback', {@myDispatch,obj.container,'deleteItem',obj.container.findItem(obj.uuid)});
jmenu.add(menuItem);
end
end
methods(Static), function triggerSaveHeader(~,evnt), saveHeader(evnt.AffectedObject, 'f'); end;end
end
%--
function copyMMF(EEG,streamObj)
% bytes = dir(streamObj.binFile);
% bytes = bytes.bytes/1e9;
% if bytes < 0.5
fid = fopen(EEG.data,'w');
if fid < 0, return;end
if ~iscell(streamObj), streamObj = {streamObj};end
N = length(streamObj{1}.timeStamp);
N2 = length(streamObj);
% precision = streamObj{1}.precision;
precision = 'single';
for it=1:N2, if ~strcmp(streamObj{it}.isMemoryMappingActive,'active'), fclose(fid);return;end;end
bufferSize = 1000;
streamObj{1}.container.container.initStatusbar(1,N,'Creating EEG.data...');
for it=1:bufferSize:N
try writeThis = [];
for jt=1:N2, writeThis = [writeThis;streamObj{jt}.data(it:it+bufferSize-1,:)'];end%#ok
catch writeThis = [];%#ok
for jt=1:N2, writeThis = [writeThis;streamObj{jt}.data(it:end,:)'];end%#ok
end
fwrite(fid,writeThis(:),precision);
streamObj{1}.container.container.statusbar(it);
end
streamObj{1}.container.container.statusbar(inf);
fclose(fid);
end