use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class ClusterUtils method fetchRankingResults.
public static List<NodeCluster> fetchRankingResults(CyNetwork network) {
List<NodeCluster> clusters = new ArrayList<>();
String clusterAttribute = getClusterAttribute(network);
String rankingAttribute = getRankingAttribute(network);
Map<Integer, ArrayList<CyNode>> clusterMap = new HashMap<>();
Map<Integer, Double> clusterScoreMap = new HashMap<>();
for (CyNode node : network.getNodeList()) {
if (ModelUtils.hasAttribute(network, node, clusterAttribute) && ModelUtils.hasAttribute(network, node, rankingAttribute)) {
Integer cluster = network.getRow(node).get(clusterAttribute, Integer.class);
Double clusterScore = network.getRow(node).get(rankingAttribute, Double.class, 0.0);
if (!clusterMap.containsKey(cluster)) {
clusterMap.put(cluster, new ArrayList<>());
clusterScoreMap.put(cluster, clusterScore);
}
clusterMap.get(cluster).add(node);
}
}
for (int clusterNum : clusterMap.keySet()) {
NodeCluster cluster = new NodeCluster(clusterMap.get(clusterNum));
cluster.setClusterNumber(clusterNum);
cluster.setRankScore(clusterScoreMap.get(clusterNum));
clusters.add(cluster);
}
ascendingSort(clusters);
return clusters;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class HopachablePAMTest method testCollapse.
@Test
public void testCollapse() {
Double[] data = { .9, .9, .8, .8, .4, .4, .5, .5, .1, .1, .0, .0 };
int k = 3;
CyMatrix mat = CyMatrixFactory.makeSmallMatrix(6, 2, data);
HopachablePAM pam = new HopachablePAM(null, mat, DistanceMetric.CITYBLOCK);
Clusters c1 = pam.cluster(k);
Clusters c2 = pam.collapse(0, 1, c1);
Clusters c3 = pam.collapse(1, 2, c1);
Clusters c4 = pam.collapse(0, 2, c1);
// check that the size has reduced
--k;
assertEquals(c2.getSizes().length, k);
assertEquals(c3.getSizes().length, k);
assertEquals(c4.getSizes().length, k);
Clusters c5 = pam.collapse(0, 1, c2);
--k;
assertEquals(c5.getSizes().length, k);
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class HopachablePAMTest method testSubset.
@Test
public void testSubset() {
Double[] data = { .9, .9, .8, .8, .4, .4, .5, .5, .1, .1, .0, .0 };
int k = 3;
// new order
int[] index = { 0, 1, 5, 2, 4, 3 };
// expected results based on new order
int[] ans = { 0, 0, 1, 2, 1, 2 };
CyMatrix mat = CyMatrixFactory.makeSmallMatrix(6, 2, data);
HopachablePAM pam = new HopachablePAM(null, mat, DistanceMetric.CITYBLOCK);
// permute sample order
Hopachable pamPermuted = pam.subset(index);
Clusters c = pamPermuted.cluster(k);
// the number of clusters should not change because it should always
// return the specified number of clusters
assertEquals(c.getNumberOfClusters(), k);
// check that the clustering results match
for (int i = 0; i < c.size(); ++i) {
assertEquals(c.getClusterIndex(i), ans[i]);
}
// minor test case
// subset the last 4 elements
int[] subsetIndex = { 2, 3, 4, 5 };
int[] subsetAns = { 0, 0, 1, 1 };
int subsetK = 2;
Hopachable pamSubset = pam.subset(subsetIndex);
Clusters c2 = pamSubset.cluster(subsetK);
// check number of clusters
assertEquals(c2.getNumberOfClusters(), subsetK);
// check cluster assignments
for (int i = 0; i < c2.size(); ++i) {
assertEquals(c2.getClusterIndex(i), subsetAns[i]);
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class PAM method cluster.
// @Override
public Clusters cluster(int k) {
int n = size();
if (n == 0) {
throw new IllegalArgumentException("No data elements are indexed.");
}
if (k > n) {
throw new IllegalArgumentException("Number of clusters must be less than the number of data elements.");
} else if (k == n) {
// build trivial single clusters
return new Clusters(k);
}
this.nClusters = k;
initialize();
buildPhase();
swapPhase();
clusters = new Clusters(nearestMedoids, getCost());
return clusters;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class Hopach method collapse.
/**
* Attempt to collapse clusters at the specified level.
* @param level
*/
Clusters collapse(int level) {
// split will be collapsed in-place
Clusters split = splits.get(level);
// a valid split must already be stored at the specified level
if (split == null) {
throw new IllegalArgumentException("Specified split level for collapse does not exist.");
}
int k = split.getNumberOfClusters();
if (k <= 2) {
// therefore, the correct action is: do nothing and return original split
return split;
}
int maxCollapses = k - 2;
for (int nCollapses = 0; nCollapses < maxCollapses; ++nCollapses) {
Pair nearest = nearestClusters(split);
Clusters collapsedSplit = partitioner.collapse(nearest.x, nearest.y, split);
// reduction in cost
double r = split.getCost() - collapsedSplit.getCost();
if (r >= minCostReduction) {
// save collapsed split, and continue
split = collapsedSplit;
} else {
// insufficient cost reduction: reject collapse and stop
break;
}
}
return split;
}
Aggregations