Skip to content

Commit

Permalink
Test for clustering result
Browse files Browse the repository at this point in the history
  • Loading branch information
SimDing committed Feb 24, 2022
1 parent 4ada621 commit 5797920
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions jplag/src/test/java/de/jplag/clustering/ClusteringResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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<Integer> 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<Integer> 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);
}
}

0 comments on commit 5797920

Please sign in to comment.