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

Last change on this file since 76 was 68, checked in by sherbold, 9 years ago
  • added the concept of result storages to the framework and implemented a very simple first prototype of a MySQLResultStorage (that currently only works with a locally running database)
  • Property svn:mime-type set to text/plain
File size: 13.5 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        final Attribute loc = testdata.attribute("loc");
214        if (loc == null) {
215            return 0.0;
216        }
217
218        final List<Integer> bugPredicted = new ArrayList<>();
219        final List<Integer> nobugPredicted = new ArrayList<>();
220        double totalLoc = 0.0d;
221        int totalBugs = 0;
222        for (int i = 0; i < testdata.numInstances(); i++) {
223            try {
224                if (Double.compare(classifier.classifyInstance(testdata.instance(i)), 0.0d) == 0) {
225                    nobugPredicted.add(i);
226                }
227                else {
228                    bugPredicted.add(i);
229                }
230            }
231            catch (Exception e) {
232                throw new RuntimeException(
233                                           "unexpected error during the evaluation of the review effort",
234                                           e);
235            }
236            if (Double.compare(testdata.instance(i).classValue(), 1.0d) == 0) {
237                totalBugs++;
238            }
239            totalLoc += testdata.instance(i).value(loc);
240        }
241
242        final List<Double> reviewLoc = new ArrayList<>(testdata.numInstances());
243        final List<Double> bugsFound = new ArrayList<>(testdata.numInstances());
244
245        double currentBugsFound = 0;
246
247        while (!bugPredicted.isEmpty()) {
248            double minLoc = Double.MAX_VALUE;
249            int minIndex = -1;
250            for (int i = 0; i < bugPredicted.size(); i++) {
251                double currentLoc = testdata.instance(bugPredicted.get(i)).value(loc);
252                if (currentLoc < minLoc) {
253                    minIndex = i;
254                    minLoc = currentLoc;
255                }
256            }
257            if (minIndex != -1) {
258                reviewLoc.add(minLoc / totalLoc);
259
260                currentBugsFound += testdata.instance(bugPredicted.get(minIndex)).classValue();
261                bugsFound.add(currentBugsFound);
262
263                bugPredicted.remove(minIndex);
264            }
265            else {
266                throw new RuntimeException("Shouldn't happen!");
267            }
268        }
269
270        while (!nobugPredicted.isEmpty()) {
271            double minLoc = Double.MAX_VALUE;
272            int minIndex = -1;
273            for (int i = 0; i < nobugPredicted.size(); i++) {
274                double currentLoc = testdata.instance(nobugPredicted.get(i)).value(loc);
275                if (currentLoc < minLoc) {
276                    minIndex = i;
277                    minLoc = currentLoc;
278                }
279            }
280            if (minIndex != -1) {
281                reviewLoc.add(minLoc / totalLoc);
282
283                currentBugsFound += testdata.instance(nobugPredicted.get(minIndex)).classValue();
284                bugsFound.add(currentBugsFound);
285                nobugPredicted.remove(minIndex);
286            }
287            else {
288                throw new RuntimeException("Shouldn't happen!");
289            }
290        }
291
292        double auc = 0.0;
293        for (int i = 0; i < bugsFound.size(); i++) {
294            auc += reviewLoc.get(i) * bugsFound.get(i) / totalBugs;
295        }
296
297        return auc;
298    }
299
300    /*
301     * (non-Javadoc)
302     *
303     * @see de.ugoe.cs.cpdp.Parameterizable#setParameter(java.lang.String)
304     */
305    @Override
306    public void setParameter(String parameters) {
307        if (output != null && !outputIsSystemOut) {
308            output.close();
309        }
310        if ("system.out".equals(parameters) || "".equals(parameters)) {
311            output = new PrintWriter(System.out);
312            outputIsSystemOut = true;
313        }
314        else {
315            try {
316                output = new PrintWriter(new FileOutputStream(parameters));
317                outputIsSystemOut = false;
318                int filenameStart = parameters.lastIndexOf('/')+1;
319                int filenameEnd = parameters.lastIndexOf('.');
320                configurationName = parameters.substring(filenameStart, filenameEnd);
321            }
322            catch (FileNotFoundException e) {
323                throw new RuntimeException(e);
324            }
325        }
326    }
327}
Note: See TracBrowser for help on using the repository browser.