cexp / run /

Full commit
import os
import numpy
from numpy.testing import assert_equal
import pylab
    import cPickle as pickle
    import pickle

from benchmark.benchmark import benchmark, mkdirp
from cexp.hgutils import getid

def benchmark_varying_repeat(repeat_base, repeat_expmax,
                             iter_num=10, global_kwds=''):
    list_repeat = int(repeat_base) ** numpy.arange(int(repeat_expmax))
    kwds_list = [
        'method="%s", repeat=%d' % (method, repeat)
        for method in ['with_const', 'without_const']
        for repeat in list_repeat

    data= benchmark(
        'cexp.bm_constinloop', 'run', kwds_list, global_kwds, iter_num)
    return data

def fig_benchmark(data):
    fig = pylab.figure()
    kwds_time = data['kwds_time']
    ekwds_list = [eval('dict(%s)' % k) for k in data['kwds_list']]
    method_list = ['with_const', 'without_const']
    color_list = ['b', 'r', 'g']
    marker_list = ['+', 'x']

    def gather_time(method):
        return numpy.array([
            kwds_time[k] for (e, k) in zip(ekwds_list, data['kwds_list'])
            if e['method'] == method])

    def gather_repeat(method):
        return numpy.array(
            [e['repeat'] for e in ekwds_list if e['method'] == method])

    time = [gather_time(method) for method in method_list]
    repeat = [gather_repeat(method) for method in method_list]
    repeat0 = repeat[0]
    repeat1 = repeat[1]
    assert_equal(repeat0, repeat1)
    time_mean = [t.mean(axis=1) for t in time]
    time_min = [t.min(axis=1) for t in time]
    time_all_mean = numpy.concatenate(time, axis=1).mean(axis=1)

    ax1l = pylab.subplot(211)
    ax1r = ax1l.twinx()
    lines = []
    for (i, method) in enumerate(method_list):
        for j in range(time[i].shape[1]):
                repeat0, time[i][:,j] / time_all_mean, marker_list[i],
                color=color_list[i], markersize=12)
        ln = ax1l.semilogx(
            repeat0, time_mean[i] / time_all_mean, '-',
            label=method, color=color_list[i])
    ln = ax1r.loglog(repeat0, time_all_mean, color=color_list[i+1])
    # draw y=1
    (xmin, xmax) = ax1l.get_xlim()
    ax1l.hlines(1, xmin, xmax, label="_nolegend_")
    ax1l.set_xlim(xmin, xmax)
    pylab.legend(lines, method_list + ['mean (right)'], loc='best')
    ax1l.set_ylabel('normalized time')

    ax2l = pylab.subplot(212)
    ax2r = ax2l.twinx()
    l1 = ax2l.semilogx(repeat0, time_min[0] / time_min[1], 'b-')
    l2 = ax2r.semilogx(repeat0, time_mean[0] / time_mean[1], 'r-')
    # draw y=1
    (xlim, ylim) = ax2l.get_xlim()
    for (a, c) in [(ax2l, 'b'), (ax2r, 'r')]:
        a.hlines(1, xmin, xmax, label="_nolegend_",
                 linestyle='dotted', color=c)
    ax2l.set_xlim(xmin, xmax)
    pylab.legend([l1, l2], ['min (left)', 'mean (right)'], loc='best')
    ax2l.set_ylabel('%s / %s' % tuple(method_list))

    data['datestr'] = data['date'].strftime('%Y-%m-%d %H:%M:%S')
        'constinloop: kwds="%(global_kwds)s"'
        ' @%(node)s %(datestr)s (%(hg_rev)s)' % data)

    return fig

def main(datadir, repeat_base, repeat_expmax, iter_num, global_kwds):
    datapath = os.path.join(datadir, 'plotbm.pickle')
    figpath = os.path.join(datadir, 'plotbm.png')
    data = benchmark_varying_repeat(
        repeat_base, repeat_expmax, iter_num, global_kwds)
    fig = fig_benchmark(data)
    pickle.dump(data, file(datapath, 'w'))
    print "saved to", datadir

if __name__ == '__main__':
    from optparse import OptionParser
    datadir_default = "Data/plotbm_constinloop"
    parser = OptionParser()
    parser.add_option("-d", "--datadir", default=None,
                      help=('if this starts with "+", it will be appended'
                            ' to the base directory "%s"' % datadir_default))
    parser.add_option("--repeat_base", default=4, type=int)
    parser.add_option("--repeat_expmax", default=5, type=int)
    parser.add_option("--iter_num", default=10, type=int)
    parser.add_option("--global_kwds", default='')

    (opts, args) = parser.parse_args()
    if opts.datadir is None:
        datadir = os.path.join(datadir_default, 'default')
    elif opts.datadir.startswith('+'):
        datadir = os.path.join(datadir_default, opts.datadir[1:])
        datadir = opts.datadir

    main(datadir, opts.repeat_base, opts.repeat_expmax,
         opts.iter_num, opts.global_kwds)