Source

polify / src / main / scala / updown / app / experiment / ExperimentalResult.scala

package updown.app.experiment

import updown.data.SentimentLabel

case class ExperimentalResult(name: String, n: Int, accuracy: Double, classes: List[LabelResult]) {
  def header: String = "\n%15s%5s%11s%8s%9s%9s\n".format("Label", "N", "Precision", "Recall", "F-Score", "MSE")

  override def toString(): String =
    "%s Results:\n".format(name) +
      "%10s%6d\n".format("N", n) +
      "%10s%6.2f\n".format("Accuracy", accuracy) +
      header +
      (for (res <- classes) yield res.toString).mkString("\n") + "\n"

  def rename(newName: String): ExperimentalResult =
    ExperimentalResult(newName, n,accuracy,classes)

  def +(other: ExperimentalResult): ExperimentalResult = {
    val classesMap = (classes.groupBy((labelResult) => labelResult.label).map((tup) => {
      val (k, (v: LabelResult) :: vs) = tup
      (k, v)
    })).toMap
    val otherClassesMap = (other.classes.groupBy((labelResult) => labelResult.label).map((tup) => {
      val (k, (v: LabelResult) :: vs) = tup
      (k, v)
    }).toMap).withDefaultValue(LabelResult(0, SentimentLabel.Abstained, 0.0, 0.0, 0.0, 0.0))
    ExperimentalResult(name, n + other.n, accuracy + other.accuracy,
      (for ((label, classResult) <- classesMap.toList) yield classResult + otherClassesMap(label)).toList
    )
  }

  def *(other: ExperimentalResult): ExperimentalResult = {
    val classesMap = (classes.groupBy((labelResult) => labelResult.label).map((tup) => {
      val (k, (v: LabelResult) :: vs) = tup
      (k, v)
    })).toMap
    val otherClassesMap = (other.classes.groupBy((labelResult) => labelResult.label).map((tup) => {
      val (k, (v: LabelResult) :: vs) = tup
      (k, v)
    }).toMap).withDefaultValue(LabelResult(0, SentimentLabel.Abstained, 0.0, 0.0, 0.0, 0.0))
    ExperimentalResult(name, n * other.n, accuracy * other.accuracy,
      (for ((label, classResult) <- classesMap.toList) yield classResult * otherClassesMap(label)).toList
    )
  }

  def -(other: ExperimentalResult): ExperimentalResult = {
    val negOther = other * -1
    this + negOther
  }

  def /(scalar: Double): ExperimentalResult = {
    ExperimentalResult(name, (n.toFloat / scalar).toInt, accuracy / scalar,
      (for (labelResult <- classes) yield labelResult / scalar).toList
    )
  }

  def *(scalar: Double): ExperimentalResult = {
    ExperimentalResult(name, (n.toFloat * scalar).toInt, accuracy * scalar,
      (for (labelResult <- classes) yield labelResult * scalar).toList
    )
  }
}


case class LabelResult(n: Int, label: SentimentLabel.Type, precision: Double, recall: Double, f: Double, mse: Double) {
  override def toString(): String = "%15s%5d%11.2f%8.2f%9.2f%9.2f".format(SentimentLabel.toEnglishName(label), n, precision, recall, f, mse)

  def +(other: LabelResult): LabelResult = {
    assert(label == other.label)
    LabelResult(n + other.n, label, precision + other.precision, recall + other.recall, f + other.f, mse + other.mse)
  }

  def *(other: LabelResult): LabelResult = {
    assert(label == other.label)
    LabelResult(n * other.n, label, precision * other.precision, recall * other.recall, f * other.f, mse * other.mse)
  }

  def /(scalar: Double): LabelResult = LabelResult((n.toFloat / scalar).toInt, label, precision / scalar, recall / scalar, f / scalar, mse / scalar)

  def *(scalar: Double): LabelResult = LabelResult((n.toFloat * scalar).toInt, label, precision * scalar, recall * scalar, f * scalar, mse / scalar)
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.