rtsEnviRandomForest2C_validate.m
1.68 KB
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
function [Croc, Cauc] = rtsEnviRandomForest2C_validate(RF, EnviFileName, EnviHeaderName, RoiImages, reference)
%Validates a 2-class random forest classifier, creating ROC curves.
%parameters
maxPixels = 200000;
%determine the number of classes
nClasses = length(RoiImages);
%make sure that there are only two classes
if nClasses > 2
disp('This classifier only supports 2 classes.');
return;
end
%for each class, load the validation data
T = [];
F = [];
for c = 1:nClasses
%load the class mask
maskImage = imread(RoiImages{c});
maskBinary = (maskImage(:, :, 1) > 0)';
disp('------------------------------------------------------');
%load epithelium spectra
disp(['Loading Validation Class ' num2str(c) ' pixels: ' EnviFileName]);
tLoadTime = tic;
fid = rtsEnviOpen(EnviFileName, EnviHeaderName, maskBinary);
Fc = rtsEnviRead(fid, maxPixels);
rtsEnviClose(fid);
if c == 1
Tc = ones(size(Fc, 2), 1);
else
Tc = zeros(size(Fc, 2), 1);
end
disp(['Time: ' num2str(toc(tLoadTime)) 's']);
%add features and targets to the final vectors
T = [T; Tc];
F = [F; Fc'];
end
%apply the reference
if nargin == 5
Fnorm = repmat(F(:, reference), 1, size(F, 2));
F = F./Fnorm;
F(:, reference) = [];
end
%classify the data (get the estimated posterior probability)
disp('Validating...');
tClassTime = tic;
Tpost = predict(RF, F);
disp(['Time: ' num2str(toc(tClassTime)) 's']);
%calculate and display the ROC curve
disp('Calculating ROC...');
tRocTime = tic;
[Croc, Cauc, threshList] = rtsROC(Tpost, T);
disp(['Time: ' num2str(toc(tRocTime)) 's']);
disp(['AUC = ' num2str(Cauc)]);
plot(Croc(:, 1), Croc(:, 2));