-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbokeshif.py
279 lines (246 loc) · 10 KB
/
bokeshif.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
import uuid
from datetime import datetime
import colorsys
from copy import deepcopy
from collections import OrderedDict
from bokeh.document import Document
from bokeh.session import Session
from bokeh.widgetobjects import VBox, HBox, PreText, MultiSelect, DateRangeSlider, TextInput
from bokeh.objects import DataRange1d, ColumnDataSource, Plot, LinearAxis, Glyph, Grid, DatetimeAxis, HoverTool, DatetimeTickFormatter
from bokeh.glyphs import Quad, Circle
from bokeh.embed import autoload_server
from datasource import ElasticDS as EDS
class Bokeshif:
def __init__(self, es_index='bokeshif'):
self.document = Document()
self.session = Session(load_from_config=False)
self.ds = EDS(index='bokeshif')
self.session.use_doc(str(uuid.uuid4()))
self.session.load_document(self.document)
self.layout = None
self.big = True
self.vis = {
'counter': PreText(text=''),
'layout': None,
}
self.query = {}
self.count = 0
self.timeline_source = self._make_timeline_source()
self.document_source = self._make_document_source()
self.colors = self._make_palette(len(self.ds.date_columns))
self._update_data()
self._make_vis()
@property
def link(self):
return self.session.object_link(self.document._plotcontext)
@property
def embed_tag(self):
return autoload_server(self.layout, self.session)
def reload(self):
self.session.load_document(self.document)
def _make_timeline_source(self):
data = self.ds.get_data()['hist']
cols = self.ds.date_columns
source = ColumnDataSource(
data=data,
column_names=cols+['left','right','bottom']
)
return source
def _make_document_source(self):
data = self.ds.get_data()['data']
source = ColumnDataSource(
data=data,
)
return source
def _make_ranges(self):
xdr = DataRange1d(sources=[self.timeline_source.columns('date')])
ydr = DataRange1d(sources=[self.timeline_source.columns(c) for c in self.ds.date_columns])
return xdr, ydr
def _make_palette(self, N=8):
# from jhrf-- http://stackoverflow.com/questions/876853/generating-color-ranges-in-python
HSV_tuples = [(x*1.0/N, 1.0, 0.5) for x in xrange(N)]
hex_out = []
for rgb in HSV_tuples:
rgb = map(lambda x: int(x*255),colorsys.hsv_to_rgb(*rgb))
hex_out.append("#"+"".join(map(lambda x: chr(x).encode('hex'),rgb)))
return hex_out
def _update_data(self):
data = self.ds.get_data(self.query)
self.timeline_source.data = data['hist']
self.document_source.data = data['data']
schema = self.ds.get_schema()
self.count = data['hits']
self.vis['counter'].text = 'Showing {0} out of {1} documents'.format(self.count, schema['__global']['size'])
'''
too_big = (self.count <= 1000 and self.big)
too_small = (self.count > 1000 and not self.big)
if too_big or too_small:
self._rebuild_timelines()
'''
self.session.store_document(self.document)
def _controls(self):
def select_change(obj, attr, old, new):
self.query[obj._owner] = new
self._update_data()
schema = self.ds.get_schema()
subset = {k:v for k,v in schema.iteritems() if not k == '__global' and v['type'] == 'string'}
selects = []
for k,v in subset.iteritems():
ms = MultiSelect.create(
name=k,
options=v['cats']
)
ms._owner = k
ms.on_change('value', select_change)
selects.append(ms)
def search_change(obj, attr, old, new):
self.query[obj._owner] = new
self._update_data()
subset = {k:v for k,v in schema.iteritems() if not k == '__global' and v['type'] == 'text'}
searches = []
for k,v in subset.iteritems():
ip = TextInput(
name=k,
title=k,
value=''
)
ip._owner = k
ip.on_change('value', search_change)
searches.append(ip)
return VBox(children=selects+searches)
def _resolve_glyph(self):
factory = None
if self.count > 1000:
self.big = True
def __factory(top, color='black', alpha=0.5):
return Quad(left='left',
right='right',
bottom=0,
top=top,
line_color=color,
line_alpha=alpha)
factory = __factory
else:
self.big = False
def __factory(null, color='black', alpha=0.5):
return Circle(x='date',
y=1,
size=5,
fill_color=color,
fill_alpha=alpha)
factory = __factory
return factory
def _timelines_info(self):
counter = self.vis['counter']
schema = self.ds.get_schema()
counter.text = 'Showing {0} out of {1} documents'.format(self.count, schema['__global']['size'])
return counter
def _timeline(self, idx, xdr, ydr, glyph_factory):
column = self.ds.date_columns[idx]
plot = Plot(data_sources=[self.timeline_source],
x_range=xdr,
y_range=ydr,
title=None,
plot_width=675,
plot_height=100,
min_border=5,
min_border_left=50,
min_border_bottom=25,
min_border_right=1)
color = self.colors[idx]
glyph = glyph_factory(column, color=color, alpha=0.5)
_renderer = Glyph(data_source=self.timeline_source,
xdata_range=xdr,
ydata_range=ydr,
glyph=glyph)
plot.renderers.append(_renderer)
background = Quad(left='left',
right='right',
bottom=0,
top='max',
line_color=color,
line_alpha='{0}_density'.format(column))
bg_renderer = Glyph(data_source=self.timeline_source,
xdata_range=xdr,
ydata_range=ydr,
glyph=background)
plot.renderers.append(bg_renderer)
formatter = DatetimeTickFormatter(formats=dict(years=['%Y'],
months=['%Y-%m'],
days=['%Y-%m-%d']))
xaxis = DatetimeAxis(plot=plot,
location='bottom',
dimension=0,
formatter=formatter)
Grid(plot=plot,
dimension=0,
axis=xaxis)
yaxis = LinearAxis(plot=plot,
location='left',
dimension=1,
major_label_text_font_size="6pt")
Grid(plot=plot,
dimension=1,
axis=yaxis)
hover = HoverTool(plot=plot,
tooltips=OrderedDict([
("Date", "@date")
]),
always_active=True)
plot.tools.append(hover)
return plot
def _timeline_slider(self, idx, xdr, ydr, onchange):
column = self.ds.date_columns[idx]
slider = DateRangeSlider(bounds=[datetime.fromtimestamp(d) for d in self.ds.domain],
value=[datetime.fromtimestamp(d) for d in self.ds.domain],
range=(dict(days=1), None),
title=column,
name=column)
slider._owner = column
slider.on_change('value', onchange)
return slider
def _timelines(self):
def slide_change(obj, attr, old, new):
if isinstance(new[0], float):
return
else:
dates = [t.split('T')[0] for t in new]
q = {
'from': dates[0],
'to': dates[1]
}
self.query[obj._owner] = q
self._update_data()
xdr, ydr = self._make_ranges()
glyph_factory = self._resolve_glyph()
timelines = []
for i in range(len(self.ds.date_columns)):
slider = self._timeline_slider(i, xdr, ydr, slide_change)
plot = self._timeline(i, xdr, ydr, glyph_factory)
timelines.append(VBox(children=[plot, slider]))
info = self._timelines_info()
container = VBox(children=[info]+timelines)
return container
def _rebuild_timelines(self):
layout = deepcopy(self.vis['layout'])
layout.children[1] = self._timelines()
document = Document()
document.add(layout)
self.document = document
self.session.load_document(self.document)
self.session.store_document(self.document)
def _documents(self):
# TODO not arbitrary
documents = []
for row in self.ds.get_data()['data']['show']:
documents.append(PreText(text=row))
#return VBox(children=documents)
return VBox(children=[])
def _make_vis(self):
layout = HBox(children=[self._controls(),
self._timelines(),
self._documents()])
self.vis['layout'] = layout
self.layout = layout
self.document.add(layout)
self.session.store_document(self.document)