classdef janis_validatorTestGroup < janis_validator

   methods

        function obj = janis_validatorTestGroup(classCollection, classifier,patternParser)
           obj = obj@janis_validator(classCollection, classifier,patternParser);
           if ~isa(classCollection,'janis_classCollection')
               throw(MException('janis:WrongClass','This is not a classCollection-Class!'));
           elseif  ~isa(classifier,'janis_classifier')
               throw(MException('janis:WrongClass','This is not a classifier-Class!'));
           else
               % rounds should be half of the amount of patterns, i.e. the
               % amount of subjects in each group.
               obj.rounds = classCollection.patternCount/classCollection.size;
           end
        end


               % runs the validation
        function results = runValidation(this)
            % test all subjects in specified groups
            testIdx = cell2mat(cellfun(@(x) repmat(x.testGroup,x.size,1) ,this.patternParser.classCollection.classes,'UniformOutput',false));
            this.classPerformance = classperf(this.patternParser.getLabels);
            this = this.computeForSubjects(testIdx);
            this.actualRound = 1;
            notify(this,'cycle');
            this.roundPerformances = this.classPerformance.LastCorrectRate;
            results = this.classPerformance;
        end


       function this = computeForSubjects(this, testIdx)
                   for h=1:size(this.patternParser.intraValidationProcessors)
                      if isa(this.patternParser.intraValidationProcessors{h},'janis_pro_2ndLevel')
                          this.patternParser.intraValidationProcessors{h}.setLeaveOut(leaveOuts);
                      end
                   end
                   [patternMat, trainLabels, testMat, testLabels] = this.patternParser.get(~testIdx);
                   this.classifier = this.classifier.train(patternMat, trainLabels);
                   [prediction decFactors] = this.classifier.test(testMat);
                   groupCount = 1;
                   memberCount = 1;
                   for k=1:size(prediction,1)
                       if memberCount > this.patternParser.classCollection.classes{groupCount}.size
                           groupCount = groupCount + 1;
                           memberCount = 1;
                       end
                       while ~this.patternParser.classCollection.classes{groupCount}.testGroup
                          groupCount = groupCount + 1;
                          memberCount = 1;
                       end
                       subject = regexp(this.patternParser.classCollection.classes{groupCount}.members{memberCount}.description, filesep, 'split');
                       subject = subject{end}(end);
                       fprintf('subject %s predicted as: %d\n', subject{:}, prediction(k,1));
                       memberCount = memberCount + 1;
                   end
                   fprintf('-------------------------\n');
                   obs = unique(prediction);
                   for k=1:length(obs)
                        fprintf('%d as class %d\n',sum(prediction==obs(k)),obs(k));
                   end
                   testIdx = testIdx.*[1:length(testIdx)]';
                   this.classPerformance = classperf(this.classPerformance,prediction,testIdx(testIdx>0));
                   %jv.predicted = [jv.predicted; prediction'];
                   %decFactors.label = testLabels;
                   %jv.decFactors = [jv.decFactors decFactors];
        end


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

   end

end