source: trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/AbstractWekaEvaluation.java @ 117

Last change on this file since 117 was 115, checked in by sherbold, 9 years ago
  • updated evaluation such that AUCEC also works with the MDP and AEEEM data
  • Property svn:mime-type set to text/plain
File size: 13.7 KB
Line 
1// Copyright 2015 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.cpdp.eval;
16
17import java.io.FileNotFoundException;
18import java.io.FileOutputStream;
19import java.io.PrintWriter;
20import java.util.ArrayList;
21import java.util.Iterator;
22import java.util.LinkedList;
23import java.util.List;
24
25import de.ugoe.cs.cpdp.training.ITrainer;
26import de.ugoe.cs.cpdp.training.IWekaCompatibleTrainer;
27import de.ugoe.cs.util.StringTools;
28import weka.classifiers.Classifier;
29import weka.classifiers.Evaluation;
30import weka.core.Attribute;
31import weka.core.Instances;
32
33/**
34 * Base class for the evaluation of results of classifiers compatible with the {@link Classifier}
35 * interface. For each classifier, the following metrics are calculated:
36 * <ul>
37 * <li>succHe: Success with recall>0.7, precision>0.5</li>
38 * <li>succZi: Success with recall>0.7, precision>0.7</li>
39 * <li>succG75: Success with gscore>0.75</li>
40 * <li>succG60: Success with gscore>0.6</li>
41 * <li>error</li>
42 * <li>recall</li>
43 * <li>precision</li>
44 * <li>fscore</li>
45 * <li>gscore</li>
46 * <li>MCC</li>
47 * <li>AUC</li>
48 * <li>AUCEC (weighted by LOC, if applicable; 0.0 if LOC not available)</li>
49 * <li>tpr: true positive rate</li>
50 * <li>tnr: true negative rate</li>
51 * <li>fpr: false positive rate</li>
52 * <li>fnr: false negative rate</li>
53 * <li>tp: true positives</li>
54 * <li>fp: false positives</li>
55 * <li>tn: true negatives</li>
56 * <li>fn: false negatives</li>
57 * </ul>
58 *
59 * @author Steffen Herbold
60 */
61public abstract class AbstractWekaEvaluation implements IEvaluationStrategy {
62
63    /**
64     * writer for the evaluation results
65     */
66    private PrintWriter output = new PrintWriter(System.out);
67
68    private boolean outputIsSystemOut = true;
69   
70    private String configurationName = "default";
71
72    /**
73     * Creates the Weka evaluator. Allows the creation of the evaluator in different ways, e.g., for
74     * cross-validation or evaluation on the test data.
75     *
76     * @param testdata
77     *            test data
78     * @param classifier
79     *            classifier used
80     * @return evaluator
81     */
82    protected abstract Evaluation createEvaluator(Instances testdata, Classifier classifier);
83
84    /*
85     * (non-Javadoc)
86     *
87     * @see de.ugoe.cs.cpdp.eval.EvaluationStrategy#apply(weka.core.Instances, weka.core.Instances,
88     * java.util.List, boolean)
89     */
90    @Override
91    public void apply(Instances testdata,
92                      Instances traindata,
93                      List<ITrainer> trainers,
94                      boolean writeHeader,
95                      List<IResultStorage> storages)
96    {
97        final List<Classifier> classifiers = new LinkedList<>();
98        final List<ExperimentResult> experimentResults = new LinkedList<>();
99        String productName = testdata.relationName();
100       
101        for (ITrainer trainer : trainers) {
102            if (trainer instanceof IWekaCompatibleTrainer) {
103                classifiers.add(((IWekaCompatibleTrainer) trainer).getClassifier());
104                experimentResults.add(new ExperimentResult(configurationName, productName, ((IWekaCompatibleTrainer) trainer).getName()));
105            }
106            else {
107                throw new RuntimeException("The selected evaluator only support Weka classifiers");
108            }
109        }
110
111        if (writeHeader) {
112            output.append("version,size_test,size_training");
113            for (ITrainer trainer : trainers) {
114                output.append(",succHe_" + ((IWekaCompatibleTrainer) trainer).getName());
115                output.append(",succZi_" + ((IWekaCompatibleTrainer) trainer).getName());
116                output.append(",succG75_" + ((IWekaCompatibleTrainer) trainer).getName());
117                output.append(",succG60_" + ((IWekaCompatibleTrainer) trainer).getName());
118                output.append(",error_" + ((IWekaCompatibleTrainer) trainer).getName());
119                output.append(",recall_" + ((IWekaCompatibleTrainer) trainer).getName());
120                output.append(",precision_" + ((IWekaCompatibleTrainer) trainer).getName());
121                output.append(",fscore_" + ((IWekaCompatibleTrainer) trainer).getName());
122                output.append(",gscore_" + ((IWekaCompatibleTrainer) trainer).getName());
123                output.append(",mcc_" + ((IWekaCompatibleTrainer) trainer).getName());
124                output.append(",auc_" + ((IWekaCompatibleTrainer) trainer).getName());
125                output.append(",aucec_" + ((IWekaCompatibleTrainer) trainer).getName());
126                output.append(",tpr_" + ((IWekaCompatibleTrainer) trainer).getName());
127                output.append(",tnr_" + ((IWekaCompatibleTrainer) trainer).getName());
128                output.append(",fpr_" + ((IWekaCompatibleTrainer) trainer).getName());
129                output.append(",fnr_" + ((IWekaCompatibleTrainer) trainer).getName());
130                output.append(",tp_" + ((IWekaCompatibleTrainer) trainer).getName());
131                output.append(",fn_" + ((IWekaCompatibleTrainer) trainer).getName());
132                output.append(",tn_" + ((IWekaCompatibleTrainer) trainer).getName());
133                output.append(",fp_" + ((IWekaCompatibleTrainer) trainer).getName());
134            }
135            output.append(StringTools.ENDLINE);
136        }
137
138        output.append(productName);
139        output.append("," + testdata.numInstances());
140        output.append("," + traindata.numInstances());
141
142        Evaluation eval = null;
143        Iterator<Classifier> classifierIter = classifiers.iterator();
144        Iterator<ExperimentResult> resultIter = experimentResults.iterator();
145        while (classifierIter.hasNext()) {
146            Classifier classifier = classifierIter.next();
147            eval = createEvaluator(testdata, classifier);
148
149            double pf =
150                eval.numFalsePositives(1) / (eval.numFalsePositives(1) + eval.numTrueNegatives(1));
151            double gmeasure = 2 * eval.recall(1) * (1.0 - pf) / (eval.recall(1) + (1.0 - pf));
152            double aucec = calculateReviewEffort(testdata, classifier);
153            double succHe = eval.recall(1) >= 0.7 && eval.precision(1) >= 0.5 ? 1.0 : 0.0;
154            double succZi = eval.recall(1) >= 0.7 && eval.precision(1) >= 0.7 ? 1.0 : 0.0;
155            double succG75 = gmeasure > 0.75 ? 1.0 : 0.0;
156            double succG60 = gmeasure > 0.6 ? 1.0 : 0.0;
157           
158            output.append("," + succHe);
159            output.append("," + succZi);
160            output.append("," + succG75);
161            output.append("," + succG60);           
162            output.append("," + eval.errorRate());
163            output.append("," + eval.recall(1));
164            output.append("," + eval.precision(1));
165            output.append("," + eval.fMeasure(1));
166            output.append("," + gmeasure);
167            output.append("," + eval.matthewsCorrelationCoefficient(1));
168            output.append("," + eval.areaUnderROC(1));
169            output.append("," + aucec);
170            output.append("," + eval.truePositiveRate(1));
171            output.append("," + eval.trueNegativeRate(1));
172            output.append("," + eval.falsePositiveRate(1));
173            output.append("," + eval.falseNegativeRate(1));
174            output.append("," + eval.numTruePositives(1));
175            output.append("," + eval.numFalseNegatives(1));
176            output.append("," + eval.numTrueNegatives(1));
177            output.append("," + eval.numFalsePositives(1));
178           
179            ExperimentResult result = resultIter.next();
180            result.setSizeTestData(testdata.numInstances());
181            result.setSizeTrainingData(traindata.numInstances());
182            result.setSuccHe(succHe);
183            result.setSuccZi(succZi);
184            result.setSuccG75(succG75);
185            result.setSuccG60(succG60);
186            result.setError(eval.errorRate());
187            result.setRecall(eval.recall(1));
188            result.setPrecision(eval.precision(1));
189            result.setFscore(eval.fMeasure(1));
190            result.setGscore(gmeasure);
191            result.setMcc(eval.matthewsCorrelationCoefficient(1));
192            result.setAuc(eval.areaUnderROC(1));
193            result.setAucec(aucec);
194            result.setTpr(eval.truePositiveRate(1));
195            result.setTnr(eval.trueNegativeRate(1));
196            result.setFpr(eval.falsePositiveRate(1));
197            result.setFnr(eval.falseNegativeRate(1));
198            result.setTp(eval.numTruePositives(1));
199            result.setFn(eval.numFalseNegatives(1));
200            result.setTn(eval.numTrueNegatives(1));
201            result.setFp(eval.numFalsePositives(1));
202            for( IResultStorage storage : storages ) {
203                storage.addResult(result);
204            }
205        }
206
207        output.append(StringTools.ENDLINE);
208        output.flush();
209    }
210
211    private double calculateReviewEffort(Instances testdata, Classifier classifier) {
212
213        Attribute loc = testdata.attribute("loc");
214        if (loc == null) {
215            loc = testdata.attribute("LOC_EXECUTABLE");
216        }
217        if (loc == null) {
218            loc = testdata.attribute("numberOfLinesOfCode");
219        }
220        if( loc == null ) {
221            return 0.0;
222        }
223
224        final List<Integer> bugPredicted = new ArrayList<>();
225        final List<Integer> nobugPredicted = new ArrayList<>();
226        double totalLoc = 0.0d;
227        int totalBugs = 0;
228        for (int i = 0; i < testdata.numInstances(); i++) {
229            try {
230                if (Double.compare(classifier.classifyInstance(testdata.instance(i)), 0.0d) == 0) {
231                    nobugPredicted.add(i);
232                }
233                else {
234                    bugPredicted.add(i);
235                }
236            }
237            catch (Exception e) {
238                throw new RuntimeException(
239                                           "unexpected error during the evaluation of the review effort",
240                                           e);
241            }
242            if (Double.compare(testdata.instance(i).classValue(), 1.0d) == 0) {
243                totalBugs++;
244            }
245            totalLoc += testdata.instance(i).value(loc);
246        }
247
248        final List<Double> reviewLoc = new ArrayList<>(testdata.numInstances());
249        final List<Double> bugsFound = new ArrayList<>(testdata.numInstances());
250
251        double currentBugsFound = 0;
252
253        while (!bugPredicted.isEmpty()) {
254            double minLoc = Double.MAX_VALUE;
255            int minIndex = -1;
256            for (int i = 0; i < bugPredicted.size(); i++) {
257                double currentLoc = testdata.instance(bugPredicted.get(i)).value(loc);
258                if (currentLoc < minLoc) {
259                    minIndex = i;
260                    minLoc = currentLoc;
261                }
262            }
263            if (minIndex != -1) {
264                reviewLoc.add(minLoc / totalLoc);
265
266                currentBugsFound += testdata.instance(bugPredicted.get(minIndex)).classValue();
267                bugsFound.add(currentBugsFound);
268
269                bugPredicted.remove(minIndex);
270            }
271            else {
272                throw new RuntimeException("Shouldn't happen!");
273            }
274        }
275
276        while (!nobugPredicted.isEmpty()) {
277            double minLoc = Double.MAX_VALUE;
278            int minIndex = -1;
279            for (int i = 0; i < nobugPredicted.size(); i++) {
280                double currentLoc = testdata.instance(nobugPredicted.get(i)).value(loc);
281                if (currentLoc < minLoc) {
282                    minIndex = i;
283                    minLoc = currentLoc;
284                }
285            }
286            if (minIndex != -1) {
287                reviewLoc.add(minLoc / totalLoc);
288
289                currentBugsFound += testdata.instance(nobugPredicted.get(minIndex)).classValue();
290                bugsFound.add(currentBugsFound);
291                nobugPredicted.remove(minIndex);
292            }
293            else {
294                throw new RuntimeException("Shouldn't happen!");
295            }
296        }
297
298        double auc = 0.0;
299        for (int i = 0; i < bugsFound.size(); i++) {
300            auc += reviewLoc.get(i) * bugsFound.get(i) / totalBugs;
301        }
302
303        return auc;
304    }
305
306    /*
307     * (non-Javadoc)
308     *
309     * @see de.ugoe.cs.cpdp.Parameterizable#setParameter(java.lang.String)
310     */
311    @Override
312    public void setParameter(String parameters) {
313        if (output != null && !outputIsSystemOut) {
314            output.close();
315        }
316        if ("system.out".equals(parameters) || "".equals(parameters)) {
317            output = new PrintWriter(System.out);
318            outputIsSystemOut = true;
319        }
320        else {
321            try {
322                output = new PrintWriter(new FileOutputStream(parameters));
323                outputIsSystemOut = false;
324                int filenameStart = parameters.lastIndexOf('/')+1;
325                int filenameEnd = parameters.lastIndexOf('.');
326                configurationName = parameters.substring(filenameStart, filenameEnd);
327            }
328            catch (FileNotFoundException e) {
329                throw new RuntimeException(e);
330            }
331        }
332    }
333}
Note: See TracBrowser for help on using the repository browser.