forked from al20139471/Bodo-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyc-parking.py
197 lines (162 loc) · 6.09 KB
/
nyc-parking.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
"""
Source: https://github.com/JBlumstein/NYCParking/blob/master/NYC_Parking_Violations_Mapping_Example.ipynb
NYC parking ticket violations
Usage:
mpiexec -n [cores] python nyc-parking.py
Data for 2016 and 2017 is in S3 bucket (s3://bodo-example-data/nyc-parking-tickets)
or you can get data from https://www.kaggle.com/new-york-city/nyc-parking-tickets
"""
import numpy as np
import pandas as pd
import time
import bodo
import os
@bodo.jit(distributed=["many_year_df"], cache=True)
def load_parking_tickets():
"""
Load data from S3 bucket and aggregate by day, violation type, and police precinct.
"""
start = time.time()
year_2016_df = pd.read_csv(
"s3://bodo-example-data/nyc-parking-tickets/Parking_Violations_Issued_-_Fiscal_Year_2016.csv",
parse_dates=["Issue Date"],
)
year_2016_df = year_2016_df.groupby(
["Issue Date", "Violation County", "Violation Precinct", "Violation Code"],
as_index=False,
)["Summons Number"].count()
year_2017_df = pd.read_csv(
"s3://bodo-example-data/nyc-parking-tickets/Parking_Violations_Issued_-_Fiscal_Year_2017.csv",
parse_dates=["Issue Date"],
)
year_2017_df = year_2017_df.groupby(
["Issue Date", "Violation County", "Violation Precinct", "Violation Code"],
as_index=False,
)["Summons Number"].count()
# concatenate all dataframes into one dataframe
many_year_df = pd.concat([year_2016_df, year_2017_df])
end = time.time()
print("Reading Time: ", end - start)
print(many_year_df.head())
return many_year_df
main_df = load_parking_tickets()
@bodo.jit
def load_violation_precincts_codes(dir_path):
"""
Load violation codes and precincts information.
"""
start = time.time()
violation_codes = pd.read_csv(f"{dir_path}/DOF_Parking_Violation_Codes.csv")
violation_codes.columns = [
"Violation Code",
"Definition",
"manhattan_96_and_below",
"all_other_areas",
]
nyc_precincts_df = pd.read_csv(f"{dir_path}/nyc_precincts.csv", index_col="index")
end = time.time()
print("Violation and precincts load Time: ", end - start)
return violation_codes, nyc_precincts_df
dir_path = os.path.dirname(os.path.realpath(__file__))
violation_codes, nyc_precincts_df = load_violation_precincts_codes(dir_path)
@bodo.jit(distributed=["main_df"], cache=True)
def elim_code_36(main_df):
"""
Remove undefined violations (code 36)
"""
start = time.time()
main_df = main_df[main_df["Violation Code"] != 36].sort_values(
"Summons Number", ascending=False
)
end = time.time()
print("Eliminate undefined violations time: ", end - start)
print(main_df.head())
return main_df
main_df = elim_code_36(main_df)
@bodo.jit(distributed=["main_df"], cache=True)
def remove_outliers(main_df):
"""
Delete entries that have dates outside our dataset dates
"""
start = time.time()
main_df = main_df[
(main_df["Issue Date"] >= "2016-01-01")
& (main_df["Issue Date"] <= "2017-12-31")
]
end = time.time()
print("Remove outliers time: ", (end - start))
print(main_df.head())
return main_df
main_df = remove_outliers(main_df)
@bodo.jit(distributed=["main_df"], cache=True)
def merge_violation_code(main_df):
"""
Merge violation information in the main_df
"""
start = time.time()
# left join main_df and violation_codes df so that there's more info on violation in main_df
main_df = pd.merge(main_df, violation_codes, on="Violation Code", how="left")
# cast precincts as integers from floats (inadvertent type change by merge)
main_df["Violation Precinct"] = main_df["Violation Precinct"].astype(int)
end = time.time()
print("Merge time: ", (end - start))
print(main_df.head())
return main_df
main_df = merge_violation_code(main_df)
@bodo.jit(distributed=["main_df"], cache=True)
def calculate_total_summons(main_df):
"""
Calculate the total summonses in dollars for a violation in a precinct on a day
"""
start = time.time()
# create column for portion of precinct 96th st. and below
n = len(main_df)
portion_manhattan_96_and_below = np.empty(n, np.int64)
# NOTE: To run pandas, use this loop.
# for i in range(n):
for i in bodo.prange(n):
x = main_df["Violation Precinct"].iat[i]
if x < 22 or x == 23:
portion_manhattan_96_and_below[i] = 1.0
elif x == 22:
portion_manhattan_96_and_below[i] = 0.75
elif x == 24:
portion_manhattan_96_and_below[i] = 0.5
else: # other
portion_manhattan_96_and_below[i] = 0
main_df["portion_manhattan_96_and_below"] = portion_manhattan_96_and_below
# create column for average dollar amount of summons based on location
main_df["average_summons_amount"] = (
main_df["portion_manhattan_96_and_below"] * main_df["manhattan_96_and_below"]
+ (1 - main_df["portion_manhattan_96_and_below"]) * main_df["all_other_areas"]
)
# get total summons dollars by multiplying average dollar amount by number of summons given
main_df["total_summons_dollars"] = (
main_df["Summons Number"] * main_df["average_summons_amount"]
)
main_df = main_df.sort_values(by=["total_summons_dollars"], ascending=False)
end = time.time()
print("Calculate Total Summons Time: ", (end - start))
print(main_df.head())
return main_df
main_df = calculate_total_summons(main_df)
@bodo.jit(distributed=["main_df", "precinct_offenses_df"], cache=True)
def aggregate(main_df):
"""function that aggregates and filters data
e.g. total violations by precinct
"""
start = time.time()
filtered_dataset = main_df[
["Violation Precinct", "Summons Number", "total_summons_dollars"]
]
precinct_offenses_df = (
filtered_dataset.groupby(by=["Violation Precinct"])
.sum()
.reset_index()
.fillna(0)
)
end = time.time()
print("Aggregate code time: ", (end - start))
print(precinct_offenses_df.head())
return precinct_offenses_df
precinct_offenses_df = aggregate(main_df)