-
Notifications
You must be signed in to change notification settings - Fork 0
/
bokehCharts.py
193 lines (143 loc) · 6.7 KB
/
bokehCharts.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
from bokeh.io import curdoc, output_file, show
from bokeh.layouts import widgetbox, column, layout
from bokeh.models.widgets import TextInput, Button, DataTable, TableColumn, Select
import requests
import pandas as pd
from bokeh.models import ColumnDataSource
from io import StringIO
from bokeh.plotting import figure
import numpy as np
import scipy.special
from bokeh.layouts import gridplot
text_input = TextInput(value="http://test.com", title="Url:")
button = Button(button_type = 'success', label='Submit')
select1 = Select(title='x axis variable', options=[])
select2 = Select(title='y axis variable', options=[])
columns = []
sourceplt1 = ColumnDataSource(data={'x':[], 'y':[]})
sourceplt2hist = ColumnDataSource(data={ 'left':[], 'right':[], 'hist':[]})
sourceplt3hist = ColumnDataSource(data={ 'left':[], 'right':[], 'hist':[]})
sourceplt2pdf = ColumnDataSource(data={'x':[], 'pdf':[]})
sourceplt3pdf = ColumnDataSource(data={'x':[], 'pdf':[]})
sourceplt2cdf = ColumnDataSource(data={'x':[], 'cdf':[]})
sourceplt3cdf = ColumnDataSource(data={'x':[], 'cdf':[]})
xf = pd.DataFrame({'x':[], 'y':[]})
lstval1 = ' '
lstval2 = ' '
plot1 = figure(title ='Scatter Plot', x_axis_label='', y_axis_label='', width=450)
plot2 = figure(title ='Data Distribution with probability density function for x axis', x_axis_label='', y_axis_label='', background_fill_color="#E8DDCB", width=600)
plot3 = figure(title ='Data Distribution with probability density function for y axis', x_axis_label='', y_axis_label='', background_fill_color="#E8DDCB", width=600)
plot4 = figure(title ='cumulative density function for x axis variable', x_axis_label='', y_axis_label='', background_fill_color="#E8DDCB", width=420)
plot5 = figure(title ='cumulative density function for y axis variable', x_axis_label='', y_axis_label='', background_fill_color="#E8DDCB", width=420)
def update():
link = text_input.value
data = None
if link.startswith('http'):
r = requests.get(link)
data = StringIO(r.text)
else:
data = link
df = pd.read_csv(data)
global xf
xf = df.copy()
source = ColumnDataSource(df)
select1.options.append(' ')
select2.options.append(' ')
select1.options = [x for x in xf.columns]
select2.options = [x for x in xf.columns]
columns = []
if len(layout.children) >= 3:
layout.children.pop()
layout.children.pop()
layout.children.pop()
for x in df.columns:
columns.append(TableColumn(title=x, field=x))
data_table = DataTable(source=source, columns=columns, width=1266, height=400)
widg = widgetbox(select1, select2)
c = column(data_table)
layout.children.append(c)
layout.children.append(widg)
plot1.xaxis.axis_label = xf.columns[0]
plot1.yaxis.axis_label = xf.columns[0]
sourceplt1.data={'x' : xf[xf.columns[0]], 'y': xf[xf.columns[0]] }
plot1.circle('x', 'y', source=sourceplt1)
global lstval1, lstval2
lstval1 = xf.columns[0]
lstval2 = xf.columns[0]
mu, sigma = np.mean(xf[xf.columns[0]]), np.std(xf[xf.columns[0]])
hist, edges = np.histogram(xf[xf.columns[0]], density=True, bins=50)
x = np.linspace( np.min( xf[xf.columns[0]] ) , np.max( xf[xf.columns[0]] ), 1000)
pdf = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2*sigma**2))
cdf = (1+scipy.special.erf((x-mu)/np.sqrt(2*sigma**2)))/2
sourceplt2hist.data = {'left':edges[:-1], 'right':edges[1:], 'hist':hist }
sourceplt3hist.data = { 'left':edges[:-1], 'right':edges[1:], 'hist':hist }
sourceplt2pdf.data = {'x' : x, 'pdf':pdf }
sourceplt3pdf.data = {'x' : x, 'pdf' : pdf}
sourceplt2cdf.data = {'x' : x, 'cdf': cdf }
sourceplt3cdf.data = {'x' : x, 'cdf' : cdf}
plot2.quad(top='hist', bottom=0, left='left', right='right',
fill_color="#036564", line_color="#033649", source=sourceplt2hist)
plot2.line('x', 'pdf', line_color="#D95B43", line_width=8, alpha=0.7, legend="PDF", source=sourceplt2pdf)
plot4.line('x', 'cdf', line_color="white", line_width=2, alpha=0.7, legend="CDF", source=sourceplt2cdf)
plot2.legend.location = "center_right"
plot2.legend.background_fill_color = "darkgrey"
plot2.xaxis.axis_label = xf.columns[0]
plot2.yaxis.axis_label = 'Pr(' + xf.columns[0] +')'
plot3.quad(top='hist', bottom=0, left='left', right='right',
fill_color="#036564", line_color="#033649", source=sourceplt3hist)
plot3.line('x', 'pdf', line_color="#D95B43", line_width=8, alpha=0.7, legend="PDF", source= sourceplt3pdf)
plot5.line('x', 'cdf', line_color="white", line_width=2, alpha=0.7, legend="CDF", source=sourceplt3cdf)
plot3.legend.location = "center_right"
plot3.legend.background_fill_color = "darkgrey"
plot3.xaxis.axis_label = xf.columns[0]
plot3.yaxis.axis_label = 'Pr(' + xf.columns[0] +')'
plot4.legend.location = "center_right"
plot4.legend.background_fill_color = "darkgrey"
plot4.xaxis.axis_label = xf.columns[0]
plot4.yaxis.axis_label = 'Pr(' + xf.columns[0] +')'
plot5.legend.location = "center_right"
plot5.legend.background_fill_color = "darkgrey"
plot5.xaxis.axis_label = xf.columns[0]
plot5.yaxis.axis_label = 'Pr(' + xf.columns[0] +')'
layout.children.append(gridplot(plot1,plot4,plot5, ncols=3))
layout.children.append(gridplot(plot2, plot3, ncols=2))
def callback1(attr, old, new):
plot1.xaxis.axis_label=new
sourceplt1.data = {'x' : xf[new], 'y': xf[lstval2]}
global lstval1
lstval1 = new
mu, sigma = np.mean(xf[new]), np.std(xf[new])
hist, edges = np.histogram(xf[new], density=True, bins=50)
x = np.linspace(np.min(xf[new]), np.max(xf[new]), 1000)
pdf = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2*sigma**2))
cdf = (1+scipy.special.erf((x-mu)/np.sqrt(2*sigma**2)))/2
plot2.xaxis.axis_label = new
plot2.yaxis.axis_label = 'Pr(' + new +')'
plot4.xaxis.axis_label = new
plot4.yaxis.axis_label = 'Pr(' + new +')'
sourceplt2hist.data = { 'left':edges[:-1], 'right':edges[1:], 'hist':hist }
sourceplt2pdf.data = {'x' : x, 'pdf':pdf }
sourceplt2cdf.data = {'x' : x, 'cdf':cdf }
def callback2(attr, old, new):
plot1.yaxis.axis_label=new
sourceplt1.data = {'x' : xf[lstval1], 'y': xf[new]}
global lstval2
lstval2 = new
mu, sigma = np.mean(xf[new]), np.std(xf[new])
hist, edges = np.histogram(xf[new], density=True, bins=50)
x = np.linspace(np.min(xf[new]), np.max(xf[new]), 1000)
pdf = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2*sigma**2))
cdf = (1+scipy.special.erf((x-mu)/np.sqrt(2*sigma**2)))/2
plot3.xaxis.axis_label = new
plot3.yaxis.axis_label = 'Pr(' + new +')'
plot5.xaxis.axis_label = new
plot5.yaxis.axis_label = 'Pr(' + new +')'
sourceplt3hist.data = { 'left':edges[:-1], 'right':edges[1:], 'hist':hist }
sourceplt3pdf.data = {'x' : x, 'pdf':pdf }
sourceplt3cdf.data = {'x' : x, 'cdf':cdf }
select1.on_change('value', callback1)
select2.on_change('value', callback2)
button.on_click(update)
widget = widgetbox(text_input, button)
layout = layout([[widget]])
curdoc().add_root(layout)