source: trunk/CrossPare/src/de/ugoe/cs/cpdp/execution/CrossProjectExperiment.java @ 42

Last change on this file since 42 was 41, checked in by sherbold, 9 years ago
  • formatted code and added copyrights
  • Property svn:mime-type set to text/plain
File size: 12.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.execution;
16
17import java.io.File;
18import java.util.Collections;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.logging.Level;
22
23import org.apache.commons.collections4.list.SetUniqueList;
24
25import weka.core.Instances;
26import de.ugoe.cs.cpdp.ExperimentConfiguration;
27import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
28import de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy;
29import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
30import de.ugoe.cs.cpdp.dataselection.ISetWiseDataselectionStrategy;
31import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
32import de.ugoe.cs.cpdp.loader.IVersionLoader;
33import de.ugoe.cs.cpdp.training.ISetWiseTrainingStrategy;
34import de.ugoe.cs.cpdp.training.ITrainer;
35import de.ugoe.cs.cpdp.training.ITrainingStrategy;
36import de.ugoe.cs.cpdp.versions.IVersionFilter;
37import de.ugoe.cs.cpdp.versions.SoftwareVersion;
38import de.ugoe.cs.util.console.Console;
39
40/**
41 * Class responsible for executing an experiment according to an {@link ExperimentConfiguration}.
42 * The steps of an experiment are as follows:
43 * <ul>
44 * <li>load the data from the provided data path</li>
45 * <li>filter the data sets according to the provided version filters</li>
46 * <li>execute the following steps for each data sets as test data that is not ignored through the
47 * test version filter:
48 * <ul>
49 * <li>filter the data sets to setup the candidate training data:
50 * <ul>
51 * <li>remove all data sets from the same project</li>
52 * <li>filter all data sets according to the training data filter
53 * </ul>
54 * </li>
55 * <li>apply the setwise preprocessors</li>
56 * <li>apply the setwise data selection algorithms</li>
57 * <li>apply the setwise postprocessors</li>
58 * <li>train the setwise training classifiers</li>
59 * <li>unify all remaining training data into one data set</li>
60 * <li>apply the preprocessors</li>
61 * <li>apply the pointwise data selection algorithms</li>
62 * <li>apply the postprocessors</li>
63 * <li>train the normal classifiers</li>
64 * <li>evaluate the results for all trained classifiers on the training data</li>
65 * </ul>
66 * </li>
67 * </ul>
68 *
69 * Note that this class implements {@link Runnable}, i.e., each experiment can be started in its own
70 * thread.
71 *
72 * @author Steffen Herbold
73 */
74public class CrossProjectExperiment implements IExecutionStrategy {
75
76    /**
77     * configuration of the experiment
78     */
79    private final ExperimentConfiguration config;
80
81    /**
82     * Constructor. Creates a new experiment based on a configuration.
83     *
84     * @param config
85     *            configuration of the experiment
86     */
87    public CrossProjectExperiment(ExperimentConfiguration config) {
88        this.config = config;
89    }
90
91    /**
92     * Executes the experiment with the steps as described in the class comment.
93     *
94     * @see Runnable#run()
95     */
96    @Override
97    public void run() {
98        final List<SoftwareVersion> versions = new LinkedList<>();
99
100        for (IVersionLoader loader : config.getLoaders()) {
101            versions.addAll(loader.load());
102        }
103
104        for (IVersionFilter filter : config.getVersionFilters()) {
105            filter.apply(versions);
106        }
107        boolean writeHeader = true;
108        int versionCount = 1;
109        int testVersionCount = 0;
110
111        for (SoftwareVersion testVersion : versions) {
112            if (isVersion(testVersion, config.getTestVersionFilters())) {
113                testVersionCount++;
114            }
115        }
116
117        // sort versions
118        Collections.sort(versions);
119
120        for (SoftwareVersion testVersion : versions) {
121            if (isVersion(testVersion, config.getTestVersionFilters())) {
122                Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: starting",
123                                                          config.getExperimentName(), versionCount,
124                                                          testVersionCount,
125                                                          testVersion.getVersion()));
126
127                // Setup testdata and training data
128                Instances testdata = testVersion.getInstances();
129                String testProject = testVersion.getProject();
130                SetUniqueList<Instances> traindataSet =
131                    SetUniqueList.setUniqueList(new LinkedList<Instances>());
132                for (SoftwareVersion trainingVersion : versions) {
133                    if (isVersion(trainingVersion, config.getTrainingVersionFilters())) {
134                        if (trainingVersion != testVersion) {
135                            if (!trainingVersion.getProject().equals(testProject)) {
136                                traindataSet.add(trainingVersion.getInstances());
137                            }
138                        }
139                    }
140                }
141
142                for (ISetWiseProcessingStrategy processor : config.getSetWisePreprocessors()) {
143                    Console.traceln(Level.FINE, String
144                        .format("[%s] [%02d/%02d] %s: applying setwise preprocessor %s",
145                                config.getExperimentName(), versionCount, testVersionCount,
146                                testVersion.getVersion(), processor.getClass().getName()));
147                    processor.apply(testdata, traindataSet);
148                }
149                for (ISetWiseDataselectionStrategy dataselector : config.getSetWiseSelectors()) {
150                    Console.traceln(Level.FINE, String
151                        .format("[%s] [%02d/%02d] %s: applying setwise selection %s",
152                                config.getExperimentName(), versionCount, testVersionCount,
153                                testVersion.getVersion(), dataselector.getClass().getName()));
154                    dataselector.apply(testdata, traindataSet);
155                }
156                for (ISetWiseProcessingStrategy processor : config.getSetWisePostprocessors()) {
157                    Console.traceln(Level.FINE, String
158                        .format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
159                                config.getExperimentName(), versionCount, testVersionCount,
160                                testVersion.getVersion(), processor.getClass().getName()));
161                    processor.apply(testdata, traindataSet);
162                }
163                for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) {
164                    Console.traceln(Level.FINE, String
165                        .format("[%s] [%02d/%02d] %s: applying setwise trainer %s",
166                                config.getExperimentName(), versionCount, testVersionCount,
167                                testVersion.getVersion(), setwiseTrainer.getName()));
168                    setwiseTrainer.apply(traindataSet);
169                }
170                Instances traindata = makeSingleTrainingSet(traindataSet);
171                for (IProcessesingStrategy processor : config.getPreProcessors()) {
172                    Console.traceln(Level.FINE, String
173                        .format("[%s] [%02d/%02d] %s: applying preprocessor %s",
174                                config.getExperimentName(), versionCount, testVersionCount,
175                                testVersion.getVersion(), processor.getClass().getName()));
176                    processor.apply(testdata, traindata);
177                }
178                for (IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors())
179                {
180                    Console.traceln(Level.FINE, String
181                        .format("[%s] [%02d/%02d] %s: applying pointwise selection %s",
182                                config.getExperimentName(), versionCount, testVersionCount,
183                                testVersion.getVersion(), dataselector.getClass().getName()));
184                    traindata = dataselector.apply(testdata, traindata);
185                }
186                for (IProcessesingStrategy processor : config.getPostProcessors()) {
187                    Console.traceln(Level.FINE, String
188                        .format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
189                                config.getExperimentName(), versionCount, testVersionCount,
190                                testVersion.getVersion(), processor.getClass().getName()));
191                    processor.apply(testdata, traindata);
192                }
193                for (ITrainingStrategy trainer : config.getTrainers()) {
194                    Console.traceln(Level.FINE, String
195                        .format("[%s] [%02d/%02d] %s: applying trainer %s",
196                                config.getExperimentName(), versionCount, testVersionCount,
197                                testVersion.getVersion(), trainer.getName()));
198                    trainer.apply(traindata);
199                }
200                File resultsDir = new File(config.getResultsPath());
201                if (!resultsDir.exists()) {
202                    resultsDir.mkdir();
203                }
204                for (IEvaluationStrategy evaluator : config.getEvaluators()) {
205                    Console.traceln(Level.FINE, String
206                        .format("[%s] [%02d/%02d] %s: applying evaluator %s",
207                                config.getExperimentName(), versionCount, testVersionCount,
208                                testVersion.getVersion(), evaluator.getClass().getName()));
209                    List<ITrainer> allTrainers = new LinkedList<>();
210                    for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) {
211                        allTrainers.add(setwiseTrainer);
212                    }
213                    for (ITrainingStrategy trainer : config.getTrainers()) {
214                        allTrainers.add(trainer);
215                    }
216                    if (writeHeader) {
217                        evaluator.setParameter(config.getResultsPath() + "/" +
218                            config.getExperimentName() + ".csv");
219                    }
220                    evaluator.apply(testdata, traindata, allTrainers, writeHeader);
221                    writeHeader = false;
222                }
223                Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: finished",
224                                                          config.getExperimentName(), versionCount,
225                                                          testVersionCount,
226                                                          testVersion.getVersion()));
227                versionCount++;
228            }
229        }
230    }
231
232    /**
233     * Helper method that checks if a version passes all filters.
234     *
235     * @param version
236     *            version that is checked
237     * @param filters
238     *            list of the filters
239     * @return true, if the version passes all filters, false otherwise
240     */
241    private boolean isVersion(SoftwareVersion version, List<IVersionFilter> filters) {
242        boolean result = true;
243        for (IVersionFilter filter : filters) {
244            result &= !filter.apply(version);
245        }
246        return result;
247    }
248
249    /**
250     * Helper method that combines a set of Weka {@link Instances} sets into a single
251     * {@link Instances} set.
252     *
253     * @param traindataSet
254     *            set of {@link Instances} to be combines
255     * @return single {@link Instances} set
256     */
257    public static Instances makeSingleTrainingSet(SetUniqueList<Instances> traindataSet) {
258        Instances traindataFull = null;
259        for (Instances traindata : traindataSet) {
260            if (traindataFull == null) {
261                traindataFull = new Instances(traindata);
262            }
263            else {
264                for (int i = 0; i < traindata.numInstances(); i++) {
265                    traindataFull.add(traindata.instance(i));
266                }
267            }
268        }
269        return traindataFull;
270    }
271}
Note: See TracBrowser for help on using the repository browser.