Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
20 changes: 20 additions & 0 deletions +eegtoolkit/+aggregation/@AggregatorBase/AggregatorBase.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
% Abstract class for aggregation. Implement the functions for any
% fusion methodology (e.g. late fusion, VLAD, channel concatenation, etc.)
%
% Properties:
% featextractors: cell array with the the feature extractors to be used on
% the trials. Each cell contains a featextraction object, and the trials
% are given to each of the objects within the Experimenter class
% instanceSet: output - the aggregated features. Can be a simple
% InstanceSet or the extended FusionInstanceSet
%
% Functions:
%Implement this function to process the trials of each featextraction
%object in the cell array featextractors. It should store in the
%instanceSet property the features.
% obj.aggregate();
%Info & run time so that the experiments are easily documented. configInfo
%is a string with the configuration information and time is of type double.
% configInfo = obj.getConfigInfo();
% time = obj.getTime();

classdef (Abstract) AggregatorBase < handle

properties
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
classdef CSPFilterBankWrapper < eegtoolkit.classification.ClassifierBase;
properties (Constant)

end
properties
baseClassifier;
channel;
cspFilters;
filterBanks;
samplingRate;
end

methods (Access = public)
function CSPFB = CSPFilterBankWrapper(filterBanks, samplingRate)
CSPFB.cspFilters = {};
CSPFB.filterBanks = filterBanks;
CSPFB.samplingRate = samplingRate;
%filter_banks=[8 12; 12 16; 16 20;20 24;24 28];
% if(isa(instanceSet,'eegtoolkit.util.RawSignalSet')
% CSP.instanceSet = instanceSet;
% else
% error('RawSignal extractor should be used with CSPWrapper');
% end
end


function CSPFB = build(CSPFB)
% Builds the classification models
CSPFB.learnCSPMatrix(CSPFB.instanceSet.sMatrix,CSPFB.instanceSet.labels);
instanceLocal = CSPFB.extract(CSPFB.instanceSet.sMatrix);
CSPFB.baseClassifier.instanceSet = eegtoolkit.util.InstanceSet(instanceLocal,CSPFB.instanceSet.labels);
CSPFB.baseClassifier.build;
CSPFB.reset;
end

function [output, probabilities, ranking] = classifyInstance(CSPFB,instance)

% instance = CSP.extract
%input = instance matrix rows = instances, cols = attributes
%output = predicted class
%probabilities = probability for predicted class
%ranking = propabilities for all classes (e.g. to use with mAP)

%TODO:should print an error if 'build' has not been called
testInstances = CSPFB.extract(instance);
[output, probabilities, ranking] = CSPFB.baseClassifier.classifyInstance(testInstances);
% numModels = length(LSVM.models);
% [numinstance, ~] = size(testInstances);
% scores = zeros(numModels,numinstance);
% for i=1:numModels
% %predict using the stored models
% [~, ~, t] = svmpredict(eye(numinstance,1),testInstances, LSVM.models{i},'-b 1 -q');
% %store probability for each class
% scores(i,:) = t(:,1);
% end
% output = zeros(numinstance,1);
% probabilities = zeros(numinstance,1);
% ranking = scores;
% for i=1:numinstance
% %select the class with the highest probability
% [prob, idx] = max(scores(:,i));
% uniqueLabels = unique(LSVM.instanceSet.getLabels);
% %output the label with highest probability
% output(i,1) = uniqueLabels(idx);
% %return the probability for the output label
% probabilities(i,1) = prob;
% end
end

function CSPFB = reset(CSPFB)
% 'Resets' the classifier.
% CSP.models = {};
end

function configInfo = getConfigInfo(CSPFB)
configInfo = '\n';
end


function time = getTime(CSPFB)
time = 0;
end
end
methods (Access = private)
function [] = learnCSPMatrix(CSPFB, sMatrix, labels)
%[trialsMat,labels] = ssveptoolkit.util.Trial.trialsCellToMat(in);
[numTrials,numChannels,numSamples] = size(sMatrix);
% numTrials = length(CSPFB.trials);
% [numChannels,numSamples] = size(CSPFB.trials{1}.signal);
% samplingRate = CSPFB.trials{1}.samplingRate;
filter_banks= CSPFB.filterBanks;
[nfb, mfb] = size(filter_banks);

for iter_fb=1:nfb

[b,a]=butter(3,filter_banks(iter_fb)/(CSPFB.samplingRate/2));
% labels = zeros(numTrials,1);
%numTrials X numChannels X numSamples
trialsMat = permute(sMatrix,[2,3,1]);
% trialsMat = zeros(numChannels,numSamples,numTrials);
% for i = 1 : length(CSP_Feat.trials)
% for i_ch = 1:numChannels
% trialsMat(i_ch,:,i) = filtfilt(b,a,CSP_Feat.trials{i}.signal(i_ch,:));
% end
% labels(i) = CSP_Feat.trials{i}.label;
% end

trialsMat = permute(trialsMat,[2 1 3]);
[N1, Nch1, Ntr1] = size(trialsMat);
for i = 1:Nch1
for j = 1:Ntr1
trialsMat(:,i,j) = (trialsMat(:,i,j) - mean(trialsMat(:,i,j)));
end
end
x_train = trialsMat;%(:,:,CSP_Feat.trainIdx);
y_train = labels;%(CSP_Feat.trainIdx);
[N, Nch, Ntr] = size(x_train);
trialCov=zeros(Nch,Nch,Ntr);
for t=1:length(y_train)
E = x_train(:,:,t)';
EE = E * E';
trialCov(:,:,t) = EE ./ trace(EE);
end
for c=1:2
covMat{c} = mean(trialCov(:,:,y_train == c),3);
end
[U, D] = eig(covMat{1},covMat{2},'qz');
eigenvalues = diag(D);
[~, ind] = sort(eigenvalues, 'descend');
U = U(:,ind);
CSPFB.cspFilters{iter_fb} = U';
% CSP_Filter = U';
end
%trialsMat2=zeros(N,3,Ntr);

end

function instances = extract(CSPFB,sMatrix)
trialsMat = permute(sMatrix,[2,3,1]);
trialsMat = permute(trialsMat,[2,1,3]);
[numTrials,numChannels,~] = size(sMatrix);
final_instances = zeros(numTrials, numChannels*length(CSPFB.filterBanks));
for iter_fb=1:length(CSPFB.cspFilters)
for j = 1:size(trialsMat,3)
trialsMat(:,:,j) = (CSPFB.cspFilters{iter_fb}*trialsMat(:,:,j)')';
end

instances = zeros(numTrials, numChannels);
%labels = zeros(numTrials,1);

for i=1:numTrials

projectedTrial = trialsMat(:,:,i);%Filter * CSP_Feat.trials{i}.signal(:,i);% EEGSignals.x(:,:,t)';
%generating the features as the log variance of the projected signals
variances = var(projectedTrial,0,1);
instances(i,:) = log(variances)';
%labels(i,1) = floor(CSP_Feat.trials{i}.label);
end
final_instances(:,numChannels*(iter_fb-1)+1:numChannels*iter_fb) = instances;
% CSP_Feat.avgTime = toc/numTrials;
% CSP_Feat.instanceSet = ssveptoolkit.util.InstanceSet(final_instances,labels);
end
instances = final_instances;
end



end
% [numTrials,numChannels,~] = size(sMatrix);
% sMatrix = permute(sMatrix,[3,2,1]);
% for i=1:size(sMatrix,3);
% sMatrix(:,:,i) = (CSPFB.cspFilter*sMatrix(:,:,i)')';
% end
% instances = zeros(numTrials,numChannels);
% for i=1:numTrials
% projectedTrial = sMatrix(:,:,i);
% variances = var(projectedTrial,0,1);
% instances(i,:) = log(variances)';
% end
% % instanceSet = eegtoolkit.util.InstanceSet(instances,labels);
% end
%end
end

29 changes: 28 additions & 1 deletion +eegtoolkit/+classification/@ClassifierBase/ClassifierBase.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
% Abstract class for classification. Implement the functions for any
% classifier (e.g. LibSVM, LDA, MLR, RFs, Adaboost, etc.)
%
% Properties:
% instanceSet: the training/test set in an InstanceSet structure
%
% Functions:
%Implement this function to train the classification models. Each
%implementation should define the type of models that are to be trained by
%the build function.
% obj = obj.build();
%Implement this function to apply the classification models to the provided
%instance, where instance is a matrix #instances x #features including the
%instances. The function should return the following three outputs; 1)
%output, the classified labels of the instances (#instances x 1), 2)
%probabilities, the probabilities for the output label (instances x 1) and
%3) ranking, a matrix #instances x #classes with the probabilities of each
%instance for each class (used for multiclass classification).
% [output, probabilities, ranking] = obj.classifyInstance(instance);
%Implement this function to reset the classifier (e.g. delete the stored
%classification models
% obj.reset();
%Info & run time so that the experiments are easily documented. configInfo
%is a string with the configuration information and time is of type double.
% configInfo = obj.getConfigInfo();
% time = obj.getTime();

classdef (Abstract) ClassifierBase < handle

properties
instanceSet;
end

methods (Abstract = true)
obj = classifyInstance(obj);
[output, probabilities, ranking] = classifyInstance(obj,instance);
obj = build(obj);
obj = reset(obj);
configInfo = getConfigInfo(obj);
Expand Down
14 changes: 8 additions & 6 deletions +eegtoolkit/+experiment/@Experimenter/Experimenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
%Before running an experiment all the required properties must be set.
% Required:
% - session
% - transformer
% - featextraction
% - evalMethod
% - classifier
% - classification
% Optional:
% - extractor
% - featselection
% - aggregator
% - preprocessing
%
% to run the experiment execute the "run()" method.
% Example:
% experiment = eegtoolkit.experiment.Experimenter;
% experiment.session = eegtoolkit.util.Session;
% experiment.session.loadSubject(1);
% experiment.transformer = eegtoolkit.transformer.PWelchTransformer;
% experiment.extractor = eegtoolkit.extractor.FEASTFilter;
% experiment.classifier = eegtoolkit.classifier.LIBSVMClassifier;
% experiment.transformer = eegtoolkit.featextraction.PWelch;
% experiment.extractor = eegtoolkit.featselection.FEAST;
% experiment.classification = eegtoolkit.classification.LIBSVM;
% experiment.evalMethod = experiment.evalMethod.EVAL_METHOD_LOSO;
% experiment.run;
% results = experiment.results;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
% Abstract class for feature extraction. Implement the functions for any
% feature extractor (e.g. PWelch, FFT, DWT, PYAR, etc.)
%
% Properties:
% instanceSet: Output - the features in an InstanceSet structure
% trials: Input - the signals in a cell of Trial structures
%
% Functions:
%Implement this function to extract the features from the input Trials
%and return the features in an instanceSet
% out = obj.extract(in);
%Info & run time so that the experiments are easily documented. configInfo
%is a string with the configuration information and time is of type double.
% configInfo = obj.getConfigInfo();
% time = obj.getTime();


classdef (Abstract) FeatureExtractionBase < handle
%Base class for a feature transformer
%For writing your own FeatureTransformer extend this class and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
% Abstract class for feature selection. Implement the functions for any
% feature selection method (e.g. PCA, SVD, FEAST, etc.)
%
% Properties:
% originalInstanceSet: Input - the original instanceSet with the features
% filteredInstanceSet: Output - the filtered instanceSet
%
% Functions:
%Implement this function to process the originalInstanceSet trials and
%return the filteredInstanceSet
% obj.compute();
%Info & run time so that the experiments are easily documented. configInfo
%is a string with the configuration information and time is of type double.
% configInfo = obj.getConfigInfo();
% time = obj.getTime();

classdef (Abstract) FeatureSelectionBase < handle
%Base class for a FeatureExtractor
%Subclasses must implement the filter method

properties
originalInstanceSet; % Input: The original dataset
filteredInstanceSet; % Output: The filtered dataset
Expand Down
17 changes: 17 additions & 0 deletions +eegtoolkit/+preprocessing/@PreprocessingBase/PreprocessingBase.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
% Abstract class for preprocessing. Implement the functions for any
% preprocessing step (e.g. filtering, artifact removal, sub-sampling,
% rereferencing, etc.
%
% Properties:
% originalTrials: cell array with the trials to be processed
% processedTrials: the processed trials to be returned
%
% Functions:
%Implement this function to process the in trials and return the processed
%trials (out)
% out = obj.process(in);
%Info & run time so that the experiments are easily documented. configInfo
%is a string with the configuration information and time is of type double.
% configInfo = obj.getConfigInfo();
% time = obj.getTime();

classdef PreprocessingBase < handle

properties
Expand Down
Loading