Search in sources :

Example 16 with Clusters

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;
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 17 with 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);
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Clusters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters) Test(org.junit.Test)

Example 18 with Clusters

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]);
    }
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Clusters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters) Hopachable(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.hopach.types.Hopachable) Test(org.junit.Test)

Example 19 with Clusters

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;
}
Also used : Clusters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)

Example 20 with 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;
}
Also used : Clusters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)

Aggregations

ArrayList (java.util.ArrayList)30 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)27 CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)22 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)21 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)16 HashMap (java.util.HashMap)16 List (java.util.List)15 CyNode (org.cytoscape.model.CyNode)13 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)10 Test (org.junit.Test)9 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)8 ConnectedComponent (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent)6 Random (java.util.Random)5 Vector (java.util.Vector)5 Semaphore (java.util.concurrent.Semaphore)5 Edges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges)3 ICCEdges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ICCEdges)3 IParameters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.IParameters)3 ClusteringManager (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager)3 PREdge (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.ranking.units.PREdge)3