forked from austinlasseter/flying-dog-beers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_p1.py
492 lines (429 loc) · 17.7 KB
/
app_p1.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# -*- coding: utf-8 -*-
import sys
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
import numpy as np
#import glob
from datetime import datetime
import dash_table as dt
from plotly import graph_objs as go
import plotly.plotly as py
from plotly.graph_objs import *
from uszipcode import Zipcode
from uszipcode import SearchEngine
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="Movin\'OnUp") #"specify_your_app_name_here"
#data from US CENSUS
search = SearchEngine(simple_zipcode=True)
app = dash.Dash(__name__) #__name__, external_stylesheets=external_stylesheets
app.title = 'Movin\'OnUP!'
# API keys and datasets
mapbox_access_token = 'pk.eyJ1IjoiZHluZGwiLCJhIjoiY2p4M2gyYm9wMDBzbDRhbmxzYWMya2tvZCJ9.xWu9JsGNMrFmk6yiydXlqw'
print('so far so good!')
# loading data
path = "https://raw.githubusercontent.com/dyndl/Movin-On-UP/master/MOU_data/"
#path = r'C:\DRO\DCL_rawdata_files' # use your path
#all_files = glob.glob(path + "ZILLOW2/Z*_MSPAHforecast.csv")
print(path + 'ZILLOW2/Z*_MSPAHforecast.csv')
# print('-----------------------------------------')
# filenames = [filename for filename in all_files]
# print(filenames)
li = []
LA_zipcodes = ['90014','90013','90017','90006','90029','90071','90037','90008',\
'90018','90023','90031','90822','90731','90028','90019','90025',\
'90049','91406','91405','91335','90065','90062','90003','90043',\
'90291','90304','91605','90020','91356','91364','91303','90057',\
'90015','90007','90033','90035','90012','91607','90004','91604',\
'90059','90010','90064','90011','91331','90061','90032','90044',\
'90502','90063','90744','90292','91340','91402','91324','90021',\
'90079','90026','90016','90710','90248','90732','90717','90046',\
'90038','90069','90048','90036','90211','90404','90024','90067',\
'90077','90034','90402','91423','90210','91403','91411','91401',\
'91436','91316','91343','91325','91201','90041','90042','91205',\
'90039','91204','90305','90047','90001','90058','90066','90293',\
'90094','90056','90045','90405','90230','91602','91608','91601',\
'91606','91214','91352','91040','91504','91342','91306','91326',\
'91344','91304','90002','90247','91345','90005','91367','90027',\
'91030','90501','90068','90272','90232','91505','91206','91311',\
'90301','90212','91307','90804','90740','90403','91301','90814','90302']
LA_other_zipcodes = ['90009','90030','90050','90051','90052','90053','90054',\
'90055','90060','90070','90072','90074','90075','90076','90078','90079','90080',\
'90081','90082','90083','90084','90085','90086','90087','90088','90090','90091',\
'90093','90095','90096','90099','90134','90189']
#print(LA_other_zipcodes)
count = 0
for filename in all_files:
LA_zipcode = filename.split('_', 2)[1].split('Z')[2]
if LA_zipcode not in LA_other_zipcodes:
df = pd.read_csv(filename, index_col=None, header=0)
df['zipcode'] = LA_zipcode
lat_lon = search.by_zipcode(LA_zipcode).values()[7:9]
df['Latitude'] = lat_lon[0]
df['Longitude'] = lat_lon[1]
li.append(df)
count+=1
# count = 0
# for LA_zipcode in LA_zipcodes:
# #LA_zipcode = filename.split('_', 2)[1].split('Z')[2]
# #print(LA_zipcode)
# if LA_zipcode not in LA_other_zipcodes:
# filename = path + "ZILLOW2/Z"+LA_zipcode+"_MSPAHforecast.csv"
# df = pd.read_csv(filename, index_col=None, header=0)
# df['zipcode'] = LA_zipcode
# lat_lon = search.by_zipcode(LA_zipcode).values()[7:9]
# df['Latitude'] = lat_lon[0]
# df['Longitude'] = lat_lon[1]
# li.append(df)
# count+=1
#print(df)
userforecast = pd.concat(li, axis=0, ignore_index=True)
forecast_data = userforecast[['ds','zipcode','trend','trend_lower','trend_upper','Latitude','Longitude']]
#data = pd.DataFrame(columns = ['Rankings', 'Zipcode', 'Trend','Avg. ROI','ROI Rank' ,'Wealth Rank','Latitude','Longitude'])
ranked_data = pd.DataFrame(columns = ['Rankings', 'Zipcode', 'Trend','Avg. ROI','ROI Rank' ,'Wealth Rank','Latitude','Longitude'])
#set initial values for min and max budget price
minMSP = 250000
maxMSP = 600000
colors = {
'background': '#111111',
'text': '#7FDBFF',
'light_text': '#D8D8D8'
}
# Boostrap CSS.
# app.css.append_css({'external_url': 'https://codepen.io/amyoshino/pen/jzXypZ.css'})
app.css.append_css({'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'})
app.config['suppress_callback_exceptions']=True
# Layouts
layout_table = dict(
autosize=True,
height=500,
font=dict(color="#191A1A"),
titlefont=dict(color="#191A1A", size='14'),
margin=dict(
l=45,
r=0,
b=5,
t=0
),
hovermode="closest",
plot_bgcolor='#fffcfc',
paper_bgcolor='#fffcfc',
legend=dict(font=dict(size=10), orientation='h'),
)
layout_table['font-size'] = '12'
layout_table['margin-top'] = '20'
layout_map = dict(
autosize=True,
height=500,
font=dict(color="#191A1A"),
titlefont=dict(color="#191A1A", size='14'),
margin=dict(
l=-20,
r=0,
b=0,
t=0
),
hovermode="closest",
plot_bgcolor='#fffcfc',
paper_bgcolor='#fffcfc',
legend=dict(font=dict(size=10), orientation='h'),
title='LA Home Location Desirables by Zipcode',
mapbox=dict(
accesstoken=mapbox_access_token,
style="light",
center=dict(
lon=-118.2437,
lat=34.0522
),
zoom=10,
)
)
# functions
def gen_map(map_data):
# groupby returns a dictionary mapping the values of the first field
# 'classification' onto a list of record dictionaries with that
# classification value.
return {
"data": [{
"type": "scattermapbox",
"lat": list(map_data['Latitude']),
"lon": list(map_data['Longitude']),
"hoverinfo": "text",
"hovertext": [["Name: {} <br>Type: {} <br>Provider: {}".format(i,j,k)]
for i,j,k in zip(map_data['Name'], map_data['Type'],map_data['Provider'])],
"mode": "markers",
"name": list(map_data['Name']),
"marker": {
"size": 6,
"opacity": 0.7
}
}],
"layout": layout_map
}
#app calls
#__________________________________
#__________________________________
app.layout = html.Div([
#headers and initial user input
#------------------------------
html.Div([
html.Div([
html.H1(children='Movin\'OnUP!',
style={
'textAlign': 'left',
'color': colors['text']},
className = "nine columns"
),
html.Img(
src="https://assets-global.website-files.com/575a31d2ce5d01dc7a20de45/575a31d2ce5d01dc7a20ded3_insight_logo.png",
className='three columns',
style={
'height': '14%',
'width': '14%',
'float': 'right',
'position': 'relative',
'margin-top': 20,
'margin-right': 20
},
),
html.Div(children='''A home pre-search optimization app''',
style={
'margin-top': 0,
'margin-right': 15,
'margin-bottom': 15,
'textAlign': 'left',
'color': colors['text']
},
className = 'nine columns'
),
], className = "row"
)
]),
#Right side
#---------
html.Div([
html.Div([
dcc.Input(id='minMSPinput', type='number', value=minMSP,
placeholder='Enter min. price',
style={
'height': '20%',
'width': '42%',
'float': 'left',
'textAlign': 'left'
}),
dcc.Input(id='maxMSPinput', type='number', value=maxMSP,
placeholder='Enter max. price',
style={
'height': '20%',
'width': '42%',
'float': 'left',
'textAlign': 'left'
}),
html.Div(children='Min. Home Price <---> Max. Home Price (USD)',
style={
# 'float':'left',
'fontSize': 16,
'margin-top': 0,
'margin-right': 45,
'margin-bottom': 0,
'textAlign': 'left'
}),
dcc.Graph(id='map-graph',
animate=True,
style={'margin-top': 0,'margin-bottom': 0,'padding-bottom':0}
),
html.Div(id='Yearly-outlook-output-container',style={'fontSize': 16,'margin-top': 0,'padding-top':0,'textAlign': 'center'},
children='Predicted Future Outlook'
),
dcc.Slider(
id='Yearly_outlook',
min=0,
max=10,
value=0,
marks={
0: {'label': ' Now', 'style': {'color': '#77b0b1'}},
1: {'label': '1 Year'},
2: {'label': ''},
3: {'label': ''},
4: {'label': ''},
5: {'label': '5 Years'},
6: {'label': ''},
7: {'label': ''},
8: {'label': ''},
9: {'label': ''},
10: {'label': '10 Years', 'style': {'color': '#f50'}}
},
),
], className = "five columns"
),
html.Div([
html.H2(id='Desirables-Value-header',style={'fontSize': 40,'margin-top': 0,'padding-bottom':0,'textAlign': 'center','color':'#f50'},
children ='Desirables'
),
html.Div(id='Wealth-Value-output-container',style={'fontSize': 16,'margin-top': 0,'padding-top':0,'textAlign': 'center'},
children='Local Economics'
),
dcc.Slider(
id='Wealth_Value',
min=0,
max=4,
value=2,
marks={
0: {'label': 'Not Important', 'style': {'color': '#77b0b1'}},
1: {'label': 'Less Important'},
2: {'label': 'Important'},
3: {'label': 'Very Important'},
4: {'label': 'Extremely Important', 'style': {'color': '#f50'}}
}
),
html.Div(id='ROI-Value-output-container',style={'fontSize': 16,'margin-top': 0,'padding-top':60,'textAlign': 'center'},
children='Home Apprecitaion'
),
dcc.Slider(
id='ROI_Value',
min=0,
max=4,
value=2,
marks={
0: {'label': 'Not Important', 'style': {'color': '#77b0b1'}},
1: {'label': 'Less Important'},
2: {'label': 'Important'},
3: {'label': 'Very Important'},
4: {'label': 'Extremely Important', 'style': {'color': '#f50'}}
}
),
html.Div(id='Crime-Value-output-container',style={'fontSize': 16,'margin-top': 0,'padding-top':60,'textAlign': 'center'},
children='Public Safety'
),
dcc.Slider(
id='Crime_Value',
min=0,
max=4,
value=2,
marks={
0: {'label': 'Not Important', 'style': {'color': '#77b0b1'}},
1: {'label': 'Less Important'},
2: {'label': 'Important'},
3: {'label': 'Very Important'},
4: {'label': 'Extremely Important', 'style': {'color': '#f50'}}
}
),
],className = "three columns"
),
html.Div([
dt.DataTable(
id='datatable',
data=ranked_data.to_dict('records'),
columns=[{'id': c, 'name': c} for c in ranked_data.columns],
#title='Best Locations in LA',
row_selectable='multi',
# filtering=True,
sorting=True,
virtualization=True
# pagination_mode='fe',
# # #selected_rows=[],
# pagination_settings={
# "current_page": 0,
# "page_size": 10
# },
)
],
style = layout_table,
className="four columns"
)
],className= "row"
),
html.Div([
html.P('Developed by Duane M. Lee, Ph.D - ', style = {'display': 'inline'}
),
html.A('duane.m.lee@gmail.com', href = 'mailto:duane.m.lee@gmail.com'
)
], className = "twelve columns",
style = {'fontSize': 18, 'padding-top': 25,'margin-top':20,'textAlign':'right'}
)
],className = 'ten columns offset-by-one'
)
#callback section
#-------------------
# @app.callback(
# Output('map-graph', 'figure'),
# [Input('datatable', 'data'),
# Input('datatable', 'selected_rows')])
# def map_selection(data, selected_rows):
# aux = pd.DataFrame(rows)
# temp_df = aux.ix[selected_rows, :]
# if len(selected_rows) == 0:
# return gen_map(aux)
# return gen_map(temp_df)
@app.callback(
Output('datatable', 'data'),
[Input('minMSPinput', 'value'),
Input('maxMSPinput', 'value'),
Input('Yearly_outlook', 'value'),
Input('Wealth_Value', 'value'),
])
def update_selected_row_indices(minMSP, maxMSP,year,wealth):
global forecast_data, ranked_data
map_aux = forecast_data.copy()
ranked_aux = ranked_data.copy()
now = datetime.today().strftime('%Y')
now0 = datetime.today()
nowp = datetime.today().strftime('%Y-%m-%d')
selected_year = np.int(now) + year
#print(selected_year,now)
projected = now0.replace(year=selected_year).strftime('%Y-%m-%d')
#print(selected_year,now,projected)
map_aux['ds'] = pd.to_datetime(map_aux['ds'])
five_years = np.int(now) + 5
projected_5yrs = now0.replace(year=five_years).strftime('%Y-%m-%d')
five_year_projection_arr = map_aux.ds.dt.strftime('%Y-%m-%d') == projected_5yrs
now_projection_arr = map_aux.ds.dt.strftime('%Y-%m-%d') == nowp
a = map_aux.zipcode.loc[five_year_projection_arr]
b = map_aux.zipcode.loc[now_projection_arr]
c = [a] + [b]
c0 = [y for x in c for y in x]
c1 = pd.DataFrame({'mergedzips':c0})
nonmatchedarr = c1.drop_duplicates(keep=False)
arr = [np.where(map_aux.zipcode.loc[now_projection_arr] == nonmatchedarr.mergedzips.values[i])[0][0] for i in range(len(nonmatchedarr))]
adjust_arr = []
i = 0
j = 0
for i in range(len(now_projection_arr)):
if now_projection_arr.values[i] == True:
j+=1
if j in arr:
adjust_arr.append(i)
now_projection_arr[adjust_arr] = False
# User weighting scores
#----------------------
#ROI score
avgROI = (map_aux.trend.loc[five_year_projection_arr].values - map_aux.trend.loc[now_projection_arr].values)/(5 * map_aux.trend.loc[now_projection_arr].values) * 100
ROIraw = (map_aux.trend.loc[five_year_projection_arr].values - map_aux.trend.loc[now_projection_arr].values)/(5 * map_aux.trend.loc[now_projection_arr].values) \
* np.exp((map_aux.trend_lower.loc[five_year_projection_arr].values - map_aux.trend_lower.loc[now_projection_arr].values)/(5 * map_aux.trend_lower.loc[now_projection_arr].values))
ROIraw[ROIraw < 0] = 0
ROIscore = ROIraw/np.max(ROIraw) * 100
#Wealth score
med_income = 50000
wealth_factors = ( (med_income/np.max(med_income))**2 + (map_aux['trend'].loc[now_projection_arr].values/np.max(map_aux['trend'].loc[now_projection_arr].values))**2 )
wealth_score = np.sqrt( wealth_factors/2 ) * 100
overall_score = np.sqrt((ROIscore**2 + wealth_score**2)/2) * 100
overall_rank = np.argsort(overall_score)
projected_data = map_aux.loc[map_aux.ds.dt.strftime('%Y-%m-%d') == projected]
#ranked table
ranked_aux['Rankings'] = overall_rank + 1
ranked_aux['Zipcode'] = map_aux['zipcode'].loc[now_projection_arr].values
ranked_aux['Trend'] = map_aux['trend'].loc[now_projection_arr].values
ranked_aux['Avg. ROI'] = avgROI
ranked_aux['ROI Rank'] = ROIscore
ranked_aux['Wealth Rank'] = wealth_score
ranked_aux['Latitude'] = map_aux['Latitude'].loc[now_projection_arr].values
ranked_aux['Longitude'] = map_aux['Longitude'].loc[now_projection_arr].values
#ranked data within budget
ranked_budget_data = ranked_aux[np.logical_and(ranked_aux['Trend'] >= minMSP,ranked_aux['Trend'] < maxMSP)]
ranked_budget_data.sort_values('Rankings',inplace = True)
ranked_data = ranked_budget_data.to_dict('records')
return ranked_data
if __name__ == "__main__":
app.run_server(debug=False)