annotate py/stats2.py @ 45:0f4163dbbafc

SVG charts: separate database-querying and chart-printing code. Create Game methods in rlgall.py to get histogram data for score, xl, maxdepth. In stats2.py, make the chart-printing functions use this data and avoid running hard-coded queries.
author John "Elwin" Edwards
date Thu, 16 Jan 2014 16:25:09 -0800
parents 3ecbd4fa2a08
children 5cf88bd4e556
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
1 #!/usr/bin/python3
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
2
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
3 import psycopg2
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
4 import rlgall
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
5 from datetime import datetime
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
6
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
7 sitename = "rlgallery.org"
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
8 svgpath = rlgall.webdir
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
9 timestr = datetime.utcnow().strftime("%Y-%m-%d %H:%M")
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
10
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
11 dochead = """<?xml version="1.0" encoding="UTF-8"?>
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
12 <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="900" height="600">
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
13 """
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
14 stylesheet = """<style type="text/css">
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
15 rect.frame {{
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
16 fill:#ffffff;
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
17 fill-opacity:1;
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
18 stroke:#000000;
40
1ddd2d950e31 SVG charts: reduce frame stroke width.
John "Elwin" Edwards
parents: 39
diff changeset
19 stroke-width:2;
38
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
20 stroke-opacity:1;
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
21 }}
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
22 rect.bar {{
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
23 fill:#{0};
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
24 fill-opacity:1;
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
25 stroke:#000000;
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
26 stroke-width:2;
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
27 stroke-opacity:1;
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
28 }}
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
29 </style>
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
30 """
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
31 framerect = '<rect width="750" height="500" x="100" y="50" class="frame"/>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
32 barstr = '<rect width="{0}" height="{1}" x="{2}" y="{3}" class="bar"/>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
33 xllabel = '<text x="{0}" y="570" font-size="15" text-anchor="middle">{1}</text>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
34 ylabel = '<text x="90" y="{0}" font-size="16" text-anchor="end">{1}</text>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
35 xlabelf = '<text x="475" y="590" font-size="15" text-anchor="middle">{0}</text>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
36 ltitle = '<text x="100" y="35" font-size="16" text-anchor="start">{0}</text>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
37 ctitle = '<text x="475" y="34" font-size="18" text-anchor="middle">{0}</text>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
38 rtitle = '<text x="850" y="35" font-size="16" text-anchor="end">{0}</text>\n'
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
39 ylabelf = """<g transform="translate(100, 300)">
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
40 <g transform="rotate(-90)">
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
41 <text x="0" y="-60" font-size="16" text-anchor="middle">{0}</text>
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
42 </g>
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
43 </g>
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
44 """
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
45
45
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
46 class SVGChart():
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
47 def __init__(self, filename):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
48 self.of = open(filename, "w", encoding="utf-8")
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
49 self.of.write(dochead)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
50 def style(self, barcolor):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
51 self.of.write(stylesheet.format(barcolor))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
52 def write(self, obj):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
53 self.of.write(obj)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
54 def close(self):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
55 self.of.write('</svg>\n')
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
56 self.of.close()
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
57
38
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
58 def ylimits(x):
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
59 ll = [2, 3, 4, 5, 6, 8, 10, 15]
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
60 m = 1
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
61 size = 0
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
62 while True:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
63 for i in ll:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
64 if i * m > x:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
65 size = i
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
66 lim = i * m
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
67 break
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
68 if size:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
69 break
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
70 else:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
71 m *= 10
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
72 if size in [3, 6]:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
73 if lim == 3:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
74 divs = 3
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
75 else:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
76 divs = 6
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
77 elif size in [4, 8]:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
78 divs = 4
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
79 else:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
80 if lim == 2:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
81 divs = 2
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
82 else:
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
83 divs = 5
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
84 return divs, lim
d2c3c29ca4f9 Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff changeset
85
45
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
86 def titles(l, c, r):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
87 return ltitle.format(l) + ctitle.format(c) + rtitle.format(r)
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
88
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
89 def mkxlgraph(game, xldata):
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
90 xlgraph = SVGChart("{0}/xl-{1}.svg".format(svgpath, game.uname))
0f4163dbbafc SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents: 43
diff changeset
91 xlgraph.style("0000ff")
43
3ecbd4fa2a08 stats2.py: make graphs of maxdepth.
John "Elwin" Edwards
parents: 40
diff changeset