def ppm_group_algo_contour_plot_helper(test_name, fnames, algos,
                                       granularity=config.PPM_CONTOUR_GRANULARITY,
                                       alpha_start=config.PPM_ALPHA_START, alpha_end=config.PPM_ALPHA_END,
                                       beta_start=config.PPM_BETA_START, beta_end=config.PPM_BETA_END):
  if type(fnames) == str:
    fnames = [fnames]
  alpha_range = (float(alpha_start), float(alpha_end))
  beta_range = (float(beta_start), float(beta_end))
  granularity = int(granularity)
  algos = algos.split(",")
  kwargs = contour_settings(config.PPM_GROUP_CONTOUR_DEFAULT_ARGS,
                            config.PPM_GROUP_CONTOUR_OVERRIDES, test_name, fnames)

  fig = plot_contour_base(xlim=beta_range, ylim=alpha_range)
  colors = kwargs['colormap'](np.linspace(0, 1, len(algos)))
  del kwargs['colormap']
  for algo, color in zip(algos, colors):
    prior, depth = algo.split("/")
    depth = int(depth)
    optimum, evals = benchmark.tasks.contour_data(prior, paranoia, depth,
                                                  alpha_range, beta_range, granularity, fnames)
    label = short_name(config.SHORT_PRIOR_NAME, prior)
    plot_optimum(optimum, label=label, color=color)
    plot_contour_lines(optimum, evals, colors=color, **kwargs)

  return plot.save_figure(fig, general.sanitize_fname(test_name), fnames)
def generate_resource_figure(test, settings, data):
  file = settings['file']
  algos = settings['algos']
  resources, format = process_resource_data(settings, data)
  resources = resources[file]

  # flatten data
  flattened = [confidence_interval(resources[algo], config.RESOURCE_ALPHA) for algo in algos]
  x = np.arange(len(algos))
  y = [z[0] for z in flattened]
  yerr = [z[1] for z in flattened]

  xticks = list(map(config.ALGO_ABBREVIATIONS.get, algos))
  colors = ppl.brewer2mpl.get_map('Set2', 'qualitative', len(algos)).mpl_colors

  if settings['style']:
    plot.set_style(settings['style'])
  plot.new_figure()
  fig, ax = plt.subplots()
  rects = ppl.bar(x, y, xticklabels=xticks, yerr=yerr, log=True, grid='y', color=colors)

  # Annotate
  for rect in rects:
    bar_x = rect.get_x() + rect.get_width()/2.
    bar_y = rect.get_height()

    label = format(bar_y)
    plt.annotate(label, xy=(bar_x, bar_y), xytext=(0, 10), textcoords='offset points',
                 horizontalalignment='center', verticalalignment='bottom')

  plt.xlabel('Compressor')
  if settings['col'] == 'runtime':
    plt.ylabel(r'Runtime (\si{\second})')
  elif settings['col'] == 'memory':
    plt.ylabel(r'Memory (\si{\byte})')
    ax.set_yscale('log', basey=2) # units are in powers of two, so scale should be as well

    # hack to make labels fit
    ymin, ymax = plt.ylim()
    plt.ylim((ymin, ymax*2))

  plot.save_figure(fig, 'resources', [test])
def ppm_efficiency_by_depth_helper2(fnames, priors, depths, test_name, opts):
  test_files = list(itertools.chain(*config.PPM_EFFICIENCY_BY_DEPTH_FILESETS.values()))
  original_sizes = {f : benchmark.tasks.corpus_size(f) for f in fnames}
  work = []
  for opt, (prior, depth) in zip(opts, itertools.product(priors, depths)):
    x, fun, status = opt
    a, b = x
    work += [benchmark.tasks.my_compressor.s(test_file, paranoia, prior,
                                             ['ppm:d={0}:a={1}:b={2}'.format(depth, a, b)])
            for test_file in test_files]
  raw_res = celery.group(work)().get()

  res = {}
  for effectiveness, (prior, depth, test_file) in zip(raw_res,
                                                      itertools.product(priors, depths, test_files)):
    by_prior = res.get(prior, {})
    by_depth = by_prior.get(depth, {})
    by_depth[test_file] = effectiveness
    by_prior[depth] = by_depth
    res[prior] = by_prior

  fig = plot.new_figure()
  colors = ppl.brewer2mpl.get_map('Set2', 'qualitative', len(config.PPM_EFFICIENCY_BY_DEPTH_FILESETS)).mpl_colors
  for (name, fileset), color in zip(config.PPM_EFFICIENCY_BY_DEPTH_FILESETS.items(), colors):
    for prior in priors:
      y = []
      for d in depths:
        by_file = res[prior][d]
        mean = np.mean([by_file[f] / original_sizes[f] * 8 for f in fileset])
        y.append(mean)

      linestyle = config.PPM_EFFICIENCY_BY_DEPTH_PRIOR_LINESTYLES[prior]
      marker = config.PPM_EFFICIENCY_BY_DEPTH_PRIOR_MARKERS[prior]
      min_i = np.argmin(y)
      markevery = list(range(0, min_i)) + list(range(min_i + 1, len(depths)))
      ppl.plot(depths, y, label='{1} on {0}'.format(name, short_name(config.SHORT_PRIOR_NAME, prior)),
               color=color, linestyle=linestyle, marker=marker, markevery=markevery)

      min_depth = depths[min_i]
      min_y = y[min_i]
      ppl.plot([min_depth], [min_y], color=color, linestyle='None', marker='D')

  plt.xlabel(r'Maximal context depth $d$')
  plt.ylabel(r'Compression effectiveness (bits/byte)')

  # stretch x-axis slightly so markers are visible
  plt.xlim(min(depths) - 0.1, max(depths) + 0.1)

  ppl.legend(handlelength=4, # increase length of line segments so that linestyles can be seen
             numpoints=1 # but only show marker once
             )

  return plot.save_figure(fig, test_name, ["dummy"])
  def callback(res):
    fig = plot_contour_base(xlim=beta_range, ylim=alpha_range)
    colors = kwargs['colormap'](np.linspace(0, 1, len(fnames)))
    del kwargs['colormap']

    for file_res, fname, color in zip(res, fnames, colors):
      optimum, evals = file_res
      label = short_name(config.SHORT_FILE_NAME, fname)
      plot_optimum(optimum, label=label, color=color)
      plot_contour_lines(optimum, evals, colors=color, **kwargs)

    return plot.save_figure(fig, test_name, fnames)
def ppm_contour_plot_helper(test_name, fnames, prior, depth,
                            granularity=config.PPM_CONTOUR_GRANULARITY,
                            alpha_start=config.PPM_ALPHA_START, alpha_end=config.PPM_ALPHA_END,
                            beta_start=config.PPM_BETA_START, beta_end=config.PPM_BETA_END):
  if type(fnames) == str:
    fnames = [fnames]

  alpha_range = (float(alpha_start), float(alpha_end))
  beta_range = (float(beta_start), float(beta_end))
  depth = int(depth)
  granularity = int(granularity)

  optimum, evals = benchmark.tasks.contour_data(prior, paranoia, depth,
                                                alpha_range, beta_range, granularity, fnames)
  settings = contour_settings(config.PPM_CONTOUR_DEFAULT_ARGS,
                            config.PPM_CONTOUR_OVERRIDES, test_name, fnames)

  fig = plot_contour_base(xlim=beta_range, ylim=alpha_range)
  plot_optimum(optimum[0], **settings['optimum'])
  for kwargs in settings['markers'].values():
    plot_optimum(**kwargs)
  plot_contour_lines(optimum, evals, **settings['lines'])
  return plot.save_figure(fig, test_name, fnames)