Search in sources :

Example 41 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class MatrixTest method matrixSquareTest.

@Test
public void matrixSquareTest() {
    initialize();
    // First, simpleMatrix
    timeStart();
    Matrix resultSimple = simpleMatrix.ops().multiplyMatrix(simpleMatrix);
    timeEnd("square multiply simple matrix");
    // System.out.println("resultSimple: "+resultSimple.printMatrixInfo());
    timeStart();
    Matrix resultColt = coltMatrix.ops().multiplyMatrix(coltMatrix);
    timeEnd("square multiply colt matrix");
    // System.out.println("resultColt: "+resultColt.printMatrixInfo());
    assertArrayEquals(resultSimple.toArray(), resultColt.toArray(), DELTA);
    timeStart();
    Matrix resultAlgo = ojAlgoMatrix.ops().multiplyMatrix(ojAlgoMatrix);
    timeEnd("square multiply ojAlgo matrix");
    // System.out.println("resultOjAlgo: "+ resultAlgo.printMatrixInfo());
    assertArrayEquals(resultSimple.toArray(), resultAlgo.toArray(), DELTA);
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) Test(org.junit.Test)

Example 42 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class RunMCL method run.

public List<NodeCluster> run(CyNetwork network, TaskMonitor monitor) {
    // String networkID = network.getIdentifier();
    Long networkID = network.getSUID();
    long startTime = System.currentTimeMillis();
    // Matrix matrix;
    double numClusters;
    debugln("Initial matrix:", matrix);
    // Normalize
    normalize(matrix, clusteringThresh, false);
    debugln("Normalized matrix:", matrix);
    double residual = 1.0;
    double progress = 1.0;
    // IntIntDoubleFunction myPow = new MatrixPow(inflationParameter);
    debugln("residual = " + residual + " maxResidual = " + maxResidual);
    for (int i = 0; (i < number_iterations) && (residual > maxResidual); i++) {
        progress = (double) (i * 3) / (double) (number_iterations * 3);
        monitor.setProgress(progress);
        // Expand
        {
            long t = System.currentTimeMillis();
            // monitor.setStatus();
            monitor.setStatusMessage("Iteration: " + (i + 1) + " expanding ");
            debugln("Iteration: " + (i + 1) + " expanding ");
            debugln("matrix: ", matrix);
            Matrix multiMatrix = matrix.ops().multiplyMatrix(matrix);
            matrix = matrix.copy(multiMatrix);
            // Normalize
            normalize(matrix, clusteringThresh, false);
            monitor.showMessage(TaskMonitor.Level.INFO, "Expansion " + (i + 1) + " took " + (System.currentTimeMillis() - t) + "ms");
        }
        debugln("^ " + (i + 1) + " after expansion");
        progress = (double) (i * 3 + 1) / (double) (number_iterations * 3);
        monitor.setProgress(progress);
        // Inflate
        // DoubleMatrix2D m = matrix.getColtMatrix();
        {
            long t = System.currentTimeMillis();
            // monitor.setStatusMessage
            monitor.setStatusMessage("Iteration: " + (i + 1) + " inflating");
            debugln("Iteration: " + (i + 1) + " inflating");
            matrix.ops().powScalar(inflationParameter);
            // Normalize
            normalize(matrix, clusteringThresh, true);
        }
        debugln("^ " + (i + 1) + " after inflation");
        progress = (double) (i * 3 + 2) / (double) (number_iterations * 3);
        monitor.setProgress(progress);
        /*
			double newResidual  = calculateResiduals(matrix);
			if (newResidual >= residual) break;
			residual = newResidual;
			*/
        double newResidual = calculateResiduals(matrix);
        if (forceDecliningResidual && newResidual >= residual)
            break;
        residual = newResidual;
        debugln("Iteration: " + (i + 1) + " residual: " + residual);
        monitor.showMessage(TaskMonitor.Level.INFO, "Iteration " + (i + 1) + " complete.  Residual=" + residual);
        if (canceled) {
            // monitor.setStatusMessage
            monitor.setStatusMessage("canceled");
            return null;
        }
    }
    // If we're in debug mode, output the matrix
    debugln("Matrix: ", matrix);
    // monitor.setStatusMessage
    monitor.setStatusMessage("Assigning nodes to clusters");
    clusterCount = 0;
    HashMap<Integer, NodeCluster> clusterMap = new HashMap<Integer, NodeCluster>();
    ClusterMatrix clusterMat = new ClusterMatrix(clusterMap);
    for (int row = 0; row < matrix.nRows(); row++) {
        for (int col = 0; col < matrix.nColumns(); col++) {
            clusterMat.apply(row, col, matrix.doubleValue(row, col));
        }
    }
    // Update node attributes in network to include clusters. Create cygroups from clustered nodes
    monitor.setStatusMessage("Created " + clusterCount + " clusters");
    monitor.setStatusMessage("Cluster map has " + clusterMap.keySet().size() + " clusters");
    // 
    if (clusterCount == 0) {
        monitor.setStatusMessage("Created 0 clusters!!!!");
        monitor.setStatusMessage("Cluster map has " + clusterMap.keySet().size() + " clusters");
        return null;
    }
    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++;
    }
    monitor.setStatusMessage("Total runtime = " + (System.currentTimeMillis() - startTime) + "ms");
    Set<NodeCluster> clusters = cMap.keySet();
    return new ArrayList<NodeCluster>(clusters);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)

Example 43 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class Creator method run.

public void run(HashMap<Integer, String> proteins2integers, HashMap<String, Integer> integers2proteins) throws IOException {
    if (Config.source == Config.BLAST) {
        if (Config.gui)
            Console.println("Read Fasta file ... ");
        int[] proteinLengths = InOut.readFastaFile(Config.fastaFile, proteins2integers, integers2proteins);
        if (Config.gui)
            Console.println();
        if (Config.gui)
            Console.println("Read Blast file ... ");
        BlastFile bf = InOut.readBlastFileWithArray(Config.blastFile, integers2proteins, proteinLengths);
        if (Config.gui)
            Console.println();
        if (Config.gui)
            Console.println("Create similarity file ...");
        createSimilarityFileFromArray(Config.similarityFile, bf, proteins2integers, proteinLengths, Config.costModel);
        if (Config.gui)
            Console.println();
        if (Config.splitAndWriteCostMatrices) {
            bf = null;
            System.gc();
        }
    } else {
    // if(Config.gui) Console.println("Read Matrix ...");
    // createSimilarityFileFromExpressionMatrix(Config.similarityFile, Config.expressionMatrix, Config.withHeader, Config.withRowDescription, Config.costModel);
    }
}
Also used : BlastFile(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.BlastFile)

Example 44 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class RunFuzzifier method getFuzzyCenters.

/**
 * The method calculates the centers of fuzzy clusters
 *
 * @param cData matrix to store the data for cluster centers
 */
public void getFuzzyCenters(CyMatrix cData) {
    // To store the sum of memberships(raised to fuzziness index) corresponding to each cluster
    int nelements = distanceMatrix.nRows();
    for (NodeCluster cluster : Clusters) {
        int c = Clusters.indexOf(cluster);
        double numerator = 0;
        Double distance = 0.0;
        int i = 0;
        for (int e = 0; e < nelements; e++) {
            numerator = 0;
            for (CyNode node : cluster) {
                i = nodeList.indexOf(node);
                distance = distanceMatrix.doubleValue(i, e);
                numerator += distance;
            }
            cData.setValue(c, e, (numerator / cluster.size()));
        }
    }
}
Also used : FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) CyNode(org.cytoscape.model.CyNode)

Example 45 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix 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)

Aggregations

CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)34 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)22 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)13 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)12 CyNode (org.cytoscape.model.CyNode)12 List (java.util.List)9 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)7 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)7 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)5 CyTable (org.cytoscape.model.CyTable)5 Test (org.junit.Test)4 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)3 ColtMatrix (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.ColtMatrix)3 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)2 DistanceMetric (edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric)2 ScatterPlotDialog (edu.ucsf.rbvi.clusterMaker2.internal.ui.ScatterPlotDialog)2 CyEdge (org.cytoscape.model.CyEdge)2 DoubleArrayList (cern.colt.list.tdouble.DoubleArrayList)1 IntArrayList (cern.colt.list.tint.IntArrayList)1