Skip to content

Commit

Permalink
modifications to make the demo ERP analysis work
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminBlankertz committed Aug 17, 2012
1 parent c85772c commit 6bfcebf
Show file tree
Hide file tree
Showing 41 changed files with 878 additions and 524 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
27 changes: 13 additions & 14 deletions demos/demo_analysis_ERPs.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
file= 'VPibv_10_11_02/calibration_CenterSpellerMVEP_VPibv';

%% Load data
hdr= eegfile_readBVheader([file '*']);
hdr= file_readBVheader([file '*']);
Wps= [42 49]/hdr.fs*2;
[n, Ws]= cheb2ord(Wps(1), Wps(2), 3, 40);
[filt.b, filt.a]= cheby2(n, 50, Ws);
[cnt, mrk_orig]= eegfile_readBV([file '*'], 'fs',100, ...
'filt',filt);
[cnt, mrk_orig]= file_readBV([file '*'], 'Fs',100, 'Filt',filt);

%% Marker struct
stimDef= {[31:46], [11:26];
Expand All @@ -29,7 +28,7 @@
'T7,C5,C3,C1,Cz,C2,C4,C6,T8\n' ...
'P7,P5,P3,P1,Pz,P2,P4,P6,P8\n' ...
'PO9,PO7,PO3,O1,Oz,O2,PO4,PO8,PO10']);
mnt= getElectrodePositions(cnt.clab);
mnt= get_electrodePositions(cnt.clab);
mnt= mnt_setGrid(mnt, grd);

% Define some settings
Expand All @@ -52,8 +51,8 @@
epo= proc_segmentation(cnt, mrk, disp_ival);

% Artifact rejection based on maxmin difference criterion on frontal chans
epo= proc_rejectArtifactsMaxMin(epo, crit_maxmin, ...
'clab',crit_clab, 'ival',crit_ival, 'verbose',1);
epo= proc_rejectArtifactsMaxMin(epo, crit_maxmin, 'Clab',crit_clab, ...
'Ival',crit_ival, 'Verbose',1);

% Baseline subtraction, and calculation of a measure of discriminability
epo= proc_baseline(epo, ref_ival);
Expand All @@ -66,24 +65,24 @@
{1, [200 350], {'P3-4','CP3-4','C3-4'}, [200 400]}, ...
{1, [400 500], {'P3-4','CP3-4','C3-4'}, [350 600]}};
[ival_scalps, nfo]= ...
select_time_intervals(epo_r, 'visualize', 1, 'visu_scalps', 1, ...
'title', untex(file), ...
'clab',{'not','E*'}, ...
'constraint', constraint);
select_time_intervals(epo_r, 'Visualize', 1, 'VisuScalps', 1, ...
'Title', untex(file), ...
'Clab',{'not','E*'}, ...
'Constraint', constraint);
printFigure('r_matrix', [18 13]);
ival_scalps= visutil_correctIvalsForDisplay(ival_scalps, 'fs',epo.fs);

fig_set(3)
H= grid_plot(epo, mnt, defopt_erps, 'colorOrder',colOrder);
H= grid_plot(epo, mnt, defopt_erps, 'ColorOrder',colOrder);
grid_addBars(epo_r, 'h_scale',H.scale);
%printFigure(['erp'], [19 12]);

fig_set(2);
H= scalpEvolutionPlusChannel(epo, mnt, clab, ival_scalps, defopt_scalp_erp2, ...
'colorOrder',colOrder);
H= plot_scalpEvolutionPlusChannel(epo, mnt, clab, ival_scalps, defopt_scalp_erp, ...
'ColorOrder',colOrder);
grid_addBars(epo_r);
%printFigure(['erp_topo'], [20 4+5*size(epo.y,1)]);

fig_set(4, 'shrink',[1 2/3]);
scalpEvolutionPlusChannel(epo_r, mnt, clab, ival_scalps, defopt_scalp_r2);
plot_scalpEvolutionPlusChannel(epo_r, mnt, clab, ival_scalps, defopt_scalp_r);
%printFigure(['erp_topo_r'], [20 9]);
13 changes: 7 additions & 6 deletions fileio/file_readBV.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
% - There was an bug in the check for the lag


global EEG_RAW_DIR
global BBCI_RAW_DIR

props= {'CLab' '' 'CHAR|CELL{CHAR}'
'Fs' 'raw' 'CHAR|DOUBLE'
Expand All @@ -92,9 +92,10 @@
'Verbose' 1 'BOOL'};
props_readBVmarkers= file_readBVmarkers;
props_readBVheader= file_readBVheader;
props= opt_catProps(props, props_readBVmarkers, props_readBVheader);

if nargin==0,
H= opt_catProps(props, props_readBVmarkers, props_readBVheader);
varargout= {props};
return
end

Expand Down Expand Up @@ -122,12 +123,12 @@
end

fileNames = cell(1,length(file));
% use EEG_RAW_DIR as default dir
% use BBCI_RAW_DIR as default dir
for filePos = 1:length(file)
if isabsolutepath(file{filePos}(1)),
fileNames{filePos}= file{filePos};
else
fileNames{filePos} = fullfile(EEG_RAW_DIR, file{filePos});
fileNames{filePos} = fullfile(BBCI_RAW_DIR, file{filePos});
end
end

Expand Down Expand Up @@ -224,7 +225,7 @@
% sampling rate
if ~isempty(opt.IvalSa),
if ~isdefault.Start || ~isdefault.MaxLen,
error('specify either <ivalSa> or <Start/MaxLen> but not both');
error('specify either <IvalSa> or <Start/MaxLen> but not both');
end
skip= opt.IvalSa(1)-1;
maxlen = diff(opt.IvalSa)+1;
Expand Down Expand Up @@ -359,7 +360,7 @@
opt_mrk= opt_substruct(opt, props_readBVmarkers(:,1));
curmrk= file_readBVmarkers(fileNames{filePos}, opt_mrk);
% find markers in the loaded interval
inival= find(curmrk.time >= (skip+1)/cnt.fs*1000 & ...
inival= find(curmrk.time > skip/cnt.fs*1000 & ...
curmrk.time <= (skip+maxlen)/cnt.fs*1000);
curmrk= mrk_selectEvents(curmrk, inival);
%let the markers start at zero
Expand Down
21 changes: 11 additions & 10 deletions fileio/file_readBVmarkers.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

global EEG_RAW_DIR

props= {'MarkerFormat' 'string' 'CHAR'};
props= {'MarkerFormat' 'numeric' 'CHAR(string numeric)'};

if nargin==0,
Mrk= props; return
Expand Down Expand Up @@ -58,7 +58,7 @@
end
opt_read= {'delimiter',',', 'headerlines',skip-1};

[mrkno,Mrk.type,Mrk.desc,pos,Mrk.length,Mrk.chan,Mrk.clock]= ...
[mrkno, M_type, M_desc, pos, M_length, M_chan, M_clock]= ...
textread([fullName '.vmrk'], 'Mk%u=%s%s%u%u%u%s', opt_read{:});

keyword= 'SamplingInterval';
Expand All @@ -69,15 +69,16 @@
Mrk.time= pos'/fs*1000;
% Round time to micro seconds
% Mrk.time= round(Mrk.time*1000)/1000;
Mrk.event.desc= Mrk.desc';
Mrk.event.type= Mrk.type';
Mrk.event.length= Mrk.length';
Mrk.event.chan= Mrk.chan';
Mrk.event.clock= Mrk.clock';

Mrk.desc= M_desc';
Mrk.event.type= M_type';
Mrk.event.length= M_length';
Mrk.event.chan= M_chan';
Mrk.event.clock= M_clock';
Mrk.fs= fs;

if strcmp(opt.MarkerFormat, 'numeric'),
[toe,idx]= marker_mapping_SposRneg(Mrk.event.desc);
Mrk.event.desc= zeros(size(Mrk.event.desc));
Mrk.event.desc(idx)= toe;
[toe,idx]= marker_mapping_SposRneg(Mrk.desc);
Mrk.desc= zeros(size(Mrk.desc));
Mrk.desc(idx)= toe;
end
24 changes: 24 additions & 0 deletions markers/marker_mapping_SposRneg.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function [toe, idx]= marker_mapping_SposRneg(desc)
%MARKER_MAPPING_SPOSRNEG - Map BV Markers to numeric values
%
%Synopsis:
% [TOE, IDX]= marker_mapping_SposRneg(DESC)
%
%Arguments:
% DESC - Cell of strings: BV markers, like {'S 23', 'R134'}
%
%Output:
% TOE - Numeric representation of the type of event. S-markers are mapped
% to positive and R-markers to negative values
% IDX - Indices of mapped markers (non-S/R markers are discarded)

% 03-2011 Benjamin Blankertz


iS= strmatch('S', desc);
iR= strmatch('R', desc);
toe= zeros(size(desc));
toe(iS)= cellfun(@(x)(str2double(x(2:end))), desc(iS));
toe(iR)= -cellfun(@(x)(str2double(x(2:end))), desc(iR));
idx= find(toe);
toe= toe(idx);
39 changes: 20 additions & 19 deletions markers/mrk_defineClasses.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
% MRK_OUT= mrk_defineClasses(MRK_IN, CLASS_DEF, <OPT>)
%
%Arguments:
% MRK_IN: Marker structure as received by eegfile_loadBV
% MRK_IN: Marker structure as received by file_readBVmarkers.
% MarkerFormat can be numeric or string.
% CLASS_DEF: Class array of size {2 x nClasses}. The first row
% specifies the markers of each class, each cell being either
% a cell array of strings or a vector of integers. The second
% row specifies the class names, each cell begin a string.
% a cell array of strings (for MarkerFormat='string') or a vector
% of integers (for MarkerFormat='numeric').
% The second row specifies the class names, each cell begin a string.
% (If the second row does not exist, generic class names are defined.)
% OPT: struct or property/value list of optional properties:
% 'RemoveVoidClasses': Void classes are removed from the list of classes,
% default 0.
Expand All @@ -21,10 +24,12 @@
% (fields 'y' and 'className')
%
%Example:
% [cnt,mk]= eegfile_loadBV('Gabriel_01_07_24/selfpaced1sGabriel');
% file= 'Gabriel_01_07_24/selfpaced1sGabriel';
% [cnt,mk]= eegfile_readBV(file);
% classDef= {[65 70], [74 192]; 'left','right'};
% mrk= mrk_defineClasses(mk, classDef);
%
% [cnt,mk]= eegfile_readBV(file, 'MarkerFormat','string');
% classDef= {{'S 65','S 70'},{'S 74', 'S192'}; 'left','right'}
% mrk= mrk_defineClasses(mk, classDef);
% %% does the same
Expand All @@ -46,31 +51,27 @@
misc_checkType('mk', 'STRUCT(time)');
misc_checkType('classDef', 'CELL');

%iS= ~apply_cellwise2(regexp(mk.type, 'Stimulus'),'isempty');
%iR= ~apply_cellwise2(regexp(mk.type, 'Response'),'isempty');
iS= ~cellfun(@isempty, regexp(mk.type, 'Stimulus'));
iR= ~cellfun(@isempty, regexp(mk.type, 'Response'));
valid= find(iS|iR);
sgn= iS-iR;
mrk.time= mk.time(valid);
mrk_desc= cellfun(@(x)(str2double(x(2:end))), mk.desc(valid));
mrk.desc= sgn(valid) .* mrk_desc;

nClasses= size(classDef,2);
nEvents= length(valid);
mrk.y= zeros(nClasses, nEvents);
mrk= struct('time', mk.time);
mrk.y= zeros(nClasses, numel(mrk.time));
for cc= 1:nClasses,
if isnumeric(classDef{1,cc}),
mrk.y(cc,:)= ismember(mrk.desc, classDef{1,cc});
% vector as in {[10 11], [20:26]; 'target', 'nontarget'}
mrk.y(cc,:)= ismember(mk.desc, classDef{1,cc});
elseif iscell(classDef{1,cc}),
mrk.y(cc,:)= ismember(mk.desc(valid), classDef{1,cc});
% cell of strings as in {{'S 10','S 11'}, {'S 20','S 21'};
% 'target', 'nontarget'}
mrk.y(cc,:)= ismember(mk.desc, classDef{1,cc});
else
mrk.y(cc,:)= ismember(mk.desc(valid), classDef(1,cc));
% single string as in {'S10', 'S20'; 'target', 'nontarget'}
mrk.y(cc,:)= ismember(mk.desc, classDef(1,cc));
end
end

if size(classDef,1)>1,
mrk.className= classDef(2,:);
else
mrk.className= cprintf('class %d', 1:nClasses);
end

if ~opt.KeepAllMarkers,
Expand Down
41 changes: 41 additions & 0 deletions misc/bbci_typechecking.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function oldstate= bbci_typechecking(onoff)
%BBCI_TYPECHECKING - Switch type checking on or off
%
%Synopsis:
% bbci_typechecking(<SWITCH>)
% OLD_STATE= bbci_typechecking(<SWITCH>)
%
%Arguments:
% SWITCH: CHAR|BOOL - may be 'on'/1 (default) or 'off'/0
%
%Returns:
% OLD_STATE: CHAR - State ('on'/'off') that type checking had before
%
%Example:
% tcstate= bbci_typechecking('off');
% % do time-critical things
% bbci_typechecking(tcstat);


global BBCI_TYPECHECKING

oldstate= BBCI_TYPECHECKING;

if nargin==0,
onoff= 'on';
end

BBCI_TYPECHECKING= onoff;

switch(BBCI_TYPECHECKING),
case {1, 'on'},
BBCI_TYPECHECKING= 1;
case {0, 'off'},
BBCI_TYPECHECKING= 0;
otherwise
error('only ''on'' and ''off'' are allowed arguments');
end

if nargout==0,
clear oldstate;
end
42 changes: 42 additions & 0 deletions misc/cmap_bluewhite.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function cmap= cmap_bluewhite(m, varargin)
%CMAP_BLUEWHITE - Colormap going from blue to white
%
%Usage:
% MAP= cmap_bluewhite(M, <OPT>)
%
%Input:
% M : Size of the colormap (number of entries). Default value: Same size
% as current colormap
% OPT: Struct or property/value list of optinal properties:
% 'MinSat': minimal saturation (HSV model) from which fading is started
% 'MinVal': minimal value (in HSV model) from which fading is started
%
%Output:
% MAP: A colormap matrix of size [M 3]
%
%Example:
% clf;
% colormap(cmap_bluewhite(15));
% imagesc(toeplitz(1:15)); colorbar;
%
%See also COLORMAP, HSV2RGB, CMAP_HSVFADE

% 01-2005 Benjamin Blankertz

props= {'MinSat', 0.25, 'DOUBLE[1]'
'MinVal', 0, 'DOUBLE[1]'};

if nargin<1 | isempty(m),
m= size(get(gcf,'colormap'),1);
end

opt= opt_proplistToStruct(varargin{:});
[opt, isdefault]= opt_setDefaults(opt, props);
opt_checkProplist(opt, props);

m1= floor(m/2);
m2= m-m1;
map1= cmap_hsvFade(m1+1, 4/6, 1, [opt.MinSat 1]);
map2= cmap_hsvFade(m2+1, 4/6, [1 opt.MinVal], 1);

cmap= [map1; map2(3:end,:)];
Loading

0 comments on commit 6bfcebf

Please sign in to comment.