use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.
the class ColtMatrix method like.
public Matrix like(int rows, int columns, double[][] initial) {
Matrix newMat = like();
newMat.initialize(rows, columns, initial);
return newMat;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.
the class CyMatrixFactory method makeMatrix.
public static CyMatrix makeMatrix(CyNetwork network, String[] attributes, boolean selectedOnly, boolean ignoreMissing, boolean transpose, boolean assymetric, MatrixType type) {
// Create our local copy of the weightAtributes array
String[] attributeArray = new String[attributes.length];
if (attributes.length >= 1 && attributes[0].startsWith("node.")) {
// Get rid of the leading type information
for (int i = 0; i < attributes.length; i++) {
attributeArray[i] = attributes[i].substring(5);
}
List<CyNode> nodeList = ModelUtils.getSortedNodeList(network, selectedOnly);
Map<CyNode, Map<String, Double>> nodeCondMap = getNodeCondMap(network, nodeList, attributeArray, ignoreMissing);
CyMatrix matrix = makeTypedMatrix(network, nodeCondMap.size(), attributeArray.length, transpose, type);
matrix.setAssymetricalEdge(false);
return makeAttributeMatrix(network, matrix, nodeList, nodeCondMap, attributeArray, transpose);
} else if (attributes.length == 1 && attributes[0].startsWith("edge.")) {
String weight = attributes[0].substring(5);
if (!assymetric) {
// Get the list of nodes and edges of interest
List<CyEdge> edgeList = getEdgeList(network, selectedOnly);
List<CyNode> nodeList = getNodesFromEdges(network, edgeList, weight, ignoreMissing);
Collections.sort(nodeList, new CyIdentifiableNameComparator(network));
CyMatrix matrix = makeTypedMatrix(network, nodeList.size(), nodeList.size(), false, type);
matrix.setAssymetricalEdge(false);
return makeSymmetricalMatrix(network, matrix, nodeList, edgeList, weight);
} else {
List<CyEdge> edgeList = getEdgeList(network, selectedOnly);
List<CyNode> targetNodeList = new ArrayList<CyNode>();
List<CyNode> sourceNodeList = getNodesFromEdges(network, edgeList, targetNodeList, weight, ignoreMissing);
Collections.sort(targetNodeList, new CyIdentifiableNameComparator(network));
Collections.sort(sourceNodeList, new CyIdentifiableNameComparator(network));
CyMatrix matrix = makeTypedMatrix(network, sourceNodeList.size(), targetNodeList.size(), false, type);
matrix.setAssymetricalEdge(true);
return makeAssymmetricalMatrix(network, matrix, sourceNodeList, targetNodeList, edgeList, weight);
}
}
return null;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix 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.api.Matrix 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.api.Matrix 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;
}
Aggregations