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

Last change on this file since 143 was 135, checked in by sherbold, 8 years ago
  • code documentation and formatting
  • Property svn:mime-type set to text/plain
File size: 7.8 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,
110                                String.format("[%s] [%02d/%02d] %s: applying preprocessor %s",
111                                              config.getExperimentName(), versionCount,
112                                              versions.size(), testVersion.getProject(),
113                                              processor.getClass().getName()));
114                processor.apply(testdata, traindata);
115            }
116
117            for (IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors()) {
118                Console
119                    .traceln(Level.FINE,
120                             String.format("[%s] [%02d/%02d] %s: applying pointwise selection %s",
121                                           config.getExperimentName(), versionCount,
122                                           versions.size(), testVersion.getProject(),
123                                           dataselector.getClass().getName()));
124                traindata = dataselector.apply(testdata, traindata);
125            }
126
127            for (IProcessesingStrategy processor : config.getPostProcessors()) {
128                Console
129                    .traceln(Level.FINE,
130                             String.format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
131                                           config.getExperimentName(), versionCount,
132                                           versions.size(), testVersion.getProject(),
133                                           processor.getClass().getName()));
134                processor.apply(testdata, traindata);
135            }
136
137            // Trainerlist for evaluation later on
138            List<ITrainer> allTrainers = new LinkedList<>();
139
140            for (ITrainingStrategy trainer : config.getTrainers()) {
141
142                // Add trainer to list for evaluation
143                allTrainers.add(trainer);
144
145                // Train classifier
146                trainer.apply(traindata);
147
148                if (config.getSaveClassifier()) {
149                    // If classifier should be saved, train him and save him
150                    // be careful with typecasting here!
151                    IWekaCompatibleTrainer trainerToSave = (IWekaCompatibleTrainer) trainer;
152                    // Console.println(trainerToSave.getClassifier().toString());
153                    try {
154                        weka.core.SerializationHelper.write(resultsDir.getAbsolutePath() + "/" +
155                            trainer.getName() + "-" + testVersion.getProject(),
156                                                            trainerToSave.getClassifier());
157                    }
158                    catch (Exception e) {
159                        e.printStackTrace();
160                    }
161
162                }
163            }
164
165            for (IEvaluationStrategy evaluator : config.getEvaluators()) {
166                Console.traceln(Level.FINE,
167                                String.format("[%s] [%02d/%02d] %s: applying evaluator %s",
168                                              config.getExperimentName(), versionCount,
169                                              versions.size(), testVersion.getProject(),
170                                              evaluator.getClass().getName()));
171
172                if (writeHeader) {
173                    evaluator.setParameter(config.getResultsPath() + "/" +
174                        config.getExperimentName() + ".csv");
175                }
176                evaluator.apply(testdata, traindata, allTrainers, efforts, writeHeader,
177                                config.getResultStorages());
178                writeHeader = false;
179            }
180
181            versionCount++;
182
183            Console.traceln(Level.INFO,
184                            String.format("[%s] [%02d/%02d] %s: finished",
185                                          config.getExperimentName(), versionCount, versions.size(),
186                                          testVersion.getProject()));
187
188        }
189
190    }
191
192}
Note: See TracBrowser for help on using the repository browser.