Search in sources :

Example 11 with DistanceMetric

use of edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric in project clusterMaker2 by RBVI.

the class CySimpleMatrix method getDistanceMatrix.

public CyMatrix getDistanceMatrix(DistanceMetric metric) {
    if (dist != null && metric == distanceMetric)
        return dist;
    distanceMetric = metric;
    Matrix cMatrix = super.getDistanceMatrix(metric);
    // System.out.println("CyMatrix got Matrix distance matrix -- making copy");
    dist = new CySimpleMatrix(this.network);
    SimpleMatrix sMatrix = (SimpleMatrix) cMatrix;
    dist.data = sMatrix.data;
    dist.transposed = sMatrix.transposed;
    dist.symmetric = sMatrix.symmetric;
    dist.minValue = sMatrix.minValue;
    dist.maxValue = sMatrix.maxValue;
    // System.out.println("Copying labels");
    dist.rowLabels = sMatrix.rowLabels;
    dist.columnLabels = sMatrix.columnLabels;
    dist.nRows = sMatrix.nRows;
    dist.nColumns = sMatrix.nColumns;
    if (rowNodes != null) {
        dist.rowNodes = Arrays.copyOf(rowNodes, nRows);
        dist.columnNodes = Arrays.copyOf(rowNodes, nRows);
    }
    // System.out.println("CyMatrix got Matrix distance matrix -- done");
    return dist;
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 12 with DistanceMetric

use of edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric in project clusterMaker2 by RBVI.

the class RunKCluster method kcluster.

// The kmeans implementation of a k-clusterer
public int kcluster(int nClusters, int nIterations, CyMatrix matrix, DistanceMetric metric, int[] clusterID) {
    // System.out.println("Running kmeans with "+nClusters+" clusters");
    int nelements = matrix.nRows();
    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;
    if (monitor != null)
        monitor.setProgress(0);
    // System.out.println("Creating matrix for "+nClusters);
    // This matrix will store the centroid data
    // Matrix cData = new Matrix(network, nClusters, matrix.nColumns());
    CyMatrix cData = CyMatrixFactory.makeSmallMatrix(network, nClusters, matrix.nColumns());
    // 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;
    }
    // System.out.println("Entering do loop for "+nClusters);
    int iteration = 0;
    do {
        if (monitor != null)
            monitor.setProgress(((double) iteration / (double) nIterations));
        double total = Double.MAX_VALUE;
        int counter = 0;
        int period = 10;
        // Randomly assign elements to clusters
        if (nIterations != 0) {
            if (!context.kcluster.initializeNearCenter) {
                // System.out.println("Randomly assigning elements "+nClusters);
                // Use the cluster 3.0 version to be consistent
                chooseRandomElementsAsCenters(nelements, nClusters, tclusterid);
            // System.out.println("Done randomly assigning elements "+nClusters);
            // if (nIterations != 0) debugAssign(nClusters, nelements, tclusterid);
            } else {
                int[] centers = chooseCentralElementsAsCenters(nelements, nClusters, matrix.getDistanceMatrix(metric).toArray(), tclusterid);
            }
        }
        // Initialize
        for (int i = 0; i < nClusters; i++) counts[i] = 0;
        for (int i = 0; i < nelements; i++) counts[tclusterid[i]]++;
        // System.out.println("Inner loop starting "+nClusters);
        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++;
            // Find the center
            // System.out.println("Assigning cluster means "+nClusters);
            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 = metric(ndata,data,cdata,mask,cmask,weight,i,k,transpose);
                distance = metric.getMetric(matrix, cData, i, k);
                for (int j = 0; j < nClusters; j++) {
                    double tdistance;
                    if (j == k)
                        continue;
                    // tdistance = metric(ndata,data,cdata,mask,cmask,weight,i,j,transpose);
                    tdistance = metric.getMetric(matrix, cData, i, j);
                    if (tdistance < distance) {
                        distance = tdistance;
                        counts[tclusterid[i]]--;
                        tclusterid[i] = j;
                        counts[j]++;
                    }
                }
                total += distance;
            }
            // System.out.println("total = "+total+", previous = "+previous+" nClusters="+nClusters);
            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;
                    // System.out.println("Mapping tclusterid to clusterid nClusters = "+nClusters);
                    for (int i = 0; i < nelements; i++) clusterID[i] = tclusterid[i];
                }
                break;
            }
        }
        if (element == nelements)
            ifound++;
    /* break statement not encountered */
    } while (++iteration < nIterations);
    // System.out.println("ifound = "+ifound+", error = "+error);
    return ifound;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 13 with DistanceMetric

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

Example 14 with DistanceMetric

use of edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric in project clusterMaker2 by RBVI.

the class RunHopachPAM method kcluster.

@Override
public int kcluster(int nClusters, int nIterations, CyMatrix matrix, DistanceMetric metric, int[] clusterId) {
    monitor.setProgress(0);
    Summarizer summarizer;
    PrimitiveSummarizer psummarizer;
    switch(summaryMethod) {
        case MEDIAN:
            summarizer = new MedianSummarizer();
            psummarizer = new PrimitiveMedianSummarizer();
            break;
        case MEAN:
        default:
            summarizer = new MeanSummarizer();
            psummarizer = new PrimitiveMeanSummarizer();
            break;
    }
    HopachablePAM partitioner = new HopachablePAM(network, matrix, metric);
    partitioner.setParameters(K, L, splitCost, summarizer);
    HopachPAM hopachPam = new HopachPAM(partitioner);
    hopachPam.setParameters(maxLevel, minCostReduction, forceInitSplit, psummarizer);
    Clusters c = hopachPam.run();
    // copy results into clusterId
    for (int i = 0; i < c.size(); ++i) {
        clusterId[i] = c.getClusterIndex(i);
    }
    return c.getNumberOfClusters();
}
Also used : PrimitiveMeanSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMeanSummarizer) MeanSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MeanSummarizer) PrimitiveMeanSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMeanSummarizer) PrimitiveSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveSummarizer) PrimitiveMedianSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMedianSummarizer) Clusters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters) HopachablePAM(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.pam.HopachablePAM) PrimitiveMeanSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMeanSummarizer) MedianSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MedianSummarizer) PrimitiveSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveSummarizer) MeanSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MeanSummarizer) PrimitiveMedianSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMedianSummarizer) Summarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.Summarizer) MedianSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MedianSummarizer) PrimitiveMedianSummarizer(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMedianSummarizer)

Aggregations

CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)7 KnnView (edu.ucsf.rbvi.clusterMaker2.internal.ui.KnnView)4 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)3 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)2 DistanceMetric (edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric)2 HopachablePAM (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.pam.HopachablePAM)1 MeanSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MeanSummarizer)1 MedianSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MedianSummarizer)1 PrimitiveMeanSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMeanSummarizer)1 PrimitiveMedianSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMedianSummarizer)1 PrimitiveSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveSummarizer)1 Summarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.Summarizer)1 TreeView (edu.ucsf.rbvi.clusterMaker2.internal.ui.TreeView)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 CyNode (org.cytoscape.model.CyNode)1