use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster in project clusterMaker2 by RBVI.
the class RunFCM method run.
/**
* The method run has the actual implementation of the fuzzy c-means code
* @param monitor, Task monitor for the process
* @return List of FuzzyNodeCLusters
*/
public List<FuzzyNodeCluster> run(CyNetwork network, TaskMonitor monitor, int[] mostRelevantCluster) {
Long networkID = network.getSUID();
CyTable netAttributes = network.getDefaultNetworkTable();
CyTable nodeAttributes = network.getDefaultNodeTable();
long startTime = System.currentTimeMillis();
random = null;
int nelements = distanceMatrix.nRows();
// Matrix to store the temporary cluster membership values of elements
double[][] tClusterMemberships = new double[nelements][number_clusters];
// Initializing all membership values to 0
for (int i = 0; i < nelements; i++) {
for (int j = 0; j < number_clusters; j++) {
tClusterMemberships[i][j] = 0;
}
}
// Matrix to store cluster memberships of the previous iteration
double[][] prevClusterMemberships = new double[nelements][number_clusters];
// This matrix will store the centroid data
CyMatrix cData = CyMatrixFactory.makeSmallMatrix(network, number_clusters, nelements);
int iteration = 0;
boolean end = false;
do {
if (monitor != null)
monitor.setProgress(((double) iteration / (double) number_iterations));
// Initializing the membership values by randomly assigning a cluster to each element
if (iteration == 0 && number_iterations != 0) {
randomAssign(tClusterMemberships);
prevClusterMemberships = tClusterMemberships;
// Find the centers
getFuzzyCenters(cData, tClusterMemberships);
}
// Calculate Fuzzy Memberships
getClusterMemberships(cData, tClusterMemberships);
// Now calculate the new fuzzy centers
getFuzzyCenters(cData, tClusterMemberships);
end = checkEndCriterion(tClusterMemberships, prevClusterMemberships);
if (end) {
break;
}
} while (++iteration < number_iterations);
HashMap<CyNode, double[]> membershipMap = createMembershipMap(tClusterMemberships);
List<FuzzyNodeCluster> fuzzyClusters = new ArrayList<FuzzyNodeCluster>();
List<CyNode> clusterNodes = distanceMatrix.getRowNodes();
// List<CyNode> fuzzyNodeList;
for (int i = 0; i < number_clusters; i++) {
List<CyNode> fuzzyNodeList = new ArrayList<CyNode>();
HashMap<CyNode, Double> clusterMembershipMap = new HashMap<CyNode, Double>();
for (CyNode node : clusterNodes) {
if (membershipMap.get(node)[i] > membershipThreshold) {
fuzzyNodeList.add(node);
clusterMembershipMap.put(node, membershipMap.get(node)[i]);
}
}
fuzzyClusters.add(new FuzzyNodeCluster(fuzzyNodeList, clusterMembershipMap));
}
// Setting up the most relevant cluster per node
for (int i = 0; i < nelements; i++) {
// List tempMemList = Arrays.asList(ArrayUtils.toObject(tClusterMemberships[i]));
int maxIndex = 0;
for (int j = 1; j < number_clusters; j++) {
if (tClusterMemberships[i][j] > tClusterMemberships[i][maxIndex]) {
maxIndex = j;
}
}
mostRelevantCluster[i] = maxIndex + 1;
}
clusterMemberships = tClusterMemberships;
return fuzzyClusters;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster in project clusterMaker2 by RBVI.
the class Fuzzifier method run.
/**
* The method run creates an instance of the RunFuzzifier and creates the fuzzy clusters
* by calling the fuzzifier algorithm.
* Also creates fuzzy groups and the Fuzzy Cluster Table
*
* @param Task Monitor
*/
public void run(TaskMonitor monitor) {
monitor.setTitle("Performing Fuzzifier clustering");
this.monitor = monitor;
if (network == null)
network = clusterManager.getNetwork();
super.network = network;
this.Clusters = getClusters();
this.cNumber = Clusters.size();
// Make sure to update the context
context.setNetwork(network);
Long networkID = network.getSUID();
CyTable nodeAttributes = network.getDefaultNodeTable();
CyMatrix distanceMatrix = context.edgeAttributeHandler.getMatrix();
if (distanceMatrix == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
return;
}
// Update our tunable results
clusterAttributeName = context.getClusterAttribute();
runFuzzifier = new RunFuzzifier(Clusters, distanceMatrix, cNumber, context.membershipThreshold.getValue(), context.maxThreads, monitor);
runFuzzifier.setDebug(debug);
if (canceled)
return;
monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
List<FuzzyNodeCluster> FuzzyClusters = runFuzzifier.run(network, monitor);
// Canceled?
if (FuzzyClusters == 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);
List<List<CyNode>> nodeClusters = createFuzzyGroups(network, FuzzyClusters, GROUP_ATTRIBUTE);
results = new AbstractClusterResults(network, FuzzyClusters);
monitor.showMessage(TaskMonitor.Level.INFO, "Done. Fuzzifier 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));
} else {
monitor.showMessage(TaskMonitor.Level.INFO, "Done. Fizzifier results:\n" + results);
}
System.out.println("Creating fuzzy table");
createFuzzyTable(FuzzyClusters);
System.out.println("Done");
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster in project clusterMaker2 by RBVI.
the class RunFuzzifier method run.
/**
* The method run has the actual implementation of the fuzzifier code
* @param monitor, Task monitor for the process
* @return List of FuzzyNodeCLusters
*/
public List<FuzzyNodeCluster> run(CyNetwork network, TaskMonitor monitor) {
Long networkID = network.getSUID();
long startTime = System.currentTimeMillis();
int nelements = distanceMatrix.nRows();
nodeList = distanceMatrix.getRowNodes();
// Matrix to store the temporary cluster membership values of elements
double[][] ClusterMemberships = new double[nelements][number_clusters];
// Initializing all membership values to 0
for (int i = 0; i < nelements; i++) {
for (int j = 0; j < number_clusters; j++) {
ClusterMemberships[i][j] = 0;
}
}
// This matrix will store the centroid data
CyMatrix cData = CyMatrixFactory.makeSmallMatrix(network, number_clusters, nelements);
getFuzzyCenters(cData);
for (CyNode node : nodeList) {
int nodeIndex = nodeList.indexOf(node);
double sumDistances = 0;
for (int i = 0; i < Clusters.size(); i++) {
sumDistances += cData.doubleValue(i, nodeIndex);
}
for (int i = 0; i < Clusters.size(); i++) {
ClusterMemberships[nodeIndex][i] = cData.doubleValue(i, nodeIndex) / sumDistances;
}
}
HashMap<CyNode, double[]> membershipMap = createMembershipMap(ClusterMemberships);
List<FuzzyNodeCluster> fuzzyClusters = new ArrayList<FuzzyNodeCluster>();
// Adding the nodes which have memberships greater than the threshold to fuzzy node clusters
List<CyNode> fuzzyNodeList;
for (int i = 0; i < number_clusters; i++) {
fuzzyNodeList = new ArrayList<CyNode>();
HashMap<CyNode, Double> clusterMembershipMap = new HashMap<CyNode, Double>();
for (CyNode node : nodeList) {
if (membershipMap.get(node)[i] > membershipThreshold) {
fuzzyNodeList.add(node);
clusterMembershipMap.put(node, membershipMap.get(node)[i]);
}
}
fuzzyClusters.add(new FuzzyNodeCluster(fuzzyNodeList, clusterMembershipMap));
}
return fuzzyClusters;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster 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.FuzzyNodeCluster 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;
}
Aggregations