use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class GATKProtectedMathUtils method rowVariances.
public static double[] rowVariances(final RealMatrix matrix) {
Utils.nonNull(matrix);
final Variance varianceEvaluator = new Variance();
return IntStream.range(0, matrix.getRowDimension()).mapToDouble(r -> varianceEvaluator.evaluate(matrix.getRow(r))).toArray();
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class SparkConverter method convertSparkMatrixToRealMatrix.
/**
* Convert a local (not distributed) Spark Matrix to an Apache Commons matrix.
*
* @param r Never {@code null}
* @return Not {@code null}
*/
public static RealMatrix convertSparkMatrixToRealMatrix(final Matrix r) {
final RealMatrix result = new Array2DRowRealMatrix(r.numRows(), r.numCols());
final double[] columnMajorMat = r.toArray();
for (int i = 0; i < r.numRows(); i++) {
result.setRow(i, Arrays.copyOfRange(columnMajorMat, i * r.numCols(), i * r.numCols() + r.numCols()));
}
return result;
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class MatrixSummaryUtils method getColumnMedians.
/**
* Return an array containing the median for each column in the given matrix.
* @param m Not {@code null}. Size MxN, where neither dimension is zero. If any entry is NaN, it is disregarded
* in the calculation.
* @return array of size N. Never {@code null}
*/
public static double[] getColumnMedians(final RealMatrix m) {
Utils.nonNull(m, "Cannot calculate medians on a null matrix.");
final Median medianCalculator = new Median();
return IntStream.range(0, m.getColumnDimension()).boxed().mapToDouble(i -> medianCalculator.evaluate(m.getColumn(i))).toArray();
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class MatrixSummaryUtils method getRowMedians.
/**
* Return an array containing the median for each row in the given matrix.
* @param m Not {@code null}. Size MxN. If any entry is NaN, it is disregarded
* in the calculation.
* @return array of size M. Never {@code null}
*/
public static double[] getRowMedians(final RealMatrix m) {
Utils.nonNull(m, "Cannot calculate medians on a null matrix.");
final Median medianCalculator = new Median();
return IntStream.range(0, m.getRowDimension()).boxed().mapToDouble(i -> medianCalculator.evaluate(m.getRow(i))).toArray();
}
use of org.apache.commons.math3.linear.RealMatrix in project gatk-protected by broadinstitute.
the class HDF5PCACoveragePoNCreationUtilsUnitTest method testSubtractMedianOfMedians.
@Test(dataProvider = "readCountOnlyData")
public void testSubtractMedianOfMedians(final ReadCountCollection readCounts) {
final RealMatrix counts = readCounts.counts();
final Median median = new Median();
final double[] columnMedians = IntStream.range(0, counts.getColumnDimension()).mapToDouble(i -> median.evaluate(counts.getColumn(i))).toArray();
final double center = median.evaluate(columnMedians);
final double[][] expected = new double[counts.getRowDimension()][];
for (int i = 0; i < expected.length; i++) {
expected[i] = counts.getRow(i).clone();
for (int j = 0; j < expected[i].length; j++) {
expected[i][j] -= center;
}
}
HDF5PCACoveragePoNCreationUtils.subtractMedianOfMedians(readCounts, NULL_LOGGER);
final RealMatrix newCounts = readCounts.counts();
Assert.assertEquals(newCounts.getColumnDimension(), expected[0].length);
Assert.assertEquals(newCounts.getRowDimension(), expected.length);
for (int i = 0; i < expected.length; i++) {
for (int j = 0; j < expected[i].length; j++) {
Assert.assertEquals(newCounts.getEntry(i, j), expected[i][j], 0.000001);
}
}
}
Aggregations