| 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>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>
|
|---|
| 49 | * </ul>
|
|---|
| 50 | *
|
|---|
| 51 | * Note that this class implements {@link Runnable}, i.e., each experiment can be started in its own thread.
|
|---|
| 52 | * @author Steffen Herbold
|
|---|
| 53 | */
|
|---|
| 54 | public class CrossProjectExperiment implements IExecutionStrategy {
|
|---|
| 55 |
|
|---|
| 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 | }
|
|---|
| 205 | }
|
|---|