private/xsvm_subject_loop.m
8959ce94
 %% subject loop
 function decode = xsvm_subject_loop(header,subjectdata,svmopts)
48fb1827
 global NODALYZE_LIBSVM;
 addpath(fullfile(getTbxPath,NODALYZE_LIBSVM));
c73e97a6
 
8959ce94
 nSubjects = numel(subjectdata);
9760fd2f
 if(nSubjects < 2) 
     error('SVMCrossVal:xsvmSubjectLoop:tooFewSubjects','You need at least 2 Subjects in this Across-Subject analysis!');
 end
8959ce94
 
a37f1788
 RANDOMIZE_DATAPOINTS = header.svmrnd;
 NAN_AS_ZERO = header.nantozero;
c73e97a6
 
8959ce94
 decode = struct;
 decode.decodePerformance = [];
 decode.rawTimeCourse     = [];
 
c0e4506e
 disp(sprintf('computinig additional datastructs for %u subjects',nSubjects));
8959ce94
 
 timeline = header.timeline;
feeb21e9
 timeline.frameShiftStart = header.frameShift.frameShiftStart;
 timeline.frameShiftEnd   = header.frameShift.frameShiftEnd;
 timeline.decodeDuration  = header.frameShift.decodeDuration;
8959ce94
 
c73e97a6
 % TimePointMatrix
8959ce94
 for subjectDataID = 1:nSubjects
     currentSubject = subjectdata{subjectDataID};
     timePointArgs.pst           = currentSubject.pst;
     timePointArgs.labelMap      = LabelMap(header.classDef.labelCells,header.classDef.conditionCells);
     timePointArgs.eventList     = header.classDef.eventMatrix;
 
     timePointMatrix{subjectDataID} = buildTimePointMatrix(timeline,timePointArgs);
c73e97a6
     
     decode.rawTimeCourse = [decode.rawTimeCourse currentSubject.pst];
8959ce94
 end
 
c73e97a6
 % timeframe x-subject validation
8959ce94
 timeLineStart   = timeline.frameShiftStart;
 timeLineEnd     = timeline.frameShiftEnd;
 
c0e4506e
 display(sprintf('%u -fold cross validation for %u timeslices.\n',nSubjects,size(1:timeLineEnd-timeLineStart+1,2)));
c73e97a6
 % disp(sprintf('Press ANY-Key to continue.\n Use Retrun if your Keyboard lacks the ANY-Key.'));
 % pause
 
8959ce94
 for timeIndex = 1:timeLineEnd-timeLineStart+1
c0e4506e
     cross_value = [];
8959ce94
     for validationSubjectID = 1:nSubjects
c0e4506e
         svm_train_label = [];
         svm_train_data  = [];
         svm_validation_label = [];
         svm_validation_data  = [];
8959ce94
         for subjectDataID = 1:nSubjects
             svmstruct = calculateSVMTables(timePointMatrix{subjectDataID},timeIndex);
             if subjectDataID == validationSubjectID
                 svm_validation_label = svmstruct.svmlabel;
                 svm_validation_data  = svmstruct.svmdata;
             else
                 svm_train_label = [svm_train_label; svmstruct.svmlabel];
                 svm_train_data  = [svm_train_data;  svmstruct.svmdata];
             end
         end
         
c73e97a6
         if RANDOMIZE_DATAPOINTS
             rndindex  = randperm(length(svm_train_label));
             svm_train_data   = svm_train_data(rndindex,:);
             svm_train_label  = svm_train_label(rndindex);
         end
a37f1788
         
         if NAN_AS_ZERO
             svm_train_data(isnan(svm_train_data))=0;
         end
         
c73e97a6
 
8959ce94
         svmmodel = svmtrain(svm_train_label,svm_train_data,svmopts);
         
         [plabel accuracy dvalue] = svmpredict(svm_validation_label,svm_validation_data,svmmodel,'');
c0e4506e
         cross_value = [cross_value accuracy(1)];
8959ce94
         
c0e4506e
     end
c73e97a6
     decode.decodePerformance = [decode.decodePerformance; cross_value];
c0e4506e
     
8959ce94
 end
2de68580
 
 disp('decode done');
8959ce94
 end