[86] | 1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany
|
---|
[41] | 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 |
|
---|
[32] | 15 | package de.ugoe.cs.cpdp.execution;
|
---|
| 16 |
|
---|
| 17 | import java.io.File;
|
---|
| 18 | import java.util.LinkedList;
|
---|
| 19 | import java.util.List;
|
---|
| 20 | import java.util.logging.Level;
|
---|
| 21 |
|
---|
| 22 | import weka.core.Instances;
|
---|
| 23 | import de.ugoe.cs.cpdp.ExperimentConfiguration;
|
---|
| 24 | import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
|
---|
| 25 | import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
|
---|
| 26 | import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
|
---|
| 27 | import de.ugoe.cs.cpdp.loader.IVersionLoader;
|
---|
| 28 | import de.ugoe.cs.cpdp.training.ITrainer;
|
---|
| 29 | import de.ugoe.cs.cpdp.training.ITrainingStrategy;
|
---|
| 30 | import de.ugoe.cs.cpdp.training.IWekaCompatibleTrainer;
|
---|
| 31 | import de.ugoe.cs.cpdp.versions.SoftwareVersion;
|
---|
| 32 | import de.ugoe.cs.util.console.Console;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
[41] | 35 | * Class responsible for executing an experiment according to an {@link ExperimentConfiguration}.
|
---|
| 36 | * The steps of this ClassifierCreationExperiment are as follows:
|
---|
[32] | 37 | * <ul>
|
---|
[41] | 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>
|
---|
[32] | 51 | * </ul>
|
---|
[41] | 52 | * </ul>
|
---|
| 53 | * </ul>
|
---|
| 54 | *
|
---|
| 55 | * Note that this class implements {@link IExectuionStrategy}, i.e., each experiment can be started
|
---|
[32] | 56 | * in its own thread.
|
---|
| 57 | *
|
---|
| 58 | * @author Fabian Trautsch
|
---|
| 59 | */
|
---|
| 60 | public class ClassifierCreationExperiment implements IExecutionStrategy {
|
---|
| 61 |
|
---|
[41] | 62 | /**
|
---|
| 63 | * configuration of the experiment
|
---|
| 64 | */
|
---|
| 65 | private final ExperimentConfiguration config;
|
---|
[32] | 66 |
|
---|
[41] | 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 | }
|
---|
[32] | 76 |
|
---|
[41] | 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<>();
|
---|
[32] | 85 |
|
---|
[41] | 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);
|
---|
[132] | 103 | List<Double> efforts = testVersion.getEfforts();
|
---|
[41] | 104 |
|
---|
| 105 | // Give the dataset a new name
|
---|
| 106 | testdata.setRelationName(testVersion.getProject());
|
---|
| 107 |
|
---|
| 108 | for (IProcessesingStrategy processor : config.getPreProcessors()) {
|
---|
[135] | 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()));
|
---|
[41] | 114 | processor.apply(testdata, traindata);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | for (IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors()) {
|
---|
[135] | 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()));
|
---|
[41] | 124 | traindata = dataselector.apply(testdata, traindata);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | for (IProcessesingStrategy processor : config.getPostProcessors()) {
|
---|
[135] | 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()));
|
---|
[41] | 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() + "/" +
|
---|
[135] | 155 | trainer.getName() + "-" + testVersion.getProject(),
|
---|
[41] | 156 | trainerToSave.getClassifier());
|
---|
| 157 | }
|
---|
| 158 | catch (Exception e) {
|
---|
| 159 | e.printStackTrace();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | for (IEvaluationStrategy evaluator : config.getEvaluators()) {
|
---|
[135] | 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()));
|
---|
[41] | 171 |
|
---|
| 172 | if (writeHeader) {
|
---|
| 173 | evaluator.setParameter(config.getResultsPath() + "/" +
|
---|
| 174 | config.getExperimentName() + ".csv");
|
---|
| 175 | }
|
---|
[135] | 176 | evaluator.apply(testdata, traindata, allTrainers, efforts, writeHeader,
|
---|
| 177 | config.getResultStorages());
|
---|
[41] | 178 | writeHeader = false;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | versionCount++;
|
---|
| 182 |
|
---|
[135] | 183 | Console.traceln(Level.INFO,
|
---|
| 184 | String.format("[%s] [%02d/%02d] %s: finished",
|
---|
| 185 | config.getExperimentName(), versionCount, versions.size(),
|
---|
| 186 | testVersion.getProject()));
|
---|
[41] | 187 |
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[32] | 192 | }
|
---|