/
SpectralClusteringAlgorithmTest.java
75 lines (62 loc) · 2.94 KB
/
SpectralClusteringAlgorithmTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package sg.edu.nus.comp.lms.algorithm;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import sg.edu.nus.comp.lms.algorithm.single.SpectralClustering;
import sg.edu.nus.comp.lms.domain.Settings;
import sg.edu.nus.comp.lms.domain.reader.CSVReader;
import sg.edu.nus.comp.lms.domain.weka.util.InstancesUtils;
import weka.core.Instances;
import java.io.File;
import java.util.Arrays;
import java.util.List;
public class SpectralClusteringAlgorithmTest {
private static File getFileFromResources(String filename) {
return new File(Thread.currentThread().getContextClassLoader().getResource(filename).getFile());
}
@Test
public void simpleTest() throws Exception {
Instances instances = new CSVReader(getFileFromResources("simpleTest.csv")).readAll();
ClusteringAlgorithm clusteringAlgorithm = new SpectralClustering(instances, 2);
clusteringAlgorithm.cluster();
List<String> ids = InstancesUtils.extractStringAttr(instances, Settings.ID_ATTR);
for (int clusterSize : buildDistribution(clusteringAlgorithm, ids)) {
MatcherAssert.assertThat(clusterSize, Matchers.equalTo(2));
}
}
@Test
public void simpleTest2() throws Exception {
Instances instances = new CSVReader(getFileFromResources("simpleTest2.csv")).readAll();
ClusteringAlgorithm clusteringAlgorithm = new SpectralClustering(instances, 3);
clusteringAlgorithm.cluster();
List<String> ids = InstancesUtils.extractStringAttr(instances, Settings.ID_ATTR);
int[] distr = buildDistribution(clusteringAlgorithm, ids);
int max = Arrays.stream(distr).max().getAsInt();
int min = Arrays.stream(distr).min().getAsInt();
MatcherAssert.assertThat(max, Matchers.equalTo(2));
MatcherAssert.assertThat(min, Matchers.equalTo(1));
}
// @Test
// public void simpleTest3() throws Exception {
// try (BufferedReader reader = new BufferedReader(new FileReader(getFileFromResources("simpleTest3.arff")))) {
// Instances instances = new ArffLoader.ArffReader(reader).getData();
//
// ClusteringAlgorithm clustering = new SpectralClustering(instances, 3);
// clustering.cluster();
//
// List<String> ids = InstancesUtils.extractStringAttr(instances, Settings.ID_ATTR);
// for (int clusterSize : buildDistribution(clustering, ids)) {
// MatcherAssert.assertThat(clusterSize, Matchers.equalTo(2));
// }
// }
// }
private int[] buildDistribution(ClusteringAlgorithm clusteringAlgorithm, List<String> ids) {
int[] clusterDistribution = new int[clusteringAlgorithm.clusterNumber()];
for (String key : ids) {
int clusterId = clusteringAlgorithm.getClusterIndex(key);
System.out.println(key + " " + clusterId);
clusterDistribution[clusterId]++;
}
return clusterDistribution;
}
}