source: trunk/CrossPare/src/de/ugoe/cs/cpdp/execution/ClassifierCreationExperiment.java @ 132

Last change on this file since 132 was 132, checked in by sherbold, 8 years ago
  • rather intrusive and large change to correctly evaluate AUCEC in case metrics are modified. The effort is now stored directly with a software version and it is the duty of the loader to specify the review effort for each instance. This required changes to the execution strategiey, data loading, and evaluation process.
  • Property svn:mime-type set to text/plain
File size: 7.4 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.execution;
16
17import java.io.File;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.logging.Level;
21
22import weka.core.Instances;
23import de.ugoe.cs.cpdp.ExperimentConfiguration;
24import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
25import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
26import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
27import de.ugoe.cs.cpdp.loader.IVersionLoader;
28import de.ugoe.cs.cpdp.training.ITrainer;
29import de.ugoe.cs.cpdp.training.ITrainingStrategy;
30import de.ugoe.cs.cpdp.training.IWekaCompatibleTrainer;
31import de.ugoe.cs.cpdp.versions.SoftwareVersion;
32import de.ugoe.cs.util.console.Console;
33
34/**
35 * Class responsible for executing an experiment according to an {@link ExperimentConfiguration}.
36 * The steps of this ClassifierCreationExperiment are as follows:
37 * <ul>
38 * <li>load the data from the provided data path</li>
39 * <li>check if given resultsdir exists, if not create one</li>
40 * <li>execute the following steps for each data set:
41 * <ul>
42 * <li>load the dataset</li>
43 * <li>set testdata == traindata</li>
44 * <li>preprocess the data</li>
45 * <li>postprocess the data</li>
46 * <li>for each configured trainer do the following:</li>
47 * <ul>
48 * <li>if the classifier should be saved, train it with the dataset</li>
49 * <li>save it in the results dir</li>
50 * <li>For each configured evaluator: Do the evaluation and save results</li>
51 * </ul>
52 * </ul>
53 * </ul>
54 *
55 * Note that this class implements {@link IExectuionStrategy}, i.e., each experiment can be started
56 * in its own thread.
57 *
58 * @author Fabian Trautsch
59 */
60public class ClassifierCreationExperiment implements IExecutionStrategy {
61
62    /**
63     * configuration of the experiment
64     */
65    private final ExperimentConfiguration config;
66
67    /**
68     * Constructor. Creates a new experiment based on a configuration.
69     *
70     * @param config
71     *            configuration of the experiment
72     */
73    public ClassifierCreationExperiment(ExperimentConfiguration config) {
74        this.config = config;
75    }
76
77    /**
78     * Executes the experiment with the steps as described in the class comment.
79     *
80     * @see Runnable#run()
81     */
82    @Override
83    public void run() {
84        final List<SoftwareVersion> versions = new LinkedList<>();
85
86        boolean writeHeader = true;
87
88        for (IVersionLoader loader : config.getLoaders()) {
89            versions.addAll(loader.load());
90        }
91
92        File resultsDir = new File(config.getResultsPath());
93        if (!resultsDir.exists()) {
94            resultsDir.mkdir();
95        }
96
97        int versionCount = 1;
98        for (SoftwareVersion testVersion : versions) {
99
100            // At first: traindata == testdata
101            Instances testdata = testVersion.getInstances();
102            Instances traindata = new Instances(testdata);
103            List<Double> efforts = testVersion.getEfforts();
104
105            // Give the dataset a new name
106            testdata.setRelationName(testVersion.getProject());
107
108            for (IProcessesingStrategy processor : config.getPreProcessors()) {
109                Console.traceln(Level.FINE, String
110                    .format("[%s] [%02d/%02d] %s: applying preprocessor %s",
111                            config.getExperimentName(), versionCount, versions.size(),
112                            testVersion.getProject(), processor.getClass().getName()));
113                processor.apply(testdata, traindata);
114            }
115
116            for (IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors()) {
117                Console.traceln(Level.FINE, String
118                    .format("[%s] [%02d/%02d] %s: applying pointwise selection %s",
119                            config.getExperimentName(), versionCount, versions.size(),
120                            testVersion.getProject(), dataselector.getClass().getName()));
121                traindata = dataselector.apply(testdata, traindata);
122            }
123
124            for (IProcessesingStrategy processor : config.getPostProcessors()) {
125                Console.traceln(Level.FINE, String
126                    .format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
127                            config.getExperimentName(), versionCount, versions.size(),
128                            testVersion.getProject(), processor.getClass().getName()));
129                processor.apply(testdata, traindata);
130            }
131
132            // Trainerlist for evaluation later on
133            List<ITrainer> allTrainers = new LinkedList<>();
134
135            for (ITrainingStrategy trainer : config.getTrainers()) {
136
137                // Add trainer to list for evaluation
138                allTrainers.add(trainer);
139
140                // Train classifier
141                trainer.apply(traindata);
142
143                if (config.getSaveClassifier()) {
144                    // If classifier should be saved, train him and save him
145                    // be careful with typecasting here!
146                    IWekaCompatibleTrainer trainerToSave = (IWekaCompatibleTrainer) trainer;
147                    // Console.println(trainerToSave.getClassifier().toString());
148                    try {
149                        weka.core.SerializationHelper.write(resultsDir.getAbsolutePath() + "/" +
150                                                                trainer.getName() + "-" +
151                                                                testVersion.getProject(),
152                                                            trainerToSave.getClassifier());
153                    }
154                    catch (Exception e) {
155                        e.printStackTrace();
156                    }
157
158                }
159            }
160
161            for (IEvaluationStrategy evaluator : config.getEvaluators()) {
162                Console.traceln(Level.FINE, String
163                    .format("[%s] [%02d/%02d] %s: applying evaluator %s",
164                            config.getExperimentName(), versionCount, versions.size(),
165                            testVersion.getProject(), evaluator.getClass().getName()));
166
167                if (writeHeader) {
168                    evaluator.setParameter(config.getResultsPath() + "/" +
169                        config.getExperimentName() + ".csv");
170                }
171                evaluator.apply(testdata, traindata, allTrainers, efforts, writeHeader, config.getResultStorages());
172                writeHeader = false;
173            }
174
175            versionCount++;
176
177            Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: finished",
178                                                      config.getExperimentName(), versionCount,
179                                                      versions.size(), testVersion.getProject()));
180
181        }
182
183    }
184
185}
Note: See TracBrowser for help on using the repository browser.