-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_fxns.py
executable file
·455 lines (374 loc) · 15.2 KB
/
app_fxns.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
#import dash
import dash_core_components as dcc
import dash_html_components as html
#from dash.dependencies import Input, Output, State
#import dash_table
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from plotly.colors import n_colors
#import warnings
#import sys
#import re
import numpy as np
#from scipy import stats
#import statsmodels.api as sm
#import urllib
#from urllib.request import urlopen
#import json
#import base64
#import io
#import json
#import ast
#import time
#import numpy.polynomial.polynomial as poly
#from statsmodels.regression.linear_model import OLS
#from statsmodels.tools import add_constant
def description_card1():
"""
:return: A Div containing dashboard title & descriptions.
"""
return html.Div(
id="description-card1",
children=[
html.H3("Predicting crimes in Chicago", style={'textAlign': 'left', 'color': '#3385ff'}),
html.P("This tool uses over 7.2 million crimes committed from 2001 to present. " +
" It allows the user to build profiles for specific crimes. It then displays " +
" the chance of that crime profile happening across Chicago. " +
" The app tests its own accuracy by predicting where crimes" +
" fitting the profile have occurred in 2020 and 2021.",
style={
'textAlign': 'left',
}),
html.P("This tool crunches a lot of data to find its predictions. Updating takes a few seconds.",
style={
'textAlign': 'left',
}),
],
)
def description_card12():
"""
:return: A Div containing dashboard title & descriptions.
"""
return html.Div(
id="description-card12",
children=[
html.H3("Examining crimes in Chicago throughout 2020 and 2021",
style={'textAlign': 'left', 'color': '#3385ff'}),
html.P("This tool allows users to examine crimes committed in Chicago from " +
"Jan 1st 2020 to present.",
style={
'textAlign': 'left',
}),
html.P("This tool crunches a lot of data. Updating takes a few seconds.",
style={
'textAlign': 'left',
}),
],
)
def description_card2():
"""
:return: A Div containing dashboard title & descriptions.
"""
return html.Div(
id="description-card2",
children=[
html.H5("Comparisons between Loshiko, City of Chicago, and Neighborhood Scout", style={
'textAlign': 'left',
}),
html.H6('Our measure of app quality', style={'textAlign': 'left', 'color': '#3385ff'}),
dcc.Markdown('''
We award a point for whether an app satisfies each of 10 criteria.
These criteria do not include features that all apps have.
For example, if each app provides a map, then providing a map
is not a useful criterion for comparison. An app's score can range
between 0 and 10.
'''),
#html.Br(),
],
)
def comparison_table():
cr = ['The app uses data from the current year',
'The app actually maps crimes',
'The app allows users to filter crime data',
'The app provides useful statistics',
'The app makes predictions',
'The map includes useful hover-over information',
'The app provides trends across time',
'The app allows users to download data',
'The app is easy to use, not complicated or cumbersome',
'The app provides text summaries of data insights']
#colors = n_colors('rgb(255, 200, 200)', 'rgb(200, 0, 0)', 2, colortype='rgb')
lo = ['<b>Yes</b>', '<b>Yes</b>', '<b>Yes</b>', '<b>Yes</b>', '<b>Yes</b>',
'<b>Yes</b>', '<b>No</b>', '<b>No</b>', '<b>Yes</b>', '<b>Yes</b>']
lo_clrs = []
for l in lo:
if l == '<b>Yes</b>':
lo_clrs.append('#9ae59a')
else:
lo_clrs.append('#ffb3b3')
ch = ['<b>Yes</b>', '<b>Yes</b>', '<b>Yes</b>', '<b>No</b>', '<b>No</b>',
'<b>No</b>', '<b>No</b>', '<b>Yes</b>', '<b>No</b>', '<b>No</b>']
ch_clrs = []
for l in ch:
if l == '<b>Yes</b>':
ch_clrs.append('#9ae59a')
else:
ch_clrs.append('#ffb3b3')
ns = ['<b>No</b>', '<b>No</b>', '<b>No</b>', '<b>Yes</b>', '<b>Yes</b>',
'<b>No</b>', '<b>No</b>', '<b>No</b>', '<b>No</b>', '<b>Yes</b>']
ns_clrs = []
for l in ns:
if l == '<b>Yes</b>':
ns_clrs.append('#9ae59a')
else:
ns_clrs.append('#ffb3b3')
figure = go.Figure(data=[go.Table(
columnwidth = [80,40,40,40],
header=dict(
values=['<b>Criterion</b>',
'<b>Loshiko (Score = 8)</b>',
'<b>City of Chicago (Score = 4)</b>',
'<b>Neighborhood Scout (Score = 3)</b>'],
#line_color='black',
#fill_color='white',
align='center',
font = dict(color='black', size=14)
),
cells=dict(
values=[cr, lo, ch, ns],
#line_color='black',
fill_color=[['#f2f2f2']*10,
lo_clrs, ch_clrs, ns_clrs],
align=['left', 'center', 'center', 'center'],
font=dict(color=['black', 'white', 'white', 'white'], size=14),
height=45,
))
])
figure.update_layout(
#coloraxis_showscale=False,
#autosize=True,
#showlegend=False,
#hovermode='closest',
#mapbox_style="light",
height=502,
margin={"r":0,"t":0,"l":0,"b":0},
)
return figure
def replace_fields(crimes_df):
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['AIRCRAFT',
'AIRPORT BUILDING NON-TERMINAL - NON-SECURE AREA',
'AIRPORT BUILDING NON-TERMINAL - SECURE AREA',
'AIRPORT EXTERIOR - NON-SECURE AREA',
'AIRPORT EXTERIOR - SECURE AREA',
'AIRPORT PARKING LOT',
'AIRPORT TERMINAL LOWER LEVEL - NON-SECURE AREA',
'AIRPORT TERMINAL LOWER LEVEL - SECURE AREA',
'AIRPORT TERMINAL MEZZANINE - NON-SECURE AREA',
'AIRPORT TERMINAL UPPER LEVEL - NON-SECURE AREA',
'AIRPORT TERMINAL UPPER LEVEL - SECURE AREA',
'AIRPORT TRANSPORTATION SYSTEM (ATS)',
'AIRPORT VENDING ESTABLISHMENT',
], 'AIRPORT/AIRCRAFT')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['CHA APARTMENT',
'CHA BREEZEWAY',
'CHA ELEVATOR',
'CHA GROUNDS',
'CHA HALLWAY',
'CHA HALLWAY / STAIRWELL / ELEVATOR',
'CHA HALLWAY/STAIRWELL/ELEVATOR',
'CHA LOBBY',
'CHA PARKING LOT',
'CHA PARKING LOT / GROUNDS',
'CHA PARKING LOT/GROUNDS',
'CHA PLAY LOT',
'CHA STAIRWELL',
], 'CHA Property')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['CHURCH',
'CHURCH / SYNAGOGUE / PLACE OF WORSHIP',
'CHURCH PROPERTY',
'CHURCH/SYNAGOGUE/PLACE OF WORSHIP',
], 'PLACE OF WORSHIP')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['SCHOOL - PRIVATE BUILDING',
'SCHOOL - PRIVATE GROUNDS',
'SCHOOL - PUBLIC BUILDING',
'SCHOOL - PUBLIC GROUNDS',
'SCHOOL YARD',
'SCHOOL, PRIVATE, BUILDING',
'SCHOOL, PRIVATE, GROUNDS',
'SCHOOL, PUBLIC, BUILDING',
'SCHOOL, PUBLIC, GROUNDS',
'PUBLIC GRAMMAR SCHOOL',
'PUBLIC HIGH SCHOOL',
], 'SCHOOL')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['VACANT LOT',
'VACANT LOT / LAND',
'VACANT LOT/LAND'], 'VACANT LOT')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['RESIDENCE',
'APARTMENT',
'RESIDENCE - GARAGE',
'RESIDENCE - PORCH / HALLWAY',
'RESIDENCE - YARD (FRONT / BACK)',
'RESIDENCE PORCH/HALLWAY',
'RESIDENCE-GARAGE',
'RESIDENTIAL YARD (FRONT/BACK)',
'YARD', 'PORCH', 'APARTMENT',
'ROOMING HOUSE',
'DRIVEWAY',
'GARAGE',
'HOUSE',
'BASEMENT',
'DRIVEWAY - RESIDENTIAL'], 'RESIDENTIAL')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['HOTEL',
'HOTEL / MOTEL',
'MOTEL',
], 'HOTEL/MOTEL')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['LAKEFRONT / WATERFRONT / RIVERBANK',
], 'LAKEFRONT/WATERFRONT/RIVERBANK')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['HOSPITAL',
'HOSPITAL BUILDING/GROUNDS',
'HOSPITAL BUILDING / GROUNDS',
], 'HOSPITAL')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['MEDICAL / DENTAL OFFICE',
], 'MEDICAL/DENTAL OFFICE')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['GAS STATION DRIVE/PROP.',
], 'GAS STATION')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['FACTORY',
'FACTORY / MANUFACTURING BUILDING',
'FEDERAL BUILDING',
], 'FACTORY/MANUFACTURING BUILDING')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['GOVERNMENT BUILDING',
'GOVERNMENT BUILDING / PROPERTY',
], 'GOVERNMENT BUILDING/PROPERTY')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['CTA "L" PLATFORM',
'CTA "L" TRAIN',
'CTA BUS',
'CTA BUS STOP',
'CTA GARAGE / OTHER PROPERTY',
'CTA PARKING LOT / GARAGE / OTHER PROPERTY',
'CTA PLATFORM',
'CTA PROPERTY',
'CTA STATION',
'CTA SUBWAY STATION',
'CTA TRACKS - RIGHT OF WAY',
'CTA TRAIN',
], 'CTA PROPERTY')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['COLLEGE / UNIVERSITY - GROUNDS',
'COLLEGE / UNIVERSITY - RESIDENCE HALL',
'COLLEGE/UNIVERSITY GROUNDS',
'COLLEGE/UNIVERSITY RESIDENCE HALL',
], 'COLLEGE/UNIVERSITY')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['APPLIANCE STORE',
'ATHLETIC CLUB',
'AUTO',
'BAR OR TAVERN',
'BARBERSHOP',
'BARBER SHOP/BEAUTY SALON',
'BOWLING ALLEY',
'CAR WASH',
'CLEANING STORE',
'CLUB',
'COMMERCIAL / BUSINESS OFFICE',
'DEPARTMENT STORE',
'DRUG STORE',
'GROCERY FOOD STORE',
'MOVIE HOUSE/THEATER',
'MOVIE HOUSE / THEATER',
'NEWSSTAND',
'POOL ROOM',
'POOLROOM',
'RESTAURANT',
'RETAIL STORE',
'SMALL RETAIL STORE',
'TAVERN',
'TAVERN/LIQUOR STORE',
'YMCA',
'TAVERN / LIQUOR STORE',
'PAWN SHOP',
'AUTO / BOAT / RV DEALERSHIP',
'CONVENIENCE STORE',
'BANQUET HALL',
'FUNERAL PARLOR',
'LAUNDRY ROOM',
'LIQUOR STORE',
'LIVERY AUTO',
'LIVERY STAND OFFICE',
'LOADING DOCK',
'OFFICE',
'WAREHOUSE',
], 'BUSINESS')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['HIGHWAY / EXPRESSWAY',
'EXPRESSWAY EMBANKMENT',
], 'HIGHWAY/EXPRESSWAY')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['DELIVERY TRUCK', 'VEHICLE NON-COMMERCIAL',
'VEHICLE-COMMERCIAL', 'VEHICLE - COMMERCIAL',
'VEHICLE - COMMERCIAL: ENTERTAINMENT / PARTY BUS',
'VEHICLE - COMMERCIAL: TROLLEY BUS',
'VEHICLE - DELIVERY TRUCK',
'VEHICLE - OTHER RIDE SERVICE',
'VEHICLE - OTHER RIDE SHARE SERVICE (E.G., UBER, LYFT)',
'VEHICLE - OTHER RIDE SHARE SERVICE (LYFT, UBER, ETC.)',
'VEHICLE NON-COMMERCIAL',
'VEHICLE-COMMERCIAL',
'VEHICLE-COMMERCIAL - ENTERTAINMENT/PARTY BUS',
'VEHICLE-COMMERCIAL - TROLLEY BUS',
'TAXI CAB',
'TAXICAB',
'TRUCK',
'AUTO',
], 'VEHICLE')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['BOAT / WATERCRAFT',
], 'BOAT/WATERCRAFT')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['BANK',
'CREDIT UNION',
'CURRENCY EXCHANGE',
'SAVINGS AND LOAN',
], 'FINANCIAL INST.')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['SPORTS ARENA / STADIUM',
], 'SPORTS ARENA/STADIUM')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['POLICE FACILITY / VEHICLE PARKING LOT',
'POLICE FACILITY/VEH PARKING LOT',
], 'POLICE FACILITY')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['PARKING LOT',
'PARKING LOT / GARAGE (NON RESIDENTIAL)',
'PARKING LOT/GARAGE(NON.RESID.)',
], 'PARKING LOT')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['OTHER RAILROAD PROP / TRAIN DEPOT',
'OTHER RAILROAD PROPERTY / TRAIN DEPOT',
'RAILROAD PROPERTY',
], 'RAILROAD PROPERTY')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['OTHER (SPECIFY)',
'VESTIBULE',
'OTHER COMMERCIAL TRANSPORTATION',
], 'OTHER')
crimes_df['Location Description'] = crimes_df['Location Description'].replace(
['NURSING / RETIREMENT HOME',
'NURSING HOME',
'NURSING HOME/RETIREMENT HOME',
], 'NURSING HOME')
return crimes_df