-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
201 lines (182 loc) · 6.32 KB
/
app.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
from os.path import join, abspath, split
import time
import jinja2
import numpy as np
import pandas as pd
import scipy.ndimage
from flask import jsonify, send_from_directory, request
from werkzeug.exceptions import BadRequest
import logging
logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(logging.WARNING)
from kitchensink import setup_client, client, do, du, dp
from kitchensink.admin import timethis
setup_client('http://localhost:6323/')
from bokeh.server.app import bokeh_app
from bokeh.server.utils.plugins import object_page
from bokeh.server.views import make_json
import ujson
from objects import (AjaxDataSource, TaxiApp, ARDataSource, ds, get_data,
get_time_histogram, get_distance_histogram)
_templates_path = join(abspath(split(__file__)[0]), "templates")
_web_path = join(abspath(split(__file__)[0]), "web")
def render(template, **context):
with open(join(_templates_path, template)) as f:
template = f.read()
template = jinja2.Template(template)
return template.render(**context)
@bokeh_app.route("/bokeh/taxis/")
@object_page("taxis")
def main():
return TaxiApp.create()
@bokeh_app.route('/bokehjs/static/app/<path:filename>')
def arstatics(filename):
return send_from_directory(_web_path, filename)
gbounds = ds.gbounds
@bokeh_app.route("/bokeh/taxidata/<pickup>/", methods=['GET', 'POST'])
def taxidata(pickup):
st = time.time()
if pickup == 'pickup':
pickup = True
else:
pickup = False
try:
data = request.get_json()
except BadRequest:
import pdb; pdb.set_trace()
data = {}
if data.get('filter_url'):
filters = du(data.get('filter_url'))
else:
filters = None
if data:
bounds = (data['xmin'], data['xmax'], data['ymin'], data['ymax'])
bounds = (max(bounds[0], gbounds[0]),
min(bounds[1], gbounds[1]),
max(bounds[2], gbounds[2]),
min(bounds[3], gbounds[3]))
else:
bounds = gbounds
with timethis('regular project'):
data = get_data(pickup, bounds, filters)
data = data ** 0.2
data = data - data.min()
data = data / data.max()
data = data *255
data = data.astype('int64')
xmin, xmax, ymin, ymax = bounds
output = dict(x=[xmin],
y=[ymin],
dw=[xmax-xmin],
dh=[ymax-ymin],
palette=["Greys-256"])
print output
output['image'] = [data.tolist()]
result = make_json(ujson.dumps(output))
return result
@bokeh_app.route("/bokeh/taxidata/timehist/", methods=['GET', 'POST'])
def taxidatatimehist():
try:
data = request.get_json()
except BadRequest:
data = {}
if data.get('filter_url'):
filters = du(data.get('filter_url'))
else:
filters = None
print 'HIST', filters
data = get_time_histogram(filters)
data['y_bounds'] = [min(data['counts']), max(data['counts'])]
result = make_json(ujson.dumps(data))
return result
@bokeh_app.route("/bokeh/taxidata/distancehist/", methods=['GET', 'POST'])
def taxidatadistancehist():
try:
data = request.get_json()
except BadRequest:
data = {}
if data.get('filter_url'):
filters = du(data.get('filter_url'))
else:
filters = None
print 'HIST', filters
data = get_distance_histogram(filters)
data['y_bounds'] = [min(data['counts']), max(data['counts'])]
result = make_json(ujson.dumps(data))
return result
gbounds = ds.gbounds
@bokeh_app.route("/bokeh/taxidatavsregular/<pickup>/", methods=['GET', 'POST'])
def taxidatavsregular(pickup):
st = time.time()
if pickup == 'pickup':
pickup = True
else:
pickup = False
try:
data = request.get_json()
except BadRequest:
data = {}
if data.get('filter_url'):
filters = du(data.get('filter_url'))
else:
filters = None
if data and data.get('xmin'):
bounds = (data['xmin'], data['xmax'], data['ymin'], data['ymax'])
bounds = (max(bounds[0], gbounds[0]),
min(bounds[1], gbounds[1]),
max(bounds[2], gbounds[2]),
min(bounds[3], gbounds[3]))
else:
bounds = gbounds
if filters:
with timethis('unfiltered'):
unfiltered = get_data(pickup, bounds, None)
with timethis('filtered'):
filtered = get_data(pickup, bounds, filters)
# marker = np.array([[1,1], [1,1]])
# filtered = scipy.ndimage.convolve(filtered, marker)
# unfiltered = scipy.ndimage.convolve(unfiltered, marker)
# import cPickle as pickle
# with open("unfiltered.pkl", "w+") as f:
# pickle.dump(unfiltered, f, -1)
# with open("filtered.pkl", "w+") as f:
# pickle.dump(filtered, f, -1)
with timethis('procesing'):
percents = np.percentile(unfiltered[unfiltered!=0], np.arange(100))
unfiltered = np.interp(unfiltered, percents, np.arange(100))
percents = np.percentile(filtered[filtered!=0], np.arange(100))
filtered = np.interp(filtered, percents, np.arange(100))
##truncate data
filtered[filtered < 50] = 0
unfiltered[filtered < 50] = 0
data = filtered - unfiltered
#flatten the dynamic range
#data = data ** 0.333
data [data > 0] = data[data>0] ** 0.333
#linearize it
data[data > 0] = data[data > 0] / data.max()
data[data < 0] = - (data[data < 0] / data.min())
#convert to ints
data = data - data.min()
data = data / data.max()
data = data *255
data = data.astype('int64')
palette = 'seismic-256'
else:
data = get_data(pickup, bounds, None)
data = data ** 0.2
data = data - data.min()
data = data / data.max()
data = data *255
data = data.astype('int64')
palette = 'Greys-256'
xmin, xmax, ymin, ymax = bounds
output = dict(x=[xmin],
y=[ymin],
dw=[xmax-xmin],
dh=[ymax-ymin],
palette=[palette])
print output
output['image'] = [data.tolist()]
with timethis('json'):
result = make_json(ujson.dumps(output))
return result