-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphs.py
320 lines (273 loc) · 12.2 KB
/
Graphs.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import wx
from matplotlib import pyplot as plt
from Dialogues import GraphDialog, RegressDialog
from Settings import settings
from mpl_toolkits.mplot3d import Axes3D
from scipy import stats
import numpy as np
import seaborn as sns
import pandas as pd
import warnings
warnings.simplefilter("error", np.RankWarning)
class GraphMenu(wx.Menu):
def __init__(self, parent):
wx.Menu.__init__(self)
self.parent = parent
# 1 Dimension
parent.Bind(wx.EVT_MENU, self.createHist,
self.Append(wx.NewId(), 'Histogram'))
parent.Bind(wx.EVT_MENU, self.createBoxplot,
self.Append(wx.NewId(), 'Boxplot'))
parent.Bind(wx.EVT_MENU, self.createViolin,
self.Append(wx.NewId(), "Violinplot"))
parent.Bind(wx.EVT_MENU, self.createQQ,
self.Append(wx.NewId(), "QQ Plot"))
self.AppendSeparator()
# Regression
parent.Bind(wx.EVT_MENU, self.createTime,
self.Append(wx.NewId(), "Time Series"))
parent.Bind(wx.EVT_MENU, self.createScatter,
self.Append(wx.NewId(), "Scatter Plot"))
parent.Bind(wx.EVT_MENU, self.createMatrix,
self.Append(wx.NewId(), "Matrix Plot"))
parent.Bind(wx.EVT_MENU, self.createInteraction,
self.Append(wx.NewId(), "Interaction Plot"))
parent.Bind(wx.EVT_MENU, self.createMatrixInteract,
self.Append(wx.NewId(), "Matrix Interaction Plot"))
# Multivariate
self.AppendSeparator()
parent.Bind(wx.EVT_MENU, self.createBiDensity,
self.Append(wx.NewId(), "Bivariate Density Fit"))
parent.Bind(wx.EVT_MENU, self.create3DScatter,
self.Append(wx.NewId(), "3D Scatter Plot"))
def createHist(self, event):
# TODO avoid hardcoding sizes. Find smart way to decide on sizes
dlg = GraphDialog(self.parent, "Histogram Input", ("Select Data",),
size=(500,200))
# options
hsize1 = wx.BoxSizer(wx.HORIZONTAL)
bars = wx.CheckBox(dlg, label="Bars")
density = wx.CheckBox(dlg, label="Density")
bars.SetValue(True)
density.SetValue(True)
hsize1.Add(bars)
hsize1.Add(density)
dlg.Add(hsize1)
numBins = dlg.AddSpinCtrl("# of Bins", 1, 999,
np.sqrt(len(self.parent.data)), size=(50, -1))
bandwidth = dlg.AddSpinCtrl("Density Bandwidth", -99, 99, 0,
size=(50, -1))
bars.Bind(wx.EVT_CHECKBOX, lambda e: numBins.Enable(bars.GetValue()))
density.Bind(wx.EVT_CHECKBOX,
lambda e: bandwidth.Enable(density.GetValue()))
if dlg.ShowModal() == wx.ID_OK:
ds = [d[0] for d in dlg.GetName()]
# account for grouping
groups, datas = dlg.GetValue(self.parent.data)
bars, density = bars.GetValue(), density.GetValue()
bandwidth = np.exp(-0.2 * bandwidth.GetValue())
if groups:
ds = self._groupLabels(ds, groups)
newDs = []
for d in ds:
newDs += [d + "-" + str(g) for g in groups]
ds = newDs
dlg.Destroy()
# d.min() gets minimum for each column. d.min.min() gets global min
a, b = min(d.min().min() for d in datas), max(d.max().max() for d in datas)
bins = np.arange(a, b, float(b-a) / numBins.GetValue())
for d, data in zip(ds, datas):
data = data[data.columns[0]]
d, data = d, data.astype(float)
# astype float b/c of bug in seaborn.
if bars and not density:
plt.hist(data, bins=bins, alpha=1.0/len(ds), label=d)
else:
data = data[np.isfinite(data)]
bw = stats.gaussian_kde(data).factor * bandwidth
if density and not bars:
sns.kdeplot(data, shade=True, label=d, bw=bw)
else:
sns.distplot(data, bins=bins, kde_kws={"bw":bw, "label":d})
plt.legend(loc='best')
plt.show()
def _groupLabels(self, labels, groups):
new = []
for l in labels:
new += [l + "-" + str(g) for g in groups]
return new
def createBoxplot(self, event):
dlg = GraphDialog(self.parent, "Boxplot Input", queries=("Select Data",),
size=(500, 200))
if dlg.ShowModal() == wx.ID_OK:
ds = [d[0] for d in dlg.GetName()]
groups, datas = dlg.GetValue(self.parent.data)
if groups:
ds = self._groupLabels(ds, groups)
dlg.Destroy()
sns.boxplot(datas, names=ds, color=settings["color"])
plt.show()
def createViolin(self, event):
dlg = GraphDialog(self.parent, "Violinplot Input", ("Select Data",),
size=(500, 200))
if dlg.ShowModal() == wx.ID_OK:
ds = [d[0] for d in dlg.GetName()]
groups, datas = dlg.GetValue(self.parent.data)
if groups:
ds = self._groupLabels(ds, groups)
# convert dataframe to series to avoid seaborn error
for i in xrange(len(datas)):
datas[i] = datas[i][datas[i].columns[0]]
dlg.Destroy()
# TODO Allow user input for bandwidth
#sns.violinplot([self.parent.data[[ds[0]]]], names=ds)
sns.violinplot(datas, names=ds, color=settings["color"])
plt.show()
def createQQ(self, event):
dlg = GraphDialog(self.parent, "QQ Plot Input", ("Select Data",),
size=(700, 200), add=False, groups=False)
dists = [dist for dist in dir(stats.distributions)
if dist + "_gen" in dir(stats.distributions) and "_" not in dist]
dlg.Add(wx.StaticText(dlg, label="Select Distribution"))
dist = wx.ComboBox(dlg, 1, choices=dists,
style=wx.CB_DROPDOWN | wx.CB_READONLY)
dist.SetValue("norm")
dlg.Add(dist)
if dlg.ShowModal() == wx.ID_OK:
ds = dlg.GetName()
dist = dist.GetValue()
pdf = stats.distributions.__dict__[dist] #hacky, but works
dlg.Destroy()
for d in (x[0] for x in ds):
data = self.parent.data[d]
data = data[np.isfinite(data)]
stats.probplot(data, pdf.fit(data), dist, plot=plt)
plt.show()
def createTime(self, event):
dlg = GraphDialog(self.parent, "Time Series Input", ("Select Data",),
size=(500, 200), groups=False)
if dlg.ShowModal() == wx.ID_OK:
ds = [x[0] for x in dlg.GetName()]
dlg.Destroy()
self.parent.data[ds].plot()
plt.show()
def createScatter(self, event):
dlg = GraphDialog(self.parent, "Scatterplot Input", ("X", "Y"),
size=(700, 200), groups=False)
regress = wx.CheckBox(dlg, label="Add Regression Polynomial?")
regress.SetValue(True)
jitter = wx.CheckBox(dlg, label="Jitter?")
jitter.SetValue(False)
dlg.Add(jitter)
ci = dlg.AddSpinCtrl("Confidence (>=100 for None)", 0, 101, 95)
order = dlg.AddSpinCtrl("Polynomial Degree", 1, 10, 1)
regress.Bind(wx.EVT_CHECKBOX,
lambda e: ci.Enable(regress.GetValue()) and order.Enable(regress.GetValue()))
dlg.Add(regress)
if dlg.ShowModal() == wx.ID_OK:
ds = dlg.GetName()
dlg.Destroy()
regress, ci = regress.GetValue(), ci.GetValue()
order, jitter = order.GetValue(), jitter.GetValue()
data = self.parent.data[list({b for bs in ds for b in bs})].astype(float)
snData = pd.DataFrame()
for x, y in ds: # Deals with silly SNS stuff
d = {"x":data[x], "y":data[y], "group":np.repeat(y, len(data[x]))}
d = pd.DataFrame(d)
snData = snData.append(d, ignore_index=True)
if jitter:
xjitter = snData["x"].std() / 4
yjitter = snData["y"].std() / 4
else:
xjitter, yjitter = 0, 0
try:
if ci < 100 and regress:
sns.lmplot("x", "y", snData, hue="group", ci=ci, order=order,
x_jitter=xjitter, y_jitter=yjitter)
else:
sns.lmplot("x", "y", snData, fit_reg=regress, ci=None, order=order,
x_jitter=xjitter, y_jitter=yjitter)
plt.show()
except np.RankWarning:
dlg = wx.MessageDialog(self.parent, "Polynomial Degree Too High",
style = wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
plt.show()
def createMatrix(self, event):
# TODO Fix ugly gridlines. sns.setStyle('nogrid') failed
dlg = GraphDialog(self.parent, "Matrix Plot Input", ("Select Data",),
size=(500, 300), groups=False)
if dlg.ShowModal() == wx.ID_OK:
ds = [d[0] for d in dlg.GetName()]
df = self.parent.data[ds]
n = len(ds)
dlg.Destroy()
pd.scatter_matrix(df, grid=False)
plt.show()
def createMatrixInteract(self, event):
dlg = RegressDialog(self.parent, "Matrix Interaction Plot")
log = wx.CheckBox(dlg, label="Logistic Fit?")
dlg.Add(log)
if dlg.ShowModal() == wx.ID_OK:
y, xs = dlg.GetValue()
log = log.GetValue()
data = self.parent.data[list(xs) + [y]]
df = data[list(xs)]
fig, axes = plt.subplots(nrows=len(xs), ncols=len(xs))
for i, l1 in enumerate(df):
for j, l2 in enumerate(df):
ax = axes[j, i]
ax.grid(False)
plt.subplot(ax)
if i == j:
sns.regplot(data[l1], data[y], ax=ax),
# would like to do logistic plot, but takes too long
elif i < j:
sns.interactplot(l1, l2, y, data, ax=ax, logistic=log,
cmap=settings["cmap"])
if i != 0 and j != 0:
ax.yaxis.set_visible(False)
if j != len(xs) - 1:
ax.xaxis.set_visible(False)
plt.show()
def createInteraction(self, event):
dlg = GraphDialog(self.parent, "Matrix Plot Input", ("X1", "X2", "Y"),
size=(700, 200), add=False, groups=False)
fill = wx.CheckBox(dlg, label="Fill")
fill.SetValue(True)
log = wx.CheckBox(dlg, label="Logistic Fit?")
dlg.Add(fill)
dlg.Add(log)
if dlg.ShowModal() == wx.ID_OK:
(x1, x2, y), fill = dlg.GetName()[0], fill.GetValue()
log = log.GetValue()
data = self.parent.data[[x1, x2, y]].astype(float)
dlg.Destroy()
temp = data[[x1, x2, y]]
sns.interactplot(x1, x2, y, temp, cmap=settings["cmap"], filled=fill,
logistic=log)
plt.show()
def createBiDensity(self, event):
dlg = GraphDialog(self.parent, "Bivariate Density Fit", ("X1", "X2"),
size=(700, 200), add=False, groups=False)
fill = wx.CheckBox(dlg, label="Fill")
dlg.Add(fill)
if dlg.ShowModal() == wx.ID_OK:
ds, fill = dlg.GetName(), fill.GetValue()
data = self.parent.data[list({b for bs in ds for b in bs})].astype(float)
dlg.Destroy()
for x1, x2 in ds:
temp = data[[x1, x2]]
sns.kdeplot(temp, shade=fill)
plt.show()
def create3DScatter(self, event):
dlg = GraphDialog(self.parent, "3D Scatter Plot Fit", ("X1", "X2", "Y"),
size=(700, 200), add=False, groups=False)
if dlg.ShowModal() == wx.ID_OK:
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
x1, x2, y = dlg.GetName()[0]
data = self.parent.data[[x1, x2, y]]
ax.scatter(data[x1], data[x2], data[y])
plt.show()