Search in sources :

Example 11 with DoubleMatrix2D

use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.

the class RunTransClust method run.

public List<NodeCluster> run(TaskMonitor monitor, CyNetwork network) {
    DoubleMatrix2D matrix = this.distanceMatrix.getColtMatrix();
    nodes = distanceMatrix.getRowNodes();
    HashMap<String, CyNode> nodeHash = new HashMap<String, CyNode>();
    for (CyNode node : nodes) {
        nodeHash.put(ModelUtils.getNodeName(network, node), node);
    }
    HashMap<String, Integer> integers2proteins = new HashMap<String, Integer>();
    HashMap<Integer, String> proteins2integers = new HashMap<Integer, String>();
    int count = 0;
    for (CyNode node : this.nodes) {
        integers2proteins.put(ModelUtils.getNodeName(network, node), count);
        proteins2integers.put(count, ModelUtils.getNodeName(network, node));
        count++;
    }
    Edges es = new Edges(this.nodes.size() * this.nodes.size(), this.nodes.size());
    count = 0;
    for (int i = 0; i < this.nodes.size(); i++) {
        CyNode cyNodeI = this.nodes.get(i);
        es.startPositions[integers2proteins.get(cyNodeI.getSUID())] = count;
        for (int j = 0; j < this.nodes.size(); j++) {
            CyNode cyNodeJ = this.nodes.get(j);
            es.sources[count] = i;
            es.targets[count] = j;
            Double val = distanceMatrix.getValue(i, j);
            if (val != null) {
                es.values[count] = val.floatValue();
                count++;
            }
        }
        es.endPositions[integers2proteins.get(cyNodeI.getSUID())] = count - 1;
    }
    Semaphore s = new Semaphore(1);
    TaskConfig.mode = TaskConfig.COMPARISON_MODE;
    TaskConfig.monitor = monitor;
    IteratorThread it = new IteratorThread(es, integers2proteins, proteins2integers, s);
    TaskConfig.minThreshold = threshold;
    TaskConfig.maxThreshold = threshold;
    try {
        s.acquire();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    it.start();
    monitor.showMessage(TaskMonitor.Level.INFO, "Executing TransClust Clustering...");
    try {
        s.acquire();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Assigning nodes to clusters");
    String result = it.resultsStringBuffer.toString();
    String[] clusters = result.split("\t")[2].split(";");
    Map<Integer, NodeCluster> clusterMap = getClusterMap(clusters, nodeHash);
    // Update node attributes in network to include clusters. Create cygroups from clustered nodes
    monitor.showMessage(TaskMonitor.Level.INFO, "Created " + clusterMap.size() + " clusters");
    if (clusterCount == 0) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Created 0 clusters!!!!");
        return null;
    }
    int clusterNumber = 1;
    Map<NodeCluster, NodeCluster> cMap = new HashMap();
    for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
        if (cMap.containsKey(cluster))
            continue;
        cMap.put(cluster, cluster);
        cluster.setClusterNumber(clusterNumber);
        clusterNumber++;
    }
    Set<NodeCluster> clusters2 = cMap.keySet();
    return new ArrayList<NodeCluster>(clusters2);
}
Also used : HashMap(java.util.HashMap) IteratorThread(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.iterativeclustering.IteratorThread) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) Edges(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) CyNode(org.cytoscape.model.CyNode)

Example 12 with DoubleMatrix2D

use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.

the class KCluster method kmeans.

// perform Kmeans clustering on columns of matrix, the size of whose row is K
public static int kmeans(int nClusters, int nIterations, DoubleMatrix2D matrix, int[] clusterID) {
    int nelements = matrix.rows();
    // System.out.println("NClusters " + nClusters + " NElements " + nelements);
    int ifound = 1;
    int[] tclusterid = new int[nelements];
    int[] saved = new int[nelements];
    int[] mapping = new int[nClusters];
    int[] counts = new int[nClusters];
    double error = Double.MAX_VALUE;
    // This matrix will store the KxK centroid data.matrix
    DoubleMatrix2D cData = DoubleFactory2D.sparse.make(nClusters, nClusters);
    // Outer initialization
    if (nIterations <= 1) {
        for (int i = 0; i < clusterID.length; i++) {
            tclusterid[i] = clusterID[i];
        }
        nIterations = 1;
    } else {
        for (int i = 0; i < nelements; i++) clusterID[i] = 0;
    }
    int iteration = 0;
    boolean firstIteration = true;
    do {
        double total = Double.MAX_VALUE;
        int counter = 0;
        int period = 10;
        // Randomly assign elements to clusters
        if (nIterations != 0)
            randomAssign(nClusters, nelements, tclusterid);
        // Initialize
        for (int i = 0; i < nClusters; i++) counts[i] = 0;
        for (int i = 0; i < nelements; i++) counts[tclusterid[i]]++;
        while (true) {
            double previous = total;
            total = 0.0;
            if (// Save the current cluster assignments
            counter % period == 0) {
                for (int i = 0; i < nelements; i++) saved[i] = tclusterid[i];
                if (period < Integer.MAX_VALUE / 2)
                    period *= 2;
            }
            counter++;
            // assign centroids as to maximize orthogonality on first iteration of kmeans
            if (firstIteration) {
                selectCentroidsOrthogonally(nClusters, matrix, cData);
                firstIteration = false;
            } else
                // Find the centeroids based on cluster means on all other iterations
                getClusterMeans(nClusters, matrix, cData, tclusterid);
            for (int i = 0; i < nelements; i++) {
                // Calculate the distances
                double distance;
                int k = tclusterid[i];
                if (counts[k] == 1)
                    continue;
                // Get the distance
                distance = getDistance(i, k, matrix, cData);
                for (int j = 0; j < nClusters; j++) {
                    double tdistance;
                    if (j == k)
                        continue;
                    tdistance = getDistance(i, j, matrix, cData);
                    if (tdistance < distance) {
                        distance = tdistance;
                        counts[tclusterid[i]]--;
                        tclusterid[i] = j;
                        counts[j]++;
                    }
                }
                total += distance;
            }
            if (total >= previous)
                break;
            /* total>=previous is FALSE on some machines even if total and previous
				 * are bitwise identical. */
            int i;
            for (i = 0; i < nelements; i++) if (saved[i] != tclusterid[i])
                break;
            if (i == nelements)
                break;
        /* Identical solution found; break out of this loop */
        }
        if (nIterations <= 1) {
            error = total;
            break;
        }
        for (int i = 0; i < nClusters; i++) mapping[i] = -1;
        int element = 0;
        for (element = 0; element < nelements; element++) {
            int j = tclusterid[element];
            int k = clusterID[element];
            if (mapping[k] == -1)
                mapping[k] = j;
            else if (mapping[k] != j) {
                if (total < error) {
                    ifound = 1;
                    error = total;
                    for (int i = 0; i < nelements; i++) clusterID[i] = tclusterid[i];
                }
                break;
            }
        }
        if (element == nelements)
            ifound++;
    /* break statement not encountered */
    } while (++iteration < nIterations);
    return ifound;
}
Also used : DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D)

Example 13 with DoubleMatrix2D

use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.

the class RunSCPS method getLMat.

// L = D^-1/2 * S * D^-1/2
public DoubleMatrix2D getLMat(DoubleMatrix2D sMat) {
    DenseDoubleAlgebra alg = new DenseDoubleAlgebra();
    DoubleMatrix2D dMat = getDMat(sMat);
    DoubleMatrix2D transDMat = getNegSqrRootDMat(dMat);
    return alg.mult(transDMat, alg.mult(sMat, transDMat));
}
Also used : DenseDoubleAlgebra(cern.colt.matrix.tdouble.algo.DenseDoubleAlgebra) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D)

Example 14 with DoubleMatrix2D

use of cern.colt.matrix.tdouble.DoubleMatrix2D 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 15 with DoubleMatrix2D

use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.

the class RunSCPS method getNegSqrRoot.

// Calculate negative square root of matrix using singular value decomposition
public DoubleMatrix2D getNegSqrRoot(DoubleMatrix2D A) {
    // A = USV, where S is Diagnol Matrix
    DenseDoubleSingularValueDecomposition decomp = new DenseDoubleSingularValueDecomposition(A, true, true);
    DoubleMatrix2D U = decomp.getU();
    DoubleMatrix2D S = decomp.getS();
    DoubleMatrix2D V = decomp.getV();
    // S^1/2 = Square root of every value in diangol matrix
    for (int i = 0; i < S.rows(); i++) S.set(i, i, Math.pow(S.get(i, i), .5));
    // A^1/2 = VS^1/2U
    DenseDoubleAlgebra alg = new DenseDoubleAlgebra();
    DoubleMatrix2D sqrtA = alg.mult(alg.mult(V, S), U);
    // return A^-1/2
    return alg.inverse(sqrtA);
}
Also used : DenseDoubleAlgebra(cern.colt.matrix.tdouble.algo.DenseDoubleAlgebra) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) DenseDoubleSingularValueDecomposition(cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleSingularValueDecomposition)

Aggregations

DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)30 IntIntDoubleFunction (cern.colt.function.tdouble.IntIntDoubleFunction)8 DenseDoubleAlgebra (cern.colt.matrix.tdouble.algo.DenseDoubleAlgebra)5 DoubleArrayList (cern.colt.list.tdouble.DoubleArrayList)3 IntArrayList (cern.colt.list.tint.IntArrayList)3 DoubleMatrix1D (cern.colt.matrix.tdouble.DoubleMatrix1D)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CyNode (org.cytoscape.model.CyNode)3 DenseDoubleEigenvalueDecomposition (cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleEigenvalueDecomposition)2 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)2 DenseDoubleSingularValueDecomposition (cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleSingularValueDecomposition)1 SparseDoubleMatrix2D (cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D)1 Edges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges)1 IteratorThread (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.iterativeclustering.IteratorThread)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 structures._Node (structures._Node)1