Просмотр исходного кода

Move clustering options to dedicated class

Simon Ding 4 лет назад
Родитель
Сommit
32a128554e

+ 1 - 1
jplag/src/main/java/de/jplag/JPlag.java

@@ -92,7 +92,7 @@ public class JPlag {
         JPlagResult result = comparisonStrategy.compareSubmissions(submissionSet);
         JPlagResult result = comparisonStrategy.compareSubmissions(submissionSet);
         errorCollector.print("\nTotal time for comparing submissions: " + TimeUtil.formatDuration(result.getDuration()), null);
         errorCollector.print("\nTotal time for comparing submissions: " + TimeUtil.formatDuration(result.getDuration()), null);
 
 
-        result.setClusteringResult(ClusteringFactory.getClusterings(result.getComparisons(), options));
+        result.setClusteringResult(ClusteringFactory.getClusterings(result.getComparisons(), options.getClusteringOptions()));
 
 
         return result;
         return result;
     }
     }

+ 15 - 16
jplag/src/main/java/de/jplag/clustering/ClusteringFactory.java

@@ -13,43 +13,42 @@ import de.jplag.clustering.algorithm.TopDownHierarchicalClustering;
 import de.jplag.clustering.preprocessors.CdfPreprocessor;
 import de.jplag.clustering.preprocessors.CdfPreprocessor;
 import de.jplag.clustering.preprocessors.PercentileThresholdProcessor;
 import de.jplag.clustering.preprocessors.PercentileThresholdProcessor;
 import de.jplag.clustering.preprocessors.ThresholdPreprocessor;
 import de.jplag.clustering.preprocessors.ThresholdPreprocessor;
-import de.jplag.options.JPlagOptions;
 
 
 /**
 /**
  * Produces the clustering according to the options.
  * Produces the clustering according to the options.
  */
  */
 public class ClusteringFactory {
 public class ClusteringFactory {
-    public static List<ClusteringResult<Submission>> getClusterings(Collection<JPlagComparison> comparisons, JPlagOptions options) {
-        if (!options.isClustering()) return Collections.emptyList();
+    public static List<ClusteringResult<Submission>> getClusterings(Collection<JPlagComparison> comparisons, ClusteringOptions options) {
+        if (!options.isEnabled()) return Collections.emptyList();
 
 
         // init algorithm
         // init algorithm
         ClusteringAlgorithm ca = null;
         ClusteringAlgorithm ca = null;
-        if (options.getClusteringAlgorithm() == Algorithms.AGGLOMERATIVE) {
+        if (options.getAlgorithm() == Algorithms.AGGLOMERATIVE) {
             TopDownHierarchicalClustering.ClusteringOptions clusteringOptions = new TopDownHierarchicalClustering.ClusteringOptions();
             TopDownHierarchicalClustering.ClusteringOptions clusteringOptions = new TopDownHierarchicalClustering.ClusteringOptions();
-            clusteringOptions.minimalSimilarity = options.getClusteringAgglomerativeThreshold();
-            clusteringOptions.similarity = options.getClusteringAgglomerativeInterClusterSimilarity();
+            clusteringOptions.minimalSimilarity = options.getAgglomerativeThreshold();
+            clusteringOptions.similarity = options.getAgglomerativeInterClusterSimilarity();
             ca = new TopDownHierarchicalClustering(clusteringOptions);
             ca = new TopDownHierarchicalClustering(clusteringOptions);
-        } else if (options.getClusteringAlgorithm() == Algorithms.SPECTRAL) {
+        } else if (options.getAlgorithm() == Algorithms.SPECTRAL) {
             SpectralClustering.ClusteringOptions clusteringOptions = new SpectralClustering.ClusteringOptions();
             SpectralClustering.ClusteringOptions clusteringOptions = new SpectralClustering.ClusteringOptions();
-            clusteringOptions.GPVariance = options.getClusteringSpectralGPVariance();
-            clusteringOptions.kernelBandwidth = options.getClusteringSpectralKernelBandwidth();
-            clusteringOptions.maxKMeansIterations = options.getClusteringSpectralMaxKMeansIterationPerRun();
-            clusteringOptions.maxRuns = options.getClusteringSpectralMaxRuns();
-            clusteringOptions.minRuns = options.getClusteringSpectralMinRuns();
+            clusteringOptions.GPVariance = options.getSpectralGPVariance();
+            clusteringOptions.kernelBandwidth = options.getSpectralKernelBandwidth();
+            clusteringOptions.maxKMeansIterations = options.getSpectralMaxKMeansIterationPerRun();
+            clusteringOptions.maxRuns = options.getSpectralMaxRuns();
+            clusteringOptions.minRuns = options.getSpectralMinRuns();
             ca = new SpectralClustering(clusteringOptions);
             ca = new SpectralClustering(clusteringOptions);
         }
         }
 
 
         // init preprocessor
         // init preprocessor
         Preprocessor preprocessor;
         Preprocessor preprocessor;
-        switch (options.getClusteringPreprocessor()) {
+        switch (options.getPreprocessor()) {
             case CDF:
             case CDF:
                 preprocessor = new CdfPreprocessor();
                 preprocessor = new CdfPreprocessor();
                 break;
                 break;
             case THRESHOLD:
             case THRESHOLD:
-                preprocessor = new ThresholdPreprocessor(options.getClusteringPreprocessorThreshold());
+                preprocessor = new ThresholdPreprocessor(options.getPreprocessorThreshold());
                 break;
                 break;
             case PERCENTILE:
             case PERCENTILE:
-                preprocessor = new PercentileThresholdProcessor(options.getClusteringPreprocessorPercentile());
+                preprocessor = new PercentileThresholdProcessor(options.getPreprocessorPercentile());
                 break;
                 break;
             case NONE:
             case NONE:
             default:
             default:
@@ -62,7 +61,7 @@ public class ClusteringFactory {
         }
         }
 
 
         // init adapter
         // init adapter
-        ClusteringAdapter adapter = new ClusteringAdapter(comparisons, options.getClusteringSimilarityMetric());
+        ClusteringAdapter adapter = new ClusteringAdapter(comparisons, options.getSimilarityMetric());
 
 
         // run clustering
         // run clustering
         ClusteringResult<Submission> result = adapter.doClustering(ca);
         ClusteringResult<Submission> result = adapter.doClustering(ca);

+ 201 - 0
jplag/src/main/java/de/jplag/clustering/ClusteringOptions.java

@@ -0,0 +1,201 @@
+
+package de.jplag.clustering;
+
+import de.jplag.clustering.algorithm.TopDownHierarchicalClustering;
+import de.jplag.options.SimilarityMetric;
+
+public class ClusteringOptions {
+
+    public static ClusteringOptions DEFAULTS = new Builder().build();
+
+    private SimilarityMetric similarityMetric;
+    private float spectralKernelBandwidth;
+    private float spectralGPVariance;
+    private int spectralMinRuns;
+    private int spectralMaxRuns;
+    private int spectralMaxKMeansIterationPerRun;
+    private float agglomerativeThreshold;
+    private Preprocessors preprocessor;
+    private boolean enabled;
+    private Algorithms algorithm;
+    private TopDownHierarchicalClustering.InterClusterSimilarity agglomerativeInterClusterSimilarity;
+    private float preprocessorThreshold;
+    private float preprocessorPercentile;
+
+    public SimilarityMetric getSimilarityMetric() {
+        return similarityMetric;
+    }
+
+    public float getSpectralKernelBandwidth() {
+        return spectralKernelBandwidth;
+    }
+
+    public float getSpectralGPVariance() {
+        return spectralGPVariance;
+    }
+
+    public int getSpectralMinRuns() {
+        return spectralMinRuns;
+    }
+
+    public int getSpectralMaxRuns() {
+        return spectralMaxRuns;
+    }
+
+    public int getSpectralMaxKMeansIterationPerRun() {
+        return spectralMaxKMeansIterationPerRun;
+    }
+
+    public float getAgglomerativeThreshold() {
+        return agglomerativeThreshold;
+    }
+
+    public Preprocessors getPreprocessor() {
+        return preprocessor;
+    }
+
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    public Algorithms getAlgorithm() {
+        return algorithm;
+    }
+
+    public TopDownHierarchicalClustering.InterClusterSimilarity getAgglomerativeInterClusterSimilarity() {
+        return agglomerativeInterClusterSimilarity;
+    }
+
+    public float getPreprocessorThreshold() {
+        return preprocessorThreshold;
+    }
+
+    public float getPreprocessorPercentile() {
+        return preprocessorPercentile;
+    }
+
+    public static class Builder {
+
+        private SimilarityMetric similarityMetric;
+        private float spectralKernelBandwidth;
+        private float spectralGPVariance;
+        private int spectralMinRuns;
+        private int spectralMaxRuns;
+        private int spectralMaxKMeansIterationPerRun;
+        private float agglomerativeThreshold;
+        private Preprocessors preprocessor;
+        private boolean enabled;
+        private Algorithms algorithm;
+        private TopDownHierarchicalClustering.InterClusterSimilarity agglomerativeInterClusterSimilarity;
+        private float preprocessorThreshold;
+        private float preprocessorPercentile;
+
+        public Builder() {
+            // Setting the defaults here
+            similarityMetric(SimilarityMetric.MAX);
+            spectralKernelBandwidth(20.f);
+            spectralGPVariance(0.05f * 0.05f);
+            spectralMinRuns(5);
+            spectralMaxRuns(50);
+            spectralMaxKMeansIterationPerRun(200);
+            agglomerativeThreshold(0.2f);
+            preprocessor(Preprocessors.CDF);
+            enabled(true);
+            algorithm(Algorithms.SPECTRAL);
+            agglomerativeInterClusterSimilarity(TopDownHierarchicalClustering.InterClusterSimilarity.AVERAGE);
+            preprocessorThreshold(0.2f);
+            preprocessorPercentile(0.5f);
+        }
+
+        public Builder similarityMetric(SimilarityMetric similarityMetric) {
+            this.similarityMetric = similarityMetric;
+            return Builder.this;
+        }
+
+        public Builder spectralKernelBandwidth(float spectralKernelBandwidth) {
+            this.spectralKernelBandwidth = spectralKernelBandwidth;
+            return Builder.this;
+        }
+
+        public Builder spectralGPVariance(float spectralGPVariance) {
+            this.spectralGPVariance = spectralGPVariance;
+            return Builder.this;
+        }
+
+        public Builder spectralMinRuns(int spectralMinRuns) {
+            this.spectralMinRuns = spectralMinRuns;
+            return Builder.this;
+        }
+
+        public Builder spectralMaxRuns(int spectralMaxRuns) {
+            this.spectralMaxRuns = spectralMaxRuns;
+            return Builder.this;
+        }
+
+        public Builder spectralMaxKMeansIterationPerRun(int spectralMaxKMeansIterationPerRun) {
+            this.spectralMaxKMeansIterationPerRun = spectralMaxKMeansIterationPerRun;
+            return Builder.this;
+        }
+
+        public Builder agglomerativeThreshold(float agglomerativeThreshold) {
+            this.agglomerativeThreshold = agglomerativeThreshold;
+            return Builder.this;
+        }
+
+        public Builder preprocessor(Preprocessors preprocessor) {
+            this.preprocessor = preprocessor;
+            return Builder.this;
+        }
+
+        public Builder enabled(boolean enabled) {
+            this.enabled = enabled;
+            return Builder.this;
+        }
+
+        public Builder algorithm(Algorithms algorithm) {
+            this.algorithm = algorithm;
+            return Builder.this;
+        }
+
+        public Builder agglomerativeInterClusterSimilarity(
+                TopDownHierarchicalClustering.InterClusterSimilarity agglomerativeInterClusterSimilarity) {
+            this.agglomerativeInterClusterSimilarity = agglomerativeInterClusterSimilarity;
+            return Builder.this;
+        }
+
+        public Builder preprocessorThreshold(float preprocessorThreshold) {
+            this.preprocessorThreshold = preprocessorThreshold;
+            return Builder.this;
+        }
+
+        public Builder preprocessorPercentile(float preprocessorPercentile) {
+            this.preprocessorPercentile = preprocessorPercentile;
+            return Builder.this;
+        }
+
+        public ClusteringOptions build() {
+
+            return new ClusteringOptions(this);
+        }
+    }
+
+    private ClusteringOptions(Builder builder) {
+        this.similarityMetric = builder.similarityMetric;
+        this.spectralKernelBandwidth = builder.spectralKernelBandwidth;
+        this.spectralGPVariance = builder.spectralGPVariance;
+        this.spectralMinRuns = builder.spectralMinRuns;
+        this.spectralMaxRuns = builder.spectralMaxRuns;
+        this.spectralMaxKMeansIterationPerRun = builder.spectralMaxKMeansIterationPerRun;
+        this.agglomerativeThreshold = builder.agglomerativeThreshold;
+        this.preprocessor = builder.preprocessor;
+        this.enabled = builder.enabled;
+        this.algorithm = builder.algorithm;
+        this.agglomerativeInterClusterSimilarity = builder.agglomerativeInterClusterSimilarity;
+        this.preprocessorThreshold = builder.preprocessorThreshold;
+        this.preprocessorPercentile = builder.preprocessorPercentile;
+    }
+
+    public void doSomething() {
+        // do something
+    }
+}

+ 1 - 1
jplag/src/main/java/de/jplag/clustering/ClusteringResult.java

@@ -53,7 +53,7 @@ public interface ClusteringResult<T> {
         ClusteringResult<T> getClusteringResult();
         ClusteringResult<T> getClusteringResult();
 
 
         /**
         /**
-         * @return How much each member o f this cluster contributes to the {@link ClusteringResult#getCommunityStrength} 
+         * @return How much each member of this cluster contributes to the {@link ClusteringResult#getCommunityStrength} 
          */
          */
         default float getCommunityStrengthPerConnection() {
         default float getCommunityStrengthPerConnection() {
             int size = getMembers().size();
             int size = getMembers().size();

+ 12 - 178
jplag/src/main/java/de/jplag/options/JPlagOptions.java

@@ -10,9 +10,7 @@ import java.util.Optional;
 import java.util.Set;
 import java.util.Set;
 
 
 import de.jplag.Language;
 import de.jplag.Language;
-import de.jplag.clustering.Algorithms;
-import de.jplag.clustering.Preprocessors;
-import de.jplag.clustering.algorithm.TopDownHierarchicalClustering;
+import de.jplag.clustering.ClusteringOptions;
 import de.jplag.strategy.ComparisonMode;
 import de.jplag.strategy.ComparisonMode;
 
 
 public class JPlagOptions {
 public class JPlagOptions {
@@ -66,7 +64,7 @@ public class JPlagOptions {
 
 
     /**
     /**
      * Tunes the comparison sensitivity by adjusting the minimum token required to be counted as matching section. A smaller
      * Tunes the comparison sensitivity by adjusting the minimum token required to be counted as matching section. A smaller
-     * <n> increases the sensitivity but might lead to more false-positves.
+     * <n> increases the sensitivity but might lead to more false-positives.
      */
      */
     private Integer minimumTokenMatch;
     private Integer minimumTokenMatch;
 
 
@@ -117,77 +115,9 @@ public class JPlagOptions {
     private Verbosity verbosity;
     private Verbosity verbosity;
 
 
     /**
     /**
-     * This similarity metric is used for clustering.
+     * Clustering options
      */
      */
-    private SimilarityMetric clusteringSimilarityMetric = SimilarityMetric.MAX;
-
-    /**
-     * The kernel bandwidth for the matern kernel used in the gaussian process for the automatic search for the number of clusters in spectral clustering.
-     * Affects the runtime and results of the spectral clustering.
-     */
-    private float clusteringSpectralKernelBandwidth = 20.f;
-
-    /**
-     * This is the assumed level of noise in the evaluation results of a spectral clustering.
-     * Acts as normalization parameter for the gaussian process.
-     * The default setting works well with similarity scores in the range between zero and one.
-     * Affects the runtime and results of the spectral clustering.
-     */
-    private float clusteringSpectralGPVariance = 0.05f * 0.05f;
-
-    /**
-     * The minimal number of runs of the spectral clustering algorithm.
-     * These runs will use predefined numbers of clusters and will not use the bayesian optimization to determine the number of clusters. 
-     */
-    private int clusteringSpectralMinRuns = 5;
-
-    /**
-     * Maximal number of runs of the spectral clustering algorithm.
-     * The bayesian optimization may be stopped before, when no more maxima of the acquisition-function are found.
-     */
-    private int clusteringSpectralMaxRuns = 50;
-
-    /**
-     * Maximum number of iterations of the kMeans clustering per run of the spectral clustering algorithm.
-     */
-    private int clusteringSpectralMaxKMeansIterationPerRun = 200;
-
-    /**
-     * Preprocessing for the similarity values before clustering.
-     * This is mandatory for spectral clustering and optional for agglomerative clustering.
-     */
-    private Preprocessors clusteringPreprocessor = Preprocessors.CDF;
-
-    /**
-     * Sets up to which similarity the threshold preprocessor zeroes out the similarities.
-     */
-    private float clusteringPreprocessorThreshold = 0.2f;
-
-    /**
-     * Sets up to which percentile of the percentile preprocessor zeroes out the similarities.
-     */
-    private float clusteringPreprocessorPercentile = 0.5f;
-
-    /**
-     * Agglomerative clustering will merge clusters that have a similarity higher than this threshold.
-     */
-    private float clusteringAgglomerativeThreshold = 0.2f;
-
-    /**
-     * Similarity measure between clusters in agglomerative clustering. 
-     */
-    private TopDownHierarchicalClustering.InterClusterSimilarity clusteringAgglomerativeInterClusterSimilarity = TopDownHierarchicalClustering.InterClusterSimilarity.AVERAGE;
-
-    /**
-     * The clustering algorithm to use.
-     */
-    private Algorithms clusteringAlgorithm = Algorithms.SPECTRAL;
-
-    /**
-     * If clustering should be performed.
-     */
-    private boolean clusteringDoClustering = true;
-
+    private ClusteringOptions clusteringOptions = new ClusteringOptions.Builder().build();
 
 
     /**
     /**
      * Constructor with required attributes.
      * Constructor with required attributes.
@@ -261,6 +191,10 @@ public class JPlagOptions {
         return debugParser;
         return debugParser;
     }
     }
 
 
+    public ClusteringOptions getClusteringOptions() {
+        return this.clusteringOptions;
+    }
+
     public void setBaseCodeSubmissionName(String baseCodeSubmissionName) {
     public void setBaseCodeSubmissionName(String baseCodeSubmissionName) {
         if (baseCodeSubmissionName == null || baseCodeSubmissionName.isEmpty()) {
         if (baseCodeSubmissionName == null || baseCodeSubmissionName.isEmpty()) {
             this.baseCodeSubmissionName = Optional.empty();
             this.baseCodeSubmissionName = Optional.empty();
@@ -319,58 +253,6 @@ public class JPlagOptions {
             this.maximumNumberOfComparisons = maximumNumberOfComparisons;
             this.maximumNumberOfComparisons = maximumNumberOfComparisons;
         }
         }
     }
     }
-
-    public SimilarityMetric getClusteringSimilarityMetric() {
-        return clusteringSimilarityMetric;
-    }
-
-    public float getClusteringSpectralKernelBandwidth() {
-        return clusteringSpectralKernelBandwidth;
-    }
-
-    public float getClusteringSpectralGPVariance() {
-        return clusteringSpectralGPVariance;
-    }
-
-    public int getClusteringSpectralMinRuns() {
-        return clusteringSpectralMinRuns;
-    }
-
-    public int getClusteringSpectralMaxRuns() {
-        return clusteringSpectralMaxRuns;
-    }
-
-    public int getClusteringSpectralMaxKMeansIterationPerRun() {
-        return clusteringSpectralMaxKMeansIterationPerRun;
-    }
-
-    public float getClusteringAgglomerativeThreshold() {
-        return clusteringAgglomerativeThreshold;
-    }
-
-    public Preprocessors getClusteringPreprocessor() {
-        return clusteringPreprocessor;
-    }
-
-    public boolean isClustering() {
-        return clusteringDoClustering;
-    }
-
-    public Algorithms getClusteringAlgorithm() {
-        return clusteringAlgorithm;
-    }
-
-    public TopDownHierarchicalClustering.InterClusterSimilarity getClusteringAgglomerativeInterClusterSimilarity() {
-        return clusteringAgglomerativeInterClusterSimilarity;
-    }
-
-    public float getClusteringPreprocessorThreshold() {
-        return clusteringPreprocessorThreshold;
-    }
-
-    public float getClusteringPreprocessorPercentile() {
-        return clusteringPreprocessorPercentile;
-    }
     
     
     public void setMinimumTokenMatch(Integer minimumTokenMatch) {
     public void setMinimumTokenMatch(Integer minimumTokenMatch) {
         if (minimumTokenMatch != null && minimumTokenMatch < 1) {
         if (minimumTokenMatch != null && minimumTokenMatch < 1) {
@@ -409,6 +291,10 @@ public class JPlagOptions {
         this.verbosity = verbosity;
         this.verbosity = verbosity;
     }
     }
 
 
+    public void setClusteringOptions(ClusteringOptions clusteringOptions) {
+        this.clusteringOptions = clusteringOptions;
+    }
+
     private boolean hasFileSuffixes() {
     private boolean hasFileSuffixes() {
         return fileSuffixes != null && fileSuffixes.length > 0;
         return fileSuffixes != null && fileSuffixes.length > 0;
     }
     }
@@ -417,56 +303,4 @@ public class JPlagOptions {
         return minimumTokenMatch != null;
         return minimumTokenMatch != null;
     }
     }
 
 
-    public void setClusteringSimilarityMetric(SimilarityMetric clusteringSimilarityMetric) {
-        this.clusteringSimilarityMetric = clusteringSimilarityMetric;
-    }
-
-    public void setClusteringSpectralKernelBandwidth(float clusteringSpectralKernelBandwidth) {
-        this.clusteringSpectralKernelBandwidth = clusteringSpectralKernelBandwidth;
-    }
-
-    public void setClusteringSpectralGPVariance(float clusteringSpectralGPVariance) {
-        this.clusteringSpectralGPVariance = clusteringSpectralGPVariance;
-    }
-
-    public void setClusteringSpectralMinRuns(int clusteringSpectralMinRuns) {
-        this.clusteringSpectralMinRuns = clusteringSpectralMinRuns;
-    }
-
-    public void setClusteringSpectralMaxRuns(int clusteringSpectralMaxRuns) {
-        this.clusteringSpectralMaxRuns = clusteringSpectralMaxRuns;
-    }
-
-    public void setClusteringSpectralMaxKMeansIterationPerRun(int clusteringSpectralMaxKMeansIterationPerRun) {
-        this.clusteringSpectralMaxKMeansIterationPerRun = clusteringSpectralMaxKMeansIterationPerRun;
-    }
-
-    public void setClusteringPreprocessor(Preprocessors clusteringPreprocessor) {
-        this.clusteringPreprocessor = clusteringPreprocessor;
-    }
-
-    public void setClusteringAgglomerativeThreshold(float clusteringAgglomerativeThreshold) {
-        this.clusteringAgglomerativeThreshold = clusteringAgglomerativeThreshold;
-    }
-
-    public void setClusteringAgglomerativeInterClusterSimilarity(
-            TopDownHierarchicalClustering.InterClusterSimilarity clusteringAgglomerativeInterClusterSimilarity) {
-        this.clusteringAgglomerativeInterClusterSimilarity = clusteringAgglomerativeInterClusterSimilarity;
-    }
-
-    public void setClusteringAlgorithm(Algorithms clusteringAlgorithm) {
-        this.clusteringAlgorithm = clusteringAlgorithm;
-    }
-
-    public void setClustering(boolean clustering) {
-        this.clusteringDoClustering = clustering;
-    }
-
-    public void setClusteringPreprocessorThreshold(float clusteringPreprocessorThreshold) {
-        this.clusteringPreprocessorThreshold = clusteringPreprocessorThreshold;
-    }
-
-    public void setClusteringPreprocessorPercentile(float clusteringPreprocessorPercentile) {
-        this.clusteringPreprocessorPercentile = clusteringPreprocessorPercentile;
-    }
 }
 }