Search in sources :

Example 96 with Array2DRowRealMatrix

use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.

the class SingularValueDecomposerUnitTest method assertSVD.

public void assertSVD(final SVD svd, final RealMatrix m) {
    final RealMatrix U = svd.getU();
    final RealMatrix V = svd.getV();
    final double[] s = svd.getSingularValues();
    final RealMatrix S = new Array2DRowRealMatrix(U.getColumnDimension(), V.getColumnDimension());
    IntStream.range(0, s.length).forEach(n -> S.setEntry(n, n, s[n]));
    final RealMatrix SPrime = new Array2DRowRealMatrix(V.getColumnDimension(), U.getColumnDimension());
    IntStream.range(0, s.length).forEach(n -> SPrime.setEntry(n, n, 1 / s[n]));
    Assert.assertEquals(U.multiply(S).multiply(V.transpose()), m);
    Assert.assertEquals(V.transpose().multiply(V), MatrixUtils.createRealIdentityMatrix(2), "V is unitary");
    Assert.assertEquals(U.transpose().multiply(U), MatrixUtils.createRealIdentityMatrix(2), "U is unitary");
    Assert.assertTrue(isUnitaryMatrix(U), "matrix U");
    Assert.assertTrue(isUnitaryMatrix(V), "matrix V");
    Assert.assertEquals(U.multiply(S).multiply(V.transpose()), m);
    Assert.assertEquals(V.multiply(SPrime).multiply(U.transpose()), svd.getPinv());
    assertPseudoinverse(m, svd.getPinv());
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix)

Example 97 with Array2DRowRealMatrix

use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.

the class CaseToPoNTargetMapper method fromCaseToPoNCounts.

/**
     * Re-arrange case read counts counts based on PoN target order.
     * @param caseCounts the input counts to rearrange.
     * @return never {@code null}, a new matrix with row sorted according to the PoN target order.
     */
public RealMatrix fromCaseToPoNCounts(final RealMatrix caseCounts) {
    Utils.nonNull(caseCounts, "Case-sample counts cannot be null.");
    final RealMatrix result = new Array2DRowRealMatrix(ponTargetNames.size(), caseCounts.getColumnDimension());
    for (int i = 0; i < ponTargetNames.size(); i++) {
        final String targetName = ponTargetNames.get(i);
        final Integer inputIndex = caseTargetIndexes.get(targetName);
        if (inputIndex == null) {
            throw new UserException.BadInput(String.format("missing PoN target name %s in input read counts", targetName));
        }
        result.setRow(i, caseCounts.getRow(inputIndex));
    }
    return result;
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix)

Example 98 with Array2DRowRealMatrix

use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.

the class CaseToPoNTargetMapper method fromPoNToCaseCounts.

/**
     * Re-arrange the input rows from the PoN to the case data target order.
     * @param ponCounts count matrix with row organized using the PoN target order.
     * @return never {@code null} a new matrix with the row order changed according to the case read count target order.
     */
public RealMatrix fromPoNToCaseCounts(final RealMatrix ponCounts) {
    final Map<String, Integer> ponTargetsIndexes = IntStream.range(0, ponTargetNames.size()).boxed().collect(Collectors.toMap(ponTargetNames::get, Function.identity()));
    final RealMatrix result = new Array2DRowRealMatrix(ponCounts.getRowDimension(), ponCounts.getColumnDimension());
    for (int i = 0; i < outputTargets.size(); i++) {
        final Target target = outputTargets.get(i);
        result.setRow(i, ponCounts.getRow(ponTargetsIndexes.get(target.getName())));
    }
    return result;
}
Also used : Target(org.broadinstitute.hellbender.tools.exome.Target) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix)

Example 99 with Array2DRowRealMatrix

use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.

the class SparkConverter method convertSparkRowMatrixToRealMatrix.

/**
     * Create an Apache Commons RealMatrix from a Spark RowMatrix.
     *
     * @param r Never {@code null}
     * @param cachedNumRows Checking the number of rows in {@code r} can be time-consuming.  Provide the value here, if it is already known.
     *                      Use {@code -1} if unknown.
     * @return Never {@code null}
     */
public static RealMatrix convertSparkRowMatrixToRealMatrix(final RowMatrix r, final int cachedNumRows) {
    Utils.nonNull(r, "Input row matrix cannot be null");
    int numRows;
    if (cachedNumRows == -1) {
        // This takes a while in Spark
        numRows = (int) r.numRows();
    } else {
        numRows = cachedNumRows;
    }
    final int numCols = (int) r.numCols();
    // This cast is required, even though it would not seem necessary, at first.  Exact reason why is unknown.
    //   Will fail compilation if the cast is removed.
    final Vector[] rowVectors = (Vector[]) r.rows().collect();
    final RealMatrix result = new Array2DRowRealMatrix(numRows, numCols);
    for (int i = 0; i < numRows; i++) {
        result.setRow(i, rowVectors[i].toArray());
    }
    return result;
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Vector(org.apache.spark.mllib.linalg.Vector)

Example 100 with Array2DRowRealMatrix

use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.

the class IntegerCopyNumberTransitionMatrixUnitTest method testUnnormalizedProbability.

@Test
public void testUnnormalizedProbability() {
    /* it should normalize unnormalized transition matrices and give a warning */
    final IntegerCopyNumberTransitionMatrix transitionMatrix = new IntegerCopyNumberTransitionMatrix(new Array2DRowRealMatrix(new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }), 0);
    for (int i = 0; i < 3; i++) {
        final double[] col = transitionMatrix.getTransitionMatrix().getColumn(i);
        Assert.assertEquals(Arrays.stream(col).sum(), 1.0, 1e-12);
    }
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)141 RealMatrix (org.apache.commons.math3.linear.RealMatrix)101 Test (org.testng.annotations.Test)60 IntStream (java.util.stream.IntStream)31 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)28 File (java.io.File)27 Collectors (java.util.stream.Collectors)25 ArrayList (java.util.ArrayList)24 Assert (org.testng.Assert)24 List (java.util.List)22 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)22 Target (org.broadinstitute.hellbender.tools.exome.Target)18 java.util (java.util)15 Random (java.util.Random)14 ReadCountCollection (org.broadinstitute.hellbender.tools.exome.ReadCountCollection)14 ParamUtils (org.broadinstitute.hellbender.utils.param.ParamUtils)14 DataProvider (org.testng.annotations.DataProvider)14 Stream (java.util.stream.Stream)13 Arrays (java.util.Arrays)12 DoubleStream (java.util.stream.DoubleStream)12