From f5dd20760eafc246e0bd183cdc02c7b393d335cc Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey Date: Wed, 3 Sep 2014 19:28:20 +0100 Subject: [PATCH] Implement --batch and --outdir switches to ostagraph.py that can produce multiple graphs from one sim stats load using instructions in a file Loading the stats corpus takes quite some time, and this gets worse the more graphs we want to introduce. Better to load just once and then produce multiple graphs from the same corpus. Json file format will be detailed once this implementation is complete. Currently can't control which log (set) a particular stat is graphed from, but this capability will come shortly. --- .../src/ostagraph.py | 96 ++++++++++++++----- 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/analysis/opensimulator-stats-analyzer/src/ostagraph.py b/analysis/opensimulator-stats-analyzer/src/ostagraph.py index 3520975..9d51ce4 100755 --- a/analysis/opensimulator-stats-analyzer/src/ostagraph.py +++ b/analysis/opensimulator-stats-analyzer/src/ostagraph.py @@ -1,6 +1,7 @@ #!/usr/bin/python import argparse +import json import matplotlib.pyplot as plt import sys from pylab import * @@ -15,17 +16,48 @@ def plotNoneAction(stats, type): def plotSumAction(stats, type): totalsStat = OSimStatsHelper.sumStats(stats) - plt.plot(totalsStat[type]['values'], label=totalsStat['container']) + plt.plot(totalsStat[type]['values'], label=totalsStat['container']) + +def produceGraph(select, statType, action, show, save, outPath): + stats = corpus.getStats(select) + + if len(stats) <= 0: + print "No stats matching %s" % (select) + return + + # Used to fetch data that will be the same for all stats + oneStat = stats[stats.keys()[0]] + + plt.title(select) + plt.ylabel(oneStat[statType]['units']) + plt.xlabel("samples") + + if action == 'sum': + plotSumAction(stats.values(), statType) + else: + plotNoneAction(stats.values(), statType) + + plt.legend() + + if save: + savefig(outPath) + + if show: + plt.show() ############ ### MAIN ### ############ parser = argparse.ArgumentParser(formatter_class = argparse.RawTextHelpFormatter) +parser.add_argument( + '--batch', + help = "Path to a json file containing batch instructions for producing graphs. If this is set then any options are ignored except for --outpath", + default = argparse.SUPPRESS) + parser.add_argument( '--select', - help = "Select the full name of a stat to graph (e.g. \"scene.Keynote 1.RootAgents\")", - required = True) + help = "Select the full name of a stat to graph (e.g. \"scene.Keynote 1.RootAgents\")") parser.add_argument( '--type', @@ -42,6 +74,11 @@ parser.add_argument( help = "Path to output the graph rather the interactively display. Filename extension determines graphics type (e.g. \"graph.jpg\")", default = argparse.SUPPRESS) +parser.add_argument( + '--outdir', + help = "Directory to output graphs if the --batch option is used", + default = argparse.SUPPRESS) + parser.add_argument( 'statsLogPath', help = "Path to the stats log file.", @@ -54,28 +91,37 @@ corpus = OSimStatsCorpus() for path in opt.statsLogPath: corpus.load(path) - -stats = corpus.getStats(opt.select) - -if len(stats) <= 0: - print "No stats matching %s" % (opt.select) - sys.exit(1) - -# Used to fetch data that will be the same for all stats -oneStat = stats[stats.keys()[0]] - -plt.title(opt.select) -plt.ylabel(oneStat[opt.type]['units']) -plt.xlabel("samples") - -if opt.action == 'sum': - plotSumAction(stats.values(), opt.type) -else: - plotNoneAction(stats.values(), opt.type) -plt.legend() +if "batch" in opt: + batchCommands = json.load(open(opt.batch)) -if 'out' in opt: - savefig(opt.out) + for graph in batchCommands["graphs"]: + select = graph["select"] + + if "type" in graph: + type = graph["type"] + else: + type = "abs" + + if "action" in graph: + action = graph["action"] + else: + action = "none" + + if "out" in graph: + outPath = os.path.join(opt.outdir, graph["out"]) + save = True + show = False + + produceGraph(select, type, action, show, save, outPath) + else: - plt.show() \ No newline at end of file + save = "out" in opt + show = not save + + if save: + outPath = opt.out + else: + outPath = None + + produceGraph(opt.select, opt.type, opt.action, show, save, outPath) \ No newline at end of file