<commit_before>/*
 * Copyright (c) 2014. Real Time Genomics Limited.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.rtg.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.reeltwo.plot.Point2D;
import com.reeltwo.plot.PointPlot2D;
import com.reeltwo.plot.TextPlot2D;
import com.reeltwo.plot.TextPoint2D;
import com.rtg.util.ContingencyTable;
import com.rtg.vcf.eval.RocPoint;

/**
 * Holds counts.
 */
final class DataBundle {
  private static final int TOTAL_LABELS = 10;

  private final Point2D[] mPoints;
  private final String[] mScores;

  private final Point2D[] mPrecisionRecall;

  private String mTitle;
  private final int mTotalVariants;
  private final float mMinPrecision;
  private boolean mShow;

  private Point2D[] mRangedPoints = null;
  private String[] mRangedScores = null;
  private TextPoint2D[] mRangedPosPoints = null;
  private TextPoint2D[] mRangedPrecisionRecallPosPoints = null;
  private Point2D[] mRangedPrecisionRecall;
  private String mScoreName;

  DataBundle(String title, RocPoint[] points, String[] labels, int totalVariants) {
    final List<Point2D> asPoints = Arrays.stream(points)
      .map(point -> new Point2D((float) point.getFalsePositives(), (float) point.getTruePositives()))
      .collect(Collectors.toList());
    mPoints = asPoints.toArray(new Point2D[asPoints.size()]);
    mScores = labels;
    mTitle = title;
    mTotalVariants = totalVariants;
    mShow = true;
    mPrecisionRecall = new Point2D[points.length - 1];
    final boolean hasRaw = Arrays.stream(points).anyMatch(x -> x.getRawTruePositives() > 0);
    float minPrecision = 100;
    for (int i = 0; i < mPrecisionRecall.length; i++) {
      final RocPoint point = points[i + 1];
      final double truePositives = point.getTruePositives();
      final float rawTp = (float) (hasRaw ? point.getRawTruePositives() : truePositives);
      final float x = (float) (ContingencyTable.recall(truePositives, mTotalVariants - truePositives) * 100.0);
      final float y = (float) (ContingencyTable.precision(rawTp, point.getFalsePositives()) * 100.0);
      mPrecisionRecall[i] = new Point2D(x, y);
      minPrecision = Math.min(minPrecision, y);
    }
    mMinPrecision = minPrecision;

    resetRange();
  }

  boolean show() {
    return mShow;
  }

  void show(boolean flag) {
    mShow = flag;
  }

  void setTitle(String title) {
    mTitle = title;
  }

  void setScoreRange(float min, float max) {
    final int smin = (int) (min * mScores.length);<commit_msg>rocplot: Prevent exception when attempting to compute precision/sensitivity from a data file corresponding to an evaluation of called variants against zero baseline variants<commit_after>/*
 * Copyright (c) 2014. Real Time Genomics Limited.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.rtg.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.reeltwo.plot.Point2D;
import com.reeltwo.plot.PointPlot2D;
import com.reeltwo.plot.TextPlot2D;
import com.reeltwo.plot.TextPoint2D;
import com.rtg.util.ContingencyTable;
import com.rtg.vcf.eval.RocPoint;

/**
 * Holds counts.
 */
final class DataBundle {
  private static final int TOTAL_LABELS = 10;

  private final Point2D[] mPoints;
  private final String[] mScores;

  private final Point2D[] mPrecisionRecall;

  private String mTitle;
  private final int mTotalVariants;
  private final float mMinPrecision;
  private boolean mShow;

  private Point2D[] mRangedPoints = null;
  private String[] mRangedScores = null;
  private TextPoint2D[] mRangedPosPoints = null;
  private TextPoint2D[] mRangedPrecisionRecallPosPoints = null;
  private Point2D[] mRangedPrecisionRecall;
  private String mScoreName;

  DataBundle(String title, RocPoint[] points, String[] labels, int totalVariants) {
    final List<Point2D> asPoints = Arrays.stream(points)
      .map(point -> new Point2D((float) point.getFalsePositives(), (float) point.getTruePositives()))
      .collect(Collectors.toList());
    mPoints = asPoints.toArray(new Point2D[asPoints.size()]);
    mScores = labels;
    mTitle = title;
    mTotalVariants = totalVariants;
    mShow = true;
    mPrecisionRecall = new Point2D[totalVariants > 0 ? points.length - 1 : 0];
    final boolean hasRaw = Arrays.stream(points).anyMatch(x -> x.getRawTruePositives() > 0);
    float minPrecision = 100;
    for (int i = 0; i < mPrecisionRecall.length; i++) {
      final RocPoint point = points[i + 1];
      final double truePositives = point.getTruePositives();
      final float rawTp = (float) (hasRaw ? point.getRawTruePositives() : truePositives);
      final float x = (float) (ContingencyTable.recall(truePositives, mTotalVariants - truePositives) * 100.0);
      final float y = (float) (ContingencyTable.precision(rawTp, point.getFalsePositives()) * 100.0);
      mPrecisionRecall[i] = new Point2D(x, y);
      minPrecision = Math.min(minPrecision, y);
    }
    mMinPrecision = minPrecision;

    resetRange();
  }

  boolean show() {
    return mShow;
  }

  void show(boolean flag) {
    mShow = flag;
  }

  void setTitle(String title) {
    mTitle = title;
  }

  void setScoreRange(float min, float max) {
    final int smin = (int) (min * mScores.length);
{'fraction_non_alphanumeric': 0.0883761976802824, 'fraction_numerical': 0.009833585476550681, 'mean_word_length': 4.065772669220945, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '226806', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2289, 'n_words': 810}