Search in sources :

Example 36 with Matrix

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

the class RunPCoA method run.

public void run() {
    long startTime = System.currentTimeMillis();
    long time = startTime;
    // System.out.println("Calculating values");
    // double data[][]=matrix.toArray();
    // System.out.println("Length "+ distanceMatrix.nRows());
    // 
    distanceMatrix.updateMinMax();
    double max = distanceMatrix.getMaxValue() * 10;
    // the way the initial matrix is built.  Set those to Double.MAX.
    for (int row = 0; row < distanceMatrix.nRows(); row++) {
        for (int col = 0; col < distanceMatrix.nColumns(); col++) {
            double value = distanceMatrix.doubleValue(row, col);
            if (value == 0.0 || Double.isNaN(value))
                distanceMatrix.setValue(row, col, max);
        }
    }
    Matrix mean = distanceMatrix.like(distanceMatrix.nColumns(), 1);
    // TODO: center the matrix?
    for (int j = 0; j < mean.nRows(); j++) {
        mean.setValue(j, 0, CommonOps.columnMean(distanceMatrix, j));
    }
    for (int i = 0; i < distanceMatrix.nRows(); i++) {
        for (int j = 0; j < distanceMatrix.nColumns(); j++) {
            distanceMatrix.setValue(i, j, distanceMatrix.doubleValue(i, j) - mean.doubleValue(j, 0));
        }
    }
    // System.out.println("Checking CyMatrix symmetrical "+distanceMatrix.isSymmetrical());
    monitor.showMessage(TaskMonitor.Level.INFO, "Calculating Gower's Matrix");
    // Get the Gower's Matrix
    Matrix G = GowersMatrix.getGowersMatrix(distanceMatrix);
    long delta = System.currentTimeMillis() - time;
    time = System.currentTimeMillis();
    monitor.showMessage(TaskMonitor.Level.INFO, "Constructed Gower's Matrix in " + delta + "ms");
    monitor.showMessage(TaskMonitor.Level.INFO, "Doing Singlular Value Decomposition (SVD)");
    // System.out.println("Got GowersMatrix in "+delta+"ms");
    G.ops().svdInit();
    Matrix V_t = CommonOps.transpose(G.ops().svdV());
    // Get the singular values
    Matrix S = G.ops().svdS();
    double[] variances = getVariance(S);
    V_t = reshape(V_t, 2, mean.nRows());
    double[][] trafoed = new double[distanceMatrix.nRows()][2];
    result = CyMatrixFactory.makeLargeMatrix(distanceMatrix.getNetwork(), distanceMatrix.nRows(), 2);
    result.setRowNodes(distanceMatrix.getRowNodes());
    result.setRowLabels(Arrays.asList(distanceMatrix.getRowLabels()));
    result.setColumnLabel(0, "X");
    result.setColumnLabel(1, "Y");
    for (int i = 0; i < distanceMatrix.nRows(); i++) {
        trafoed[i] = sampleToEigenSpace(V_t, distanceMatrix, mean, i);
        for (int j = 0; j < trafoed[i].length; j++) {
            result.setValue(i, j, trafoed[i][j] *= -1);
        }
    }
    delta = System.currentTimeMillis() - time;
    time = System.currentTimeMillis();
    monitor.showMessage(TaskMonitor.Level.INFO, "Completed SVD Analysis in " + delta + "ms");
    monitor.showMessage(TaskMonitor.Level.INFO, String.format("Variance explained by first three axes: %2.2f%%, %2.2f%%, and %2.2f%%", variances[0], variances[1], variances[2]));
    if (context.pcoaPlot) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                // System.out.println("Scatter plot dialog call");
                ScatterPlotDialog dialog = new ScatterPlotDialog(manager, "PCoA", monitor, result);
            }
        });
    }
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) ScatterPlotDialog(edu.ucsf.rbvi.clusterMaker2.internal.ui.ScatterPlotDialog)

Example 37 with Matrix

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

the class RunPCoA method reshape.

private Matrix reshape(Matrix src, int numRows, int numCols) {
    Matrix result = src.like(numRows, numCols);
    long index = 0;
    long size = src.nRows() * src.nColumns();
    for (int row = 0; row < numRows; row++) {
        for (int col = 0; col < numCols; col++) {
            double value = 0.0;
            if (index < size) {
                int srcRow = (int) (index / src.nColumns());
                int srcCol = (int) (index % src.nColumns());
                value = src.doubleValue(srcRow, srcCol);
                index++;
            }
            result.setValue(row, col, value);
        }
    }
    return result;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)

Example 38 with Matrix

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

the class RunPCoA method sampleToEigenSpace.

public double[] sampleToEigenSpace(Matrix V_t, Matrix sampleData, Matrix mean, int row) {
    Matrix s = distanceMatrix.like(distanceMatrix.nColumns(), 1);
    for (int col = 0; col < distanceMatrix.nColumns(); col++) s.setValue(col, 0, sampleData.doubleValue(row, col));
    CommonOps.subtractElement(s, mean);
    Matrix r = CommonOps.multiplyMatrix(V_t.copy(), s);
    return r.getColumn(0);
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)

Example 39 with Matrix

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

the class tSNE method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("t-Distributed Stochastic Neighbour");
    monitor.setStatusMessage("Running t-Distributed Stochastic Neighbour");
    this.monitor = monitor;
    if (network == null)
        network = clusterManager.getNetwork();
    context.setNetwork(network);
    List<String> dataAttributes = context.attributeList.getNodeAttributeList();
    if (dataAttributes == null || dataAttributes.isEmpty()) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no attribute list selected");
        return;
    }
    if (context.selectedOnly && network.getDefaultNodeTable().countMatchingRows(CyNetwork.SELECTED, true) == 0) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no nodes selected from network");
        return;
    }
    String[] attrArray = new String[dataAttributes.size()];
    int att = 0;
    for (String attribute : dataAttributes) {
        attrArray[att++] = "node." + attribute;
    }
    // CyMatrix matrix = context.edgeAttributeHandler.getMatrix();
    CyMatrix matrix = CyMatrixFactory.makeLargeMatrix(network, attrArray, context.selectedOnly, context.ignoreMissing, false, false);
    if (matrix == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
        return;
    }
    runtsne = new RuntSNE(manager, network, networkView, context, monitor, matrix);
    runtsne.run();
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 40 with Matrix

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

the class MatrixTest method timesTest.

@Test
public void timesTest() {
    initialize();
    // First, simpleMatrix
    timeStart();
    Matrix resultSimple = simpleMatrix.ops().multiplyMatrix(simpleMatrix2);
    // resultSimple.writeMatrix("simpleTimes.mat");
    timeEnd("multiply simple matrix");
    timeStart();
    Matrix resultColt = coltMatrix.ops().multiplyMatrix(coltMatrix2);
    // resultColt.writeMatrix("coltTimes.mat");
    timeEnd("multiply colt matrix");
    assertArrayEquals(resultSimple.toArray(), resultColt.toArray(), DELTA);
    timeStart();
    Matrix resultAlgo = ojAlgoMatrix.ops().multiplyMatrix(ojAlgoMatrix2);
    // resultAlgo.writeMatrix("ojAlgoTimes.mat");
    timeEnd("multiply ojAlgo matrix");
    assertArrayEquals(resultSimple.toArray(), resultAlgo.toArray(), DELTA);
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) Test(org.junit.Test)

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