Index: trunk/tools/wsor/editor_lifecycle/normplot.py |
— | — | @@ -0,0 +1,76 @@ |
| 2 | +#!/usr/bin/python |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import matplotlib.pyplot as pp |
| 9 | + |
| 10 | +from argparse import ArgumentParser |
| 11 | +from collections import defaultdict |
| 12 | +from datetime import datetime |
| 13 | +from matplotlib.font_manager import FontProperties |
| 14 | + |
| 15 | +__prog__ = os.path.basename(__file__) |
| 16 | + |
| 17 | +parser = ArgumentParser(description=__doc__) |
| 18 | +parser.add_argument('input_paths', metavar='data', nargs='+') |
| 19 | +parser.add_argument('-t', '--title', required=1) |
| 20 | +parser.add_argument('-xlim', nargs=2, metavar='YEAR', type=int) |
| 21 | +parser.add_argument('-ylim', nargs=2, metavar='VALUE', type=float) |
| 22 | + |
| 23 | +conv = defaultdict(lambda k : float) |
| 24 | +conv[0] = lambda k : datetime.strptime(k, '%Y-%m') |
| 25 | +markers = 'ov^<>sp*D' |
| 26 | +colors = 'brcmgyk' |
| 27 | +M = len(markers) |
| 28 | +C = len(colors) |
| 29 | +labeltempl = r'$10^{%d} \leq a < 10^{%d}$' |
| 30 | + |
| 31 | +if __name__ == '__main__': |
| 32 | + ns = parser.parse_args() |
| 33 | + |
| 34 | + lines = [] |
| 35 | + |
| 36 | + fig = pp.figure(figsize=(8,4)) |
| 37 | + ax = fig.add_axes(pp.axes([.1,.1,.65,.8], axisbg='whitesmoke')) |
| 38 | + |
| 39 | + for i, path in enumerate(ns.input_paths): |
| 40 | + try: |
| 41 | + data = np.loadtxt(path, converters=conv, dtype=object, skiprows=1) |
| 42 | + except IOError, e: |
| 43 | + print >> sys.stderr, '%s: skipping %s because: %s' % (__prog__,\ |
| 44 | + path, e.message or e.args[1]) |
| 45 | + continue |
| 46 | + act, peak_date, peak_date_err, peak, peak_err = np.asfarray(data[:,1:]).T |
| 47 | + a = act[0] |
| 48 | + cohort = data[:, 0] |
| 49 | + idx = np.argsort(cohort) |
| 50 | + cohort = cohort[idx] |
| 51 | + peak = peak[idx] / peak.mean() |
| 52 | + l, = ax.plot(cohort, peak, marker=markers[i % M], color=colors[i % C], |
| 53 | + ls='none', mec=colors[i % C], label=labeltempl % (a-1, a), ms=8, |
| 54 | + alpha=.65) |
| 55 | + lines.append(l) |
| 56 | + |
| 57 | + pp.figlegend(lines, [ l.get_label() for l in lines ], |
| 58 | + loc='center right', prop=FontProperties(size='medium')) |
| 59 | + |
| 60 | + if ns.xlim: |
| 61 | + pp.xlim(datetime(ns.xlim[0],1,1), datetime(ns.xlim[1],1,1)) |
| 62 | + if ns.ylim: |
| 63 | + pp.ylim(ns.ylim) |
| 64 | + |
| 65 | + pp.xlabel('cohort') |
| 66 | + pp.ylabel(r'normalized activity $\frac{a_p}{< a_p >}$') |
| 67 | + |
| 68 | + pp.title(ns.title) |
| 69 | + ax.minorticks_on() |
| 70 | + ax.grid("on") |
| 71 | + pp.draw() |
| 72 | + |
| 73 | + output_path = ns.title.replace(' ', '_').lower() + '.pdf' |
| 74 | + pp.savefig(output_path, fmt='pdf') |
| 75 | + print 'figure saved to %s' % output_path |
| 76 | + |
| 77 | + pp.show() |