Mercurial > hg > rlgallery-misc
annotate py/stats2.py @ 46:afb58e16bca8
Mention stats2.py in the README.
author | John "Elwin" Edwards |
---|---|
date | Sat, 25 Jan 2014 13:44:44 -0800 |
parents | 0f4163dbbafc |
children | 5cf88bd4e556 |
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 | 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 | 92 xlgraph.write(framerect) |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
93 xldivs, xlmax = ylimits(max([ pt[1] for pt in xldata ])) |
43 | 94 scale = 500 / xlmax |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
95 for xl, count in xldata: |
43 | 96 barx = xl * 50 + 60 |
97 barh = round(scale * count) | |
98 bary = 550 - barh | |
99 xlgraph.write(barstr.format(30, barh, barx, bary)) | |
100 xlgraph.write(xllabel.format(barx + 15, xl)) | |
101 for yl in range(xldivs + 1): | |
102 labeln = int(xlmax * yl / xldivs) | |
103 labelh = 550 + 8 - 500 * yl / xldivs | |
104 xlgraph.write(ylabel.format(labelh, labeln)) | |
105 xlgraph.write(xlabelf.format("Experience level")) | |
106 xlgraph.write(ylabelf.format("# of games")) | |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
107 xlgraph.write(titles(sitename, game.name, timestr)) |
43 | 108 xlgraph.close() |
109 return | |
110 | |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
111 def mkscoregraph(game, scoredata): |
43 | 112 if isinstance(game, rlgall.ARogueGame): |
113 scorewidth = 1500 | |
114 else: | |
115 scorewidth = 1000 | |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
116 scoregraph = SVGChart("{0}/score-{1}.svg".format(svgpath, game.uname)) |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
117 scoregraph.style("ffff00") |
43 | 118 scoregraph.write(framerect) |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
119 scoredivs, scoremax = ylimits(max([ pt[1] for pt in scoredata ])) |
43 | 120 scale = 500 / scoremax |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
121 for block, count in scoredata: |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
122 n = block // scorewidth |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
123 barx = n * 75 + 100 |
43 | 124 barh = round(scale * count) |
125 bary = 550 - barh | |
126 scoregraph.write(barstr.format(75, barh, barx, bary)) | |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
127 scoregraph.write(xllabel.format(barx, block)) |
43 | 128 for yl in range(scoredivs + 1): |
129 labeln = int(scoremax * yl / scoredivs) | |
130 labelh = 550 + 8 - 500 * yl / scoredivs | |
131 scoregraph.write(ylabel.format(labelh, labeln)) | |
132 scoregraph.write(xlabelf.format("Score")) | |
133 scoregraph.write(ylabelf.format("# of games")) | |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
134 scoregraph.write(titles(sitename, game.name, timestr)) |
43 | 135 scoregraph.close() |
136 return | |
137 | |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
138 def mkdeepgraph(game, deepdata): |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
139 deepgraph = SVGChart("{0}/deep-{1}.svg".format(svgpath, game.uname)) |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
140 deepgraph.style("808000") |
43 | 141 deepgraph.write(framerect) |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
142 deepdivs, deepmax = ylimits(max([ pt[1] for pt in deepdata ])) |
43 | 143 scale = 500 / deepmax |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
144 for lev, count in deepdata: |
43 | 145 barx = lev * 25 + 75 |
146 barh = round(scale * count) | |
147 bary = 550 - barh | |
148 deepgraph.write(barstr.format(25, barh, barx, bary)) | |
149 if lev % 3 == 0: | |
150 deepgraph.write(xllabel.format(barx + 12.5, lev)) | |
151 for yl in range(deepdivs + 1): | |
152 labeln = int(deepmax * yl / deepdivs) | |
153 labelh = 550 + 8 - 500 * yl / deepdivs | |
154 deepgraph.write(ylabel.format(labelh, labeln)) | |
155 deepgraph.write(xlabelf.format("Deepest dungeon level")) | |
156 deepgraph.write(ylabelf.format("# of games")) | |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
157 deepgraph.write(titles(sitename, game.name, timestr)) |
43 | 158 deepgraph.close() |
159 return | |
160 | |
38
d2c3c29ca4f9
Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff
changeset
|
161 for game in rlgall.gamelist: |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
162 xldata = game.getXLCounts(15) |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
163 deepdata = game.getDepthCounts(30) |
39
a97a20571526
stats2.py: adjust x-axis scale for arogue5 score chart.
John "Elwin" Edwards
parents:
38
diff
changeset
|
164 if isinstance(game, rlgall.ARogueGame): |
a97a20571526
stats2.py: adjust x-axis scale for arogue5 score chart.
John "Elwin" Edwards
parents:
38
diff
changeset
|
165 scorewidth = 1500 |
a97a20571526
stats2.py: adjust x-axis scale for arogue5 score chart.
John "Elwin" Edwards
parents:
38
diff
changeset
|
166 else: |
a97a20571526
stats2.py: adjust x-axis scale for arogue5 score chart.
John "Elwin" Edwards
parents:
38
diff
changeset
|
167 scorewidth = 1000 |
45
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
168 scoredata = game.getScoreCounts(10, scorewidth) |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
169 mkxlgraph(game, xldata) |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
170 mkscoregraph(game, scoredata) |
0f4163dbbafc
SVG charts: separate database-querying and chart-printing code.
John "Elwin" Edwards
parents:
43
diff
changeset
|
171 mkdeepgraph(game, deepdata) |
38
d2c3c29ca4f9
Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff
changeset
|
172 |
d2c3c29ca4f9
Add a new statistics script that creates SVG plots.
John "Elwin" Edwards
parents:
diff
changeset
|
173 exit() |