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