-
Notifications
You must be signed in to change notification settings - Fork 119
/
ROCDiagramMaker.java
200 lines (165 loc) · 8.55 KB
/
ROCDiagramMaker.java
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (C) 2019 xmw13bzu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package evaluation;
import ResultsProcessing.MatlabController;
import static evaluation.ClassifierResultsAnalysis.matlabFilePath;
import evaluation.storage.ClassifierResults;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Class to convert results in the format as they are in the results pipeline (ClassifierResults)
* into the format for the roccurves.m matlab script for generating roc diagrams
*
* @author James Large (james.large@uea.ac.uk)
*/
public class ROCDiagramMaker {
public static String rocDiaPath = "dias_ROCCurve/";
/**
* Concatenates the predictions of classifiers made on different folds on the data
* into one results object per classifier.
*
* @param cresults [classifier][fold]
* @return [classifier]
*/
public static ClassifierResults[/*classifier*/] concatenateClassifierResults(ClassifierResults[/*classiifer*/][/*fold*/] cresults) throws Exception {
ClassifierResults[] concatenatedResults = new ClassifierResults[cresults.length];
if (cresults[0].length == 1) {
for (int i = 0; i < cresults.length; i++)
concatenatedResults[i] = cresults[i][0];
}
else {
//if classifierresults ever gets split into separate classes for prediction and meta info, this obviously
//gets cleaned up a lot
for (int classifierid = 0; classifierid < cresults.length; classifierid++) {
ClassifierResults newCres = new ClassifierResults();
for (int foldid = 0; foldid < cresults[classifierid].length; foldid++) {
ClassifierResults foldCres = cresults[classifierid][foldid];
for (int predid = 0; predid < foldCres.numInstances(); predid++) {
newCres.addPrediction(foldCres.getTrueClassValue(predid),
foldCres.getProbabilityDistribution(predid),
foldCres.getPredClassValue(predid),
1, //dont care
""); //dont care
}
}
concatenatedResults[classifierid] = newCres;
}
}
return concatenatedResults;
}
public static String[] formatClassifierNames(String[] cnames) {
int maxLength = -1;
for (String cname : cnames)
if (cname.length() > maxLength)
maxLength = cname.length();
String[] paddedNames = new String[cnames.length];
for (int i = 0; i < cnames.length; i++) {
paddedNames[i] = cnames[i];
while(paddedNames[i].length() < maxLength)
paddedNames[i] += " ";
}
return paddedNames;
}
public static double[] extractPosClassProbabilities(ClassifierResults results, int positiveClass) {
double[][] dists = results.getProbabilityDistributionsAsArray();
double[] posClassProbs = new double[dists.length];
for (int i = 0; i < posClassProbs.length; i++)
posClassProbs[i] = dists[i][positiveClass];
return posClassProbs;
}
public static int findMinorityClass(double[] classVals) {
HashMap<Integer,Integer> classes = new HashMap<>();
for (double classVal : classVals) {
Integer v = classes.get(classVal);
if (v == null)
v = 0;
classes.put((int)classVal, v++);
}
int minClass = -1, minCount = Integer.MAX_VALUE;
for (Map.Entry<Integer, Integer> entry : classes.entrySet()) {
if (entry.getValue() < minCount) {
minCount = entry.getValue();
minClass = entry.getKey();
}
}
return minClass;
}
public static void matlab_buildROCDiagrams(String outPath, String expName, String dsetName, ClassifierResults[] cresults, String[] cnames) {
matlab_buildROCDiagrams(outPath, expName, dsetName, cresults, cnames, findMinorityClass(cresults[0].getTrueClassValsAsArray()));
}
public static void matlab_buildROCDiagrams(String outPath, String expName, String dsetName, ClassifierResults[] cresults, String[] cnames, int positiveClassIndex) {
String targetFolder = outPath + rocDiaPath;
(new File(targetFolder)).mkdirs();
String targetFile = targetFolder + "rocDia_" + expName + "_" + dsetName;
try {
MatlabController proxy = MatlabController.getInstance();
proxy.eval("addpath(genpath('"+matlabFilePath+"'))");
proxy.eval("m_fname = '" + targetFile + "';");
//holy hacks batman
// turns [CAWPE, resnet, XGBoost]
// into ['CAWPE '; 'resnet '; 'XGBoost']
String[] paddedNames = formatClassifierNames(cnames);
// System.out.println("m_cnames = " + Arrays.toString(paddedNames).replace(", ", "'; '").replace("[", "['").replace("]", "']") + "");
proxy.eval("m_cnames = " + Arrays.toString(paddedNames).replace(", ", "'; '").replace("[", "['").replace("]", "']") + ";");
double[] cvals = cresults[0].getTrueClassValsAsArray();
int[] m_cvals = new int[cvals.length];
for (int i = 0; i < cvals.length; i++)
m_cvals[i] = (int)cvals[i];
proxy.eval("m_cvals = " + Arrays.toString(m_cvals) + ";");
StringBuilder probsSB = new StringBuilder();
for (int i = 0; i < cresults.length; i++) {
double[] probs = extractPosClassProbabilities(cresults[i], positiveClassIndex);
probsSB.append(Arrays.toString(probs).replace("[", "").replace("]", ";"));
}
proxy.eval("m_posClassProbs = [ " + probsSB.toString() + " ];");
proxy.eval("m_posClass = " + positiveClassIndex + ";");
//function [f] = roccurves(filepathandname,classifierNames,classValues,posClassProbs,posClassLabel,visible)
proxy.eval("roccurves(m_fname, m_cnames, m_cvals, m_posClassProbs, m_posClass, 'off')");
proxy.eval("clear");
proxy.discconnectMatlab();
} catch (Exception io) {
System.out.println("matlab_buildROCDiagrams failed while building " +targetFile+ "\n" + io);
}
}
public static void main(String[] args) throws Exception {
String baseReadPath = "C:/JamesLPHD/Alcohol/JOURNALPAPER/Results/";
String dset = "JWRorJWB_BlackBottle";
String[] cnames = { "CAWPE", "resnet", "XGBoost" };
int numFolds = 10;
ClassifierResults[][] res = new ClassifierResults[cnames.length][numFolds];
for (int i = 0; i < res.length; i++) {
for (int f = 0; f < numFolds; f++) {
res[i][f] = new ClassifierResults(baseReadPath + cnames[i] + "/Predictions/" + dset + "/testFold"+f+".csv");
}
}
ClassifierResults[] concatenatedRes = concatenateClassifierResults(res);
matlab_buildROCDiagrams("C:/Temp/rocDiaTest/", "testDias", dset, concatenatedRes, cnames);
//single fold
// String baseReadPath = "C:/JamesLPHD/Alcohol/JOURNALPAPER/Results/";
// String dset = "JWRorJWB_BlackBottle";
// String[] cnames = { "CAWPE", "resnet", "XGBoost" };
//
// ClassifierResults[] res = new ClassifierResults[cnames.length];
// for (int i = 0; i < res.length; i++)
// res[i] = new ClassifierResults(baseReadPath + cnames[i] + "/Predictions/" + dset + "/testFold0.csv");
//
// matlab_buildROCDiagrams("C:/Temp/rocDiaTest/", "testDias", dset, res, cnames);
}
}