Search in sources :

Example 31 with CyMatrix

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

the class CalculationMatrix method getCoordinates.

// get the coordinates for PCoA
public CyMatrix[] getCoordinates(CyMatrix matrix) {
    CyMatrix[] components = new CyMatrix[eigen_values.length];
    for (int j = eigen_values.length - 1, k = 0; j >= 0; j--, k++) {
        // double[] w = new double[vectors.length];
        CyMatrix result = CyMatrixFactory.makeLargeMatrix(matrix.getNetwork(), eigen_values.length, 1);
        for (int i = 0; i < eigen_values.length; i++) {
            // System.out.println("Setting eigen_vector["+i+"]["+j+"]");
            result.setValue(i, 0, eigen_vectors[i][j]);
        }
        // matrix.writeMatrix("matrix");
        // result.writeMatrix("result");
        // System.out.println("matrix: "+matrix.printMatrixInfo());
        // System.out.println("vector: "+result.printMatrixInfo());
        // System.out.println("Matrix rows "+matrix.printMatrixInfo());
        // System.out.println("Result rows "+result.printMatrixInfo());
        Matrix mat = matrix.ops().multiplyMatrix(result);
        // System.out.println("After vector multiply: "+mat.printMatrixInfo());
        components[k] = matrix.copy(mat);
    // components[k].printMatrixInfo();
    // components[k].writeMatrix("component_"+k+".txt");
    // System.out.println("Component matrix "+k+" has "+components[k].getRowNodes().size()+" nodes");
    }
    return components;
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 32 with CyMatrix

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

the class GowersMatrix method getGowersMatrix.

public static Matrix getGowersMatrix(CyMatrix distanceMatrix) {
    // Create the Identity matrix
    // DoubleMatrix2D I = DoubleFactory2D.sparse.identity(distancematrix.nRows());
    Matrix I = distanceMatrix.like(distanceMatrix.nRows(), distanceMatrix.nRows(), 0.0);
    for (int row = 0; row < distanceMatrix.nRows(); row++) I.setValue(row, row, 1.0);
    /*
		IntStream.range(0, distanceMatrix.nRows()).parallel()
			.forEach(row -> {
					I.setValue(row, row, 1.0);
				});
		*/
    // I.writeMatrix("I");
    // Create the ones matrix.  This is equivalent to 11'/n
    // DoubleMatrix2D one = DoubleFactory2D.dense.make(distancematrix.nRows(), distancematrix.nRows(), 1.0/distancematrix.nRows());
    Matrix one = distanceMatrix.like(distanceMatrix.nRows(), distanceMatrix.nRows(), 1.0 / distanceMatrix.nRows());
    // I.writeMatrix("one");
    // Create the subtraction matrix (I-11'/n)
    // DoubleMatrix2D mat = I.assign(one, DoubleFunctions.minus);
    Matrix mat = subtractElement(I, one);
    // mat.writeMatrix("mat");
    // Create our data matrix
    // final DoubleMatrix2D A = DoubleFactory2D.sparse.make(distancematrix.nRows(), distancematrix.nRows());
    Matrix A = distanceMatrix.like(distanceMatrix.nRows(), distanceMatrix.nRows());
    /*for (int row = 0; row < distancematrix.nRows(); row++) {
			for (int col = 0; col < distancematrix.nColumns(); col++) {
				System.out.print(distancematrix.getValue(row, col)+",");
			}
		}*/
    /*
		DoubleMatrix2D data = distancematrix.getColtMatrix();

		data.forEachNonZero(
			new IntIntDoubleFunction() {
				public double apply(int row, int column, double value) {
					double v = -Math.pow(value,2)/2.0;
					if (Math.abs(v) > EPSILON)
						A.setQuick(row, column, v);
					return value;
				}
			}
		);
		*/
    IntStream.range(0, distanceMatrix.nRows()).parallel().forEach(row -> IntStream.range(0, distanceMatrix.nColumns()).forEach(col -> {
        double v = -Math.pow(distanceMatrix.doubleValue(row, col), 2) / 2.0;
        if (Math.abs(v) > EPSILON)
            A.setValue(row, col, v);
    }));
    // A.writeMatrix("A");
    Matrix cMat = distanceMatrix.like(mat);
    Matrix cA = distanceMatrix.like(A);
    // System.out.println("Completed Gowers Matrix");
    // Finally, the Gower's matrix is mat*A*mat
    Matrix mat1 = cMat.ops().multiplyMatrix(cA);
    Matrix G = mat1.ops().multiplyMatrix(cMat);
    // G.writeMatrix("Gowers");
    return G;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) CyMatrixFactory(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.CyMatrixFactory) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) DecimalFormat(java.text.DecimalFormat) CommonOps.subtractElement(edu.ucsf.rbvi.clusterMaker2.internal.api.CommonOps.subtractElement) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 33 with CyMatrix

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

the class PCoA method getResults.

@Override
public <R> R getResults(Class<? extends R> type) {
    CyMatrix results = runpcoa.getResult();
    if (type.equals(String.class)) {
        results.setColumnLabel(0, "X");
        results.setColumnLabel(1, "Y");
        CyNetwork net = results.getNetwork();
        for (int row = 0; row < results.nRows(); row++) {
            CyNode node = results.getRowNode(row);
            results.setRowLabel(row, ModelUtils.getName(net, node));
        }
        return (R) results.printMatrix();
    } else if (type.equals(Map.class)) {
        Map<CyNode, Point2D> resultsMap = new HashMap<>();
        for (int row = 0; row < results.nRows(); row++) {
            CyNode node = results.getRowNode(row);
            resultsMap.put(node, new Point2D.Double(results.doubleValue(row, 0), results.doubleValue(row, 1)));
        }
        return (R) resultsMap;
    }
    return (R) results;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) CyNetwork(org.cytoscape.model.CyNetwork) CyNode(org.cytoscape.model.CyNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with CyMatrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix 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 35 with CyMatrix

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

Aggregations

CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)47 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 CyNode (org.cytoscape.model.CyNode)13 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)11 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)10 Test (org.junit.Test)10 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)9 List (java.util.List)8 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)6 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)6 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)4 CyTable (org.cytoscape.model.CyTable)4 ColtMatrix (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.ColtMatrix)3 BiclusterView (edu.ucsf.rbvi.clusterMaker2.internal.ui.BiclusterView)3 Map (java.util.Map)3 CyNetwork (org.cytoscape.model.CyNetwork)3 MedianSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MedianSummarizer)2 ScatterPlotDialog (edu.ucsf.rbvi.clusterMaker2.internal.ui.ScatterPlotDialog)2 CyEdge (org.cytoscape.model.CyEdge)2