use of edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric in project clusterMaker2 by RBVI.
the class KMeansCluster method run.
public void run(TaskMonitor monitor) {
this.monitor = monitor;
monitor.setTitle("Performing " + getName());
List<String> nodeAttributeList = context.attributeList.getNodeAttributeList();
String edgeAttribute = context.attributeList.getEdgeAttribute();
if (nodeAttributeList == null && edgeAttribute == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Must select either one edge column or two or more node columns");
return;
}
if (nodeAttributeList != null && nodeAttributeList.size() > 0 && edgeAttribute != null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't have both node and edge columns selected");
return;
}
if (context.selectedOnly && CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true).size() < 3) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Must have at least three nodes to cluster");
return;
}
createGroups = context.createGroups;
if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
// To make debugging easier, sort the attribute list
Collections.sort(nodeAttributeList);
}
// Get our attributes we're going to use for the cluster
String[] attributeArray;
if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
attributeArray = new String[nodeAttributeList.size()];
int i = 0;
for (String attr : nodeAttributeList) {
attributeArray[i++] = "node." + attr;
}
} else {
attributeArray = new String[1];
attributeArray[0] = "edge." + edgeAttribute;
}
monitor.setStatusMessage("Initializing");
// System.out.println("Initializing");
resetAttributes(network, SHORTNAME);
// Create a new clusterer
RunKCluster algorithm = new RunKCluster(network, attributeArray, distanceMetric, monitor, context, this);
// System.out.println("Algorithm defined");
String resultsString = "K-Means results:";
// Cluster the attributes, if requested
if (context.clusterAttributes && attributeArray.length > 1) {
monitor.setStatusMessage("Clustering attributes");
// System.out.println("Clustering attributes: k="+context.kcluster.kNumber);
Integer[] rowOrder = algorithm.cluster(clusterManager, context.kcluster.kNumber, context.iterations, true, "kmeans", context.kcluster, false);
attributeList = algorithm.getAttributeList();
updateAttributes(network, SHORTNAME, rowOrder, attributeArray, attributeList, algorithm.getMatrix());
attributeSilhouette = algorithm.getSilhouettes();
attributeOrder = getOrder(rowOrder, algorithm.getMatrix());
}
// Cluster the nodes
monitor.setStatusMessage("Clustering nodes");
Integer[] rowOrder = algorithm.cluster(clusterManager, context.kcluster.kNumber, context.iterations, false, "kmeans", context.kcluster, createGroups);
nodeList = algorithm.getAttributeList();
updateAttributes(network, SHORTNAME, rowOrder, attributeArray, nodeList, algorithm.getMatrix());
nodeSilhouette = algorithm.getSilhouettes();
nodeOrder = getOrder(rowOrder, algorithm.getMatrix());
// System.out.println(resultsString);
if (context.showUI) {
insertTasksAfterCurrentTask(new KnnView(clusterManager));
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric in project clusterMaker2 by RBVI.
the class DBSCAN method run.
public void run(TaskMonitor monitor) {
this.monitor = monitor;
monitor.setTitle("Performing " + getName());
List<String> nodeAttributeList = context.attributeList.getNodeAttributeList();
String edgeAttribute = context.attributeList.getEdgeAttribute();
if (nodeAttributeList == null && edgeAttribute == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Must select either one edge column or two or more node columns");
return;
}
if (nodeAttributeList != null && nodeAttributeList.size() > 0 && edgeAttribute != null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't have both node and edge columns selected");
return;
}
if (context.selectedOnly && CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true).size() < 3) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Must have at least three nodes to cluster");
return;
}
createGroups = context.createGroups;
if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
// To make debugging easier, sort the attribute list
Collections.sort(nodeAttributeList);
}
// Get our attributes we're going to use for the cluster
String[] attributeArray;
if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
attributeArray = new String[nodeAttributeList.size()];
int i = 0;
for (String attr : nodeAttributeList) {
attributeArray[i++] = "node." + attr;
}
} else {
attributeArray = new String[1];
attributeArray[0] = "edge." + edgeAttribute;
}
monitor.setStatusMessage("Initializing");
resetAttributes(network, SHORTNAME);
distanceMetric = context.getDistanceMetric();
// Create a new clusterer
RunDBSCAN algorithm = new RunDBSCAN(network, attributeArray, distanceMetric, monitor, context);
String resultsString = "DBSCAN results:";
// Cluster the attributes, if requested
if (context.clusterAttributes && attributeArray.length > 1) {
monitor.setStatusMessage("Clustering attributes");
int[] clusters = algorithm.cluster(true);
if (!algorithm.getMatrix().isTransposed())
createGroups(network, algorithm.getMatrix(), algorithm.getNClusters(), clusters, "dbscan");
Integer[] rowOrder = MatrixUtils.indexSort(clusters, clusters.length);
// Integer[] rowOrder = algorithm.cluster(context.kcluster.kNumber,1, true, "dbscan", context.kcluster);
updateAttributes(network, SHORTNAME, rowOrder, attributeArray, getAttributeList(), algorithm.getMatrix());
}
// Cluster the nodes
monitor.setStatusMessage("Clustering nodes");
int[] clusters = algorithm.cluster(false);
int nNodes = 0;
for (int i = 0; i < clusters.length; i++) {
if (clusters[i] >= 0)
nNodes++;
}
monitor.setStatusMessage("Allocated " + nNodes + " nodes to " + algorithm.getNClusters() + " clusters");
if (!algorithm.getMatrix().isTransposed()) {
createGroups(network, algorithm.getMatrix(), algorithm.getNClusters(), clusters, "dbscan");
}
Integer[] rowOrder = MatrixUtils.indexSort(clusters, clusters.length);
// In DBSCAN, not all nodes will be assigned to a cluster, so they will have a cluster # of -1. Find
// all of those and trim rowOrder accordingly.
Integer[] newRowOrder = new Integer[nNodes];
int newOrder = 0;
for (int i = 0; i < rowOrder.length; i++) {
int nodeIndex = rowOrder[i];
if (clusters[nodeIndex] >= 0) {
newRowOrder[newOrder] = nodeIndex;
newOrder++;
}
}
// Integer[] rowOrder = algorithm.cluster(context.kcluster.kNumber,1, false, "dbscan", context.kcluster);
updateAttributes(network, SHORTNAME, newRowOrder, attributeArray, getAttributeList(), algorithm.getMatrix());
// System.out.println(resultsString);
if (context.showUI) {
insertTasksAfterCurrentTask(new KnnView(clusterManager));
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric in project clusterMaker2 by RBVI.
the class RunFFT method kcluster.
@Override
public int kcluster(int nClusters, int nIterations, CyMatrix matrix, DistanceMetric metric, int[] clusterID) {
random = null;
int nelements = matrix.nRows();
int[] tclusterid = new int[nelements];
int[] saved = new int[nelements];
int[] mapping = new int[nClusters];
int[] counts = new int[nClusters];
CyMatrix distanceMatrix = matrix.getDistanceMatrix(metric);
HashMap<Integer, Integer> centers = new HashMap<Integer, Integer>();
double error = Double.MAX_VALUE;
if (monitor != null)
monitor.setProgress(0);
for (int i = 0; i < nelements; i++) clusterID[i] = 0;
// the first center
Random randomGenerator = new Random();
centers.put(0, randomGenerator.nextInt(nelements));
// now find the remaining centers
for (int i = 1; i < nClusters; i++) {
int y = getMaxMin(centers, distanceMatrix);
centers.put(i, y);
clusterID[y] = i;
}
/*
for (int i = 0; i < nClusters; i++)
System.out.printf("Center for %d = %d\n", i, centers.get(i));
*/
// assign clusters now
int k = centers.get(0);
for (int i = 0; i < nelements; i++) {
// Is this one of our centers?
if (centers.containsValue(i))
continue;
double minDistance = Double.MAX_VALUE;
int center = 0;
for (int cluster = 0; cluster < nClusters; cluster++) {
double dist = distanceMatrix.doubleValue(i, centers.get(cluster));
if (dist < minDistance) {
center = cluster;
minDistance = dist;
}
}
clusterID[i] = center;
}
return nClusters;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric in project clusterMaker2 by RBVI.
the class CyColtMatrix method getDistanceMatrix.
public CyMatrix getDistanceMatrix(DistanceMetric metric) {
CyColtMatrix dist = new CyColtMatrix(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.DistanceMetric 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);
}
Aggregations