-
Notifications
You must be signed in to change notification settings - Fork 3
/
ServeNN.py
205 lines (177 loc) · 7.49 KB
/
ServeNN.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
#! /usr/bin/python2
# vim: set fileencoding=utf-8
"""Display map of two cities to compare venues."""
import os
import pymongo
import flask as f
import cities as c
import ClosestNeighbor as cn
import FSCategories as fsc
import neighborhood as nb
import time
from timeit import default_timer as clock
import threading
import logging
import persistent as p
app = f.Flask(__name__)
app.config.update(dict(
DEBUG=True,
MONGO_URL=os.environ.get('MONGOHQ_URL',
"mongodb://localhost:27017/foursquare"),
))
# set the secret key. keep this really secret:
app.secret_key = os.environ['SECRET_KEY']
# TODO: find a better way to share complex object between requests
# http://pythonhosted.org/Flask-Cache/#flask.ext.cache.Cache.memoize
ORIGIN = {}
DEST = {}
SEARCH_STATUS = {}
# TODO: add two other geometries hash
KNOWN_GEO = {8710721343944074569: 'triangle', 6704630207412047940: 'latin',
-8874215139868552404: 'montmartre', 3492781793233117456:
'pigalle', 8686661700877431113: 'marais', -648696130672502955:
'official'}
def perform_search(from_city, to_city, region, metric):
start = clock()
for res, _, progress in nb.best_match(from_city, to_city, region, 900,
progressive=True,
metric=metric):
# print(progress)
try:
distance, r_vids, center, radius = res
except (ValueError, TypeError):
import json
desc = {"type": "Feature", "properties":
{"nb_venues": len(res),
"venues": res,
"origin": from_city},
"geometry": region}
with open('scratch.json', 'a') as out:
out.write(json.dumps(desc, sort_keys=True, indent=2,
separators=(',', ': '))+'\n')
return
if len(center) == 2:
center = c.euclidean_to_geo(to_city, center)
relevant = {'dst': distance, 'radius': radius, 'center': center,
'nb_venues': len(r_vids)}
SEARCH_STATUS.update(dict(seen=False, progress=progress, res=relevant))
print("done search in {:.3f}".format(clock() - start))
SEARCH_STATUS.update(dict(seen=False, progress=1.0, done=True,
res=relevant))
@app.route('/status')
def send_status():
while SEARCH_STATUS['seen']:
time.sleep(0.4)
SEARCH_STATUS['seen'] = True
return f.jsonify(r=SEARCH_STATUS)
@app.route('/seed_region', methods=['POST'])
def seed_region():
geo = f.json.loads(f.request.form['geo'])
fields = ['metric', 'candidate', 'clustering']
metric, candidate, clustering = [str(f.request.form[field])
for field in fields]
msg = 'From {}@{} to {} using {}, {}, {}'
neighborhood = KNOWN_GEO.get(hash(str(geo)), 'custom')
args = [ORIGIN['city'], neighborhood, DEST['city'], candidate,
metric if candidate == 'dst' else 'NA', clustering]
msg = msg.format(*args)
print(msg)
logging.warn(msg)
res, log = nb.one_method_seed_regions(ORIGIN['city'], DEST['city'], geo,
metric, candidate, clustering)
res = dict(r=res, info=log)
p.save_var('candidates/{}_{}_{}_{}_{}.my'.format(*args[1:]), res)
return f.jsonify(res)
@app.route('/match_neighborhood', methods=['POST'])
def start_search():
geo = f.json.loads(f.request.form['geo'])
metric = str(f.request.form['metric'])
args = (ORIGIN['city'], DEST['city'], geo, metric)
SEARCH_STATUS.update({'done': False, 'seen': False, 'progress': 0.0,
'name': str(f.request.form['name']),
'res': {'dst': 1e5, 'radius': 600, 'center': [],
'nb_venues': 0}})
threading.Thread(target=perform_search, args=args, name="search").start()
return "ok"
def connect_db():
"""Return a client to the default mongo database."""
return pymongo.MongoClient(app.config['MONGO_URL'])
def get_db():
"""Opens a new database connection if there is none yet for the current
application context.
"""
if not hasattr(f.g, 'mongo_db'):
f.g.mongo_db = connect_db()
return f.g.mongo_db
@app.teardown_appcontext
def close_db(error):
"""Closes the database again at the end of the f.request."""
if hasattr(f.g, 'mongo_db'):
f.g.mongo_db.close()
@app.route('/match', methods=['POST'])
def find_match():
side = int(f.request.form['side'])
assert side in [0, 1]
_id = f.request.form['_id']
first = DEST if side else ORIGIN
second = ORIGIN if side else DEST
query, res_ids, answers, dsts, among = cn.find_closest(_id, first, second)
_ = cn.interpret(first['features'][query, :],
second['features'][answers[0], :])
query_info, first_answer, feature_order = _
answers_info = [first_answer]
answers_info.extend([cn.interpret(first['features'][query, :],
second['features'][answer, :],
feature_order)[1]
for answer in answers[1:]])
sendf = lambda x, p: ('{:.'+str(p)+'f}').format(float(x))
res = {'query': query_info, 'answers_id': list(res_ids),
'distances': [sendf(d, 5) for d in dsts],
'explanations': answers_info, 'among': among}
return f.jsonify(r=res)
@app.route('/populate', methods=['POST'])
def get_venues():
origin = f.request.form['origin']
vids = (ORIGIN if origin == "true" else DEST)['index']
db = get_db().get_default_database()['venue']
res = db.find({'_id': {'$in': vids}}, {'name': 1, 'cat': 1,
'canonicalUrl': 1, 'loc': 1})
ven = [{'name': v['name'], 'cat': fsc.CAT_TO_ID[:v['cat']],
'_id': v['_id'], 'url': v['canonicalUrl'],
'loc': list(reversed(v['loc']['coordinates']))} for v in res]
return f.jsonify(r=ven)
@app.route('/n/<origin>/<dest>')
def neighborhoods(origin, dest):
"""Match neighborhoods."""
global ORIGIN
global DEST
origin = 'paris' if origin not in c.SHORT_KEY else origin
dest = 'helsinki' if dest not in c.SHORT_KEY else dest
ORIGIN = cn.gather_info(origin, 1, raw_features=True)
DEST = cn.gather_info(dest, 1, raw_features=True)
return f.render_template('nei.html', origin=origin, dest=dest,
lbbox=c.BBOXES[origin], rbbox=c.BBOXES[dest])
@app.route('/<origin>/<dest>/<int:knn>')
def compare(origin, dest, knn):
"""Compare two cities."""
global ORIGIN
global DEST
origin = 'barcelona' if origin not in c.SHORT_KEY else origin
dest = 'helsinki' if dest not in c.SHORT_KEY else dest
ORIGIN = cn.gather_info(origin, knn, raw_features=True)
DEST = cn.gather_info(dest, knn, raw_features=True)
return f.render_template('cnn.html', origin=origin, dest=dest, knn=knn,
lbbox=c.BBOXES[origin], rbbox=c.BBOXES[dest])
@app.route('/')
def welcome():
return f.redirect(f.url_for('compare', origin='barcelona',
dest='helsinki', knn=2))
@app.route('/gold/<city>/<neighborhood>')
def show_gold(city, neighborhood):
"""Show ground thruth for the given query."""
global ORIGIN
ORIGIN = cn.gather_info(city, 1, raw_features=True)
return f.render_template('gold.html', district=neighborhood,
bbox=c.BBOXES[city], city=city)
if __name__ == '__main__':
app.run()