[Erp5-report] r28095 - /erp5/trunk/utils/timing_log_parser/plot.py

nobody at svn.erp5.org nobody at svn.erp5.org
Thu Jul 16 14:23:31 CEST 2009


Author: vincent
Date: Thu Jul 16 14:23:31 2009
New Revision: 28095

URL: http://svn.erp5.org?rev=28095&view=rev
Log:
Add a plotting tool to render graphs from CSV files generated by timing_log_parser.py .

Added:
    erp5/trunk/utils/timing_log_parser/plot.py   (with props)

Added: erp5/trunk/utils/timing_log_parser/plot.py
URL: http://svn.erp5.org/erp5/trunk/utils/timing_log_parser/plot.py?rev=28095&view=auto
==============================================================================
--- erp5/trunk/utils/timing_log_parser/plot.py (added)
+++ erp5/trunk/utils/timing_log_parser/plot.py [utf8] Thu Jul 16 14:23:31 2009
@@ -1,0 +1,144 @@
+#!/usr/bin/python
+##############################################################################
+#
+# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
+#                    Vincent Pelletier <vincent at nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsability of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# garantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+##############################################################################
+import pylab
+from matplotlib.dates import MonthLocator, DayLocator, DateFormatter, date2num
+from matplotlib.font_manager import FontProperties
+from datetime import date
+from os import path
+
+"""
+  Usage:
+    plot.py file1.csv [file2.csv [...]]
+  Result:
+    Generates, in current directory, a graph per csv column in png format.
+    Their name is composed of:
+    - the ratio of present points (100 to 000). The higher the number, the
+      more the plot will be complete (less holes, longer timespan coverage).
+    - csv file basename (without extension)
+    - csv column title
+    - 'png' extension
+
+  CSV files must have been generated by parse_timing_log.py tool.
+"""
+
+class CSVFile(object):
+  def __init__(self, file_name, field_delim=','):
+    file = open(file_name, 'r')
+    self.column_dict = column_dict = {}
+    self.column_list = column_list = []
+    self.ratio_dict = ratio_dict = {}
+    line_num = 0
+    self.value_max = value_max = {}
+    next_ord = 0
+    for x, title in enumerate(file.readline().split(field_delim)):
+      title = title.strip()
+      title = title.strip('"')
+      if title in column_dict:
+        title = next_ord
+        while title in column_dict:
+          title += 1
+        next_ord = title + 1
+        title = str(title)
+      column_dict[title] = []
+      column_list.append(title)
+    for line in file.readlines():
+      line_num += 1
+      for x, cell in enumerate(line.split(field_delim)):
+        cell = cell.strip()
+        key = column_list[x]
+        if x != 0:
+          cell = computeExpr(cell)
+          if cell is not None:
+            ratio = ratio_dict.get(key, 0)
+            ratio_dict[key] = ratio + 1
+            if cell > value_max.get(key, 0):
+              value_max[key] = cell
+        column_dict[key].append(cell)
+    line_num = float(line_num) / 100
+    for key in ratio_dict:
+      ratio_dict[key] /= line_num
+
+  def getColumn(self, column_id):
+    return self.column_dict[self.column_list[column_id]]
+
+  def iterColumns(self, start=0, stop=None):
+    if stop is None:
+      column_list = self.column_list[start:]
+    else:
+      column_list = self.column_list[start:stop]
+    return ((x, self.column_dict[x], self.value_max.get(x, 0), self.ratio_dict.get(x, 0)) for x in column_list)
+
+def computeExpr(expr):
+  # only supports '=x/y'
+  if expr:
+    assert expr[0] == '='
+    num, denom = expr[1:].split('/')
+    result float(int(num)) / int(denom)
+  else:
+    result None
+  return result
+
+def strToDate(expr):
+  # only supports '"d/m/y"'
+  expr = expr.strip('"')
+  d, m, y = expr.split('/')
+  return date(int(y), int(m), int(d))
+
+def main(file_name_list):
+  major_locator = MonthLocator()
+  minor_locator = DayLocator()
+  major_formater = DateFormatter('%m/%y')
+  for file_name in file_name_list:
+    print 'Loading %s...' % (file_name, )
+    file = CSVFile(file_name)
+    date_list = [date2num(strToDate(x)) for x in file.getColumn(0)]
+    xlim = [date_list[0], date_list[-1]]
+    for title, column, value_max, ratio in file.iterColumns(start=1):
+      ax = pylab.subplot(111, autoscale_on=False)
+      ax.xaxis.set_major_locator(major_locator)
+      ax.xaxis.set_minor_locator(minor_locator)
+      ax.xaxis.set_major_formatter(major_formater)
+      ax.axes.set_ylim([0, max(value_max, 3)])
+      ax.axes.set_xlim(xlim)
+      pylab.plot_date(date_list, column, 'k-', xdate=1)
+
+      out_file, out_ext = path.splitext(path.basename(file_name))
+      if out_ext != '.csv':
+        out_file = '.'.join((out_file, out_ext))
+      out_file_name = '%03i_%s_%s.png' % (ratio, out_file, title)
+      print 'Saving %s...' % (out_file_name, )
+      pylab.savefig(out_file_name)
+
+      # Needed to cleanup pylab state.
+      pylab.close()
+
+if __name__ == '__main__':
+  import sys
+  main(sys.argv[1:])
+

Propchange: erp5/trunk/utils/timing_log_parser/plot.py
------------------------------------------------------------------------------
    svn:executable = *




More information about the Erp5-report mailing list