Writes reward and action data for plotting sparklines with PGF/TikZ.

    @see: http://www.texample.net/tikz/examples/weather-stations-data/

def sparklineData(data, filename):
    """ Writes reward and action data for plotting sparklines with PGF/TikZ.

    @see: http://www.texample.net/tikz/examples/weather-stations-data/
    """
    fd = file(filename, "w+b")
    for name in data.keys():
        action, reward = data[name]

        altName = name.lower().replace("_", "")

        fd.write("\def")
        fd.write("\REWARDDATA%s{" % altName)
        for i, r in enumerate(reward):
            fd.write("(%.2f,%.3f)" % (i / 10.0, r / 10.0)) # dimension too large
        fd.write("}\n")

        maxreward, maxindex = max(izip(reward, count()))
        minreward, minindex = min(izip(reward, count()))
        meanreward = scipy.mean(reward)
        fd.write("\def\REWARDMAX%s{%.1f}\n" % (altName, maxreward))
        fd.write("\def\REWARDMAXIDX%s{%d}\n" % (altName, maxindex))
        fd.write("\def\REWARDMIN%s{%.1f}\n" % (altName, minreward))
        fd.write("\def\REWARDMINIDX%s{%d}\n" % (altName, minindex))
        fd.write("\def\REWARDMEAN%s{%.1f}\n" % (altName, meanreward))

        fd.write("\def")
        fd.write("\ACTIONDATA%s{" % altName)
        for i, a in enumerate(action):
            fd.write("(%.2f,%.3f)" % (i / 10.0, a / 10.0))
        fd.write("}\n")

        maxaction, maxindex = max(izip(reward, count()))
        minaction, minindex = min(izip(reward, count()))
        meanaction = scipy.mean(reward)
        fd.write("\def\ACTIONMAX%s{%.1f}\n" % (altName, maxaction))
        fd.write("\def\ACTIONMAXIDX%s{%d}\n" % (altName, maxindex))
        fd.write("\def\ACTIONMIN%s{%.1f}\n" % (altName, minaction))
        fd.write("\def\ACTIONMINIDX%s{%d}\n" % (altName, minindex))
        fd.write("\def\ACTIONMEAN%s{%.1f}\n" % (altName, meanaction))
    fd.close()