def main(self):
    dir_label_runs = defaultdict(lambda: defaultdict(list))
    for session in self.dbs:
      q = (session.query(resultsdb.models.TuningRun)
          .filter_by(state='COMPLETE')
          .order_by('name'))

      if self.args.label:
        q = q.filter(TuningRun.name.in_(
          map(str.strip,self.args.label.split(','))))

      for tr in q:
        d = run_dir(self.args.stats_dir, tr)
        d = os.path.normpath(d)
        dir_label_runs[d][run_label(tr)].append((tr, session))

    summary_report = defaultdict(lambda: defaultdict(list))
    for d, label_runs in dir_label_runs.iteritems():
      if not os.path.isdir(d):
        os.makedirs(d)
      session = label_runs.values()[0][0][1]
      objective = label_runs.values()[0][0][0].objective
      all_run_ids = map(_[0].id, itertools.chain(*label_runs.values()))
      q = (session.query(Result)
           .filter(Result.tuning_run_id.in_(all_run_ids))
           .filter(Result.time < float('inf'))
           .filter_by(was_new_best=True, state='OK'))
      total = q.count()
      if total == 0: 
          continue
      q = objective.filter_acceptable(q)
      acceptable = q.count()
      q = q.order_by(*objective.result_order_by_terms())
      best = q.limit(1).one()
      worst = q.offset(acceptable-1).limit(1).one()

      map(len, label_runs.values())

      log.info("%s -- best %.4f / worst %.f4 "
               "-- %d of %d acceptable -- %d techniques with %d to %d runs",
               d,
               best.time,
               worst.time,
               acceptable,
               total,
               len(label_runs.values()),
               min(map(len, label_runs.values())),
               max(map(len, label_runs.values())))

      for label, runs in sorted(label_runs.items()):
        if len(runs) < self.args.min_runs:
          print len(runs) ,self.args.min_runs
          continue
        log.debug('%s/%s has %d runs %s',d, label, len(runs), runs[0][0].args.technique)
        self.combined_stats_over_time(d, label, runs, objective, worst, best)

        final_scores = list()
        for run, session in runs:
          try:
            final = (session.query(Result)
                    .filter_by(tuning_run=run,
                               configuration=run.final_config)
                    .limit(1)
                    .one())
          except sqlalchemy.orm.exc.NoResultFound:
            continue
          final_scores.append(objective.stats_quality_score(final, worst, best))
        final_scores.sort()
        if final_scores:
          norm = objective.stats_quality_score(best, worst, best)
          if norm > 0.00001:
            summary_report[d][run_label(run, short=True)] = (
                percentile(final_scores, 0.5) / norm,
                percentile(final_scores, 0.1) / norm,
                percentile(final_scores, 0.9) / norm,
              )
          else:
            summary_report[d][run_label(run, short=True)] = (
                percentile(final_scores, 0.5) + norm + 1.0,
                percentile(final_scores, 0.1) + norm + 1.0,
                percentile(final_scores, 0.9) + norm + 1.0,
              )


    with open(self.args.stats_dir+ "/summary.dat", 'w') as o:
      # make summary report
      keys = sorted(reduce(set.union,
                           [set(x.keys()) for x in summary_report.values()],
                           set()))
      print >>o, '#####',
      for k in keys:
        print >>o, k,
      print >>o
      for d, label_vals in sorted(summary_report.items()):
        print >>o, d.split('/')[-2],
        for k in keys:
          if k in label_vals:
            print >>o, '-', label_vals[k][0], label_vals[k][1], label_vals[k][2],
          else:
            print >>o, '-', '-', '-', '-',
        print >>o

    if keys:
      plotcmd = ["""1 w lines lt 1 lc rgb "black" notitle""",
                 """'summary.dat' using 3:4:5:xtic(1) ti "%s" """ % keys[0]]
      for n, k in enumerate(keys[1:]):
        plotcmd.append("""'' using %d:%d:%d ti "%s" """ % (
                        4*n + 7,
                        4*n + 8,
                        4*n + 9,
                        k))
      self.gnuplot_summary_file(self.args.stats_dir, 'summary', plotcmd)



    for d, label_runs in dir_label_runs.iteritems():
      labels = [k for k,v in label_runs.iteritems()
                if len(v)>=self.args.min_runs]
      self.gnuplot_file(d,
                        "medianperfe",
                        ['"%s_percentiles.dat" using 1:12:4:18 with errorbars title "%s"' % (l,l) for l in labels])
      self.gnuplot_file(d,
                        "meanperfe",
                        ['"%s_percentiles.dat" using 1:21:4:18 with errorbars title "%s"' % (l,l) for l in labels])
      self.gnuplot_file(d,
                        "medianperfl",
                        ['"%s_percentiles.dat" using 1:12 with lines title "%s"' % (l,l) for l in labels])
      self.gnuplot_file(d,
                        "meanperfl",
                        ['"%s_percentiles.dat" using 1:21 with lines title "%s"' % (l,l) for l in labels])

    # print
    # print "10% Scores", d
    # pprint(self.technique_scores(d, labels, '0.1'))
    # print
    # print "90% Scores", d
    # pprint(self.technique_scores(d, labels, '0.9'))
    # print
    # print "Mean Scores", d
    # pprint(self.technique_scores(d, labels, 'mean'))
      print
      print "Median Scores", d
      pprint(self.technique_scores(d, labels, '0.5'))
dataset/ETHPy150Open jansel/opentuner/opentuner/utils/stats.py/StatsMain.main/original