private/calculateDecodePerformance.m
2095645b
 % function [decodePerformance rawTimecourse ] = calculateDecodePerformance(des,timeLineStart, timeLineEnd, decodeDuration, svmargs, conditionList, sessionList, voxelList, classList, labelMap,normalize)
84376774
 function outputStruct = calculateDecodePerformance(inputStruct,SubjectID)
2095645b
 
 addpath 'libsvm-mat-2.88-1';
 
fd18aa84
 METHOD = 'single subject SVM';
 % METHOD = 'cross subject SVM';
 % METHOD = 'SOM';
9478fa59
 
2095645b
 outputStruct = struct;
 
cdb6cdac
 namehelper      = strcat('s',SubjectID);
 des             = inputStruct.(namehelper).des;
2095645b
 timeLineStart   = inputStruct.frameShiftStart;
 timeLineEnd     = inputStruct.frameShiftEnd;
 decodeDuration  = inputStruct.decodeDuration;
 svmargs         = inputStruct.svmargs;
 sessionList     = inputStruct.sessionList;
cdb6cdac
 voxelList       = inputStruct.(namehelper).voxelList;
2095645b
 % classList       = inputStruct.classList;
 % labelMap        = inputStruct.labelMap;
fd18aa84
 smoothed        = inputStruct.smoothed;
2095645b
 globalStart     = inputStruct.psthStart;
 globalEnd       = inputStruct.psthEnd;
 baselineStart   = inputStruct.baselineStart;
 baselineEnd     = inputStruct.baselineEnd;
 eventList       = inputStruct.eventList;
 
 minPerformance = inf;
 maxPerformance = -inf;
         
fd18aa84
         %% ERSETZEN DURCH ROI-IMAGE!
2095645b
         for voxel = 1:size(voxelList,1)  % [[x;x],[y;y],[z;z]]
fd18aa84
                 extr        = calculateImageData(voxelList(voxel,:),des,smoothed); 
                 rawdata     = cell2mat({extr.mean}); % Raw Data
2095645b
                 pst{voxel}  = calculatePST(des,globalStart,baselineStart,baselineEnd,globalEnd,eventList,rawdata,sessionList);
         end
90f42025
         
fd18aa84
         timePointArgs.pst = pst;
         timePointArgs.timeLineStart = timeLineStart;
         timePointArgs.timeLineEnd   = timeLineEnd;
         timePointArgs.globalStart   = globalStart;
         timePointArgs.globalEnd     = globalEnd;
         timePointArgs.decodeDuration= decodeDuration;
         timePointArgs.labelMap      = inputStruct.labelMap;
         timePointArgs.eventList     = eventList;
2095645b
         
fd18aa84
         timePointMatrix = buildTimePointMatrix(timePointArgs);
 
         decodePerformance = [];
         for index = 1:timeLineEnd-timeLineStart+1
             RANDOMIZE_DATAPOINTS = 0;
             svmdata      = timePointMatrix{index}(:,2:size(timePointMatrix{index},2));
             svmlabel     = timePointMatrix{index}(:,1);
90f42025
             
fd18aa84
             if RANDOMIZE_DATAPOINTS
                 rndindex  = randperm(length(svmlabel));
                 svmdata   = svmdata(rndindex,:);
                 svmlabel  = svmlabel(rndindex);
             end
 
8f723b08
             SVM_METHOD = 'som training'
fd18aa84
             switch SVM_METHOD;
4dbef185
                 case 'libsvm crossval'
fd18aa84
                     performance  = svmtrain(svmlabel, svmdata, svmargs);
2095645b
 
fd18aa84
                     minPerformance = min(minPerformance,performance);
                     maxPerformance = max(maxPerformance,performance);
2095645b
 
fd18aa84
                     decodePerformance = [decodePerformance; performance];
4dbef185
                 case 'class performance'
fd18aa84
                     newsvmopt = killCrossvalOpt(svmargs);
                     
                     model = svmtrain(svmlabel,svmdata,newsvmopt);
                     classperformance = [];
                     for class = unique(svmlabel)';
4dbef185
 
fd18aa84
                         filterindex = find(class == svmlabel);
4dbef185
                         testing_label = svmlabel(filterindex);
                         testing_data  = svmdata(filterindex);
                         [plabel accuracy dvalue] = svmpredict(testing_label,testing_data,model,'');
 
fd18aa84
                         classperformance = [classperformance accuracy(1)];
                     end
                     decodePerformance = [decodePerformance; classperformance];
                     
4dbef185
                 case 'som training'
                     display('SOM TRAINING');
                     addpath 'somtoolbox2';
8f723b08
                     sD = som_data_struct(svmdata,'label',num2str(svmlabel));
4dbef185
                     assignin('base','sD',sD);
                     sM = som_make(sD,'msize', [3 4],'lattice', 'rect');
                     
8f723b08
                     assignin('base','sD',sD);
                     assignin('base','sM',sM);
                     
9478fa59
             end
             
2095645b
         end
         
         outputStruct.decodePerformance  = decodePerformance;
         outputStruct.svmdata            = svmdata;
         outputStruct.svmlabel           = svmlabel;
         outputStruct.rawTimeCourse      = pst;
         outputStruct.minPerformance     = minPerformance;
         outputStruct.maxPerformance     = maxPerformance;
 end
 
fd18aa84
 function opts = killCrossvalOpt(svmopt)
 opts = '';
 idx1 = 1;
 for idx2=strfind(svmopt,' -')
     if idx1 ~= strfind(svmopt,' -v')
         opts = strcat(opts,svmopt(idx1:idx2));
     end
     idx1=idx2;
     if idx2==max(strfind(svmopt,' -'))
         opts = strcat(opts,svmopt(idx2:end));
     end
 end
 end