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