% Default parser for ordinary patterns
classdef janis_simpleParser < janis_patternParser

    methods

        % Constructor
        function pp = janis_simpleParser(classCollection)
            pp = pp@janis_patternParser(classCollection);
        end

        % gets all Patterns from all subjects and generates a Matrix
        % which is used to return the a matrix by specified subjects
        function pp = init(pp)
           % init helper Matrices
           % Patternsize: Voxels X Subjects
           pp.patternMat = zeros(pp.classCollection.get(1).voxelsPerSub,pp.classCollection.patternCount);
           pp.patternLabels = zeros(pp.classCollection.patternCount,1);
           actualIndex = 1;
           % go through all classes
           for cl=1:pp.classCollection.size
              % for each class
              actualClass = pp.classCollection.get(cl);
              pp.patternLabels(actualIndex:actualIndex+actualClass.size-1) = cl -1;
              % go through all subjects
              for subject=1:actualClass.size
                  actualSubject = actualClass.getMember(subject);
                  vector = actualSubject.get;
                  try
                  pp.patternMat(:,actualIndex-1+subject) = vector;
                  catch
                      error(['Dimension missmatch in subject ' actualSubject.description]);
                  end
              end
              actualIndex = actualIndex + actualClass.size;
           end
           pp.patternMat = pp.applyProcessors(pp.patternMat);
        end

        % return specific pattern
        function [trainPattern, trainLabels, testPattern, testLabels] = get(pp,indices)
            trainPattern = pp.patternMat(:,indices);
            testPattern  = pp.patternMat(:,~indices);
            trainLabels = pp.patternLabels(indices);
            testLabels  = pp.patternLabels(~indices);
            for ivp=1:size(pp.intraValidationProcessors,2)
                pp.intraValidationProcessors{ivp}.preprocessorHistory = pp.preprocessors;
                [trainPattern, testPattern] = pp.intraValidationProcessors{ivp}.operate(trainPattern,testPattern,trainLabels);
            end
        end

        % call reconstruct Functions from Feature Selection methods in
        % reverse order they were applied
        function pattern = reconstructFromProcessors(pp,pattern)
            % go through reverse
            for ivp=size(pp.intraValidationProcessors,2):-1:1
                pattern = pp.intraValidationProcessors{ivp}.reconstruct(pattern);
            end
            for ivp=size(pp.preprocessors,2):-1:1
                pattern = pp.preprocessors{ivp}.reconstruct(pattern);
            end
            origDim = size(pp.classCollection.classes{1}.members{1}.VoxelValues);
            pattern = reshape(pattern,origDim);
        end

        % return all the labels
        function labelsAll = getLabels(pp)
            labelsAll = pp.patternLabels;
        end

        function new = copy(this)
           new =  copy@janis_patternParser(this);
        end
    end
end