<commit_before>package org.scalameter
package reporting



import org.jfree.data.xy.{XYSeries, XYSeriesCollection, YIntervalSeriesCollection, YIntervalSeries}
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer
import org.jfree.chart.renderer.xy.DeviationRenderer
import org.jfree.chart.renderer.category.BarRenderer
import org.jfree.chart.plot.{XYPlot, CategoryPlot}
import org.jfree.data.statistics._
import org.jfree.chart.{ChartFactory => JFreeChartFactory}
import org.jfree.chart.plot.PlotOrientation
import org.jfree.data.category.DefaultCategoryDataset
import org.jfree.chart._
import java.io._
import collection._
import utils.Tree
import utils.Statistics._
import java.awt.BasicStroke
import java.awt.Color
import Key.reports._



case class ChartReporter(drawer: ChartReporter.ChartFactory, fileNamePrefix: String = "", wdt: Int = 1600, hgt: Int = 1200) extends Reporter {

  def report(result: CurveData, persistor: Persistor) {
    // nothing - the charts are generated only at the end
  }

  def report(result: Tree[CurveData], persistor: Persistor) = for ((ctx, curves) <- result.scopes if curves.nonEmpty) {
    val scopename = ctx.scope
    val history = persistor.load(ctx)
    val chart = drawer.createChart(scopename, curves, history)
    val dir = result.context.goe(resultDir, "tmp")
    new File(dir).mkdir()
    val chartfile = new File(s"$dir/$fileNamePrefix$scopename.png")
    ChartUtilities.saveChartAsPNG(chartfile, chart, wdt, hgt)
  } 

}


object ChartReporter {

  import Key._

  trait ChartFactory {
    /** Generates a chart for the given curve data, with the given history.
     *
     *  @param scopename      name of the chart
     *  @param cs             a list of curves that should appear on the chart
     *  @param history        previous chart data for the same set of curves
     */
    def createChart(scopename: String, cs: Seq[CurveData], history: History, colors: Seq[Color] = Seq()): JFreeChart
  }

  object ChartFactory {

    case class XYLine() extends ChartFactory {
      def createChart(scopename: String, cs: Seq[CurveData], history: History, colors: Seq[Color] = Seq()): JFreeChart = {
        val seriesCollection = new XYSeriesCollection
        val chartName = scopename
        val xAxisName = cs.head.measurements.head.params.axisData.head._1
        val renderer = new XYLineAndShapeRenderer()

        for ((curve, idx) <- cs.zipWithIndex) {
          val series = new XYSeries(curve.context.goe(dsl.curve, idx.toString), true, true)
          for (measurement <- curve.measurements) {
            series.add(measurement.params.axisData.head._2.asInstanceOf[Int], measurement.time)
          }
          seriesCollection.addSeries(series)
          renderer.setSeriesShapesVisible(idx, true)
        }

        val chart = JFreeChartFactory.createXYLineChart(chartName, xAxisName, "time", seriesCollection, PlotOrientation.VERTICAL, true, true, false)
        chart.getPlot.setBackgroundPaint(new java.awt.Color(180, 180, 180))
        chart.getPlot.asInstanceOf[XYPlot].setRenderer(renderer)
        chart.setAntiAlias(true)

        chart
      }
    }

    case class Regression(showLatestCi: Boolean, showHistoryCi: Boolean, alpha: Double) extends ChartFactory {
      /**
       *
       *
       * @param colors        specifies the colors assigned to the the first `colors.size` curves from `cs`.
                              The rest of the curves are assigned some default set of colors.
       */
      def createChart(scopename: String, cs: Seq[CurveData], history: History, colors: Seq[Color] = Seq()): JFreeChart = {

        def createDataset = {
          val dataset = new YIntervalSeriesCollection
          if(history.results.isEmpty) {
              for ((curve, curveIndex) <- cs.zipWithIndex) {
                val series = new YIntervalSeries(curve.context.goe(dsl.curve, curveIndex.toString))
                for (measurement <- curve.measurements) {
                  val ciForThisPoint = if (showLatestCi) {
                    confidenceInterval(measurement.complete, alpha)
                  } else {
                    (0D, 0D)
                  }
                series.add(measurement.params.axisData.head._2.asInstanceOf[Int], measurement.time, ciForThisPoint._1, ciForThisPoint._2)
                }
              }
          } else {
            for ((curve, curveIndex) <- cs.zipWithIndex) {
              val newestSeries = new YIntervalSeries(curve.context.goe(dsl.curve, curveIndex.toString))<commit_msg>Refactor a chart factory name.<commit_after>package org.scalameter
package reporting



import org.jfree.data.xy.{XYSeries, XYSeriesCollection, YIntervalSeriesCollection, YIntervalSeries}
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer
import org.jfree.chart.renderer.xy.DeviationRenderer
import org.jfree.chart.renderer.category.BarRenderer
import org.jfree.chart.plot.{XYPlot, CategoryPlot}
import org.jfree.data.statistics._
import org.jfree.chart.{ChartFactory => JFreeChartFactory}
import org.jfree.chart.plot.PlotOrientation
import org.jfree.data.category.DefaultCategoryDataset
import org.jfree.chart._
import java.io._
import collection._
import utils.Tree
import utils.Statistics._
import java.awt.BasicStroke
import java.awt.Color
import Key.reports._



case class ChartReporter(drawer: ChartReporter.ChartFactory, fileNamePrefix: String = "", wdt: Int = 1600, hgt: Int = 1200) extends Reporter {

  def report(result: CurveData, persistor: Persistor) {
    // nothing - the charts are generated only at the end
  }

  def report(result: Tree[CurveData], persistor: Persistor) = for ((ctx, curves) <- result.scopes if curves.nonEmpty) {
    val scopename = ctx.scope
    val history = persistor.load(ctx)
    val chart = drawer.createChart(scopename, curves, history)
    val dir = result.context.goe(resultDir, "tmp")
    new File(dir).mkdir()
    val chartfile = new File(s"$dir/$fileNamePrefix$scopename.png")
    ChartUtilities.saveChartAsPNG(chartfile, chart, wdt, hgt)
  } 

}


object ChartReporter {

  import Key._

  trait ChartFactory {
    /** Generates a chart for the given curve data, with the given history.
     *
     *  @param scopename      name of the chart
     *  @param cs             a list of curves that should appear on the chart
     *  @param history        previous chart data for the same set of curves
     */
    def createChart(scopename: String, cs: Seq[CurveData], history: History, colors: Seq[Color] = Seq()): JFreeChart
  }

  object ChartFactory {

    case class XYLine() extends ChartFactory {
      def createChart(scopename: String, cs: Seq[CurveData], history: History, colors: Seq[Color] = Seq()): JFreeChart = {
        val seriesCollection = new XYSeriesCollection
        val chartName = scopename
        val xAxisName = cs.head.measurements.head.params.axisData.head._1
        val renderer = new XYLineAndShapeRenderer()

        for ((curve, idx) <- cs.zipWithIndex) {
          val series = new XYSeries(curve.context.goe(dsl.curve, idx.toString), true, true)
          for (measurement <- curve.measurements) {
            series.add(measurement.params.axisData.head._2.asInstanceOf[Int], measurement.time)
          }
          seriesCollection.addSeries(series)
          renderer.setSeriesShapesVisible(idx, true)
        }

        val chart = JFreeChartFactory.createXYLineChart(chartName, xAxisName, "time", seriesCollection, PlotOrientation.VERTICAL, true, true, false)
        chart.getPlot.setBackgroundPaint(new java.awt.Color(180, 180, 180))
        chart.getPlot.asInstanceOf[XYPlot].setRenderer(renderer)
        chart.setAntiAlias(true)

        chart
      }
    }

    case class ConfidenceIntervals(showLatestCi: Boolean, showHistoryCi: Boolean, alpha: Double) extends ChartFactory {
      /**
       *
       *
       * @param colors        specifies the colors assigned to the the first `colors.size` curves from `cs`.
                              The rest of the curves are assigned some default set of colors.
       */
      def createChart(scopename: String, cs: Seq[CurveData], history: History, colors: Seq[Color] = Seq()): JFreeChart = {

        def createDataset = {
          val dataset = new YIntervalSeriesCollection
          if(history.results.isEmpty) {
              for ((curve, curveIndex) <- cs.zipWithIndex) {
                val series = new YIntervalSeries(curve.context.goe(dsl.curve, curveIndex.toString))
                for (measurement <- curve.measurements) {
                  val ciForThisPoint = if (showLatestCi) {
                    confidenceInterval(measurement.complete, alpha)
                  } else {
                    (0D, 0D)
                  }
                series.add(measurement.params.axisData.head._2.asInstanceOf[Int], measurement.time, ciForThisPoint._1, ciForThisPoint._2)
                }
              }
          } else {
            for ((curve, curveIndex) <- cs.zipWithIndex) {
              val newestSeries = new YIntervalSeries(curve.context.goe(dsl.curve, curveIndex.toString))
{'fraction_non_alphanumeric': 0.09726208876131597, 'fraction_numerical': 0.0052991830426142635, 'mean_word_length': 2.8647610921501707, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1370165', 'n_tokens_mistral': 2712, 'n_tokens_neox': 2575, 'n_words': 647}