1 | package de.ugoe.cs.cpdp.execution;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.logging.Level;
|
---|
8 |
|
---|
9 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
10 |
|
---|
11 | import weka.core.Instances;
|
---|
12 | import de.ugoe.cs.cpdp.ExperimentConfiguration;
|
---|
13 | import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
|
---|
14 | import de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy;
|
---|
15 | import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
|
---|
16 | import de.ugoe.cs.cpdp.dataselection.ISetWiseDataselectionStrategy;
|
---|
17 | import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
|
---|
18 | import de.ugoe.cs.cpdp.loader.IVersionLoader;
|
---|
19 | import de.ugoe.cs.cpdp.training.ISetWiseTrainingStrategy;
|
---|
20 | import de.ugoe.cs.cpdp.training.ITrainer;
|
---|
21 | import de.ugoe.cs.cpdp.training.ITrainingStrategy;
|
---|
22 | import de.ugoe.cs.cpdp.versions.IVersionFilter;
|
---|
23 | import de.ugoe.cs.cpdp.versions.SoftwareVersion;
|
---|
24 | import de.ugoe.cs.util.console.Console;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Class responsible for executing an experiment according to an {@link ExperimentConfiguration}. The steps of an experiment are as follows:
|
---|
28 | * <ul>
|
---|
29 | * <li>load the data from the provided data path</li>
|
---|
30 | * <li>filter the data sets according to the provided version filters</li>
|
---|
31 | * <li>execute the following steps for each data sets as test data that is not ignored through the test version filter:
|
---|
32 | * <ul>
|
---|
33 | * <li>filter the data sets to setup the candidate training data:
|
---|
34 | * <ul>
|
---|
35 | * <li>filter all data sets according to the training data filter
|
---|
36 | * </ul></li>
|
---|
37 | * <li>apply the setwise preprocessors</li>
|
---|
38 | * <li>apply the setwise data selection algorithms</li>
|
---|
39 | * <li>apply the setwise postprocessors</li>
|
---|
40 | * <li>train the setwise training classifiers</li>
|
---|
41 | * <li>unify all remaining training data into one data set</li>
|
---|
42 | * <li>apply the preprocessors</li>
|
---|
43 | * <li>apply the pointwise data selection algorithms</li>
|
---|
44 | * <li>apply the postprocessors</li>
|
---|
45 | * <li>train the normal classifiers</li>
|
---|
46 | * <li>evaluate the results for all trained classifiers on the training data</li>
|
---|
47 | * </ul></li>
|
---|
48 | * </ul>
|
---|
49 | *
|
---|
50 | * Note that this class implements {@link Runnable}, i.e., each experiment can be started in its own thread.
|
---|
51 | * @author Steffen Herbold
|
---|
52 | */
|
---|
53 | public class RelaxedCrossProjectExperiment implements IExecutionStrategy {
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * configuration of the experiment
|
---|
57 | */
|
---|
58 | private final ExperimentConfiguration config;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Constructor. Creates a new experiment based on a configuration.
|
---|
62 | * @param config configuration of the experiment
|
---|
63 | */
|
---|
64 | public RelaxedCrossProjectExperiment(ExperimentConfiguration config) {
|
---|
65 | this.config = config;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Executes the experiment with the steps as described in the class comment.
|
---|
70 | * @see Runnable#run()
|
---|
71 | */
|
---|
72 | @Override
|
---|
73 | public void run() {
|
---|
74 | final List<SoftwareVersion> versions = new LinkedList<>();
|
---|
75 |
|
---|
76 | for(IVersionLoader loader : config.getLoaders()) {
|
---|
77 | versions.addAll(loader.load());
|
---|
78 | }
|
---|
79 |
|
---|
80 | for( IVersionFilter filter : config.getVersionFilters() ) {
|
---|
81 | filter.apply(versions);
|
---|
82 | }
|
---|
83 | boolean writeHeader = true;
|
---|
84 | int versionCount = 1;
|
---|
85 | int testVersionCount = 0;
|
---|
86 |
|
---|
87 | for( SoftwareVersion testVersion : versions ) {
|
---|
88 | if( isVersion(testVersion, config.getTestVersionFilters()) ) {
|
---|
89 | testVersionCount++;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | // sort versions
|
---|
94 | Collections.sort(versions);
|
---|
95 |
|
---|
96 | for( SoftwareVersion testVersion : versions ) {
|
---|
97 | if( isVersion(testVersion, config.getTestVersionFilters()) ) {
|
---|
98 | Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: starting", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion()));
|
---|
99 |
|
---|
100 | // Setup testdata and training data
|
---|
101 | Instances testdata = testVersion.getInstances();
|
---|
102 | String testProject = testVersion.getProject();
|
---|
103 | SetUniqueList<Instances> traindataSet = SetUniqueList.setUniqueList(new LinkedList<Instances>());
|
---|
104 | for( SoftwareVersion trainingVersion : versions ) {
|
---|
105 | if( isVersion(trainingVersion, config.getTrainingVersionFilters()) ) {
|
---|
106 | if( trainingVersion!=testVersion ) {
|
---|
107 | if( trainingVersion.getProject().equals(testProject) ) {
|
---|
108 | if( trainingVersion.compareTo(testVersion)<0 ) {
|
---|
109 | // only add if older
|
---|
110 | traindataSet.add(trainingVersion.getInstances());
|
---|
111 | }
|
---|
112 | } else {
|
---|
113 | traindataSet.add(trainingVersion.getInstances());
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | for( ISetWiseProcessingStrategy processor : config.getSetWisePreprocessors() ) {
|
---|
120 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying setwise preprocessor %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), processor.getClass().getName()));
|
---|
121 | processor.apply(testdata, traindataSet);
|
---|
122 | }
|
---|
123 | for( ISetWiseDataselectionStrategy dataselector : config.getSetWiseSelectors() ) {
|
---|
124 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying setwise selection %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), dataselector.getClass().getName()));
|
---|
125 | dataselector.apply(testdata, traindataSet);
|
---|
126 | }
|
---|
127 | for( ISetWiseProcessingStrategy processor : config.getSetWisePostprocessors() ) {
|
---|
128 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), processor.getClass().getName()));
|
---|
129 | processor.apply(testdata, traindataSet);
|
---|
130 | }
|
---|
131 | for( ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers() ) {
|
---|
132 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying setwise trainer %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), setwiseTrainer.getName()));
|
---|
133 | setwiseTrainer.apply(traindataSet);
|
---|
134 | }
|
---|
135 | Instances traindata = makeSingleTrainingSet(traindataSet);
|
---|
136 | for( IProcessesingStrategy processor : config.getPreProcessors() ) {
|
---|
137 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying preprocessor %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), processor.getClass().getName()));
|
---|
138 | processor.apply(testdata, traindata);
|
---|
139 | }
|
---|
140 | for( IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors() ) {
|
---|
141 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying pointwise selection %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), dataselector.getClass().getName()));
|
---|
142 | traindata = dataselector.apply(testdata, traindata);
|
---|
143 | }
|
---|
144 | for( IProcessesingStrategy processor : config.getPostProcessors() ) {
|
---|
145 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), processor.getClass().getName()));
|
---|
146 | processor.apply(testdata, traindata);
|
---|
147 | }
|
---|
148 | for( ITrainingStrategy trainer : config.getTrainers() ) {
|
---|
149 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying trainer %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), trainer.getName()));
|
---|
150 | trainer.apply(traindata);
|
---|
151 | }
|
---|
152 | File resultsDir = new File(config.getResultsPath());
|
---|
153 | if (!resultsDir.exists()) {
|
---|
154 | resultsDir.mkdir();
|
---|
155 | }
|
---|
156 | for( IEvaluationStrategy evaluator : config.getEvaluators() ) {
|
---|
157 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying evaluator %s", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion(), evaluator.getClass().getName()));
|
---|
158 | List<ITrainer> allTrainers = new LinkedList<>();
|
---|
159 | for( ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers() ) {
|
---|
160 | allTrainers.add(setwiseTrainer);
|
---|
161 | }
|
---|
162 | for( ITrainingStrategy trainer : config.getTrainers() ) {
|
---|
163 | allTrainers.add(trainer);
|
---|
164 | }
|
---|
165 | if( writeHeader ) {
|
---|
166 | evaluator.setParameter(config.getResultsPath() + "/" + config.getExperimentName() + ".csv");
|
---|
167 | }
|
---|
168 | evaluator.apply(testdata, traindata, allTrainers, writeHeader);
|
---|
169 | writeHeader = false;
|
---|
170 | }
|
---|
171 | Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: finished", config.getExperimentName(), versionCount, testVersionCount, testVersion.getVersion()));
|
---|
172 | versionCount++;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Helper method that checks if a version passes all filters.
|
---|
179 | * @param version version that is checked
|
---|
180 | * @param filters list of the filters
|
---|
181 | * @return true, if the version passes all filters, false otherwise
|
---|
182 | */
|
---|
183 | private boolean isVersion(SoftwareVersion version, List<IVersionFilter> filters) {
|
---|
184 | boolean result = true;
|
---|
185 | for( IVersionFilter filter : filters) {
|
---|
186 | result &= !filter.apply(version);
|
---|
187 | }
|
---|
188 | return result;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Helper method that combines a set of Weka {@link Instances} sets into a single {@link Instances} set.
|
---|
193 | * @param traindataSet set of {@link Instances} to be combines
|
---|
194 | * @return single {@link Instances} set
|
---|
195 | */
|
---|
196 | public static Instances makeSingleTrainingSet(SetUniqueList<Instances> traindataSet) {
|
---|
197 | Instances traindataFull = null;
|
---|
198 | for( Instances traindata : traindataSet) {
|
---|
199 | if( traindataFull==null ) {
|
---|
200 | traindataFull = new Instances(traindata);
|
---|
201 | } else {
|
---|
202 | for( int i=0 ; i<traindata.numInstances() ; i++ ) {
|
---|
203 | traindataFull.add(traindata.instance(i));
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | return traindataFull;
|
---|
208 | }
|
---|
209 | }
|
---|