forked from skylarbr/week-6
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
267 lines (188 loc) · 6.77 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
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
from flask import Flask
from flask import render_template
from flask import request
from flask import Response
import json
import time
import sys
import random
import math
import pyorient
from Queue import Queue
from sklearn import preprocessing
from sklearn import svm
import numpy as np
app = Flask(__name__)
q = Queue()
def point_distance(x1, y1, x2, y2):
return ((x1-x2)**2.0 + (y1-y2)**2.0)**(0.5)
def remap(value, min1, max1, min2, max2):
return float(min2) + (float(value) - float(min1)) * (float(max2) - float(min2)) / (float(max1) - float(min1))
def normalizeArray(inputArray):
maxVal = 0
minVal = 100000000000
for j in range(len(inputArray)):
for i in range(len(inputArray[j])):
if inputArray[j][i] > maxVal:
maxVal = inputArray[j][i]
if inputArray[j][i] < minVal:
minVal = inputArray[j][i]
for j in range(len(inputArray)):
for i in range(len(inputArray[j])):
inputArray[j][i] = remap(inputArray[j][i], minVal, maxVal, 0, 1)
return inputArray
def event_stream():
while True:
result = q.get()
yield 'data: %s\n\n' % str(result)
@app.route('/eventSource/')
def sse_source():
return Response(
event_stream(),
mimetype='text/event-stream')
@app.route("/")
def index():
return render_template("index.html")
@app.route("/getData/")
def getData():
q.put("starting data query...")
lat1 = str(request.args.get('lat1'))
lng1 = str(request.args.get('lng1'))
lat2 = str(request.args.get('lat2'))
lng2 = str(request.args.get('lng2'))
w = float(request.args.get('w'))
h = float(request.args.get('h'))
cell_size = float(request.args.get('cell_size'))
analysis = request.args.get('analysis')
#CAPTURE ANY ADDITIONAL ARGUMENTS SENT FROM THE CLIENT HERE
spread = int(request.args.get('spread'))
results = int(request.args.get('results'))
print "received coordinates: [" + lat1 + ", " + lat2 + "], [" + lng1 + ", " + lng2 + "]"
client = pyorient.OrientDB("localhost", 2424)
session_id = client.connect("root", "admin")
db_name = "soufun"
db_username = "admin"
db_password = "admin"
if client.db_exists( db_name, pyorient.STORAGE_TYPE_MEMORY ):
client.db_open( db_name, db_username, db_password )
print db_name + " opened successfully"
else:
print "database [" + db_name + "] does not exist! session ending..."
sys.exit()
query = 'SELECT FROM Listing WHERE latitude BETWEEN {} AND {} AND longitude BETWEEN {} AND {} AND prec = 1 AND conf > 60'
records = client.command(query.format(lat1, lat2, lng1, lng2))
#USE INFORMATION RECEIVED FROM CLIENT TO CONTROL
#HOW MANY RECORDS ARE CONSIDERED IN THE ANALYSIS
if results != 0:
random.shuffle(records)
records = records[:results]
numListings = len(records)
print 'received ' + str(numListings) + ' records'
client.db_close()
# iterate through data to find minimum and maximum price
minPrice = 1000000000
maxPrice = 0
for record in records:
price = record.price
if price > maxPrice:
maxPrice = price
if price < minPrice:
minPrice = price
print minPrice
print maxPrice
output = {"type":"FeatureCollection","features":[]}
for record in records:
feature = {"type":"Feature","properties":{},"geometry":{"type":"Point"}}
feature["id"] = record._rid
feature["properties"]["name"] = record.title
feature["properties"]["price"] = record.price
feature["properties"]["priceNorm"] = remap(record.price, minPrice, maxPrice, 0, 1)
feature["geometry"]["coordinates"] = [record.latitude, record.longitude]
output["features"].append(feature)
if analysis != "interpolation" and analysis != "heatmap":
q.put('idle')
return json.dumps(output)
q.put('starting analysis...')
output["analysis"] = []
numW = int(math.floor(w/cell_size))
numH = int(math.floor(h/cell_size))
grid = []
for j in range(numH):
grid.append([])
for i in range(numW):
grid[j].append(0)
#USE CONDITIONAL ALONG WITH UI INFORMATION RECEIVED FROM THE CLIENT TO SWITCH
#BETWEEN HEAT MAP AND INTERPOLATION ANALYSIS
## HEAT MAP IMPLEMENTATION
if analysis == "heatmap":
for record in records:
pos_x = int(remap(record.longitude, lng1, lng2, 0, numW))
pos_y = int(remap(record.latitude, lat1, lat2, numH, 0))
# USE INFORMATION RECEIVED FROM CLIENT TO CONTROL SPREAD OF HEAT MAP
for j in range(max(0, (pos_y-spread)), min(numH, (pos_y+spread))):
for i in range(max(0, (pos_x-spread)), min(numW, (pos_x+spread))):
grid[j][i] += 2 * math.exp((-point_distance(i,j,pos_x,pos_y)**2)/(2*(spread/2)**2))
q.put('idle')
## MACHINE LEARNING IMPLEMENTATION
if analysis == "interpolation":
featureData = []
targetData = []
for record in records:
featureData.append([record.latitude, record.longitude])
targetData.append(record.price)
X = np.asarray(featureData, dtype='float')
y = np.asarray(targetData, dtype='float')
breakpoint = int(numListings * .7)
print "length of dataset: " + str(numListings)
print "length of training set: " + str(breakpoint)
print "length of validation set: " + str(numListings-breakpoint)
# create training and validation set
X_train = X[:breakpoint]
X_val = X[breakpoint:]
y_train = y[:breakpoint]
y_val = y[breakpoint:]
#mean 0, variance 1
scaler = preprocessing.StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
mse_min = 10000000000000000000000
for C in [.01, 1, 100, 10000, 1000000]:
for e in [.01, 1, 100, 10000, 1000000]:
for g in [.01, 1, 100, 10000, 1000000]:
q.put("training model: C[" + str(C) + "], e[" + str(e) + "], g[" + str(g) + "]")
model = svm.SVR(C=C, epsilon=e, gamma=g, kernel='rbf', cache_size=2000)
model.fit(X_train_scaled, y_train)
y_val_p = [model.predict(i) for i in X_val]
mse = 0
for i in range(len(y_val_p)):
mse += (y_val_p[i] - y_val[i]) ** 2
mse /= len(y_val_p)
if mse < mse_min:
mse_min = mse
model_best = model
C_best = C
e_best = e
g_best = g
q.put("best model: C[" + str(C_best) + "], e[" + str(e_best) + "], g[" + str(g_best) + "]")
for j in range(numH):
for i in range(numW):
lat = remap(j, numH, 0, lat1, lat2)
lng = remap(i, 0, numW, lng1, lng2)
testData = [[lat, lng]]
X_test = np.asarray(testData, dtype='float')
X_test_scaled = scaler.transform(X_test)
grid[j][i] = model_best.predict(X_test_scaled)
grid = normalizeArray(grid)
offsetLeft = (w - numW * cell_size) / 2.0
offsetTop = (h - numH * cell_size) / 2.0
for j in range(numH):
for i in range(numW):
newItem = {}
newItem['x'] = offsetLeft + i*cell_size
newItem['y'] = offsetTop + j*cell_size
newItem['width'] = cell_size-1
newItem['height'] = cell_size-1
newItem['value'] = grid[j][i]
output["analysis"].append(newItem)
return json.dumps(output)
if __name__ == "__main__":
app.run(host='0.0.0.0',port=5000,debug=True,threaded=True)