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;
}
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;
}
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();
}
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();
}
Aggregations