use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class CyMatrixFactory method makeTypedMatrix.
private static CyMatrix makeTypedMatrix(CyNetwork network, int rows, int columns, boolean transpose, MatrixType type) {
int nrows = rows;
int ncolumns = columns;
if (transpose) {
nrows = columns;
ncolumns = rows;
}
CyMatrix matrix = null;
switch(type) {
case SIMPLE:
matrix = new CySimpleMatrix(network, nrows, ncolumns);
break;
// For now, we use Colt for both large and sparse matrices
case COLT:
case SPARSE:
matrix = new CyColtMatrix(network, nrows, ncolumns);
break;
case LARGE:
case OJALGO:
matrix = new CyOjAlgoMatrix(network, nrows, ncolumns);
break;
}
matrix.setTransposed(transpose);
return matrix;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class CyMatrixFactory method makeLargeMatrix.
/**
* Create a large, possibly sparse matrix populated with data from
* the indicated edge attribute
*
* @param network the network that will be the source of the data
* @param edgeAttribute the edge attribute to pull the data from
* @param selectedOnly only include selected edges
* @param converter the edge weight converter to use
* @param unDirected if true, the edges are undirected
* @param cutOff the minimum edge value to consider
* @return the resulting matrix
*/
public static CyMatrix makeLargeMatrix(CyNetwork network, String edgeAttribute, boolean selectedOnly, EdgeWeightConverter converter, boolean unDirected, double cutOff) {
List<CyNode> nodes;
List<CyEdge> edges;
double maxAttribute = Double.MIN_VALUE;
double minAttribute = Double.MAX_VALUE;
if (!selectedOnly) {
nodes = network.getNodeList();
edges = network.getEdgeList();
} else {
nodes = new ArrayList<CyNode>();
edges = new ArrayList<CyEdge>();
nodes.addAll(CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true));
edges.addAll(ModelUtils.getConnectingEdges(network, nodes));
}
CyMatrix matrix = makeTypedMatrix(network, nodes.size(), nodes.size(), false, MatrixType.LARGE);
matrix.setRowNodes(nodes);
matrix.setColumnNodes(nodes);
Map<CyNode, Integer> nodeMap = new HashMap<CyNode, Integer>(nodes.size());
for (int row = 0; row < nodes.size(); row++) {
CyNode node = nodes.get(row);
nodeMap.put(node, row);
matrix.setRowLabel(row, ModelUtils.getNodeName(network, node));
matrix.setColumnLabel(row, ModelUtils.getNodeName(network, node));
}
matrix.setSymmetrical(unDirected);
CyTable edgeAttributes = network.getDefaultEdgeTable();
// First, we need the min and max values for our converter
if (edgeAttributes.getColumn(edgeAttribute) == null) {
minAttribute = 1.0;
maxAttribute = 1.0;
} else {
for (CyEdge edge : edges) {
if (network.getRow(edge).getRaw(edgeAttribute) == null)
continue;
double edgeWeight = ModelUtils.getNumericValue(network, edge, edgeAttribute);
if (edgeWeight < cutOff)
continue;
minAttribute = Math.min(minAttribute, edgeWeight);
maxAttribute = Math.max(maxAttribute, edgeWeight);
}
}
for (CyEdge edge : edges) {
double value;
if (minAttribute == 1.0 && maxAttribute == 1.0) {
value = 1.0;
} else {
Double val = ModelUtils.getNumericValue(network, edge, edgeAttribute);
if (val == null)
continue;
value = val.doubleValue();
}
double weight = converter.convert(value, minAttribute, maxAttribute);
if (weight < cutOff)
continue;
int sourceIndex = nodeMap.get(edge.getSource());
int targetIndex = nodeMap.get(edge.getTarget());
matrix.setValue(targetIndex, sourceIndex, weight);
// TODO: should we consider maybe doing this on the getValue side?
if (unDirected)
matrix.setValue(sourceIndex, targetIndex, weight);
}
// System.out.println("distance matrix: "+matrix.printMatrix());
return matrix;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class CyOjAlgoMatrix method getDistanceMatrix.
public CyMatrix getDistanceMatrix(DistanceMetric metric) {
CyOjAlgoMatrix dist = new CyOjAlgoMatrix(network, nRows, nRows);
if (rowNodes != null) {
dist.rowNodes = Arrays.copyOf(rowNodes, nRows);
dist.columnNodes = Arrays.copyOf(rowNodes, nRows);
}
Matrix cMatrix = super.getDistanceMatrix(metric);
return dist.copy(cMatrix);
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix 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.CyMatrix 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;
}
Aggregations