
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Making data
clear trials classes;

load eegdata;

matfilename = 'allCVsubject2'

subject = 2;
tasks = [1 2 3 4 5];
trials = 1:5;

trials = squeeze(eegdata(subject,tasks,trials));
% Remove EOG channel
[nr,nc] = size(trials);
for r = 1:nr
  for c = 1:nc
    trials{r,c} = trials{r,c}(1:6,:);
  end
end

classes = repmat((1:length(tasks))',1,length(trials));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Specify common paramters
validationRepetitions = 5;
validationFraction = 0.2;
nchannels = 6;
lags = 0:4;
%%% max lag multiplied by 8 for raw and proj representations
lagsraw = 0:(max(lags)*8);
lagsproj = 0:6;
%%%window parms only for SVD and MSF
winSizes = [62 125];
winShift = 32;
%%%
meanSubtracts = [0 1];


if 1
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%% MSF basis vector representation

  clear parameterSets;
  k = 0;
  for winSize = winSizes
    for nl = lags
      for nModes = 1:nchannels*(nl+1)
	for firstMode = 1:(nchannels*(nl+1))-nModes+1
	  for meanSubt = meanSubtracts
	    k = k + 1;
	    parameterSets(k).nlags = nl;
	    parameterSets(k).winSize = winSize;
	    parameterSets(k).winShift = winShift;
	    parameterSets(k).firstMode = firstMode;
	    parameterSets(k).nModes = nModes;
	    parameterSets(k).meanSubtract = meanSubt;
	  end
	end
      end
    end
  end

  %%% Run all experiments
  CV.msf = runCV(trials,classes,parameterSets,...
      validationFraction,validationRepetitions,...
      @makeMSFModes,@LDA,@selectModes);

  save(matfilename,'CV');

  %%% Display results

  summarizeTestResults(CV.msf);

end

if 1
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% MSF projections representation

clear parameterSets;
k = 0;
for nl = lagsproj
  for nModes = 1:nchannels*(1+min(4,nl))
    for firstMode = 1:(nchannels*(nl+1))-nModes+1
      for meanSubt = meanSubtracts
	k = k + 1;
	parameterSets(k).nlags = nl;
	parameterSets(k).firstMode = firstMode;
	parameterSets(k).nModes = nModes;
	parameterSets(k).meanSubtract = meanSubt;
      end
    end
  end
end

%%% Run all experiments
CV.msfproj = runCV(trials,classes,parameterSets,...
    validationFraction,validationRepetitions,...
    @makeMSFProj,@LDA,@selectSamplesRows);

  save(matfilename,'CV');

%%% Display results

summarizeTestResults(CV.msfproj);

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% SVD basis vector representation

clear parameterSets;
k = 0;
for winSize = winSizes
  for nl = lags
    for nModes = 1:nchannels*(nl+1)
      for firstMode = 1:(nchannels*(nl+1))-nModes+1
	for meanSubt = meanSubtracts
	  k = k + 1;
	  parameterSets(k).nlags = nl;
	  parameterSets(k).winSize = winSize;
	  parameterSets(k).winShift = winShift;
	  parameterSets(k).firstMode = firstMode;
	  parameterSets(k).nModes = nModes;
	  parameterSets(k).meanSubtract = meanSubt;
	end
      end
    end
  end
end

%%% Run all experiments
CV.svd = runCV(trials,classes,parameterSets,...
    validationFraction,validationRepetitions,...
    @makeSVDModes,@LDA,@selectModes);

  save(matfilename,'CV');

%%% Display results

summarizeTestResults(CV.svd);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% SVD projections representation

clear parameterSets;
k = 0;
for nl = lagsproj
  for nModes = 1:nchannels*(1+min(4,nl))
    for firstMode = 1:(nchannels*(nl+1))-nModes+1
      for meanSubt = meanSubtracts
	k = k + 1;
	parameterSets(k).nlags = nl;
	parameterSets(k).firstMode = firstMode;
	parameterSets(k).nModes = nModes;
	parameterSets(k).meanSubtract = meanSubt;
      end
    end
  end
end

%%% Run all experiments
CV.svdproj = runCV(trials,classes,parameterSets,...
    validationFraction,validationRepetitions,...
    @makeSVDProj,@LDA,@selectSamplesRows);

  save(matfilename,'CV');

%%% Display results

summarizeTestResults(CV.svdproj);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Raw lagged representation

clear parameterSets;
k = 0;
for nl = lagsraw
  k = k + 1;
  parameterSets(k).nlags = nl;
end

%%% Run all experiments
CV.raw = runCV(trials,classes,parameterSets,...
    validationFraction,validationRepetitions,...
    @makeRawEEGFeatures,@LDA,@selectSamples);

  save(matfilename,'CV');

%%% Display results
summarizeTestResults(CV.raw);

