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 |
|
---|
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 | /**
|
---|
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 | */
|
---|
60 | public 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 |
|
---|
104 | // Give the dataset a new name
|
---|
105 | testdata.setRelationName(testVersion.getProject());
|
---|
106 |
|
---|
107 | for (IProcessesingStrategy processor : config.getPreProcessors()) {
|
---|
108 | Console.traceln(Level.FINE, String
|
---|
109 | .format("[%s] [%02d/%02d] %s: applying preprocessor %s",
|
---|
110 | config.getExperimentName(), versionCount, versions.size(),
|
---|
111 | testVersion.getProject(), processor.getClass().getName()));
|
---|
112 | processor.apply(testdata, traindata);
|
---|
113 | }
|
---|
114 |
|
---|
115 | for (IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors()) {
|
---|
116 | Console.traceln(Level.FINE, String
|
---|
117 | .format("[%s] [%02d/%02d] %s: applying pointwise selection %s",
|
---|
118 | config.getExperimentName(), versionCount, versions.size(),
|
---|
119 | testVersion.getProject(), dataselector.getClass().getName()));
|
---|
120 | traindata = dataselector.apply(testdata, traindata);
|
---|
121 | }
|
---|
122 |
|
---|
123 | for (IProcessesingStrategy processor : config.getPostProcessors()) {
|
---|
124 | Console.traceln(Level.FINE, String
|
---|
125 | .format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
|
---|
126 | config.getExperimentName(), versionCount, versions.size(),
|
---|
127 | testVersion.getProject(), processor.getClass().getName()));
|
---|
128 | processor.apply(testdata, traindata);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // Trainerlist for evaluation later on
|
---|
132 | List<ITrainer> allTrainers = new LinkedList<>();
|
---|
133 |
|
---|
134 | for (ITrainingStrategy trainer : config.getTrainers()) {
|
---|
135 |
|
---|
136 | // Add trainer to list for evaluation
|
---|
137 | allTrainers.add(trainer);
|
---|
138 |
|
---|
139 | // Train classifier
|
---|
140 | trainer.apply(traindata);
|
---|
141 |
|
---|
142 | if (config.getSaveClassifier()) {
|
---|
143 | // If classifier should be saved, train him and save him
|
---|
144 | // be careful with typecasting here!
|
---|
145 | IWekaCompatibleTrainer trainerToSave = (IWekaCompatibleTrainer) trainer;
|
---|
146 | // Console.println(trainerToSave.getClassifier().toString());
|
---|
147 | try {
|
---|
148 | weka.core.SerializationHelper.write(resultsDir.getAbsolutePath() + "/" +
|
---|
149 | trainer.getName() + "-" +
|
---|
150 | testVersion.getProject(),
|
---|
151 | trainerToSave.getClassifier());
|
---|
152 | }
|
---|
153 | catch (Exception e) {
|
---|
154 | e.printStackTrace();
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | for (IEvaluationStrategy evaluator : config.getEvaluators()) {
|
---|
161 | Console.traceln(Level.FINE, String
|
---|
162 | .format("[%s] [%02d/%02d] %s: applying evaluator %s",
|
---|
163 | config.getExperimentName(), versionCount, versions.size(),
|
---|
164 | testVersion.getProject(), evaluator.getClass().getName()));
|
---|
165 |
|
---|
166 | if (writeHeader) {
|
---|
167 | evaluator.setParameter(config.getResultsPath() + "/" +
|
---|
168 | config.getExperimentName() + ".csv");
|
---|
169 | }
|
---|
170 | evaluator.apply(testdata, traindata, allTrainers, writeHeader, config.getResultStorages());
|
---|
171 | writeHeader = false;
|
---|
172 | }
|
---|
173 |
|
---|
174 | versionCount++;
|
---|
175 |
|
---|
176 | Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: finished",
|
---|
177 | config.getExperimentName(), versionCount,
|
---|
178 | versions.size(), testVersion.getProject()));
|
---|
179 |
|
---|
180 | }
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | }
|
---|