|
@@ -1,9 +1,7 @@
|
|
|
package de.jplag.clustering;
|
|
package de.jplag.clustering;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertEquals;
|
|
|
-import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
-import static org.mockito.Mockito.mock;
|
|
|
|
|
-import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
+import static org.junit.Assume.assumeTrue;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileNotFoundException;
|
|
@@ -15,154 +13,130 @@ import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
import java.util.Comparator;
|
|
import java.util.Comparator;
|
|
|
import java.util.Deque;
|
|
import java.util.Deque;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Optional;
|
|
|
import java.util.Scanner;
|
|
import java.util.Scanner;
|
|
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
-import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
|
|
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
|
|
|
import org.apache.commons.math3.linear.RealMatrix;
|
|
import org.apache.commons.math3.linear.RealMatrix;
|
|
|
-import org.junit.Ignore;
|
|
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
-import org.mockito.invocation.InvocationOnMock;
|
|
|
|
|
|
|
|
|
|
-import de.jplag.JPlagComparison;
|
|
|
|
|
-import de.jplag.Submission;
|
|
|
|
|
import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
|
|
import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
|
|
|
import de.jplag.clustering.algorithm.SpectralClustering;
|
|
import de.jplag.clustering.algorithm.SpectralClustering;
|
|
|
import de.jplag.clustering.preprocessors.CumulativeDistributionFunctionPreprocessor;
|
|
import de.jplag.clustering.preprocessors.CumulativeDistributionFunctionPreprocessor;
|
|
|
|
|
|
|
|
-public class ClusteringTest {
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * These test are not meant to be run during normal unit testing.
|
|
|
|
|
+ * They can be used to test the clustering algorithms against data from
|
|
|
|
|
+ * the private pseudomized reports repository.
|
|
|
|
|
+ *
|
|
|
|
|
+ * These tests test PROBABILISTIC behavior, so use with caution!
|
|
|
|
|
+ */
|
|
|
|
|
+public class ClusteringRealDataTest {
|
|
|
|
|
|
|
|
- @Test
|
|
|
|
|
- public void testClustering() {
|
|
|
|
|
- List<Submission> submissions = IntStream.range(0, 4).mapToObj(x -> mock(Submission.class)).collect(Collectors.toList());
|
|
|
|
|
- List<JPlagComparison> comparisons = new ArrayList<>(6);
|
|
|
|
|
- for (int i = 0; i < submissions.size(); i++) {
|
|
|
|
|
- for (int j = i + 1; j < submissions.size(); j++) {
|
|
|
|
|
- JPlagComparison comparison = mock(JPlagComparison.class);
|
|
|
|
|
- when(comparison.getFirstSubmission()).thenReturn(submissions.get(i));
|
|
|
|
|
- when(comparison.getSecondSubmission()).thenReturn(submissions.get(j));
|
|
|
|
|
- comparisons.add(comparison);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private static class TestFile {
|
|
|
|
|
|
|
|
- // Mock algorithm that returns everything in a single cluster
|
|
|
|
|
- GenericClusteringAlgorithm algorithm = mock(GenericClusteringAlgorithm.class);
|
|
|
|
|
- when(algorithm.cluster(any(RealMatrix.class))).then((InvocationOnMock invocation) -> {
|
|
|
|
|
- RealMatrix arg = invocation.getArgument(0);
|
|
|
|
|
- Collection<Collection<Integer>> result = List.of(IntStream.range(0, arg.getRowDimension()).boxed().collect(Collectors.toList()));
|
|
|
|
|
- return result;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ private String uri;
|
|
|
|
|
+ private Optional<List<String>> expected;
|
|
|
|
|
|
|
|
- ClusteringAdapter clustering = new ClusteringAdapter(comparisons, x -> 0.f);
|
|
|
|
|
- ClusteringResult<Submission> clusteringResult = clustering.doClustering(algorithm);
|
|
|
|
|
|
|
+ public TestFile(String uri, Optional<List<String>> expected) {
|
|
|
|
|
+ this.uri = uri;
|
|
|
|
|
+ this.expected = expected;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- Collection<Collection<Submission>> expectedResult = List.of(submissions);
|
|
|
|
|
|
|
+ private static List<String> B_POSITIVE = Arrays.asList(new String[] { "Student (31)", "Student (223)" });
|
|
|
|
|
+ private static List<String> C_POSITIVE = Arrays
|
|
|
|
|
+ .asList(new String[] { "Student (166)", "Student (212)", "Student (236)", "Student (229)" });
|
|
|
|
|
|
|
|
- assertEquals(expectedResult, clusteringResult.getClusters().stream().map(Cluster::getMembers).collect(Collectors.toList()));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private static final TestFile[] OLD_CLUSTERING_DATA = {
|
|
|
|
|
+ new TestFile("de/jplag/PseudonymizedReports/alt/A_1000_matches_max.csv", Optional.empty()),
|
|
|
|
|
+ new TestFile("de/jplag/PseudonymizedReports/alt/B_1000_matches_max.csv", Optional.of(B_POSITIVE)),
|
|
|
|
|
+ new TestFile("de/jplag/PseudonymizedReports/alt/C_1000_matches_max.csv", Optional.of(C_POSITIVE)),
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ private static final TestFile[] NEW_CLUSTERING_DATA = {
|
|
|
|
|
+ new TestFile("de/jplag/PseudonymizedReports/neu/A_matches_avg.csv", Optional.empty()),
|
|
|
|
|
+ new TestFile("de/jplag/PseudonymizedReports/neu/B_matches_avg.csv", Optional.of(B_POSITIVE)),
|
|
|
|
|
+ new TestFile("de/jplag/PseudonymizedReports/neu/C_matches_avg.csv", Optional.of(C_POSITIVE)),
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
private String str(float f) {
|
|
private String str(float f) {
|
|
|
return String.format("%.4f", f);
|
|
return String.format("%.4f", f);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private float harmonicMean(float a, float b) {
|
|
|
|
|
- return 2 * a * b / (a + b);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private URL loadFromClasspath(String file) throws FileNotFoundException {
|
|
private URL loadFromClasspath(String file) throws FileNotFoundException {
|
|
|
URL url = getClass().getClassLoader().getResource(file);
|
|
URL url = getClass().getClassLoader().getResource(file);
|
|
|
if (url == null) {
|
|
if (url == null) {
|
|
|
- throw new FileNotFoundException(file + " not found. Was the PseudonymizedReports submodule initialized?");
|
|
|
|
|
|
|
+ assumeTrue(file
|
|
|
|
|
+ + " not found. 'de/jpag/PseudonymizedReports' must contain the data from the PseudonymizedReports repository.",
|
|
|
|
|
+ false);
|
|
|
}
|
|
}
|
|
|
return url;
|
|
return url;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Test
|
|
|
|
|
- @Ignore
|
|
|
|
|
- public void aClusteringOld() throws FileNotFoundException, URISyntaxException {
|
|
|
|
|
- URL url = loadFromClasspath("de/jplag/PseudonymizedReports/alt/C_1000_matches_max.csv");
|
|
|
|
|
- File file = new File(url.toURI());
|
|
|
|
|
- ReadResult r = readOldCsv(file);
|
|
|
|
|
- RealMatrix clusteringSimilarity = new Array2DRowRealMatrix(r.similarity.getData());
|
|
|
|
|
|
|
+ private void doTesting(ReadResult readResult, Optional<List<String>> expected) {
|
|
|
|
|
+ RealMatrix clusteringSimilarity = new Array2DRowRealMatrix(readResult.similarity.getData());
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
- * AgglomerativeClustering.ClusteringOptions options = new AgglomerativeClustering.ClusteringOptions();
|
|
|
|
|
- * options.minimalSimilarity = 0.15f; options.similarity = AgglomerativeClustering.InterClusterSimilarity.AVERAGE;
|
|
|
|
|
|
|
+ * AgglomerativeClustering.ClusteringOptions options = new
|
|
|
|
|
+ * AgglomerativeClustering.ClusteringOptions();
|
|
|
|
|
+ * options.minimalSimilarity = 0.15f; options.similarity =
|
|
|
|
|
+ * AgglomerativeClustering.InterClusterSimilarity.AVERAGE;
|
|
|
* ClusteringAlgorithm clusteringAlg = new AgglomerativeClustering(options);
|
|
* ClusteringAlgorithm clusteringAlg = new AgglomerativeClustering(options);
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
SpectralClustering clusteringAlg = new SpectralClustering(ClusteringOptions.DEFAULTS);
|
|
SpectralClustering clusteringAlg = new SpectralClustering(ClusteringOptions.DEFAULTS);
|
|
|
ClusteringPreprocessor preprocessor = new CumulativeDistributionFunctionPreprocessor();
|
|
ClusteringPreprocessor preprocessor = new CumulativeDistributionFunctionPreprocessor();
|
|
|
- GenericClusteringAlgorithm preprocessedClusteringAlg = new PreprocessedClusteringAlgorithm(clusteringAlg, preprocessor);
|
|
|
|
|
|
|
+ GenericClusteringAlgorithm preprocessedClusteringAlg = new PreprocessedClusteringAlgorithm(clusteringAlg,
|
|
|
|
|
+ preprocessor);
|
|
|
Collection<Collection<Integer>> clustering = preprocessedClusteringAlg.cluster(clusteringSimilarity);
|
|
Collection<Collection<Integer>> clustering = preprocessedClusteringAlg.cluster(clusteringSimilarity);
|
|
|
- ClusteringResult<Integer> mRes = ClusteringResult.fromIntegerCollections(new ArrayList<>(clustering), r.similarity);
|
|
|
|
|
|
|
+ ClusteringResult<Integer> mRes = ClusteringResult.fromIntegerCollections(new ArrayList<>(clustering),
|
|
|
|
|
+ readResult.similarity);
|
|
|
List<Cluster<Integer>> clusters = new ArrayList<>(mRes.getClusters());
|
|
List<Cluster<Integer>> clusters = new ArrayList<>(mRes.getClusters());
|
|
|
- clusters.sort(Comparator.comparingDouble(
|
|
|
|
|
- c -> -harmonicMean(c.getCommunityStrengthPerConnection(), avgSimilarity(new ArrayList<>(c.getMembers()), r.similarity))));
|
|
|
|
|
|
|
+ clusters.sort(Comparator.comparingDouble(c -> -c.getNormalizedCommunityStrengthPerConnection()));
|
|
|
|
|
|
|
|
System.out.println("cs\tncsm\tavgSim\tcombined\tmembers");
|
|
System.out.println("cs\tncsm\tavgSim\tcombined\tmembers");
|
|
|
for (Cluster<Integer> c : clusters) {
|
|
for (Cluster<Integer> c : clusters) {
|
|
|
float ncsm = c.getCommunityStrengthPerConnection();
|
|
float ncsm = c.getCommunityStrengthPerConnection();
|
|
|
- float avgSim = c.averageSimilarity((a, b) -> (float) r.similarity.getEntry(a, b));
|
|
|
|
|
- float combined = harmonicMean(ncsm, avgSim);
|
|
|
|
|
- System.out.println(str(c.getCommunityStrength()) + "\t" + str(ncsm) + "\t" + str(avgSim) + "\t" + str(combined) + "\t"
|
|
|
|
|
- + c.getMembers().stream().map(r.mapping::unmap).collect(Collectors.toList()));
|
|
|
|
|
|
|
+ float avgSim = c.averageSimilarity((a, b) -> (float) readResult.similarity.getEntry(a, b));
|
|
|
|
|
+ System.out.println(
|
|
|
|
|
+ str(c.getCommunityStrength()) + "\t" + str(ncsm) + "\t" + str(avgSim) + "\t"
|
|
|
|
|
+ + c.getMembers().stream().map(readResult.mapping::unmap).collect(Collectors.toList()));
|
|
|
}
|
|
}
|
|
|
System.out.println("Community Strength: " + mRes.getCommunityStrength());
|
|
System.out.println("Community Strength: " + mRes.getCommunityStrength());
|
|
|
System.out.println("Clusters: " + clusters.size());
|
|
System.out.println("Clusters: " + clusters.size());
|
|
|
|
|
+
|
|
|
|
|
+ expected.ifPresent(expectedIdentifiers -> {
|
|
|
|
|
+ Set<String> expectedIdentifiersSet = new HashSet<>(expectedIdentifiers);
|
|
|
|
|
+ Set<String> bestClusters = clusters.get(0).getMembers().stream().map(readResult.mapping::unmap)
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+ assertEquals(expectedIdentifiersSet, bestClusters);
|
|
|
|
|
+ System.out.println("hey");
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
@Test
|
|
|
- @Ignore
|
|
|
|
|
- public void aClusteringNew() throws FileNotFoundException, URISyntaxException {
|
|
|
|
|
- URL url = loadFromClasspath("de/jplag/PseudonymizedReports/neu/B_matches_avg.csv");
|
|
|
|
|
- File file = new File(url.toURI());
|
|
|
|
|
- ReadResult r = readNewCsv(file);
|
|
|
|
|
- RealMatrix clusteringSimilarity = new Array2DRowRealMatrix(
|
|
|
|
|
- new CumulativeDistributionFunctionPreprocessor().preprocessSimilarities(r.similarity.getData()));
|
|
|
|
|
-
|
|
|
|
|
- /*
|
|
|
|
|
- * AgglomerativeClustering.ClusteringOptions options = new AgglomerativeClustering.ClusteringOptions();
|
|
|
|
|
- * options.minimalSimilarity = 0.3f; options.similarity = AgglomerativeClustering.InterClusterSimilarity.AVERAGE;
|
|
|
|
|
- * ClusteringAlgorithm clusteringAlg = new AgglomerativeClustering(options);
|
|
|
|
|
- */
|
|
|
|
|
-
|
|
|
|
|
- SpectralClustering clusteringAlg = new SpectralClustering(ClusteringOptions.DEFAULTS);
|
|
|
|
|
-
|
|
|
|
|
- Collection<Collection<Integer>> clustering = clusteringAlg.cluster(clusteringSimilarity);
|
|
|
|
|
- ClusteringResult<Integer> mRes = ClusteringResult.fromIntegerCollections(new ArrayList<>(clustering), r.similarity);
|
|
|
|
|
- List<Cluster<Integer>> clusters = new ArrayList<>(mRes.getClusters());
|
|
|
|
|
- clusters.sort(Comparator.comparingDouble(
|
|
|
|
|
- c -> -harmonicMean(c.getNormalizedCommunityStrengthPerConnection(), avgSimilarity(new ArrayList<>(c.getMembers()), r.similarity))));
|
|
|
|
|
-
|
|
|
|
|
- System.out.println("cs\tncsm\tavgSim\tcombined\tmembers");
|
|
|
|
|
- for (Cluster<Integer> c : clusters) {
|
|
|
|
|
- if (c.isBadCluster())
|
|
|
|
|
- continue;
|
|
|
|
|
- float ncsm = c.getNormalizedCommunityStrengthPerConnection();
|
|
|
|
|
- float avgSim = c.averageSimilarity((a, b) -> (float) r.similarity.getEntry(a, b));
|
|
|
|
|
- float combined = harmonicMean(ncsm, avgSim);
|
|
|
|
|
- System.out.println(str(c.getCommunityStrength()) + "\t" + str(ncsm) + "\t" + str(avgSim) + "\t" + str(combined) + "\t"
|
|
|
|
|
- + c.getMembers().stream().map(r.mapping::unmap).collect(Collectors.toList()));
|
|
|
|
|
|
|
+ public void aClusteringOld() throws FileNotFoundException, URISyntaxException {
|
|
|
|
|
+ for (TestFile testFile : OLD_CLUSTERING_DATA) {
|
|
|
|
|
+ URL url = loadFromClasspath(testFile.uri);
|
|
|
|
|
+ File file = new File(url.toURI());
|
|
|
|
|
+ ReadResult r = readOldCsv(file);
|
|
|
|
|
+ doTesting(r, testFile.expected);
|
|
|
}
|
|
}
|
|
|
- System.out.println("Community Strength: " + mRes.getCommunityStrength());
|
|
|
|
|
- System.out.println("Clusters: " + clusters.size());
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private float avgSimilarity(List<Integer> submissions, RealMatrix similarity) {
|
|
|
|
|
- float similaritySum = 0;
|
|
|
|
|
- int comps = 0;
|
|
|
|
|
-
|
|
|
|
|
- for (int i = 0; i < submissions.size(); i++) {
|
|
|
|
|
- for (int j = i + 1; j < submissions.size(); j++) {
|
|
|
|
|
- similaritySum += similarity.getEntry(submissions.get(i), submissions.get(j));
|
|
|
|
|
- comps++;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void aClusteringNew() throws FileNotFoundException, URISyntaxException {
|
|
|
|
|
+ for (TestFile testFile : NEW_CLUSTERING_DATA) {
|
|
|
|
|
+ URL url = loadFromClasspath(testFile.uri);
|
|
|
|
|
+ File file = new File(url.toURI());
|
|
|
|
|
+ ReadResult r = readNewCsv(file);
|
|
|
|
|
+ doTesting(r, testFile.expected);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return similaritySum / comps;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static class ReadComparison {
|
|
private static class ReadComparison {
|
|
@@ -263,5 +237,4 @@ public class ClusteringTest {
|
|
|
return scanner.hasNextLine();
|
|
return scanner.hasNextLine();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|