-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoSearchlightLOOCV_SVM.m
executable file
·165 lines (131 loc) · 6.38 KB
/
doSearchlightLOOCV_SVM.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
% !UNDER CONSTRUCTION! Implements a Searchlight Classification using One Out Cross Validation (LOOCV) with a SVM Classifier (parallel execution).
%
% Author: Maurice Hollmann
% Date : 05/11
%
% Description:
%
% [dataset, resultStruct] = doLeaveOneOutCrossValidation_SVM_2DforceQuiet(dataset, dataSplitter, svmCommandString)
%
% Just 4D dataset input!
% It is recommended to use a mask including just grey matter to reduce computation time!
%
%
% Parameters:
% dataset - The dataset to work on (all samples are included in LOOCV)
% dataSplitter - describes the splitting of the data in LOOCV
% svmType - Types:
% ['classification', 'regression_epsilon', 'regression_nu']
% kernelMode - Kernels: ['linear', 'polynomial', 'radial', 'sigmoid']
% costParam - The slack variable C in SVM (range 0 to 1 0 = low cost, 1 = highest costs).
% It defines the costs for misclassification (How strongly are outliers punished?)
% paramStruct - [optional] - i.e. {"degree", 3}
%
% Returns:
% dataset - the datset that has been the input
% resultAccuracyMap - a 3D map with the results of searchlight classification for each voxel
% resultStruct - The struct holding the classification results:
% resultStruct.nmbTests (the number of samples tested for this result)
% resultStruct.accuracy (percentual value of correct predictions (correct * 100 / nmbSamples))
% resultStruct.sensitivity (TP/TP+FN = Proportion of true positives to all positives)
% resultStruct.specificity (TN/TN+FP = Proportion of true negatives to all negatives)
% resultStruct.TP (True positives = all correct predicted in class 1)
% resultStruct.TN (True negatives = all correct predicted in class 2)
% resultStruct.FP (False positives = all incorrect predicted in class 1)
% resultStruct.FN (False negatives = all incorrect predicted in class 2)
%
% Comments:
%
function [datasetOut, resultAccuracyMap, resultStruct] = doSearchlightLOOCV_SVM(dataset, searchlightDiameter, dataSplitter, svmType, kernelMode, costParam, paramStruct)
%extractt the SVM parameter values from paramStruct
if( ~exist('paramStruct','var'))
[paramStructIsValid, svmParamInfoStruct, cmdString] = getSVMParamInfo(svmType, kernelMode, costParam, {});
else
[paramStructIsValid, svmParamInfoStruct, cmdString] = getSVMParamInfo(svmType, kernelMode, costParam, paramStruct);
end
if( ~paramStructIsValid)
error(['Usage of doLeaveOneOutCrossValidation_SVM: [datasetOut, resultAccuracyMap3D, resultStruct] = doSearchlightLOOCV_SVM(dataset, searchlightDiameter, dataSplitter, svmType - [classification, regression_epsilon, regression_nu]', ...
'kernelMode - [linear, polynomial, radial, sigmoid] , costParam [0-1], paramStruct [optional - i.e. {"degree", 3}])']);
end
tic
%for each voxel extract all voxels in the searchlight volume
%and do a LOOCV on these...
datasetOut = [];
resultStruct = [];
if(dataset.is4D)
sizeData = size(dataset.data);
%4D case
if(isfield(dataset,'featureSelectionMap') && ~isempty(dataset.featureSelectionMap))
globalSelectionMap = dataset.featureSelectionMap;
elseif(isfield(dataset,'mask') && ~isempty(dataset.mask))
globalSelectionMap = dataset.mask;
else
globalSelectionMap = ones(sizeData(1),sizeData(2),sizeData(3));
end
%get the adjacency matrix
adjMatrix = getSearchlightAdjacencyMatrix(dataset, searchlightDiameter);
sl_numEl = length(adjMatrix(adjMatrix>0));
adjMatrix = repmat(adjMatrix, [1, 1, 1, sizeData(4)]);
elseif(dataset.is2D)
end
%now move through all voxels in feature selection mask
indVec = find(globalSelectionMap>0);
%performanceMatrices = zeros(sizeData(1),sizeData(2),sizeData(3));
searchlightRadius = floor(searchlightDiameter/2);
dataDim4 = sizeData(4);
%use as 1D because of parfor
resultAccuracyVec = zeros(1,length(indVec));
localQuietMode = easyupMVPA_getGlobals('quietMode');
if(~localQuietMode)
disp(['Running Searchlight Leave One Out Cross Validation with command string: ',cmdString,' ...']);
% create a progress display that works also for parallel loops
if(length(indVec) <50)
disp(['0%', num2str(repmat(' ',1,length(indVec)-1)),'100%']);
progressIndices = [1 1:length(indVec)];
else
disp(['0%', num2str(repmat(' ',1,50)),'100%']);
%create a vector with floored indicees for repetitions
progressIndices = [1 1:length(indVec)];
progressIndices = floor(progressIndices*(50/length(indVec)));
end
fprintf(' ');
end
%disp(length(indVec));
%for i=1:length(indVec)
parfor i=1:length(indVec)
%disp(i)
data3D = [];
if(~localQuietMode)
if(progressIndices(i)<progressIndices(i+1))
fprintf('\b\b*');
disp(['' 0]);
end
end
%place the searchlight
[indX,indY,indZ] = ind2sub(size(globalSelectionMap), indVec(i));
%try because of possible invalid indicees
try
%extract the data (at first the whole cubic volume)
data3D = dataset.data(indX-searchlightRadius:indX+searchlightRadius,...
indY-searchlightRadius:indY+searchlightRadius,...
indZ-searchlightRadius:indZ+searchlightRadius,:);
catch
continue;
end
%data2D = reshape(data3D(adjMatrix>0), sl_numEl, dataDim4);
ds2D = getEmpty2DDataset();
ds2D.data = reshape(data3D(adjMatrix>0), sl_numEl, dataDim4);
ds2D.chunks = dataset.chunks;
ds2D.classIDs = dataset.classIDs;
%printDatasetInfo(ds2D);
%tic
%disp(size(data2D));
[ds, sl_resStruct] = doLeaveOneOutCrossValidation_SVM_2DforceQuiet(ds2D, dataSplitter, cmdString);
%toc
%disp(sl_resStruct.accuracy);
resultAccuracyVec(i) = sl_resStruct.accuracy;
end
toc
resultAccuracyMap = zeros(sizeData(1), sizeData(2), sizeData(3));
resultAccuracyMap(find(globalSelectionMap>0)) = resultAccuracyVec;
end