use of org.apache.commons.math3.stat.descriptive.rank.Percentile in project gatk-protected by broadinstitute.
the class HDF5PCACoveragePoNCreationUtils method subsetReadCountsToUsableTargets.
/**
* Subsets targets in the input count to the usable ones based on the percentile threshold indicated
* by the user.
*
* <p>
* It returns a pair of object, where the left one is the updated read-counts with only the usable
* targets, and the right one is the corresponding target factors.
* </p>
*
* @param readCounts the input read-counts.
* @param targetFactorPercentileThreshold the minimum median count percentile under which targets are not considered useful.
* @return never {@code null}.
*/
@VisibleForTesting
static Pair<ReadCountCollection, double[]> subsetReadCountsToUsableTargets(final ReadCountCollection readCounts, final double targetFactorPercentileThreshold, final Logger logger) {
final double[] targetFactors = calculateTargetFactors(readCounts);
final double threshold = new Percentile(targetFactorPercentileThreshold).evaluate(targetFactors);
final List<Target> targetByIndex = readCounts.targets();
final Set<Target> result = IntStream.range(0, targetFactors.length).filter(i -> targetFactors[i] >= threshold).mapToObj(targetByIndex::get).collect(Collectors.toCollection(LinkedHashSet::new));
if (result.size() == targetByIndex.size()) {
logger.info(String.format("All %d targets are kept", targetByIndex.size()));
return new ImmutablePair<>(readCounts, targetFactors);
} else {
final int discardedCount = targetFactors.length - result.size();
logger.info(String.format("Discarded %d target(s) out of %d with factors below %.2g (%.2f percentile)", discardedCount, targetFactors.length, threshold, targetFactorPercentileThreshold));
final double[] targetFactorSubset = DoubleStream.of(targetFactors).filter(i -> i >= threshold).toArray();
return new ImmutablePair<>(readCounts.subsetTargets(result), targetFactorSubset);
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Percentile in project gatk-protected by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testTruncateExtremeCounts.
@Test(dataProvider = "readCountAndPercentileData")
public void testTruncateExtremeCounts(final ReadCountCollection readCount, final double percentile) {
final RealMatrix counts = readCount.counts();
final double[] allCounts = Stream.of(counts.getData()).flatMap(row -> DoubleStream.of(row).boxed()).mapToDouble(Double::doubleValue).toArray();
final double bottom = new Percentile(percentile).evaluate(allCounts);
final double top = new Percentile(100 - percentile).evaluate(allCounts);
final double[][] expected = new double[counts.getRowDimension()][];
for (int i = 0; i < expected.length; i++) {
expected[i] = DoubleStream.of(counts.getRow(i)).map(d -> d < bottom ? bottom : (d > top) ? top : d).toArray();
}
ReadCountCollectionUtils.truncateExtremeCounts(readCount, percentile, NULL_LOGGER);
final RealMatrix newCounts = readCount.counts();
Assert.assertEquals(newCounts.getRowDimension(), newCounts.getRowDimension());
Assert.assertEquals(newCounts.getColumnDimension(), newCounts.getColumnDimension());
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]);
}
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Percentile in project gatk-protected by broadinstitute.
the class ReadCountCollectionUtilsUnitTest method testExtremeMedianColumnsData.
@Test(dataProvider = "readCountAndPercentileData")
public void testExtremeMedianColumnsData(final ReadCountCollection readCount, final double percentile) {
final Median median = new Median();
final RealMatrix counts = readCount.counts();
final double[] columnMedians = IntStream.range(0, counts.getColumnDimension()).mapToDouble(i -> median.evaluate(counts.getColumn(i))).toArray();
final double top = new Percentile(100 - percentile).evaluate(columnMedians);
final double bottom = new Percentile(percentile).evaluate(columnMedians);
final Boolean[] toBeKept = DoubleStream.of(columnMedians).mapToObj(d -> d <= top && d >= bottom).toArray(Boolean[]::new);
final int toBeKeptCount = (int) Stream.of(toBeKept).filter(b -> b).count();
final ReadCountCollection result = ReadCountCollectionUtils.removeColumnsWithExtremeMedianCounts(readCount, percentile, NULL_LOGGER);
Assert.assertEquals(result.columnNames().size(), toBeKeptCount);
int nextIndex = 0;
for (int i = 0; i < toBeKept.length; i++) {
if (toBeKept[i]) {
int index = result.columnNames().indexOf(readCount.columnNames().get(i));
Assert.assertEquals(index, nextIndex++);
Assert.assertEquals(counts.getColumn(i), result.counts().getColumn(index));
} else {
Assert.assertEquals(result.columnNames().indexOf(readCount.columnNames().get(i)), -1);
}
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Percentile in project lightning by automatictester.
the class RespTimeNthPercentileTest method execute.
@Override
public void execute(ArrayList<String[]> originalJMeterTransactions) {
try {
JMeterTransactions transactions = filterTransactions((JMeterTransactions) originalJMeterTransactions);
transactionCount = transactions.getTransactionCount();
DescriptiveStatistics ds = new DescriptiveStatistics();
ds.setPercentileImpl(new Percentile().withEstimationType(Percentile.EstimationType.R_3));
for (String[] transaction : transactions) {
String elapsed = transaction[1];
ds.addValue(Double.parseDouble(elapsed));
}
longestTransactions = transactions.getLongestTransactions();
actualResult = (int) ds.getPercentile((double) percentile);
actualResultDescription = String.format(ACTUAL_RESULT_MESSAGE, new IntToOrdConverter().convert(percentile), actualResult);
if (actualResult > maxRespTime) {
result = TestResult.FAIL;
} else {
result = TestResult.PASS;
}
} catch (Exception e) {
result = TestResult.ERROR;
actualResultDescription = e.getMessage();
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Percentile in project nd4j by deeplearning4j.
the class Nd4jTestsC method testPercentile2.
@Test
public void testPercentile2() throws Exception {
INDArray array = Nd4j.linspace(1, 9, 9);
Percentile percentile = new Percentile(50);
double exp = percentile.evaluate(array.data().asDouble());
assertEquals(exp, array.percentileNumber(50));
}
Aggregations