| 1 | package de.ugoe.cs.cpdp.execution;
|
|---|
| 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 weka.core.Instances;
|
|---|
| 9 | import de.ugoe.cs.cpdp.ExperimentConfiguration;
|
|---|
| 10 | import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
|
|---|
| 11 | import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
|
|---|
| 12 | import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
|
|---|
| 13 | import de.ugoe.cs.cpdp.loader.IVersionLoader;
|
|---|
| 14 | import de.ugoe.cs.cpdp.training.ITrainer;
|
|---|
| 15 | import de.ugoe.cs.cpdp.training.ITrainingStrategy;
|
|---|
| 16 | import de.ugoe.cs.cpdp.training.IWekaCompatibleTrainer;
|
|---|
| 17 | import de.ugoe.cs.cpdp.versions.SoftwareVersion;
|
|---|
| 18 | import de.ugoe.cs.util.console.Console;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Class responsible for executing an experiment according to an {@link ExperimentConfiguration}. The steps
|
|---|
| 22 | * of this ClassifierCreationExperiment are as follows:
|
|---|
| 23 | * <ul>
|
|---|
| 24 | * <li>load the data from the provided data path</li>
|
|---|
| 25 | * <li>check if given resultsdir exists, if not create one</li>
|
|---|
| 26 | * <li>execute the following steps for each data set:
|
|---|
| 27 | * <ul>
|
|---|
| 28 | * <li>load the dataset</li>
|
|---|
| 29 | * <li>set testdata == traindata</li>
|
|---|
| 30 | * <li>preprocess the data</li>
|
|---|
| 31 | * <li>postprocess the data</li>
|
|---|
| 32 | * <li>for each configured trainer do the following:</li>
|
|---|
| 33 | * <ul>
|
|---|
| 34 | * <li>if the classifier should be saved, train it with the dataset</li>
|
|---|
| 35 | * <li>save it in the results dir</li>
|
|---|
| 36 | * <li>For each configured evaluator: Do the evaluation and save results</li>
|
|---|
| 37 | * </ul>
|
|---|
| 38 | * </ul>
|
|---|
| 39 | * </ul>
|
|---|
| 40 | *
|
|---|
| 41 | * Note that this class implements {@link IExectuionStrategy}, i.e., each experiment can be started
|
|---|
| 42 | * in its own thread.
|
|---|
| 43 | *
|
|---|
| 44 | * @author Fabian Trautsch
|
|---|
| 45 | */
|
|---|
| 46 | public class ClassifierCreationExperiment implements IExecutionStrategy {
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * configuration of the experiment
|
|---|
| 50 | */
|
|---|
| 51 | private final ExperimentConfiguration config;
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Constructor. Creates a new experiment based on a configuration.
|
|---|
| 55 | * @param config configuration of the experiment
|
|---|
| 56 | */
|
|---|
| 57 | public ClassifierCreationExperiment(ExperimentConfiguration config) {
|
|---|
| 58 | this.config = config;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Executes the experiment with the steps as described in the class comment.
|
|---|
| 63 | * @see Runnable#run()
|
|---|
| 64 | */
|
|---|
| 65 | @Override
|
|---|
| 66 | public void run() {
|
|---|
| 67 | final List<SoftwareVersion> versions = new LinkedList<>();
|
|---|
| 68 |
|
|---|
| 69 | boolean writeHeader = true;
|
|---|
| 70 |
|
|---|
| 71 | for(IVersionLoader loader : config.getLoaders()) {
|
|---|
| 72 | versions.addAll(loader.load());
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | File resultsDir = new File(config.getResultsPath());
|
|---|
| 77 | if (!resultsDir.exists()) {
|
|---|
| 78 | resultsDir.mkdir();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | int versionCount = 1;
|
|---|
| 83 | for( SoftwareVersion testVersion : versions ) {
|
|---|
| 84 |
|
|---|
| 85 | // At first: traindata == testdata
|
|---|
| 86 | Instances testdata = testVersion.getInstances();
|
|---|
| 87 | Instances traindata = new Instances(testdata);
|
|---|
| 88 |
|
|---|
| 89 | // Give the dataset a new name
|
|---|
| 90 | testdata.setRelationName(testVersion.getProject());
|
|---|
| 91 |
|
|---|
| 92 | for( IProcessesingStrategy processor : config.getPreProcessors() ) {
|
|---|
| 93 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying preprocessor %s", config.getExperimentName(), versionCount, versions.size(), testVersion.getProject(), processor.getClass().getName()));
|
|---|
| 94 | processor.apply(testdata, traindata);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | for( IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors() ) {
|
|---|
| 98 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying pointwise selection %s", config.getExperimentName(), versionCount, versions.size(), testVersion.getProject(), dataselector.getClass().getName()));
|
|---|
| 99 | traindata = dataselector.apply(testdata, traindata);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | for( IProcessesingStrategy processor : config.getPostProcessors() ) {
|
|---|
| 103 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s", config.getExperimentName(), versionCount, versions.size(), testVersion.getProject(), processor.getClass().getName()));
|
|---|
| 104 | processor.apply(testdata, traindata);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | // Trainerlist for evaluation later on
|
|---|
| 111 | List<ITrainer> allTrainers = new LinkedList<>();
|
|---|
| 112 |
|
|---|
| 113 | for( ITrainingStrategy trainer : config.getTrainers() ) {
|
|---|
| 114 |
|
|---|
| 115 | // Add trainer to list for evaluation
|
|---|
| 116 | allTrainers.add(trainer);
|
|---|
| 117 |
|
|---|
| 118 | // Train classifier
|
|---|
| 119 | trainer.apply(traindata);
|
|---|
| 120 |
|
|---|
| 121 | if(config.getSaveClassifier()) {
|
|---|
| 122 | // If classifier should be saved, train him and save him
|
|---|
| 123 | // be careful with typecasting here!
|
|---|
| 124 | IWekaCompatibleTrainer trainerToSave = (IWekaCompatibleTrainer) trainer;
|
|---|
| 125 | //Console.println(trainerToSave.getClassifier().toString());
|
|---|
| 126 | try {
|
|---|
| 127 | weka.core.SerializationHelper.write(resultsDir.getAbsolutePath()+"/"+trainer.getName()+"-"+testVersion.getProject(), trainerToSave.getClassifier());
|
|---|
| 128 | } catch (Exception e) {
|
|---|
| 129 | e.printStackTrace();
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | for( IEvaluationStrategy evaluator : config.getEvaluators() ) {
|
|---|
| 138 | Console.traceln(Level.FINE, String.format("[%s] [%02d/%02d] %s: applying evaluator %s", config.getExperimentName(), versionCount, versions.size(), testVersion.getProject(), evaluator.getClass().getName()));
|
|---|
| 139 |
|
|---|
| 140 | if( writeHeader ) {
|
|---|
| 141 | evaluator.setParameter(config.getResultsPath() + "/" + config.getExperimentName() + ".csv");
|
|---|
| 142 | }
|
|---|
| 143 | evaluator.apply(testdata, traindata, allTrainers, writeHeader);
|
|---|
| 144 | writeHeader = false;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | versionCount++;
|
|---|
| 148 |
|
|---|
| 149 | Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: finished", config.getExperimentName(), versionCount, versions.size(), testVersion.getProject()));
|
|---|
| 150 |
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | }
|
|---|