Search in sources :

Example 56 with Clusters

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.

the class AbstractNetworkClusterer method createGroups.

protected List<List<CyNode>> createGroups(CyNetwork network, List<NodeCluster> 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>();
    List<Double> clusterScores = new ArrayList<Double>(clusters.size());
    // Initialize
    for (NodeCluster cluster : clusters) {
        clusterScores.add(null);
    }
    boolean haveScores = NodeCluster.getScoreList(clusters) != null;
    // Remove the old column, if it's there.  Some of the algorithms don't put
    // all nodes into clusters, so we might wind up with old data lingering
    ModelUtils.deleteColumnLocal(network, CyNode.class, clusterAttributeName);
    for (NodeCluster cluster : clusters) {
        int clusterNumber = cluster.getClusterNumber();
        if (cluster.hasScore()) {
            clusterScores.set(clusterNumber - 1, cluster.getClusterScore());
            haveScores = true;
        }
        String groupName = clusterAttributeName + "_" + clusterNumber;
        List<CyNode> nodeList = new ArrayList<CyNode>();
        for (CyNode node : cluster) {
            nodeList.add(node);
            ModelUtils.createAndSetLocal(network, node, clusterAttributeName, clusterNumber, Integer.class, null);
        }
        if (createGroups) {
            CyGroup group = clusterManager.createGroup(network, clusterAttributeName + "_" + clusterNumber, nodeList, null, true);
            if (group != null) {
                groupList.add(group.getGroupNode().getSUID());
                if (NodeCluster.hasScore()) {
                    ModelUtils.createAndSetLocal(network, group.getGroupNode(), clusterAttributeName + "_Score", cluster.getClusterScore(), Double.class, null);
                }
            }
        }
        clusterList.add(nodeList);
    }
    if (haveScores)
        ModelUtils.createAndSetLocal(network, network, clusterAttributeName + "_Scores", clusterScores, List.class, Double.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;
}
Also used : ArrayList(java.util.ArrayList) CyGroup(org.cytoscape.group.CyGroup) FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) ArrayList(java.util.ArrayList) List(java.util.List) CyNode(org.cytoscape.model.CyNode)

Example 57 with Clusters

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.

the class ConnectedComponentsCluster method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Performing ConnectedComponents 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;
    Map<Integer, List<CyNode>> components = MatrixUtils.findConnectedComponents(matrix);
    // Create the NodeClusters
    Map<Integer, NodeCluster> clusterMap = new HashMap<Integer, NodeCluster>();
    for (Integer cluster : components.keySet()) {
        clusterMap.put(cluster, new NodeCluster(components.get(cluster)));
    }
    // Now get the sorted cluster map
    int clusterNumber = 1;
    HashMap<NodeCluster, NodeCluster> cMap = new HashMap<NodeCluster, NodeCluster>();
    for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
        if (cMap.containsKey(cluster))
            continue;
        cMap.put(cluster, cluster);
        cluster.setClusterNumber(clusterNumber);
        clusterNumber++;
    }
    List<NodeCluster> clusters = new ArrayList<NodeCluster>(cMap.keySet());
    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 = createGroups(network, clusters, GROUP_ATTRIBUTE);
    results = new AbstractClusterResults(network, clusters);
    monitor.showMessage(TaskMonitor.Level.INFO, "ConnectedComponent 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));
    }
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)

Example 58 with Clusters

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.

the class FCMCluster method run.

/**
 * The method run creates an instance of the RunFCM and creates the fuzzy clusters by applying the fuzzy c means algorithm.
 * Also creates fuzzy groups and the Fuzzy Cluster Table
 *
 * @param Task Monitor
 */
public void run(TaskMonitor taskmonitor) {
    this.monitor = taskmonitor;
    monitor.setTitle("Performing FCM cluster");
    if (network == null)
        network = clusterManager.getNetwork();
    // Make sure to update the context
    context.setNetwork(network);
    Long networkID = network.getSUID();
    CyTable netAttributes = network.getDefaultNetworkTable();
    CyTable nodeAttributes = network.getDefaultNodeTable();
    CyTable edgeAttributes = network.getDefaultEdgeTable();
    distanceMatrix = context.edgeAttributeHandler.getMatrix();
    if (distanceMatrix == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
        return;
    }
    createGroups = context.advancedAttributes.createGroups;
    // Update our tunable results
    clusterAttributeName = "CnC_Bicluster";
    // distanceDataMatrix = new Matrix(network,0,0);
    // distanceDataMatrix.buildDistanceMatrix(distanceMatrix);
    distanceDataMatrix = distanceMatrix.copy();
    if (context.estimateClusterNumber && context.cNumber < 0) {
        // System.out.println("Estimated number of Clusters: "+ cEstimate);
        context.cNumber = cEstimate();
    }
    int[] mostRelevantCluster = new int[network.getNodeList().size()];
    FuzzyNodeCluster.fuzzyClusterCount = 0;
    runFCM = new RunFCM(distanceMatrix, context.iterations, context.cNumber, context.fIndex, context.beta, context.membershipThreshold.getValue(), context.maxThreads, this.monitor);
    runFCM.setDebug(debug);
    if (canceled)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    List<FuzzyNodeCluster> clusters = runFCM.run(network, monitor, mostRelevantCluster);
    // 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);
    List<List<CyNode>> nodeClusters = createFuzzyGroups(network, clusters, GROUP_ATTRIBUTE);
    results = new AbstractClusterResults(network, clusters);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  FCM results:\n" + results);
    if (context.vizProperties.showUI) {
        monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
        insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !selectedOnly));
    } else {
        monitor.showMessage(TaskMonitor.Level.INFO, "Done.  FCM results:\n" + results);
    }
    createFuzzyTable(clusters);
}
Also used : CyTable(org.cytoscape.model.CyTable) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults) FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)

Example 59 with Clusters

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.

the class AbstractNetworkFilter method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Filtering using " + getName());
    this.monitor = monitor;
    if (network == null)
        network = clusterManager.getNetwork();
    clusterAttributeName = getClusterAttributeName();
    // get the cluster list
    List<NodeCluster> clusterList = AbstractNetworkClusterer.getNodeClusters(network, getClusterAttribute());
    List<NodeCluster> newClusterList = new ArrayList<NodeCluster>();
    Map<NodeCluster, List<CyNode>> addedNodeMap = new HashMap<NodeCluster, List<CyNode>>();
    // Iterate over clusters and build a new clusterList
    for (NodeCluster nodeList : clusterList) {
        NodeCluster newCluster = doFilter(nodeList, addedNodeMap);
        if (newCluster != null && newCluster.size() > 0)
            newClusterList.add(newCluster);
    }
    // Fixup our clusters
    if (addedNodeMap.size() > 0) {
        // until this stabelized...
        for (NodeCluster addedCluster : addedNodeMap.keySet()) {
            for (NodeCluster cluster : newClusterList) {
                if (cluster.equals(addedCluster))
                    continue;
                removeNodes(cluster, addedNodeMap.get(addedCluster));
            }
        }
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
    // Remove any leftover groups from previous runs
    removeGroups(network, groupAttribute);
    monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
    List<List<CyNode>> nodeClusters = createGroups(network, newClusterList, groupAttribute);
    results = new AbstractClusterResults(network, clusterList);
    ClusterResults results2 = new AbstractClusterResults(network, newClusterList);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  Results:\n\nBefore Filter:\n" + results + "\n\nAfter Filter:\n" + results2);
    if (showUI()) {
        monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
        insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, restoreEdges(), false));
    }
}
Also used : FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CyNode(org.cytoscape.model.CyNode) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults) ClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.api.ClusterResults)

Example 60 with Clusters

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters 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;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Aggregations

ArrayList (java.util.ArrayList)30 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)27 CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)22 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)21 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)16 HashMap (java.util.HashMap)16 List (java.util.List)15 CyNode (org.cytoscape.model.CyNode)13 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)10 Test (org.junit.Test)9 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)8 ConnectedComponent (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent)6 Random (java.util.Random)5 Vector (java.util.Vector)5 Semaphore (java.util.concurrent.Semaphore)5 Edges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges)3 ICCEdges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ICCEdges)3 IParameters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.IParameters)3 ClusteringManager (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager)3 PREdge (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.ranking.units.PREdge)3