
import sys

modes = {#"Plain NEST": "plain-%i.log",
         "Plain NEST": "plain-trunk-%i.log",
         #"NEST + CSA": "csanest-%i.log",
         "NEST + CSA": "csanest-fanin-%i.log"}

# more tango colors: [32, 74, 135], [92,53,102]

colors = {#"Plain NEST": [164,0,0],
          "Plain NEST": [0*255, 0.3569*255, 0.5098*255], # FZJ blue
          #"NEST + CSA": [78,158,6],
          "NEST + CSA": [0.8353*255, 0.1765*255, 0.0706*255]} # FZJ health red


data_build = dict([[label, []] for label in modes])
data_sim = dict([[label, []] for label in modes])

num_values = 0
num_threads = 24
plot_t_build = True
plot_t_sim = False

for fnum in range(1, num_threads + 1):

    for mode in modes:

        f = file(modes[mode] % fnum, "r")
        
        build_time = 0
        sim_time = 0
        num_samples = 0
        
        while True:
            line = f.readline().split(" ")
            if line == ['']: break
            
            if line[0] == "Connection":
                build_time += float(line[5])
                num_samples += 1

            if line[0] == "Simulation":
                sim_time += float(line[5])

        sim_time = sim_time / float(num_samples)
        build_time = build_time / float(num_samples)

        data_build[mode].append(build_time)
        data_sim[mode].append(sim_time)

        num_values = len(data_sim[mode])

        f.close()

import pylab
import numpy

params = {'axes.labelsize': 12,
          'text.fontsize': 12,
          'legend.fontsize': 12,
          'xtick.labelsize': 12,
          'ytick.labelsize': 12}
pylab.rcParams.update(params)

fig = pylab.figure()
plot = fig.add_subplot(111)

xticks = range(1, num_values + 1)

#for i in xticks:
#    if i%6 == 0:
#        pylab.axvline(i, color="gray")

for datum in data_build:
    
    color = numpy.array(colors[datum]) / 255.0
    lightcolor = numpy.array(map(lambda x: [x+128, 254][x+128>254], colors[datum])) / 255.0

    if plot_t_build:
        pylab.plot(xticks, [data_build[datum][0] / float(x) for x in xticks], lw=4, c=lightcolor, label=datum + " (linear)")
        pylab.plot(xticks, data_build[datum], label=datum, lw=2, marker="*", c=color)

    if plot_t_sim:
        pylab.plot(xticks, [data_sim[datum][0] / float(x) for x in xticks], lw=4, color=lightcolor, label=datum + " sim linear")
        pylab.plot(xticks, data_sim[datum], label=datum + " sim", lw=2, marker="^", c=color)

pylab.legend(numpoints=1, loc=0)
pylab.xticks(xticks)
pylab.xlim(0, max(xticks) + 1)

pylab.title("Connection time (~62.5M synapses)")
pylab.xlabel("Number of processes")
pylab.ylabel("Wallclock time (s)")

pylab.subplots_adjust(left=0.07, bottom=0.08, right=0.98, top=0.94)
pylab.savefig("csanest_bench.eps")
#pylab.show()
