use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class RunAP method run.
public List<NodeCluster> run(CyNetwork network, TaskMonitor monitor) {
double numClusters;
monitor.setProgress(0.01);
for (int i = 0; i < number_iterations; i++) {
monitor.showMessage(TaskMonitor.Level.INFO, "Exchanging messages: iteration " + i);
iterate_message_exchange(monitor, i);
if (canceled) {
monitor.showMessage(TaskMonitor.Level.INFO, "canceled");
return null;
}
monitor.setProgress((double) i / (double) number_iterations);
}
if (debug) {
for (int i = 0; i < s_matrix.rows(); i++) {
monitor.showMessage(TaskMonitor.Level.INFO, "Node " + nodes.get(i) + " has exemplar " + get_exemplar(i));
}
}
monitor.showMessage(TaskMonitor.Level.INFO, "Assigning nodes to clusters");
Map<Integer, NodeCluster> clusterMap = getClusterMap();
// 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<NodeCluster, NodeCluster>();
for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
if (cMap.containsKey(cluster))
continue;
if (debug) {
monitor.showMessage(TaskMonitor.Level.INFO, "Cluster " + clusterNumber);
String s = "";
for (CyNode node : cluster) {
s += node.toString() + "\t";
}
monitor.showMessage(TaskMonitor.Level.INFO, s);
}
cMap.put(cluster, cluster);
cluster.setClusterNumber(clusterNumber);
clusterNumber++;
}
Set<NodeCluster> clusters = cMap.keySet();
return new ArrayList<NodeCluster>(clusters);
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class RunAP method getClusterMap.
private Map<Integer, NodeCluster> getClusterMap() {
HashMap<Integer, NodeCluster> clusterMap = new HashMap<Integer, NodeCluster>();
for (int i = 0; i < s_matrix.rows(); i++) {
int exemplar = get_exemplar(i);
if (clusterMap.containsKey(exemplar)) {
if (i == exemplar)
continue;
// Already seen exemplar
NodeCluster exemplarCluster = clusterMap.get(exemplar);
if (clusterMap.containsKey(i)) {
// We've already seen i also -- join them
NodeCluster iCluster = clusterMap.get(i);
if (iCluster != exemplarCluster) {
exemplarCluster.addAll(iCluster);
// System.out.println("Combining "+i+"["+iCluster+"] and "+exemplar+" ["+exemplarCluster+"]");
clusterCount--;
clusterMap.remove(i);
}
} else {
exemplarCluster.add(nodes, i);
// System.out.println("Adding "+i+" to ["+exemplarCluster+"]");
}
// Update Clusters
updateClusters(exemplarCluster, clusterMap);
} else {
NodeCluster iCluster;
// First time we've seen this "exemplar" -- have we already seen "i"?
if (clusterMap.containsKey(i)) {
if (i == exemplar)
continue;
// Yes, just add exemplar to i's cluster
iCluster = clusterMap.get(i);
iCluster.add(nodes, exemplar);
// System.out.println("Adding "+exemplar+" to ["+iCluster+"]");
} else {
// No create new cluster from scratch
iCluster = new NodeCluster();
iCluster.add(nodes, i);
if (exemplar != i)
iCluster.add(nodes, exemplar);
// System.out.println("New cluster ["+iCluster+"]");
clusterCount++;
}
updateClusters(iCluster, clusterMap);
}
}
return clusterMap;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class AbstractFuzzyNetworkClusterer method createFuzzyTable.
/**
* Method creates a table to store the information about Fuzzy Clusters and adds it to the network
*
* @param clusters List of FuzzyNodeCLusters, which have to be put in the table
*/
protected void createFuzzyTable(List<FuzzyNodeCluster> clusters) {
CyTable networkTable = network.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS);
CyTable FuzzyClusterTable = null;
if (!CyTableUtil.getColumnNames(networkTable).contains(clusterAttributeName + "_Table.SUID")) {
network.getDefaultNetworkTable().createColumn(clusterAttributeName + "_Table.SUID", Long.class, false);
FuzzyClusterTable = tableFactory.createTable(clusterAttributeName + "_Table", "Fuzzy_Node.SUID", Long.class, true, true);
} else {
long FuzzyClusterTableSUID = network.getRow(network).get(clusterAttributeName + "_Table.SUID", Long.class);
FuzzyClusterTable = tableManager.getTable(FuzzyClusterTableSUID);
}
for (FuzzyNodeCluster cluster : clusters) {
if (FuzzyClusterTable.getColumn("Cluster_" + cluster.getClusterNumber()) == null) {
FuzzyClusterTable.createColumn("Cluster_" + cluster.getClusterNumber(), Double.class, false);
}
}
CyRow TableRow;
for (CyNode node : network.getNodeList()) {
TableRow = FuzzyClusterTable.getRow(node.getSUID());
for (FuzzyNodeCluster cluster : clusters) {
TableRow.set("Cluster_" + cluster.getClusterNumber(), cluster.getMembership(node));
}
}
network.getRow(network).set(clusterAttributeName + "_Table.SUID", FuzzyClusterTable.getSUID());
tableManager.addTable(FuzzyClusterTable);
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class AbstractNetworkClusterer method createFuzzyGroups.
protected List<List<CyNode>> createFuzzyGroups(CyNetwork network, List<FuzzyNodeCluster> clusters, String group_attr) {
// List of node lists
List<List<CyNode>> clusterList = new ArrayList<List<CyNode>>();
// keep track of the groups we create
List<Long> groupList = new ArrayList<Long>();
for (FuzzyNodeCluster cluster : clusters) {
int clusterNumber = cluster.getClusterNumber();
String groupName = clusterAttributeName + "_" + clusterNumber;
List<CyNode> nodeList = new ArrayList<CyNode>();
for (CyNode node : cluster) {
nodeList.add(node);
// network.getRow(node).set(clusterAttributeName, clusterNumber);
if (FuzzyNodeCluster.hasScore()) {
ModelUtils.createAndSetLocal(network, node, clusterAttributeName + "_" + clusterNumber + "_Membership", cluster.getMembership(node), Double.class, null);
// network.getRow(node).set(clusterAttributeName+"_Score", cluster.getClusterScore());
}
}
if (createGroups) {
CyGroup group = clusterManager.createGroup(network, clusterAttributeName + "_" + clusterNumber, nodeList, null, true);
if (group != null)
groupList.add(group.getGroupNode().getSUID());
}
clusterList.add(nodeList);
}
// Adding a column per node by the clusterAttributeName, which will store a list of all the clusters to which the node belongs
List<CyNode> nodeList = network.getNodeList();
for (int i = 0; i < nodeList.size(); i++) {
CyNode node = nodeList.get(i);
List<Integer> listOfClusters = new ArrayList<Integer>();
for (FuzzyNodeCluster cluster : clusters) {
if (cluster.getMembership(node) != null) {
listOfClusters.add(cluster.getClusterNumber());
}
}
ModelUtils.createAndSetLocal(network, node, clusterAttributeName, listOfClusters, List.class, Integer.class);
}
ModelUtils.createAndSetLocal(network, network, group_attr, groupList, List.class, Long.class);
ModelUtils.createAndSetLocal(network, network, ClusterManager.CLUSTER_TYPE_ATTRIBUTE, getShortName(), String.class, null);
ModelUtils.createAndSetLocal(network, network, ClusterManager.CLUSTER_ATTRIBUTE, clusterAttributeName, String.class, null);
if (params != null)
ModelUtils.createAndSetLocal(network, network, ClusterManager.CLUSTER_PARAMS_ATTRIBUTE, params, List.class, String.class);
return clusterList;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class APCluster method run.
public void run(TaskMonitor monitor) {
monitor.setTitle("Performing AP cluster");
this.monitor = monitor;
if (network == null)
network = clusterManager.getNetwork();
// Make sure to update the context
context.setNetwork(network);
NodeCluster.init();
CyMatrix matrix = context.edgeAttributeHandler.getMatrix();
if (matrix == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
return;
}
// Update our tunable results
clusterAttributeName = context.getClusterAttribute();
createGroups = context.advancedAttributes.createGroups;
if (canceled)
return;
// Cluster the nodes
runAP = new RunAP(matrix, context.lambda, context.preference, context.rNumber, monitor, debug);
if (canceled)
return;
monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
List<NodeCluster> clusters = runAP.run(network, monitor);
// Canceled?
if (clusters == null)
return;
monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
// Remove any leftover groups from previous runs
removeGroups(network, GROUP_ATTRIBUTE);
monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
params = new ArrayList<String>();
context.edgeAttributeHandler.setParams(params);
setParams(params);
List<List<CyNode>> nodeClusters = createGroups(network, clusters, GROUP_ATTRIBUTE);
results = new AbstractClusterResults(network, clusters);
monitor.showMessage(TaskMonitor.Level.INFO, "Done. AP results:\n" + results);
if (context.vizProperties.showUI) {
monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !context.edgeAttributeHandler.selectedOnly));
}
}
Aggregations