Skip to content

Delete a bunch of unused methods #6107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 26, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ private static int countNumberPassingFilter(final boolean[] filter) {

private static void transformToFractionalCoverage(final RealMatrix matrix) {
logger.info("Transforming read counts to fractional coverage...");
final double[] sampleSums = MathUtils.rowSums(matrix);
final double[] sampleSums = IntStream.range(0, matrix.getRowDimension())
.mapToDouble(r -> MathUtils.sum(matrix.getRow(r))).toArray();
matrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(int sampleIndex, int intervalIndex, double value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public static double[] computeResponsibilities(final Nucleotide refAllele, final
private static double computeLogPosterior(final int altDepth, final int altF1R2Depth, final int depth,
final double statePrior, final BetaDistributionShape afPseudoCounts,
final BetaDistributionShape f1r2PseudoCounts){
Utils.validateArg(MathUtils.isAProbability(statePrior), String.format("statePrior must be a probability but got %f", statePrior));
Utils.validateArg(MathUtils.goodProbability(statePrior), String.format("statePrior must be a probability but got %f", statePrior));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think isValidProbability and isValidLog10Probability are probably better names for these methods, but I'll leave it up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


return Math.log(statePrior)
+ new BetaBinomialDistribution(null, afPseudoCounts.getAlpha(), afPseudoCounts.getBeta(), depth).logProbability(altDepth)
Expand Down
361 changes: 0 additions & 361 deletions src/main/java/org/broadinstitute/hellbender/utils/MathUtils.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.stat.descriptive.moment.Variance;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.utils.MathUtils;
import org.broadinstitute.hellbender.utils.Utils;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -61,8 +62,11 @@ public void testGCCorrection() {
//
final RealMatrix correctedCoverage = readCounts.copy();
GCBiasCorrector.correctGCBias(correctedCoverage, intervalGCContent);
final double[] correctedNoiseBySample = MathUtils.rowStdDevs(correctedCoverage);
Arrays.stream(correctedNoiseBySample).forEach(x -> Assert.assertTrue(x < NON_GC_BIAS_NOISE_LEVEL * MEAN_READ_DEPTH));
Utils.nonNull(correctedCoverage);
final Variance varianceEvaluator = new Variance();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should just use StandardDeviation (although this actually uses Variance under the hood as well).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

final double[] stdDevsBySample = IntStream.range(0, correctedCoverage.getRowDimension())
.mapToDouble(r -> Math.sqrt(varianceEvaluator.evaluate(correctedCoverage.getRow(r)))).toArray();
Arrays.stream(stdDevsBySample).forEach(x -> Assert.assertTrue(x < NON_GC_BIAS_NOISE_LEVEL * MEAN_READ_DEPTH));

//check that GC-bias correction is approximately idempotent -- if you correct again, very little should happen
final RealMatrix recorrectedCoverage = correctedCoverage.copy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import htsjdk.variant.variantcontext.*;
import htsjdk.variant.vcf.VCFConstants;
import org.apache.commons.math3.util.MathArrays;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.utils.MathUtils;
import org.broadinstitute.hellbender.GATKBaseTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ public void testRunningAverage() {
Assert.assertTrue(r.stddev() - 9072.6515881128 < 2e-10);
}

@Test
public void testRMS() {
Assert.assertEquals(MathUtils.rms(Arrays.asList(2,2,2)), 2.0);
Assert.assertEquals(MathUtils.rms(Arrays.asList(1,2,3)), Math.sqrt((1.0 + 4.0 + 9.0)/3));
}

@Test
public void log10BinomialProbability() throws Exception {
Assert.assertEquals(MathUtils.log10BinomialProbability(2, 1), log10(0.5),1E-9);
Expand All @@ -81,14 +75,6 @@ public void testLog1mexp(final double x, final double expected) {
Assert.assertEquals(actual,expected,1E-9);
}

@Test
public void testRandomIntegerInRange() throws Exception {
for (int i = 0; i < 1000; i++) {
final int n = MathUtils.randomIntegerInRange(10, 20);
Assert.assertTrue(n >= 10 && n <= 20);
}
}

@DataProvider(name = "log10OneMinusPow10Data")
public Iterator<Object[]> log10OneMinusPow10Data() {

Expand Down Expand Up @@ -378,11 +364,9 @@ public void testLog10Factorial() {
public void testSum() {
double[] doubleTest = {-1,0,1,2,3};
long[] longTest = {-1,0,1,2,3};
byte[] byteTest = {-1,0,1,2,3};
int[] intTest = {-1,0,1,2,3};
Assert.assertEquals(MathUtils.sum(doubleTest), 5.0);
Assert.assertEquals(MathUtils.sum(longTest), 5);
Assert.assertEquals(MathUtils.sum(byteTest), 5);
Assert.assertEquals(MathUtils.sum(intTest), 5);
}

Expand All @@ -394,9 +378,7 @@ public void testSumLog10() {

@Test
public void testSumRange() {
long[] longTest = {-1,0,1,2,3};
double[] doubleTest = {-1,0,1,2,3};
Assert.assertEquals(MathUtils.sum(longTest, 1, 4), 3);
Assert.assertEquals(MathUtils.sum(doubleTest, 1, 4), 3.0);
}

Expand All @@ -406,15 +388,6 @@ public void testSumCollection() {
Assert.assertEquals(MathUtils.sumDoubleFunction(list, n -> 0.5 * n), 7.5);
}

@Test
public void testPromote() {
int[] test = {0, 100, (int)1e10, (int)1e20};
double[] prom = MathUtils.promote(test);
for (int i = 0; i < test.length; i++) {
Assert.assertEquals(test[i], prom[i], 1e-14);
}
}

@Test
public void testCompareDoubles(){
Assert.assertEquals(MathUtils.compareDoubles(0.1, 0.2), 1);
Expand All @@ -424,19 +397,6 @@ public void testCompareDoubles(){
Assert.assertEquals(MathUtils.compareDoubles(0.1, 0.1 + (1e-7), 1e-8), 1);
}

@Test
public void testDoubleWithinRangeWithTolerance() {
Assert.assertEquals(MathUtils.doubleWithinRangeWithTolerance(0.01, 0.0, 1.0, 0.0), true);
Assert.assertEquals(MathUtils.doubleWithinRangeWithTolerance(0.01, 0.0, 1.0, 0.1), true);
Assert.assertEquals(MathUtils.doubleWithinRangeWithTolerance(0.01, 0.0, 1.0, 0.001), true);

Assert.assertEquals(MathUtils.doubleWithinRangeWithTolerance(-0.01, 0.0, 1.0, 0.1), true);
Assert.assertEquals(MathUtils.doubleWithinRangeWithTolerance(-0.01, 0.0, 1.0, 0.001), false);

Assert.assertEquals(MathUtils.doubleWithinRangeWithTolerance(1.01, 0.0, 1.0, 0.1), true);
Assert.assertEquals(MathUtils.doubleWithinRangeWithTolerance(1.01, 0.0, 1.0, 0.001), false);
}

@Test
public void testNormalizeFromReal(){
final double error = 1e-6;
Expand Down Expand Up @@ -1080,104 +1040,6 @@ public void naturalLogSumNaturalLogTest() {
Assert.assertEquals(actual, expected, 1e-10);
}

@Test
public void meanTest() {
Assert.assertEquals(MathUtils.mean(1,2,3), 2, 1e-10);
Assert.assertEquals(MathUtils.mean(1,2), 1.5, 1e-10);
Assert.assertEquals(MathUtils.mean(new double[] {1,2}), 1.5, 1e-10);
}

@Test
public void testMean() {
Assert.assertEquals (MathUtils.mean(0, 1, 2), 1, 1e-10);
Assert.assertEquals (MathUtils.mean(0, 1), 0.5, 1e-10);
double[] test = {0,1,101};
Assert.assertEquals(MathUtils.mean(test, 0, 0), Double.NaN);
Assert.assertEquals(MathUtils.mean(test, 0, 1), 0.0);
Assert.assertEquals(MathUtils.mean(test, 0, 3), 34.0);
}

@Test
public void testRowMeans() {
final double[][] array = { {1,2,3}, {4,5,6}, {7,8,9}};
final double[] rowMeans = MathUtils.rowMeans(new Array2DRowRealMatrix(array));
Assert.assertEquals(rowMeans, new double[] {2,5,8});
}

@Test
public void testRowVariances() {
final double[][] array = { {1,2,3}, {5,5,5}, {7,8,9}};
final double[] rowVariances = MathUtils.rowVariances(new Array2DRowRealMatrix(array));
Assert.assertEquals(rowVariances[0], 1, 1e-8);
Assert.assertEquals(rowVariances[1], 0, 1e-8);
Assert.assertEquals(rowVariances[2], 1, 1e-8);
}

@Test
public void testRowStdDevs() {
final double[][] array = { {1,2,3}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] rowStdDevs = MathUtils.rowStdDevs(new Array2DRowRealMatrix(array));
Assert.assertEquals(rowStdDevs[0], 1, 1e-8);
Assert.assertEquals(rowStdDevs[1], 0, 1e-8);
Assert.assertEquals(rowStdDevs[2], 1, 1e-8);
Assert.assertEquals(rowStdDevs[3], 13.65039682, 1e-8);
}

@Test
public void testStdDevsOnAList() {
Assert.assertEquals(MathUtils.stdDev(Arrays.asList(1, 2, 3)), 1, 1e-8);
Assert.assertEquals(MathUtils.stdDev(Arrays.asList(5, 5, 5)), 0, 1e-8);
Assert.assertEquals(MathUtils.stdDev(Arrays.asList(7, 8, 9)), 1, 1e-8);
Assert.assertEquals(MathUtils.stdDev(Arrays.asList(-15, 2, 12)), 13.65039682, 1e-8);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testStdDevsOnAListWithNull() {
MathUtils.stdDev(Arrays.asList(1, null, 3));
}

@Test
public void testStdDevsOnAnArray() {
Assert.assertEquals(MathUtils.stdDev(1, 2, 3), 1, 1e-8);
Assert.assertEquals(MathUtils.stdDev(5, 5, 5), 0, 1e-8);
Assert.assertEquals(MathUtils.stdDev(7, 8, 9), 1, 1e-8);
Assert.assertEquals(MathUtils.stdDev(-15, 2, 12), 13.65039682, 1e-8);
}

@Test
public void testColumnMeans() {
final double[][] array = { {1,2,3}, {4,5,6}, {7,8,9}};
final double[] columnMeans = MathUtils.columnMeans(new Array2DRowRealMatrix(array).transpose());
Assert.assertEquals(columnMeans, new double[]{2, 5, 8});
}

@Test
public void testColumnStdDevs() {
final double[][] array = { {1,2,3}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] columnStdDevs = MathUtils.columnStdDevs(new Array2DRowRealMatrix(array).transpose());
Assert.assertEquals(columnStdDevs[0], 1, 1e-8);
Assert.assertEquals(columnStdDevs[1], 0, 1e-8);
Assert.assertEquals(columnStdDevs[2], 1, 1e-8);
Assert.assertEquals(columnStdDevs[3], 13.65039682, 1e-8);
}

@Test
public void testLinspace() {
Assert.assertEquals(MathUtils.createEvenlySpacedPoints(-5, 1, 5), new double[] {-5.0000, -3.5000, -2.0000, -0.5000, 1.0000});
Assert.assertEquals(MathUtils.createEvenlySpacedPoints(-5, 1, 0), new double[] {});
Assert.assertEquals(MathUtils.createEvenlySpacedPoints(-5, 1, 1), new double[] {1.0});
Assert.assertEquals(MathUtils.createEvenlySpacedPoints(-5, 1, 2), new double[] {-5.0, 1.0});
Assert.assertEquals(MathUtils.createEvenlySpacedPoints(-5, -5, 2), new double[] {-5.0, -5.0});
Assert.assertEquals(MathUtils.createEvenlySpacedPoints(1e-10, 1, 2), new double[] {1e-10, 1});
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testLinspaceWithNegativePoints() {
MathUtils.createEvenlySpacedPoints(-5, 1, -2);
}



@Test
public void testRandomSelectFlatProbability() {
final RandomGenerator rg = RandomGeneratorFactory.createRandomGenerator(new Random(13));
Expand All @@ -1203,93 +1065,6 @@ public void testRandomSelect() {
Assert.assertEquals(result.stream().filter(n -> n == 1).count(), NUM_SAMPLES / 2, 50);
}

@Test
public void testColumnSum() {
final double[][] array = { {1,2,3}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.columnSums(new Array2DRowRealMatrix(array));
final double[] gt = {-2, 17, 29};
Assert.assertEquals(guess.length, 3);
Assert.assertEquals(guess, gt);
}

@Test
public void testColumnNaN() {
final double[][] array = { {1,2, Double.NaN}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.columnSums(new Array2DRowRealMatrix(array));
final double[] gt = {-2, 17, Double.NaN};
Assert.assertEquals(guess.length, 3);
Assert.assertEquals(guess[0], gt[0]);
Assert.assertEquals(guess[1], gt[1]);
Assert.assertTrue(Double.isNaN(guess[2]));
}

@Test
public void testColumnInf() {
final double[][] array = { {1,2, Double.POSITIVE_INFINITY}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.columnSums(new Array2DRowRealMatrix(array));
final double[] gt = {-2, 17, Double.POSITIVE_INFINITY};
Assert.assertEquals(guess.length, 3);
Assert.assertEquals(guess[0], gt[0]);
Assert.assertEquals(guess[1], gt[1]);
Assert.assertTrue(Double.isInfinite(guess[2]));
}

@Test
public void testColumnNegInf() {
final double[][] array = { {1,2, Double.NEGATIVE_INFINITY}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.columnSums(new Array2DRowRealMatrix(array));
final double[] gt = {-2, 17, Double.NEGATIVE_INFINITY};
Assert.assertEquals(guess.length, 3);
Assert.assertEquals(guess[0], gt[0]);
Assert.assertEquals(guess[1], gt[1]);
Assert.assertTrue(Double.isInfinite(guess[2]));
}

@Test
public void testRowSum() {
final double[][] array = { {1,2,3}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.rowSums(new Array2DRowRealMatrix(array));
final double[] gt = {6, 15, 24, -1};
Assert.assertEquals(guess.length, 4);
Assert.assertEquals(guess, gt);
}

@Test
public void testRowNaN() {
final double[][] array = { {1,2, Double.NaN}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.rowSums(new Array2DRowRealMatrix(array));
final double[] gt = {Double.NaN, 15, 24, -1};
Assert.assertEquals(guess.length, 4);
Assert.assertEquals(guess[1], gt[1]);
Assert.assertEquals(guess[2], gt[2]);
Assert.assertEquals(guess[3], gt[3]);
Assert.assertTrue(Double.isNaN(guess[0]));
}

@Test
public void testRowInf() {
final double[][] array = { {1,2, Double.POSITIVE_INFINITY}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.rowSums(new Array2DRowRealMatrix(array));
final double[] gt = {Double.POSITIVE_INFINITY, 15, 24, -1};
Assert.assertEquals(guess.length, 4);
Assert.assertEquals(guess[1], gt[1]);
Assert.assertEquals(guess[2], gt[2]);
Assert.assertEquals(guess[3], gt[3]);
Assert.assertTrue(Double.isInfinite(guess[0]));
}

@Test
public void testRowNegInf() {
final double[][] array = { {1,2, Double.NEGATIVE_INFINITY}, {5,5,5}, {7,8,9}, {-15, 2, 12}};
final double[] guess = MathUtils.rowSums(new Array2DRowRealMatrix(array));
final double[] gt = {Double.NEGATIVE_INFINITY, 15, 24, -1};
Assert.assertEquals(guess.length, 4);
Assert.assertEquals(guess[1], gt[1]);
Assert.assertEquals(guess[2], gt[2]);
Assert.assertEquals(guess[3], gt[3]);
Assert.assertTrue(Double.isInfinite(guess[0]));
}

@Test
public void testSum3d() {
final double[][][] array = {{{-2, 17}, {1, 1}}, {{1, 1}, {-2, 17}}, {{1, 1}, {1, 1}}, {{1, 1}, {1, 1}}};
Expand Down Expand Up @@ -1320,47 +1095,6 @@ public void testSecondSmallestMinusSmallest() {
Assert.assertEquals(MathUtils.secondSmallestMinusSmallest(new int[] { -10, -23, 3}, -1), 13);
}

@Test
public void testSmallestPowerOfTwoGreaterThan() {
Assert.assertEquals(MathUtils.smallestPowerOfTwoGreaterThan(0), 1);
Assert.assertEquals(MathUtils.smallestPowerOfTwoGreaterThan(1), 1);
Assert.assertEquals(MathUtils.smallestPowerOfTwoGreaterThan(8), 8);
Assert.assertEquals(MathUtils.smallestPowerOfTwoGreaterThan(10), 16);
Assert.assertEquals(MathUtils.smallestPowerOfTwoGreaterThan(1023), 1024);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testSmallestPowerOfTwoGreaterThanExceptions_0() {
MathUtils.smallestPowerOfTwoGreaterThan(-1);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testSmallestPowerOfTwoGreaterThanExceptions_1() {
MathUtils.smallestPowerOfTwoGreaterThan(Integer.MAX_VALUE/2 + 1);
}

@Test
public void testNearestNeighborUniform1DInterpolate() {
ArrayAsserts.assertArrayEquals(new double[]{0.0, 1.0, 2.0},
MathUtils.nearestNeighborUniform1DInterpolate(new double[]{0.0, 1.0, 2.0}, 3), 1e-6);
ArrayAsserts.assertArrayEquals(new double[]{0.0, 1.0, 1.0, 2.0},
MathUtils.nearestNeighborUniform1DInterpolate(new double[]{0.0, 1.0, 2.0}, 4), 1e-6);
ArrayAsserts.assertArrayEquals(new double[]{0.0, 0.0, 1.0, 1.0, 2.0, 2.0},
MathUtils.nearestNeighborUniform1DInterpolate(new double[]{0.0, 1.0, 2.0}, 6), 1e-6);
}


@Test
public void testMaxDifference() {
final double[] array1 = {0.0, 1.0, 2.0};
final double[] array2 = {-0.1, 1.05, 2.0};
Assert.assertEquals(MathUtils.maxDifference(Doubles.asList(array1), Doubles.asList(array2)), 0.1, 1e-10);

final double[] array3 = {0.0, 1.0, 2.0, 3.0};
final double[] array4 = {0.0, 1.0, 2.0, 0.0};
Assert.assertEquals(MathUtils.maxDifference(Doubles.asList(array3), Doubles.asList(array4)), 3.0, 1e-10);
}

@Test
public void testSumArrayFunction(){
final int min = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ public void testBases(){
}

public void checkResults(final int[] results, final int n, final int m) {
final double[] dresults = MathUtils.promote(results);
final double mean = MathUtils.mean(dresults, 0, dresults.length);
final double std = new StandardDeviation().evaluate(dresults);
final double mean = Arrays.stream(results).average().getAsDouble();
final double std = new StandardDeviation().evaluate(Arrays.stream(results).mapToDouble(i->i).toArray());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, is there some reason we don't typically use asDoubleStream in such code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No good reason.

final double expectedMean = (n*m)/4.0;
final double s = std; // not really because it's the population not the sample dtd but it'll do
Assert.assertTrue(mean < expectedMean + 2 * s / Math.sqrt(n * m), "unexpected mean:" + mean);
Expand Down
Loading