src/Pure/ML/ml_statistics.scala
author wenzelm
Wed, 02 Jan 2013 19:23:18 +0100
changeset 50688 f02864682307
parent 50685 293e8ec4dfc8
child 50689 0607d557d073
permissions -rw-r--r--
some support for ML statistics content interpretation;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/ML/ml_statistics.ML
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     3
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     4
ML runtime statistics.
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     5
*/
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     6
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     7
package isabelle
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     8
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     9
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    10
import scala.collection.immutable.{SortedSet, SortedMap}
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    11
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    12
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    13
object ML_Statistics
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    14
{
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    15
  /* read properties from build log */
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    16
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    17
  private val line_prefix = "\fML_statistics = "
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    18
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    19
  private val syntax = Outer_Syntax.empty + "," + "(" + ")" + "[" + "]"
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    20
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    21
  private object Parser extends Parse.Parser
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    22
  {
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    23
    private def stat: Parser[(String, String)] =
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    24
      keyword("(") ~ string ~ keyword(",") ~ string ~ keyword(")") ^^
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    25
      { case _ ~ x ~ _ ~ y ~ _ => (x, y) }
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    26
    private def stats: Parser[Properties.T] =
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    27
      keyword("[") ~> repsep(stat, keyword(",")) <~ keyword("]")
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    28
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    29
    def parse_stats(s: String): Properties.T =
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    30
    {
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    31
      parse_all(stats, Token.reader(syntax.scan(s))) match {
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    32
        case Success(result, _) => result
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    33
        case bad => error(bad.toString)
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    34
      }
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    35
    }
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    36
  }
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    37
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    38
  def read_log(log: Path): List[Properties.T] =
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    39
    for {
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    40
      line <- split_lines(File.read_gzip(log))
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    41
      if line.startsWith(line_prefix)
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    42
      stats = line.substring(line_prefix.length)
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    43
    } yield Parser.parse_stats(stats)
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    44
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    45
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    46
  /* content interpretation */
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    47
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    48
  val Now = new Properties.Double("now")
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    49
  final case class Entry(time: Double, data: Map[String, Double])
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    50
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    51
  def apply(stats: List[Properties.T]): ML_Statistics = new ML_Statistics(stats)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    52
  def apply(log: Path): ML_Statistics = apply(read_log(log))
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    53
}
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    54
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    55
final class ML_Statistics private(val stats: List[Properties.T])
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    56
{
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    57
  require(!stats.isEmpty && stats.forall(props => ML_Statistics.Now.unapply(props).isDefined))
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    58
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    59
  val time_start = ML_Statistics.Now.unapply(stats.head).get
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    60
  val duration = ML_Statistics.Now.unapply(stats.last).get - time_start
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    61
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    62
  val names: Set[String] =
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    63
    SortedSet.empty[String] ++
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    64
      (for (props <- stats.iterator; (x, _) <- props.iterator if x != ML_Statistics.Now.name)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    65
        yield x)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    66
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    67
  val content: List[ML_Statistics.Entry] =
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    68
    stats.map(props => {
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    69
      val time = ML_Statistics.Now.unapply(props).get - time_start
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    70
      require(time >= 0.0)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    71
      val data =
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    72
        SortedMap.empty[String, Double] ++
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    73
          (for ((x, y) <- props.iterator if x != ML_Statistics.Now.name)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    74
            yield (x, java.lang.Double.parseDouble(y)))
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    75
      ML_Statistics.Entry(time, data)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    76
    })
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    77
}
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    78