-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatestats.py
136 lines (128 loc) · 4.68 KB
/
matestats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import argparse, gzip, re
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from collections import Counter
def open_file(filename):
open_func = gzip.open if filename.endswith(".gz") else open
return open_func(filename, "rt")
class data:
def __init__(self, filename, debug=False):
self.plies = Counter()
p = re.compile("([0-9a-zA-Z/\- ]*) bm #([0-9\-]*);")
loaded = set()
self.bmplus = self.bmminus = 0
with open_file(filename) as f:
for line in f:
m = p.match(line)
if not m:
print("---------------------> IGNORING : ", line)
else:
fen, bm = m.group(1), int(m.group(2))
if fen in loaded:
print(f"Warning: Found duplicate FEN {fen}.")
continue
loaded.add(fen)
plies = 2 * bm - 1 if bm > 0 else -2 * bm
self.plies[plies] += 1
if bm > 0:
self.bmplus += 1
else:
self.bmminus += 1
self.filename = filename[:-3] if filename.endswith(".gz") else filename
self.bmmin = (min(self.plies.keys()) + 1) // 2
self.bmmax = (max(self.plies.keys()) + 1) // 2
print(
f"Loaded {len(loaded)} unique EPDs with |bm| in [{self.bmmin}, {self.bmmax}]."
)
s = sum((key + 1) // 2 * count for key, count in self.plies.items())
l = sum(self.plies.values())
if l:
print(f"Average for |bm| is {s/l:.2f}.")
if debug:
print("bm frequencies:", end=" ")
ply_count = sorted(self.plies.items(), key=lambda x: x[0])
print(
", ".join(
[
f"#{(ply + 1) // 2 if ply % 2 else - ply // 2}: {frequency}"
for ply, frequency in ply_count
]
)
)
def create_graph(self, cutOff):
plies = Counter()
for p, freq in self.plies.items():
if p > 2 * cutOff:
plies[2 * cutOff - 1 if p % 2 == 1 else 2 * cutOff] += freq
else:
plies[p] += freq
rangeMin, rangeMax = min(plies.keys()), max(plies.keys())
fig, ax = plt.subplots()
ax.hist(
plies.keys(),
weights=plies.values(),
range=(rangeMin, rangeMax + 1),
bins=rangeMax + 1 - rangeMin,
density=False,
alpha=0.5,
color="blue",
edgecolor="black",
)
for patch in ax.patches:
bin_x = patch.get_x() + patch.get_width() / 2
if int(bin_x) % 2 == 0:
patch.set_facecolor("deepskyblue")
pos = mpatches.Patch(color="blue", label=f"bm > 0 (total: {self.bmplus})")
neg = mpatches.Patch(
color="deepskyblue", label=f"bm < 0 (total: {self.bmminus})"
)
ax.legend(handles=[pos, neg])
ax.set_xlabel("|bm|")
fig.suptitle(
f"Distribution plot for the {self.bmplus+self.bmminus} bm's in {self.filename}.",
)
if max(self.plies.keys()) > cutOff:
ax.set_title(
f"Values |bm| > {cutOff} are included in the {cutOff} buckets.",
fontsize=6,
family="monospace",
)
xticks = (
[(rangeMin + 1) // 2 * 2]
+ list(ax.get_xticks())
+ [(rangeMax + 1) // 2 * 2]
)
xticks = [
int(x) for x in xticks if x >= rangeMin and x <= rangeMax and x % 2 == 0
]
new_xtick_labels = [x // 2 for x in xticks]
ax.set_xticks(xticks)
ax.set_xticklabels(new_xtick_labels)
prefix, _, _ = self.filename.rpartition(".")
pgnname = prefix + ".png"
plt.savefig(pgnname, dpi=300)
print(f"Saved bm distribution plot in file {pgnname}.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Plot bm distribution for positions in e.g. matetrack.epd.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"filename",
help=".epd(.gz) file with positions and their cdb evals.",
)
parser.add_argument(
"-c",
"--cutOff",
help="Cutoff value for the distribution plot.",
type=int,
default=100,
)
parser.add_argument(
"--debug",
action="store_true",
help="Show frequency data on stdout.",
)
args = parser.parse_args()
d = data(args.filename, args.debug)
d.create_graph(args.cutOff)