-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
528 lines (395 loc) · 16.6 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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# This app is used to format the data from the csv files into a format that can be used by the database
import argparse
import format
import os
import pandas as pd
import pytz
import random
import string
import yaml
from tqdm import tqdm
from typing import Union
###########
# GLOBALS #
###########
# Contains the configuration for the app
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config/config.yaml')
# Contains specific edits to the raw data
EDITS_PATH = os.path.join(os.path.dirname(__file__), 'data/raw/edits.yaml')
# Folder to write debug artifacts
OUTPUT_DIR = os.path.join(os.path.dirname(__file__), 'debug')
# Name of the file to write the Restaurant table debug artifacts to
RESTAURANT_MATCH_PATH = os.path.join(OUTPUT_DIR, 'Restaurant_match.csv')
# Statistics about the data conversion
G_stats = {}
primaryKeyLength = 16
#############
# Functions #
#############
def matchRestaraunt(row: pd.Series, restaurants: list[dict]) -> Union[dict, None]:
'''
Check if a restaurant already exists in the list of restaurants. Match by street address.
'''
for restaurant in restaurants:
if restaurant['StreetAddress'] == row['StreetAddress']:
return restaurant
return None
def mergeRestaurants(match: dict, row: pd.Series) -> None:
'''
Merge strategy is that values should be exactly the same. If they are not
then raise an exception.
'''
unmatched_keys = []
for key, value in match.items():
# No need to check ID wiht Restaurant ID since it is the same
if key == 'ID':
continue
elif key == 'Zipcode':
if value != row['Postcode']:
unmatched_keys.append(key)
continue
else:
if value != row[key]:
unmatched_keys.append(key)
if len(unmatched_keys) > 0:
raise Exception(f'Unmatched keys: {unmatched_keys}')
def fillRestaurantTable(tables: dict[str, pd.DataFrame], datasets: dict[str, pd.DataFrame], debug=False) -> None:
'''
Fill the Restaurant table
'''
if 'OpenRestaurantInspections' in datasets:
if debug:
# Remove Restaurant_match.csv if it exists using try and except
try:
os.remove(RESTAURANT_MATCH_PATH)
except:
pass
df = datasets['OpenRestaurantInspections']
# Allocate a column for RestaurantID
df['RestaurantID'] = ''
# Organize restaurants by zip code for quicker matching
by_zip = {}
# Iterate through rows and add to table if restarant does not exist
for index, row in tqdm(df.iterrows()):
if row['Postcode'] not in by_zip:
by_zip[row['Postcode']] = []
match = matchRestaraunt(row, by_zip[row['Postcode']])
if match is not None:
df.loc[index, 'RestaurantID'] = match['ID']
try:
mergeRestaurants(match, row)
except Exception as e:
print(e)
if debug:
with open('debug/Restaurant_match.csv', 'a') as file:
formatted_restaurant = {
'ID': match['ID'],
'Name': row['Name'],
'LegalBusinessName': row['LegalBusinessName'],
'StreetAddress': row['StreetAddress'],
'Borough': row['Borough'],
'Zipcode': row['Postcode'],
'Latitude': row['Latitude'],
'Longitude': row['Longitude'],
'CommunityBoard': row['CommunityBoard'],
'CouncilDistrict': row['CouncilDistrict'],
'CensusTract': row['CensusTract'],
'BIN': row['BIN'],
'BBL': row['BBL'],
'NTA': row['NTA']
}
file.write(e.args[0] + '\n')
pd.DataFrame([match, formatted_restaurant]).to_csv(
file, mode='a', index=False)
file.write('\n')
else:
print('Restaurant does not match')
exit()
continue
else:
id = str(generateRandomBits(64))
by_zip[row['Postcode']].append({
'ID': id,
'Name': row['Name'],
'LegalBusinessName': row['LegalBusinessName'],
'StreetAddress': row['StreetAddress'],
'Borough': row['Borough'],
'Zipcode': row['Postcode'],
'Latitude': row['Latitude'],
'Longitude': row['Longitude'],
'CommunityBoard': row['CommunityBoard'],
'CouncilDistrict': row['CouncilDistrict'],
'CensusTract': row['CensusTract'],
'BIN': row['BIN'],
'BBL': row['BBL'],
'NTA': row['NTA']
})
df.loc[index, 'RestaurantID'] = id
restaurants = [place for places in by_zip.values() for place in places]
tables['Restaurant'] = pd.concat(
[tables['Restaurant'], pd.DataFrame(restaurants)], ignore_index=True)
def fillSidewalkInspectionTable(tables: dict[str, pd.DataFrame], df: pd.DataFrame) -> None:
'''
Fill the Restaurant table
'''
if 'OpenRestaurantInspections' in datasets:
df = datasets['OpenRestaurantInspections']
to_add = []
for _, row in tqdm(df.iterrows()):
to_add.append({
'ID': str(generateRandomBits(64)),
'RestaurantID': row['RestaurantID'],
'InspectedOn': row['InspectedOn'],
'SidewayCompliant': row['SidewayCompliant'],
'SkippedReason': row['SkippedReason'],
'AgencyCode': row['AgencyCode'],
})
tables['SidewalkInspection'] = pd.concat(
[tables['SidewalkInspection'], pd.DataFrame(to_add)], ignore_index=True)
def fillTables(datasets: dict[str, pd.DataFrame], tables: dict[str, pd.DataFrame], debug=False) -> None:
fillRestaurantTable(tables, datasets, debug)
fillSidewalkInspectionTable(tables, datasets)
def editData(dfs: dict[str, pd.DataFrame], verbose=False, debug=False) -> dict[str, pd.DataFrame]:
'''
Make edits to the raw data
'''
# Open the file
with open(EDITS_PATH) as file:
raw_edits = yaml.load(file, Loader=yaml.FullLoader)
num_edits = 0
# Iterate through each edit
for edit in raw_edits:
if edit['key'] in dfs:
for row in edit['row']:
if row in dfs[edit['key']].index:
dfs[edit['key']].loc[row, edit['column']] = edit['value']
num_edits += 1
# Test files can be reindexed after edits have been made
for df in dfs.values():
df.reset_index(inplace=True, drop=True)
if verbose:
print(f'\nMade {num_edits} edits to the raw data')
if debug:
for name, df in dfs.items():
df.to_csv(os.path.join(OUTPUT_DIR, f'{name}_edit.csv'))
return dfs
def generateRandomString(length=primaryKeyLength) -> str:
'''
Generate a random string of length characters
'''
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
def generateRandomBits(length: int) -> int:
'''
Generate a random number
'''
return random.randint(0, 2**length-1)
def assignBranchID(df: pd.DataFrame, verbose=False) -> pd.DataFrame:
'''
Assign a unique 16 character BranchID for each unique branch where branch is
determined based on location.
'''
# Create a BranchID for each row
df['BranchID'] = None
if verbose:
print('Assigning BranchIDs to each row')
grouped = tqdm(df.groupby(
['FormattedLegalBusinessName', 'FormattedBusinessAddress'], dropna=True))
else:
grouped = df.groupby(
['FormattedLegalBusinessName', 'FormattedBusinessAddress'], dropna=True)
# Iterate through location address and assign a BranchID
# TODO: Update the groupby function
for _, df_address in grouped:
branch_id = generateRandomString()
for index, _ in df_address.iterrows():
df.loc[index, 'BranchID'] = branch_id
return df
def formatOpenRestaurantApplications(df: pd.DataFrame) -> pd.DataFrame:
'''
Clean, transform and normalize the data from the
OpenRestaurantApplications.csv file
'''
# Rename LegalBusinessName to RawLegalBusinessName
df = df.rename(columns={'Legal Business Name': 'RawLegalBusinessName'})
# Standardize the LegalBusinessName
df = format.standardizeString(
df, {'RawLegalBusinessName': 'FormattedLegalBusinessName'})
return df
def formatOpenRestaurantInspections(df: pd.DataFrame, debug=False) -> pd.DataFrame:
'''
Clean, transform and normalize the data from the
OpenRestaurantInspections.csv file
'''
###########
# Borough #
###########
df['Borough'] = df['Borough'].str.upper()
###################
# Restaurant Name #
###################
df['Name'] = format.normalizeStrings(df['RestaurantName'])
#######################
# Legal Business Name #
#######################
df['LegalBusinessName'] = format.normalizeStrings(df['LegalBusinessName'])
####################
# Business Address #
####################
df['StreetAddress'] = format.normalizeAddress(
df['BusinessAddress'], G_stats)
###############
# InspectedOn #
###############
# Validate that all values in InspectedOn are convertable to a datetime and
# convert the date to UTC
# Timezone in NYC
eastern = pytz.timezone('US/Eastern')
try:
inspected_on = pd.to_datetime(
df['InspectedOn'], format='%m/%d/%Y %I:%M:%S %p', exact=True, errors='raise').dt.tz_localize(eastern)
df['InspectedOn'] = inspected_on.dt.tz_convert(pytz.utc)
except:
print('Cannot convert all values in InspectedOn to a date')
#########################################
# IsRoadwayCompliant/IsSidewayCompliant #
#########################################
# Drop IsSideWalkCompliant and rename IsRoadwayCompliant to IsSidewayCompliant
df = df.drop(columns=['IsSidewayCompliant'])
df = df.rename(columns={'IsRoadwayCompliant': 'SidewayCompliant'})
#################
# SkippedReason #
#################
# Replaced null values with 'N/A'
df['SkippedReason'] = df['SkippedReason'].fillna('NA')
###############
# Agency Code #
###############
# Do nothing. Don't want to fill in null values with 'NA' because it will be
# confusing when 3 and 4 letter codes
#######
# NTA #
#######
df['NTA'] = df['NTA'].str.upper()
if debug:
df.to_csv(os.path.join(
OUTPUT_DIR, 'OpenRestaurantInspections_formatted.csv'))
return df
def formatRestaurantInspections(df: pd.DataFrame) -> pd.DataFrame:
'''
Clean, transform and normalize the data from the RestaurantInspections.csv
file
'''
return df
def assembleTables(datasets: dict[str, pd.DataFrame], debug=False) -> dict[str, pd.DataFrame]:
'''
Assemble the tables from the dataframes
'''
tables = {}
# Create a DataFrame from a list of column names and types
tables['SidewalkInspection'] = pd.DataFrame({
'ID': pd.Series(dtype='string'),
'RestaurantID': pd.Series(dtype='string'),
'InspectedOn': pd.Series(dtype='datetime64[ns, UTC]'),
'SidewayCompliant': pd.Series(dtype='string'),
'SkippedReason': pd.Series(dtype='string'),
'AgencyCode': pd.Series(dtype='string')})
tables['Restaurant'] = pd.DataFrame({
'ID': pd.Series(dtype='string'),
'DBA': pd.Series(dtype='string'),
'Name': pd.Series(dtype='string'),
'LegalBusinessName': pd.Series(dtype='string'),
'StreetAddress': pd.Series(dtype='string'),
'Borough': pd.Series(dtype='string'),
'Zipcode': pd.Series(dtype='int32'),
'FoodServicePermit': pd.Series(dtype='int32'),
'IsPermittedToSellAlcohol': pd.Series(dtype='boolean'),
'SLASerialNumber': pd.Series(dtype='string'),
'SLALicenseType': pd.Series(dtype='string'),
'IsLandmark': pd.Series(dtype='boolean'),
'HasAgreedToLandmarkTerms': pd.Series(dtype='boolean'),
'Latitude': pd.Series(dtype='float64'),
'Longitude': pd.Series(dtype='float64'),
'CommunityBoard': pd.Series(dtype='int32'),
'CouncilDistrict': pd.Series(dtype='int32'),
'CensusTract': pd.Series(dtype='int32'),
'BIN': pd.Series(dtype='int32'),
'BBL': pd.Series(dtype='int32'),
'NTA': pd.Series(dtype='string'),
'CAMIS': pd.Series(dtype='int32'),
'Phone': pd.Series(dtype='string'),
'Cuisine': pd.Series(dtype='string')})
fillTables(datasets, tables, debug)
return tables
if __name__ == '__main__':
argparser = argparse.ArgumentParser(
description='Format the data from the csv files into a format that can be used by the database')
argparser.add_argument('dataset', metavar='dataset', type=str)
argparser.add_argument('--debug', '-d', action='store_true', default=False,
help='Generate debug artifacts in debug/')
argparser.add_argument('--verbose', '-v', action='store_true',
default=False, help='Print verbose output')
args = argparser.parse_args()
# Read the dataset argument and check if it is one of the available datasets
# in the config file
with open(CONFIG_PATH) as file:
config = yaml.load(file, Loader=yaml.FullLoader)
datasets = [dataset['name'] for dataset in config['dataset']]
if args.dataset not in datasets:
print(f'\nInvalid dataset option selected: {args.dataset}\n')
print('Available datasets:')
for dataset in datasets:
print(f'\t- {dataset}')
exit()
else:
files = config['dataset'][datasets.index(args.dataset)]['files']
if args.debug:
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
#################
# Load datasets #
#################
# Test datasets contain an Index column
datasets = {}
for file in files:
if 'OpenRestaurantApplications' in file:
datasets['OpenRestaurantApplications'] = pd.read_csv(file)
if 'Index' in datasets['OpenRestaurantApplications'].columns:
datasets['OpenRestaurantApplications'].set_index(
'Index', inplace=True)
elif 'OpenRestaurantInspections' in file:
datasets['OpenRestaurantInspections'] = pd.read_csv(file)
if 'Index' in datasets['OpenRestaurantInspections'].columns:
datasets['OpenRestaurantInspections'].set_index(
'Index', inplace=True)
elif 'RestaurantInspections' in file:
datasets['RestaurantInspections'] = pd.read_csv(file)
if 'Index' in datasets['RestaurantInspections'].columns:
datasets['RestaurantInspections'].set_index(
'Index', inplace=True)
###############
# Format Rows #
###############
editData(datasets, verbose=args.verbose, debug=args.debug)
if 'OpenRestaurantApplications' in datasets:
datasets['OpenRestaurantApplications'] = formatOpenRestaurantApplications(
datasets['OpenRestaurantApplications'])
if 'OpenRestaurantInspections' in datasets:
datasets['OpenRestaurantInspections'] = formatOpenRestaurantInspections(
datasets['OpenRestaurantInspections'], args.debug)
if 'RestaurantInspections' in datasets:
datasets['RestaurantInspections'] = formatRestaurantInspections(
datasets['RestaurantInspections'])
###################
# Assemble Tables #
###################
tables = assembleTables(datasets, args.debug)
#################
# Write To Disk #
#################
print("\nWriting to disk in data/formatted ...")
for table_name, data in tables.items():
data.to_csv(f'data/formatted/{table_name}.csv', index=False)
# Pretty print the statistics
print('\nStatistics:')
for key, value in G_stats.items():
print(f'\t{key}: {value}')