From a5bb2a3f63c4800bccbb0ca006e9600e061a4d99 Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Thu, 24 Feb 2022 11:31:14 +0100 Subject: [PATCH] Test for clustering result --- .../clustering/ClusteringResultTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 jplag/src/test/java/de/jplag/clustering/ClusteringResultTest.java diff --git a/jplag/src/test/java/de/jplag/clustering/ClusteringResultTest.java b/jplag/src/test/java/de/jplag/clustering/ClusteringResultTest.java new file mode 100644 index 0000000000..7a6a5e7285 --- /dev/null +++ b/jplag/src/test/java/de/jplag/clustering/ClusteringResultTest.java @@ -0,0 +1,58 @@ +package de.jplag.clustering; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.apache.commons.math3.linear.Array2DRowRealMatrix; +import org.apache.commons.math3.linear.RealMatrix; +import org.junit.Test; + +public class ClusteringResultTest { + + @Test + public void perfectClustering() { + RealMatrix similarity = new Array2DRowRealMatrix(4, 4); + + // These are similar + setEntries(similarity, 0, 1, 1f); + setEntries(similarity, 2, 3, 1f); + + // Others are dissimilar + + ClusteringResult result = ClusteringResult + .fromIntegerCollections(List.of(List.of(0, 1), List.of(2, 3)), similarity); + + // The maximum of the metric is 1 - 1/k for k clusters + assertEquals(0.5, result.getCommunityStrength(), 0.00001); + } + + @Test + public void uniformClustering() { + RealMatrix similarity = new Array2DRowRealMatrix(4, 4); + + // We'd obtain such weights by pre-selecting the clusters, + // then randomly picking two clusters and adding weight between random of each + // cluster + + // These are similar + setEntries(similarity, 0, 1, 0.1f); + setEntries(similarity, 2, 3, 0.1f); + + // Others are dissimilar + setEntries(similarity, 0, 2, 0.05f); + setEntries(similarity, 0, 3, 0.05f); + setEntries(similarity, 1, 2, 0.05f); + setEntries(similarity, 1, 3, 0.05f); + + ClusteringResult result = ClusteringResult + .fromIntegerCollections(List.of(List.of(0, 1), List.of(2, 3)), similarity); + + assertEquals(0.0, result.getCommunityStrength(), 0.00001); + } + + private static void setEntries(RealMatrix matrix, int i, int j, double value) { + matrix.setEntry(i, j, value); + matrix.setEntry(j, i, value); + } +}