final double mode = modeBin.getIdValue(); final double sizeOfModeBin = modeBin.getValue(); final double minimumBinSize = sizeOfModeBin/tailLimit; Histogram<K>.Bin lastBin = null; double binId = ((Number)bin.getId()).doubleValue(); binsToKeep.add(bin.getId()); else if ((lastBin != null && ((Number)lastBin.getId()).doubleValue() != binId - 1) || bin.getValue() < minimumBinSize) { break; binsToKeep.add(bin.getId());
/** Increments the value in the designated bin by the supplied increment. */ public void increment(final K id, final double increment) { Bin<K> bin = map.get(id); if (bin == null) { bin = new Bin<>(id); map.put(id, bin); } bin.value += increment; }
final int depth = Math.min((int) bin.getIdValue(), coverageCap); depthHistogramArray[depth] += bin.getValue();
final int depth = Math.min((int) bin.getIdValue(), coverageCap); depthHistogramArray[depth] += bin.getValue();
final double mode = modeBin.getIdValue(); final double sizeOfModeBin = modeBin.getValue(); final double minimumBinSize = sizeOfModeBin/tailLimit; Bin<K> lastBin = null; double binId = ((Number)bin.getId()).doubleValue(); binsToKeep.add(bin.getId()); else if ((lastBin != null && ((Number)lastBin.getId()).doubleValue() != binId - 1) || bin.getValue() < minimumBinSize) { break; binsToKeep.add(bin.getId());
final double mode = modeBin.getIdValue(); final double sizeOfModeBin = modeBin.getValue(); final double minimumBinSize = sizeOfModeBin/tailLimit; Bin<K> lastBin = null; double binId = ((Number)bin.getId()).doubleValue(); binsToKeep.add(bin.getId()); else if ((lastBin != null && ((Number)lastBin.getId()).doubleValue() != binId - 1) || bin.getValue() < minimumBinSize) { break; binsToKeep.add(bin.getId());
/** * Gets the bin in which the given percentile falls. * Should only be called on histograms with non-negative values and a positive sum of values. * * @param percentile a value between 0 and 1 * @return the bin value in which the percentile falls */ public double getPercentile(final double percentile) { if (percentile <= 0) throw new IllegalArgumentException("Cannot query percentiles of 0 or below"); if (percentile >= 1) throw new IllegalArgumentException("Cannot query percentiles of 1 or above"); values().stream() .filter(b -> b.getValue() < 0) .findFirst() .ifPresent(b -> { throw new IllegalStateException("Cannot calculate Percentile when negative counts are present " + "in histogram. Bin " + b.getId() + "=" + b.getValue()); }); final double total = getCount(); if (total == 0) throw new IllegalStateException("Cannot calculate percentiles when total is zero."); double soFar = 0; for (Bin<K> bin : values()) { soFar += bin.getValue(); if (soFar / total >= percentile) return bin.getIdValue(); } throw new IllegalStateException("UNPOSSIBLE! Could not find percentile: " + percentile); }
/** * Gets the bin in which the given percentile falls. * Should only be called on histograms with non-negative values and a positive sum of values. * * @param percentile a value between 0 and 1 * @return the bin value in which the percentile falls */ public double getPercentile(final double percentile) { if (percentile <= 0) throw new IllegalArgumentException("Cannot query percentiles of 0 or below"); if (percentile >= 1) throw new IllegalArgumentException("Cannot query percentiles of 1 or above"); values().stream() .filter(b -> b.getValue() < 0) .findFirst() .ifPresent(b -> { throw new IllegalStateException("Cannot calculate Percentile when negative counts are present " + "in histogram. Bin " + b.getId() + "=" + b.getValue()); }); final double total = getCount(); if (total == 0) throw new IllegalStateException("Cannot calculate percentiles when total is zero."); double soFar = 0; for (Bin<K> bin : values()) { soFar += bin.getValue(); if (soFar / total >= percentile) return bin.getIdValue(); } throw new IllegalStateException("UNPOSSIBLE! Could not find percentile: " + percentile); }
/** * Outputs validation summary report to out. * * @param samReader records to validate * @param reference if null, NM tag validation is skipped * @return boolean true if there are no validation errors, otherwise false */ public boolean validateSamFileSummary(final SAMFileReader samReader, final ReferenceSequenceFile reference) { init(reference, samReader.getFileHeader()); validateSamFile(samReader, out); boolean result = errorsByType.isEmpty(); if (errorsByType.getCount() > 0) { // Convert to a histogram with String IDs so that WARNING: or ERROR: can be prepended to the error type. final Histogram<String> errorsAndWarningsByType = new Histogram<String>("Error Type", "Count"); for (final Histogram<SAMValidationError.Type>.Bin bin : errorsByType.values()) { errorsAndWarningsByType.increment(bin.getId().getHistogramString(), bin.getValue()); } final MetricsFile<ValidationMetrics, String> metricsFile = new MetricsFile<ValidationMetrics, String>(); errorsByType.setBinLabel("Error Type"); errorsByType.setValueLabel("Count"); metricsFile.setHistogram(errorsAndWarningsByType); metricsFile.write(out); } cleanup(); return result; }
/** * Outputs validation summary report to out. * * @param samReader records to validate * @param reference if null, NM tag validation is skipped * @return boolean true if there are no validation errors, otherwise false */ public boolean validateSamFileSummary(final SamReader samReader, final ReferenceSequenceFile reference) { init(reference, samReader.getFileHeader()); validateSamFile(samReader, out); boolean result = errorsByType.isEmpty(); if (errorsByType.getCount() > 0) { // Convert to a histogram with String IDs so that WARNING: or ERROR: can be prepended to the error type. final Histogram<String> errorsAndWarningsByType = new Histogram<>("Error Type", "Count"); for (final Histogram.Bin<Type> bin : errorsByType.values()) { errorsAndWarningsByType.increment(bin.getId().getHistogramString(), bin.getValue()); } final MetricsFile<ValidationMetrics, String> metricsFile = new MetricsFile<>(); errorsByType.setBinLabel("Error Type"); errorsByType.setValueLabel("Count"); metricsFile.setHistogram(errorsAndWarningsByType); metricsFile.write(out); } cleanup(); return result; }
/** * Outputs validation summary report to out. * * @param samReader records to validate * @param reference if null, NM tag validation is skipped * @return boolean true if there are no validation errors, otherwise false */ public boolean validateSamFileSummary(final SamReader samReader, final ReferenceSequenceFile reference) { init(reference, samReader.getFileHeader()); validateSamFile(samReader, out); boolean result = errorsByType.isEmpty(); if (errorsByType.getCount() > 0) { // Convert to a histogram with String IDs so that WARNING: or ERROR: can be prepended to the error type. final Histogram<String> errorsAndWarningsByType = new Histogram<>("Error Type", "Count"); for (final Histogram.Bin<Type> bin : errorsByType.values()) { errorsAndWarningsByType.increment(bin.getId().getHistogramString(), bin.getValue()); } final MetricsFile<ValidationMetrics, String> metricsFile = new MetricsFile<>(); errorsByType.setBinLabel("Error Type"); errorsByType.setValueLabel("Count"); metricsFile.setHistogram(errorsAndWarningsByType); metricsFile.write(out); } cleanup(); return result; }
public double getMedian() { double total = 0; double count = getCount(); // Base cases if (count == 0) return 0; if (count == 1) return values().iterator().next().getIdValue(); final double midLow, midHigh; if (count % 2 == 0) { midLow = count / 2; midHigh = midLow + 1; } else { midLow = Math.ceil(count / 2); midHigh = midLow; } Double midLowValue = null; Double midHighValue = null; for (final Bin bin : values()) { total += bin.getValue(); if (midLowValue == null && total >= midLow) midLowValue = bin.getIdValue(); if (midHighValue == null && total >= midHigh) midHighValue = bin.getIdValue(); if (midLowValue != null && midHighValue != null) break; } return (midLowValue + midHighValue) / 2; }
public double getMedian() { double total = 0; double count = getCount(); // Base cases if (count == 0) return 0; if (count == 1) return values().iterator().next().getIdValue(); final double midLow, midHigh; if (count % 2 == 0) { midLow = count / 2; midHigh = midLow + 1; } else { midLow = Math.ceil(count / 2); midHigh = midLow; } Double midLowValue = null; Double midHighValue = null; for (final Bin<K> bin : values()) { total += bin.getValue(); if (midLowValue == null && total >= midLow) midLowValue = bin.getIdValue(); if (midHighValue == null && total >= midHigh) midHighValue = bin.getIdValue(); if (midLowValue != null && midHighValue != null) break; } return (midLowValue + midHighValue) / 2; }
public double getMedian() { double total = 0; double count = getCount(); // Base cases if (count == 0) return 0; if (count == 1) return values().iterator().next().getIdValue(); final double midLow, midHigh; if (count % 2 == 0) { midLow = count / 2; midHigh = midLow + 1; } else { midLow = Math.ceil(count / 2); midHigh = midLow; } Double midLowValue = null; Double midHighValue = null; for (final Bin<K> bin : values()) { total += bin.getValue(); if (midLowValue == null && total >= midLow) midLowValue = bin.getIdValue(); if (midHighValue == null && total >= midHigh) midHighValue = bin.getIdValue(); if (midLowValue != null && midHighValue != null) break; } return (midLowValue + midHighValue) / 2; }
/** * Calculates the median bin size */ public double getMedianBinSize() { if (size() == 0) { return 0; } final List<Double> binValues = new ArrayList<>(); for (final Bin<K> bin : values()) { binValues.add(bin.getValue()); } Collections.sort(binValues); final int midPoint = binValues.size() / 2; double median = binValues.get(midPoint); if (binValues.size() % 2 == 0) { median = (median + binValues.get(midPoint - 1)) / 2; } return median; }
/** * Calculates the median bin size */ public double getMedianBinSize() { if (size() == 0) { return 0; } final List<Double> binValues = new ArrayList<Double>(); for (final Bin bin : values()) { binValues.add(bin.getValue()); } Collections.sort(binValues); final int midPoint = binValues.size() / 2; double median = binValues.get(midPoint); if (binValues.size() % 2 == 0) { median = (median + binValues.get(midPoint-1)) / 2; } return median; }
/** * Calculates the median bin size */ public double getMedianBinSize() { if (size() == 0) { return 0; } final List<Double> binValues = new ArrayList<>(); for (final Bin<K> bin : values()) { binValues.add(bin.getValue()); } Collections.sort(binValues); final int midPoint = binValues.size() / 2; double median = binValues.get(midPoint); if (binValues.size() % 2 == 0) { median = (median + binValues.get(midPoint - 1)) / 2; } return median; }
/*** * Immutable method that divides the current Histogram by an input Histogram and generates a new one * Throws an exception if the bins don't match up exactly * @param divisorHistogram * @return * @throws IllegalArgumentException */ public Histogram<K> divideByHistogram(final Histogram<K> divisorHistogram) throws IllegalArgumentException{ Histogram<K> output = new Histogram<K>(); if (!this.keySet().equals(divisorHistogram.keySet())) throw new IllegalArgumentException("Attempting to divide Histograms with non-identical bins"); for (final K key : this.keySet()){ Bin dividend = this.get(key); Bin divisor = divisorHistogram.get(key); output.increment(key, dividend.getValue()/divisor.getValue()); } return output; }
@Test public void testAddHistogram() { final int[] is1 = {4,4,5,5,5}; final Histogram<Integer> histo1 = new Histogram<>(); Assert.assertTrue(histo1.isEmpty()); for (final int i : is1) histo1.increment(i); Assert.assertFalse(histo1.isEmpty()); final int[] is2 = {5,5, 6,6,6,6}; final Histogram<Integer> histo2 = new Histogram<>(); for (final int i : is2) histo2.increment(i); Assert.assertEquals(histo1.get(4).getValue(), 2.0); Assert.assertEquals(histo1.get(5).getValue(), 3.0); Assert.assertEquals(histo1.get(6), null); histo1.addHistogram(histo2); Assert.assertEquals(histo1.get(4).getValue(), 2.0); Assert.assertEquals(histo1.get(5).getValue(), 5.0); Assert.assertEquals(histo1.get(6).getValue(), 4.0); }