-
Notifications
You must be signed in to change notification settings - Fork 1
/
viz.py
188 lines (150 loc) · 5.71 KB
/
viz.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from typing import List, Union, Dict, Tuple
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from classes import Dist, RollDef
def histogram1(rolls, title=None, xmax=None, ymax=None):
"""
:param rolls: one dimensional matrix to plot
:param title: title to put on plot and save the image as
:param xmax: maximum x value to use on plot
:param ymax: maximum y value to use on plot
"""
plt.hist(rolls, bins=range(min(rolls), max(rolls) + 2),
align="left", edgecolor='k', facecolor="darksalmon",
density=True, rwidth=0.9)
plt.axvline(np.mean(rolls), color="navy", linestyle="--", linewidth=2)
plt.axvline(np.median(rolls), color="black", linestyle=":", linewidth=2)
plt.legend(["mean = {:0.2f}".format(np.mean(rolls)),
"median = {:0.0f}".format(np.median(rolls)),
"histogram"])
plt.ylabel("Rate of occurrence")
plt.xlabel("Value")
plt.xticks([x for x in range(min(rolls), max(rolls) + 1)])
plt.grid()
if xmax is not None:
plt.xlim([0, xmax])
if ymax is not None:
plt.ylim([0, ymax])
if title is not None:
plt.title(title)
plt.savefig("plots/{0}.png".format(title))
plt.show()
class RollDefDashboard(object):
""" A visualization that summarizes multiple rolldefs"""
def __init__(self, dists: List[Dist]):
self.dists = dists
self._fig = plt.figure(figsize=(20, 12))
self._grid = plt.GridSpec(12, 2, hspace=.8, wspace=0.5,
top=0.95, bottom=0.05, left=0.05, right=0.95)
self._hist_ax = self.__hist_ax()
self._mean_ax = self.__mean_ax()
self._median_ax = self.__median_ax()
self._cum_ax = self.__cum_ax()
self._table_ax = self.__table_ax()
@classmethod
def from_rolldefs(self, rolldefs: List[RollDef], n: Union[int, float] = None):
return RollDefDashboard(dists=[rd.dist(n) for rd in rolldefs])
def show(self):
self._pop_hist_ax()
self._pop_mean_ax()
self._pop_median_ax()
self._pop_cum_ax()
plt.show()
def __hist_ax(self):
hist_ax = self._fig.add_subplot(self._grid[:4, 0])
hist_ax.set_xticks(self.get_x_ticks())
return hist_ax
def __mean_ax(self):
mean_ax = self._fig.add_subplot(self._grid[4:6, 0], sharex=self._hist_ax)
mean_ax.xaxis.set_visible(False)
return mean_ax
def __median_ax(self):
median_ax = self._fig.add_subplot(self._grid[6:8, 0], sharex=self._hist_ax)
median_ax.xaxis.set_visible(False)
return median_ax
def __cum_ax(self):
cum_ax = self._fig.add_subplot(self._grid[8:, 0], sharex=self._hist_ax)
cum_ax.set_xticks(self.get_x_ticks())
return cum_ax
def __table_ax(self):
table_ax = self._fig.add_subplot(self._grid[:, 1])
return table_ax
def _pop_hist_ax(self):
names = self.names()
colors = self.color_dict().values()
dists = self.dists_dict().values()
ax = self._hist_ax
for n, c, d in zip(names, colors, dists):
ax.plot(
d.bins[:-1],
d.values,
color=c,
label=n,
linewidth=2)
ax.yaxis.grid()
ax.set_ylim(0, None)
ax.legend()
ax.set_ylabel('Distribution')
def _pop_mean_ax(self):
names = self.names()
colors = self.color_dict().values()
means = self.means_dict().values()
ax = self._mean_ax
for n, c, m in zip(names, colors, means):
ax.axvline(m, color=c, linewidth=2)
ax.set_ylabel("Mean")
ax.legend(means)
def _pop_median_ax(self):
names = self.names()
colors = self.color_dict().values()
medians = self.medians_dict().values()
ax = self._median_ax
for n, c, m in zip(names, colors, medians):
ax.axvline(m, color=c, linewidth=2, linestyle=':')
ax.set_ylabel("Median")
ax.legend(medians)
def _pop_cum_ax(self):
names = self.names()
colors = self.color_dict().values()
dists = self.dists_dict().values()
ax = self._cum_ax
for n, c, d in zip(names, colors, dists):
ax.plot(
d.bins[:-1],
np.cumsum(d.values),
color=c,
label=n,
linewidth=2)
ax.yaxis.grid()
ax.set_ylim(0, None)
ax.legend()
ax.set_ylabel("Cumulative")
def names(self):
return [d.rolldef.name for d in self.dists]
def color_dict(self):
colors = sns.hls_palette(len(self.dists))
color_dict = {d.rolldef.name: c for d, c in zip(self.dists, colors)}
return color_dict
def get_x_ticks(self):
all = np.hstack([d.bins for d in self.dists])
return np.unique(all)
def get_x_range(self):
ticks = self.get_x_ticks()
return min(ticks), max(ticks)
def add_accuracy(self, n: int):
""" iteratively adds accuracy to all underlying distributions """
for d in self.dists:
d.add_accuracy(n)
def dists_dict(self) -> Dict[str, Tuple[np.array, np.array]]:
""" returns a dict of the distributions """
dists_dict = {d.rolldef.name: d for d in self.dists}
return dists_dict
def means_dict(self) -> Dict[str, float]:
""" returns dict of mean values"""
mean_dict = {d.rolldef.name: d.mean for d in self.dists}
return mean_dict
def medians_dict(self) -> Dict[str, float]:
""" returns dict of median values """
median_dict = {d.rolldef.name: d.median for d in self.dists}
return median_dict