Search in sources :

Example 1 with DoubleArrayList

use of cern.colt.list.tdouble.DoubleArrayList in project clusterMaker2 by RBVI.

the class RunSCPS method getUMat.

// U constructed from top K Eigenvectors of L. After construction, each row of U is normalized to unit length.
public DoubleMatrix2D getUMat(DoubleMatrix2D eigenVect, int k) {
    DoubleMatrix2D uMat;
    IntArrayList indexList = new IntArrayList();
    DoubleArrayList valueList = new DoubleArrayList();
    // construct matrix U from first K eigenvectors (ordered in ascending value by eigenvalue in eigenVect so start with the k-to-last column)
    uMat = eigenVect.viewPart(0, eigenVect.columns() - k, eigenVect.rows(), k);
    // Normalize each row of matrix U to have unit length
    for (int i = 0; i < uMat.columns(); i++) {
        DoubleMatrix1D row = uMat.viewRow(i);
        double rowLength = Math.pow(row.zDotProduct(row), .5);
        row.getNonZeros(indexList, valueList);
        // normalize each Nozero value in row
        for (int j = 0; j < indexList.size(); j++) {
            int index = indexList.get(j);
            double value = valueList.get(j) / rowLength;
            uMat.set(i, index, value);
        }
    }
    return uMat;
}
Also used : DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.tdouble.DoubleMatrix1D) IntArrayList(cern.colt.list.tint.IntArrayList) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList)

Example 2 with DoubleArrayList

use of cern.colt.list.tdouble.DoubleArrayList in project clusterMaker2 by RBVI.

the class RunSCPS method redistributeMaxCluster.

// Takes largest cluster obtained by Kmeans and redisributes some of its elements across the other clusters via Kurucz Algorithm
public int[] redistributeMaxCluster(int[] clusters, DoubleMatrix2D sMat, int k) {
    int maxClusterID = -1;
    int maxClusterSize = -1;
    int maxClusterConnection = -1;
    double maxClusterConnectionSize = -1;
    IntArrayList indexList = new IntArrayList();
    DoubleArrayList valueList = new DoubleArrayList();
    // Array of cluster sizes
    int[] clusterSizeArray = new int[k];
    // array of redistributed clusters
    int[] redistribClusters = new int[clusters.length];
    // array summing edge connections from node in largest cluster to all other clusters
    double[] clusterConnectionCount = new double[k];
    for (int i = 0; i < clusterSizeArray.length; i++) clusterSizeArray[i] = 0;
    // compute size of each cluster
    for (int i = 0; i < clusters.length; i++) {
        int clusterID = clusters[i];
        clusterSizeArray[clusterID] += 1;
    }
    // find max cluster size and max cluster id
    for (int i = 0; i < clusterSizeArray.length; i++) {
        int clusterSize = clusterSizeArray[i];
        if (clusterSize > maxClusterSize) {
            maxClusterSize = clusterSize;
            maxClusterID = i;
        }
    }
    // run loop until no changes observed in cluster transfers
    while (true) {
        int transfer_count = 0;
        // loop through SMat redistribute elements in largest cluster based on edge weight connectivity
        for (int i = 0; i < clusters.length; i++) {
            // node belongs to one of smaller clusters. Merely add existing cluster value to redistributed cluster array
            if (clusters[i] != maxClusterID) {
                redistribClusters[i] = clusters[i];
                continue;
            }
            // index corresponds to element in main cluster. Count the cluster connections from node
            for (int j = 0; j < k; j++) clusterConnectionCount[j] = 0;
            maxClusterConnection = -1;
            maxClusterConnectionSize = -1;
            DoubleMatrix1D row = sMat.viewRow(i);
            row.getNonZeros(indexList, valueList);
            // loop through existing edges for node and record how many times the connection bridges each cluster
            for (int j = 0; j < indexList.size(); j++) {
                int connectingNode = indexList.get(j);
                int connectingNodeCluster = clusters[connectingNode];
                clusterConnectionCount[connectingNodeCluster] += valueList.get(j);
            }
            // loop through cluster connection counts and find cluster with greatest number of avg edge connections
            for (int j = 0; j < k; j++) {
                double avgConnectionSize = clusterConnectionCount[j] / (double) (clusterSizeArray[j] + 1);
                if (maxClusterConnectionSize < avgConnectionSize) {
                    maxClusterConnectionSize = avgConnectionSize;
                    maxClusterConnection = j;
                }
            }
            // update redistributed cluster array to reflect maxClusterConnection
            redistribClusters[i] = maxClusterConnection;
            if (clusters[i] != redistribClusters[i]) {
                transfer_count++;
                System.out.println("Node " + i + " moved from " + clusters[i] + " to " + redistribClusters[i]);
            }
        }
        // transfer has occured, update clusters to equal redistrib clusters
        if (transfer_count > 0) {
            for (int i = 0; i < clusters.length; i++) if (clusters[i] != redistribClusters[i]) {
                int clusterID = redistribClusters[i];
                clusterSizeArray[maxClusterID]--;
                clusterSizeArray[clusterID]++;
                clusters[i] = redistribClusters[i];
            }
            System.out.println("Transfer Count " + transfer_count + " MaxClusterSize " + clusterSizeArray[maxClusterID]);
        } else
            // No transfer occured. Break out of loop
            break;
    }
    return redistribClusters;
}
Also used : DoubleMatrix1D(cern.colt.matrix.tdouble.DoubleMatrix1D) IntArrayList(cern.colt.list.tint.IntArrayList) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList)

Example 3 with DoubleArrayList

use of cern.colt.list.tdouble.DoubleArrayList in project clusterMaker2 by RBVI.

the class RunSCPS method getSMat.

// Get Connected Components, cluster all components <= |5|, and connect the remaining components with random lowscoring edges
public DoubleMatrix2D getSMat(CyMatrix distanceMatrix) {
    // Matrix prior to filtration modification
    DoubleMatrix2D unfiltered_mat = distanceMatrix.getColtMatrix();
    // Size of newly created Umat after filtering of small components
    int sMat_rows = 0;
    HashMap<Integer, List<CyNode>> filtered_cmap = new HashMap<Integer, List<CyNode>>();
    // Connected Componets
    Map<Integer, List<CyNode>> cMap = MatrixUtils.findConnectedComponents(distanceMatrix);
    IntArrayList rowList = new IntArrayList();
    IntArrayList columnList = new IntArrayList();
    DoubleArrayList valueList = new DoubleArrayList();
    // Iterate through connected components
    int component_size_sum = 0;
    for (List<CyNode> component : cMap.values()) {
        numComponents += 1;
        // Size <= 5. Automatically create cluster and increment clusterCount.
        if (component.size() <= 5) {
            NodeCluster iCluster = new NodeCluster(component);
            iCluster.setClusterNumber(this.clusterCount);
            // iCluster.add(component,this.clusterCount);
            this.clusterMap.put(new Integer(clusterCount), iCluster);
            this.clusterCount++;
        } else {
            // iterate through components and assign them index mappings in new uMatrix
            component_size_sum += component.size();
            System.out.println("Normal Component size " + component.size() + " Total Sum " + component_size_sum);
            for (int i = 0; i < component.size(); i++) {
                CyNode n = component.get(i);
                int node_id = this.nodes.indexOf(n);
                // set mapping of new matrix index to old index
                setMap(node_id, sMat_rows);
                sMat_rows++;
            }
        }
    }
    DoubleMatrix2D sMat = DoubleFactory2D.sparse.make(sMat_rows, sMat_rows);
    // set diagnols of sMat to one
    for (int i = 0; i < sMat_rows; i++) sMat.set(i, i, 1);
    // iterate through nonzero edges. If both nodes in new index map, transfer the edge to new matrix
    unfiltered_mat.getNonZeros(rowList, columnList, valueList);
    for (int i = 0; i < rowList.size(); i++) {
        int row_id = rowList.get(i);
        int column_id = columnList.get(i);
        int new_row_id = getMap_new(row_id);
        int new_column_id = getMap_new(column_id);
        double value = valueList.get(i);
        // Set symmetrically the values in new matrix
        if (new_row_id > -1 && new_column_id > -1) {
            sMat.set(new_row_id, new_column_id, value);
            sMat.set(new_column_id, new_row_id, value);
        }
    }
    return sMat;
}
Also used : HashMap(java.util.HashMap) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList) IntArrayList(cern.colt.list.tint.IntArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CyNode(org.cytoscape.model.CyNode) IntArrayList(cern.colt.list.tint.IntArrayList)

Aggregations

DoubleArrayList (cern.colt.list.tdouble.DoubleArrayList)3 IntArrayList (cern.colt.list.tint.IntArrayList)3 DoubleMatrix1D (cern.colt.matrix.tdouble.DoubleMatrix1D)2 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)2 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CyNode (org.cytoscape.model.CyNode)1