import sys
import os
import sqlite3

from interp.tools import rms, improved

import numpy as np

from   optparse    import OptionParser

if __name__ == '__main__':
  parser = OptionParser(usage = "usage: %s [options] <db file>")

  parser.add_option("-R", "--resolution",
    type="int", dest="res", default=1,
    help="resolution (for constant resolution charts) (default: %default)")

  parser.add_option("-t", "--time",
    action="store_true", dest="time", default=False,
    help="write out timing results (default: %default)")

  parser.add_option("-T", "--truthy",
    action="store_true", dest="truthy", default=False,
    help="calculate truthiness plots (default: %default)")

  parser.add_option("-r", "--rms",
    action="store_true", dest="rms", default=False,
    help="write out rms output (default: %default)")


  (options, args) = parser.parse_args()
  if len(args) != 1:
    parser.print_usage()
    sys.exit(1)

  con = sqlite3.connect(args[0])
  cur = con.cursor()


  resolutions = np.array(cur.execute('select distinct res from results').fetchall())[:,0]
  orders      = np.array(cur.execute('select distinct ord from results').fetchall())[:,0]
  extrapoints = np.array(cur.execute('select distinct ep  from results').fetchall())[:,0]
  print "# i found these resolutions %s, orders: %s, and extra points: %s" % (resolutions, orders, extrapoints)

  if options.time:
    for ep in extrapoints:
      print "%d " % (ep,),
      for order in orders:
        for res in resolutions:
          cur_results = cur.execute('select time from results where res = ? and ep = ? and ord = ?',
                                   (int(res), int(ep),int(order)))
          cur_time = np.array(cur_results.fetchall()).mean()
          print "%e " % (cur_time,),

      print


  # print "%e" % np.array(cur.execute('select abs(exact - final) from results').fetchall()).mean()
  if options.rms:
    for ep in extrapoints:
      print "%d " % (ep,),
      for order in orders:
        for res in resolutions:
          cur_results = cur.execute('select abs(exact - final) from results where res = ? and ep = ? and ord = ?',
                                   (int(res), int(ep),int(order)))
          cur_time = rms(np.array(cur_results.fetchall()))
          print "%e " % (cur_time,),

      print


  if options.truthy:
    for ep in extrapoints:
      print "%d " % (ep,),
      for order in orders:
        for res in resolutions:
          cur_results = cur.execute('select qlin, err, final, exact from results where res = ? and ep = ? and ord = ?',
                                   (int(res), int(ep),int(order)))
          cur_array   = np.array(cur.fetchall())
          tf = [improved(*i) for i in cur_array]
          win_percent = float(tf.count(True)) / len(tf)
          print "%e " % (win_percent,),

      print
