Index: trunk/tools/wsor/editor_lifecycle/mrtchart |
— | — | @@ -0,0 +1,81 @@ |
| 2 | +#!/usr/bin/python |
| 3 | + |
| 4 | +import re |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import matplotlib.pyplot as pp |
| 10 | + |
| 11 | +from argparse import ArgumentParser |
| 12 | +from datetime import datetime |
| 13 | +from dateutil.parser import parser as DateParser |
| 14 | +from matplotlib.font_manager import FontProperties |
| 15 | + |
| 16 | +__prog__ = os.path.basename(__file__) |
| 17 | + |
| 18 | +parser = ArgumentParser(description=__doc__) |
| 19 | +parser.add_argument('data_paths', metavar='data', nargs='+') |
| 20 | +parser.add_argument('-title',) |
| 21 | + |
| 22 | +markers = 'ov^<>sp*+xD' |
| 23 | +colors = 'bgrcmykw' |
| 24 | + |
| 25 | +dateparser = DateParser() |
| 26 | + |
| 27 | +class UnsupportedFileFormatError(Exception): |
| 28 | + pass |
| 29 | + |
| 30 | +def load(path): |
| 31 | + ''' detect extension and loads data using numpy.io functions ''' |
| 32 | + _, ext = os.path.splitext(path) |
| 33 | + if re.match('^\.npy$', ext, re.I): |
| 34 | + return np.load(path)[:,[0,-1]] |
| 35 | + elif re.match('^\.tsv$', ext, re.I) or re.match('^\.txt$', ext, re.I): |
| 36 | + default = datetime(2001,1,1) |
| 37 | + def parse(timestamp): |
| 38 | + return dateparser.parse(timestamp, default) |
| 39 | + conv = {0: parse, -1: float} |
| 40 | + data = np.loadtxt(path, delimiter='\t', converters=conv, dtype=object) |
| 41 | + return data[:, [0,-1]] |
| 42 | + raise UnsupportedFileFormatError(path) |
| 43 | + |
| 44 | +def clean(data): |
| 45 | + data = filter(lambda k : k[1] < 3000 and k[1] > 0, data) |
| 46 | + data = sorted(data, key=lambda k : k[0]) |
| 47 | + return zip(*data) |
| 48 | + |
| 49 | +def main(ns): |
| 50 | + |
| 51 | + # create figure and axis |
| 52 | + fig = pp.figure(figsize=(8,4)) |
| 53 | + ax = fig.add_axes(pp.axes([.15,.1,.8,.8], axisbg='antiquewhite')) |
| 54 | + M = len(markers) |
| 55 | + C = len(colors) |
| 56 | + |
| 57 | + # plot lines |
| 58 | + for i, path in enumerate(ns.data_paths): |
| 59 | + try: |
| 60 | + name, ext = os.path.splitext(path) |
| 61 | + name.replace('_',' ') |
| 62 | + data = load(path) |
| 63 | + dates, mrt = clean(data) |
| 64 | + ax.plot(dates, mrt, ':'+markers[i % M]+colors[i % C], label=name) |
| 65 | + except UnsupportedFileFormatError,e: |
| 66 | + print >> sys.stderr, '%s: error: unsupported file type %s (.npy,'\ |
| 67 | + '.tsv, .txt accepted)' % (__prog__, e.args[0]) |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + pp.title('100-500 edits/year') |
| 71 | + pp.ylabel('average time to inactivity') |
| 72 | + pp.xlabel('') |
| 73 | + pp.legend(loc='best', prop=FontProperties(size='small')) |
| 74 | + pp.minorticks_on() |
| 75 | + pp.grid("on") |
| 76 | + pp.savefig('test.pdf') |
| 77 | + pp.show() |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + ns = parser.parse_args() |
| 81 | + main(ns) |
| 82 | + |
Property changes on: trunk/tools/wsor/editor_lifecycle/mrtchart |
___________________________________________________________________ |
Added: svn:executable |
1 | 83 | + * |