use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class HopachablePAMTest method testSplit.
@Test
public void testSplit() {
Double[] data = { .2, .2, .8, .8, .82, .82, .4, .5, .5, .4, .15, .15, .81, .81, .14, .14, .45, .45 };
int k = 3;
int[] ans = { 0, 1, 1, 2, 2, 0, 1, 0, 2 };
CyMatrix mat = CyMatrixFactory.makeSmallMatrix(9, 2, data);
HopachablePAM pam = new HopachablePAM(null, mat, DistanceMetric.CITYBLOCK);
pam.setParameters(9, 9, SplitCost.AVERAGE_SPLIT_SILHOUETTE, new MedianSummarizer());
Clusters c = pam.split(false);
// check that data are split into expected number of clusters
assertEquals(c.getNumberOfClusters(), k);
// check cluster assignments
for (int i = 0; i < c.size(); ++i) {
assertEquals(c.getClusterIndex(i), ans[i]);
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class PAMTest method testCluster.
@Test
public void testCluster() {
System.out.println("testCluster begin");
Double[] data = { .9, .9, .8, .8, .4, .4, .5, .5, .1, .1 };
int k = 3;
// {0, 0, 1, 1, 2, 2};
int[] ans = { 0, 0, 1, 1, 2 };
// BaseMatrix mat = new BaseMatrix(4, 2, data);
CyMatrix mat = CyMatrixFactory.makeSmallMatrix(4, 2, data);
PAM pam = new PAM(null, mat, DistanceMetric.CITYBLOCK);
Clusters c = pam.cluster(k);
System.out.println("testCluster end");
assertEquals(c.getNumberOfClusters(), k);
for (int i = 0; i < c.size(); ++i) {
System.out.println("c[" + i + "] = " + c.getClusterIndex(i));
}
for (int i = 0; i < c.size(); ++i) {
assertEquals(c.getClusterIndex(i), ans[i]);
}
// NB The current implementation fails the below test case:
// data = [.9, .9; .8, .8; .4, .4,; .5, .5; .1, .1]
// ans = [ 0, 0, 1, 1, 2]
//
// Instead, PAM.cluster(...) yields:
// res = [ 0, 0, 1, 2, 2]
//
// This discrepancy is due to existence of singleton clusters.
// During the build phase, element 2 is selected as a medoid, which precludes
// element 4 from becoming a medoid.
// During the swap phase, the current implementation failed to register
// the (2, 4) swap as a worthwhile swap, because the contribution to a potential
// swap is only calculated based on nonmedoids other than the candidate itself.
// There is no way to justify merging element 2 to cluster 1 and creating
// a new singleton cluster headed by element 4.
//
// In contrast, R's cluster::pam passes this test case.
// The algorithm therein likely differs from the one upon which the current
// implementation is based.
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class RunPAM method kcluster.
@Override
public int kcluster(int nClusters, int nIterations, CyMatrix matrix, DistanceMetric metric, int[] clusterId) {
PAM pam = new PAM(network, matrix, metric);
Clusters c = pam.cluster(nClusters);
// copy results into clusterId
for (int i = 0; i < c.size(); ++i) {
clusterId[i] = c.getClusterIndex(i);
}
return c.getNumberOfClusters();
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class SilhouetteCalculatorTest method testCalculateBaseMatrix.
@Test
public void testCalculateBaseMatrix() {
CyMatrix test = CyMatrixFactory.makeSmallMatrix(null, 12, 3);
Double[] data = new Double[] { 101., 102., 103., 102., 103., 104., 103., 104., 105., 111., 112., 113., 112., 113., 114., 113., 114., 115., 21., 22., 23., 22., 23., 24., 23., 24., 25., 29., 30., 31., 32., 33., 34., 33., 34., 35. };
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 12; col++) {
test.setValue(row, col, data[row * 3 + col]);
}
}
int[] labels = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3 };
// silhouettes calculated by cluster::silhouette in R
double[] ans = { 0.8636364, 0.9000000, 0.8333333, 0.8333333, 0.9000000, 0.8636364, 0.8548387, 0.8928571, 0.8200000, 0.5000000, 0.8000000, 0.7727273 };
Silhouettes out = SilhouetteCalculator.calculate(test, DistanceMetric.CITYBLOCK, labels);
assertEquals("length", ans.length, out.size());
for (int i = 0; i < ans.length; i++) {
assertEquals("silhouette[" + i + "]", ans[i], out.getSilhouette(i), epsilon);
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class HopachPAMTest method testRun.
@Test
public void testRun() {
Double[] data = { 100.9, 100.9, 100.85, 100.85, 100.8, 100.8, .15, .15, .2, .2, .12, .12, .05, .05, .04, .04, .0, .0, .02, .02 };
int[] ans = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 2 };
CyMatrix mat = CyMatrixFactory.makeSmallMatrix(10, 2, data);
HopachPAM h = new HopachPAM(mat, DistanceMetric.CITYBLOCK);
Clusters c = h.run();
// check that the clustering results match
for (int i = 0; i < c.size(); ++i) {
assertEquals(c.getClusterIndex(i), ans[i]);
}
}
Aggregations