-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataPreparer.py
339 lines (253 loc) · 12.1 KB
/
DataPreparer.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
import pandas as pd
from locationiq.geocoder import LocationIQ
import os, shutil
import time
import geopandas as gpd
from shapely.geometry import Point, Polygon
from pyproj import Proj, transform
from datetime import datetime
from HelperMethods import getYearList, getAgencyDictionary
def geocodeCases(tempShootingYear):
text_file = open("key.txt", "r")
key = text_file.read()
geocoder = LocationIQ(key)
#Initialize DataFrames
tempIncidentAddressDF = pd.DataFrame()
tempVictimAddressDF = pd.DataFrame()
#Filters Shooting Dataframe by Null Latitude and Longitude Values
noLatLong = tempShootingYear[~tempShootingYear['VLat'].notnull() | ~tempShootingYear['ILat'].notnull()]
#Initializes Lists
allIAddresses = []
allILatitudes = []
allILongitudes = []
allVAddresses = []
allVLatitudes = []
allVLongitudes = []
for i, row in noLatLong.iterrows():
tempIncidentAddress = row['IncidentAddress']
try:
tempIncidentCoordinates = geocoder.geocode(tempIncidentAddress)
tempILat = [float(tempIncidentCoordinates[0]['lat'])]
tempILng = [float(tempIncidentCoordinates[0]['lon'])]
except:
tempILat = [0]
tempILng = [0]
tempVictimAddress = row['Vaddress']
try:
tempIncidentCoordinates = geocoder.geocode(tempVictimAddress)
tempVLat = [float(tempIncidentCoordinates[0]['lat'])]
tempVLng = [float(tempIncidentCoordinates[0]['lon'])]
except:
tempVLat = [0]
tempVLng = [0]
allIAddresses.append(tempIncidentAddress)
allILatitudes.extend(tempILat)
allILongitudes.extend(tempILng)
allVAddresses.append(tempVictimAddress)
allVLatitudes.extend(tempVLat)
allVLongitudes.extend(tempVLng)
print(tempIncidentAddress)
tempIncidentAddressDF['IncidentAddress'] = allIAddresses
tempIncidentAddressDF['IncidentLatitude'] = allILatitudes
tempIncidentAddressDF['IncidentLongitude'] = allILongitudes
iLatDict = dict(zip(tempIncidentAddressDF.IncidentAddress, tempIncidentAddressDF.IncidentLatitude))
iLonDict = dict(zip(tempIncidentAddressDF.IncidentAddress, tempIncidentAddressDF.IncidentLongitude))
tempShootingYear.ILat = tempShootingYear.ILat.fillna(tempShootingYear.IncidentAddress.map(iLatDict))
tempShootingYear.ILng = tempShootingYear.ILng.fillna(tempShootingYear.IncidentAddress.map(iLonDict))
tempVictimAddressDF['VictimAddress'] = allVAddresses
tempVictimAddressDF['VictimLatitude'] = allVLatitudes
tempVictimAddressDF['VictimLongitude'] = allVLongitudes
VLatDict = dict(zip(tempVictimAddressDF.VictimAddress, tempVictimAddressDF.VictimLatitude))
VLonDict = dict(zip(tempVictimAddressDF.VictimAddress, tempVictimAddressDF.VictimLongitude))
tempShootingYear.VLat = tempShootingYear.VLat.fillna(tempShootingYear.Vaddress.map(VLatDict))
tempShootingYear.VLng = tempShootingYear.VLng.fillna(tempShootingYear.Vaddress.map(VLonDict))
return tempShootingYear
def withinFunction(dataframe):
CRN = []
KansasCity = []
JacksonCounty = []
tempWithinDataFrame = pd.DataFrame()
tempDataFrame = dataframe
#tempDataFrame = dataframe[~dataframe['JaCo'].notnull()]
geometry = [Point(xy) for xy in zip(tempDataFrame['ILng'], tempDataFrame['ILat'])]
crs = 'EPSG:4326'
points = gpd.GeoDataFrame(tempDataFrame, crs=crs, geometry=geometry)
JacksonCountyBorder = gpd.GeoDataFrame.from_file("Maps\\JacksonCountyCorrectCRS.shp").loc[0]
KansasCityBorder = gpd.GeoDataFrame.from_file("Maps\\KansasCityCorrectCRS.shp").loc[0]
for i, pt in tempDataFrame.iterrows():
CRN.append(pt['CRN'])
if JacksonCountyBorder.geometry.contains(pt.geometry) or pt['Patrol'] == 'EPD' or pt['Patrol'] == 'SPD' or pt['Patrol'] == 'EPD' or pt['Patrol'] == 'MPD' or pt['Patrol'] == 'CPD' or pt['Agency']!=2:
JacksonCounty.append("Yes")
else:
JacksonCounty.append("No")
if KansasCityBorder.geometry.contains(pt.geometry):
KansasCity.append("Yes")
else:
KansasCity.append("No")
tempWithinDataFrame['CRN'] = CRN
tempWithinDataFrame['KansasCity'] = KansasCity
tempWithinDataFrame['JacksonCounty'] = JacksonCounty
jcDict = dict(zip(tempWithinDataFrame.CRN, tempWithinDataFrame.JacksonCounty))
kcDict = dict(zip(tempWithinDataFrame.CRN, tempWithinDataFrame.KansasCity))
dataframe.JaCo = dataframe.JaCo.fillna(dataframe.CRN.map(jcDict))
dataframe.KansasCity = dataframe.KansasCity.fillna(dataframe.CRN.map(kcDict))
return dataframe
def getNewestFile(weeklyUpload):
#Initializes Blank List
allDates = []
#Loops through the Weekly Data Drop Folder
for item in os.listdir(weeklyUpload):
#Pulls the Date from Each File in the Folder
date = int(item.split("_")[1])
#Appends it to a list
allDates.append(date)
#Removes all the duplicates
allDates = list(set(allDates))
#Sorts the List without Duplicates
allDates.sort()
#Grabs the most recent file
mostRecent = str(allDates[-1])
#Returns that most recent file.
return mostRecent
def loadKarpelCases(directory, mostRecent):
karpelDataFrames = []
#Old Received Cases
oldReceivedCases = pd.read_csv("OldHistoricalKarpelData\\1 - Received.csv")
oldReceivedCases = oldReceivedCases[['File #', "CRN", "Enter Dt.", "Def. Name", "Def. Sex", "Def. Race", "Def. DOB", "Agency"]]
oldReceivedCases = oldReceivedCases[oldReceivedCases['Agency'] == 2]
#New Received Cases
receivedCases = pd.read_csv(directory + "Rcvd_"+mostRecent+"_1800.CSV", encoding='utf-8')
receivedCases = receivedCases.rename({'Def Name': 'Def. Name', 'Enter Dt ': 'Enter Dt.', 'Def DOB': "Def. DOB", "Def Race":"Def. Race", "Def Sex":"Def. Sex"}, axis=1)
receivedCases = receivedCases[['File #', "CRN", "Enter Dt.","Def. Name", "Def. Race", "Def. Sex", "Def. DOB", "Agency"]]
receivedCases = receivedCases[receivedCases['Agency'] == 2]
oldReceivedCases = pd.concat([oldReceivedCases, receivedCases])
oldReceivedCases = oldReceivedCases.dropna(subset = ['CRN'])
oldReceivedCases = oldReceivedCases[oldReceivedCases['CRN'].str.contains(r'\d')]
#oldReceivedCases['CRN'] = oldReceivedCases['CRN'].astype(str).str.replace(r'\D+', '').str[:2] + "-" + oldReceivedCases['CRN'].astype(str).str.replace(r'\D+', '').str[2:].astype('int64').astype(str)
karpelDataFrames.append(oldReceivedCases)
#Old Filed Cases
oldFiledCases = pd.read_csv("OldHistoricalKarpelData\\2 - Filed.csv")
oldFiledCases = oldFiledCases[['File #', 'Def. Name', "CRN", "Filing Dt.", "Enter Dt.", "Agency"]]
oldFiledCases = oldFiledCases[oldFiledCases['Agency'] == 2]
#New Filed Cases
filedCases = pd.read_csv(directory + "Fld_"+mostRecent+"_1800.CSV", encoding='utf-8')
filedCases = filedCases[['File #', 'Def. Name', "CRN", "Enter Dt.", "Filing Date.", "Agency"]]
filedCases = filedCases[filedCases['Agency'] == 2]
filedCases = filedCases.rename(columns={'Filing Date.': 'Filing Dt.'})
oldFiledCases = pd.concat([oldFiledCases, filedCases])
karpelDataFrames.append(oldFiledCases)
#Old Disposed Cases
oldDisposedCases = pd.read_csv("OldHistoricalKarpelData\\3 - Disposed.csv")
oldDisposedCases = oldDisposedCases[["File #", "CRN", "Disp. Code", "Disp. Dt.", "Agency", "Enter Dt."]]
oldDisposedCases = oldDisposedCases[oldDisposedCases['Agency'] == 2]
#New Disposed Cases
disposedCases = pd.read_csv(directory + "Disp_"+mostRecent+"_1800.CSV", encoding='utf-8')
disposedCases = disposedCases[["File #", "CRN", "Disp. Code", "Disp. Dt.", "Agency", "Enter Dt."]]
disposedCases = disposedCases[disposedCases['Agency'] == 2]
oldDisposedCases = pd.concat([oldDisposedCases, disposedCases])
#oldDisposedCases = oldDisposedCases.append(disposedCases)
disposalReasons = pd.read_csv("Disposition Codes.csv", encoding = 'utf-8')
oldDisposedCases = oldDisposedCases.merge(disposalReasons, on = 'Disp. Code', how = 'left')
karpelDataFrames.append(oldDisposedCases)
#Old Refused Cases
oldDeclinedCases = pd.read_csv("OldHistoricalKarpelData\\4 - Refused.csv")
oldDeclinedCases = oldDeclinedCases[["File #", "CRN", "Disp. Code", "Disp. Dt.", "Agency", "Enter Dt."]]
oldDeclinedCases = oldDeclinedCases[oldDeclinedCases['Agency'] == 2]
declinedCases = pd.read_csv(directory + "Ntfld_"+mostRecent+"_1800.csv")
declinedCases = declinedCases[["File #", "CRN", "Disp. Code", "Disp. Dt.", "Agency", "Enter Dt."]]
oldDeclinedCases = pd.concat([oldDeclinedCases, declinedCases])
declineReasons = pd.read_csv("RefusalReasons.csv", encoding = 'utf-8')
oldDeclinedCases = oldDeclinedCases.merge(declineReasons, on = 'Disp. Code', how = 'left')
oldDeclinedCases = oldDeclinedCases[["File #", "CRN", "Reason", "Disp. Dt.", "Agency", "Enter Dt.", "Activity"]]
oldDeclinedCases = oldDeclinedCases.rename(columns = {'Reason':'Disp. Code'})
karpelDataFrames.append(oldDeclinedCases)
return karpelDataFrames
def fixCRNS(shootingDataFrame):
shootingDataFrame['CRN'] = shootingDataFrame['CRN'].astype(str)
fixedCRNS = []
for i, row in shootingDataFrame.iterrows():
if "KC" not in row['CRN'] and "-" not in row['CRN'] and row['Agency'] == 2:
fixedCRN = row['CRN'][0:2] + "-" + row['CRN'][2:]
else:
fixedCRN = row['CRN']
fixedCRNS.append(fixedCRN)
shootingDataFrame['CRN'] = fixedCRNS
return shootingDataFrame
def checkReferrals(shootingDataFrame, OldHistoricalKarpelData, agencyList):
CRN = []
Referred = []
Filed = []
Disposed = []
Declined = []
Review = []
agencyDictionary = getAgencyDictionary(OldHistoricalKarpelData, agencyList)
for i, row in shootingDataFrame.iterrows():
tempAgency = row['Agency']
agencySpecificList= agencyDictionary.get(tempAgency)
#Check Received Cases
if row['CRN'] in agencySpecificList[0]['CRN'].tolist() and row['JaCo'] == 'Yes':
Referred.append("Yes")
elif row['CRN'] not in agencySpecificList[0]['CRN'].tolist() and row['JaCo'] == 'Yes':
Referred.append("No")
else:
Referred.append("NoJuris")
#Check Filed Cases
if row['CRN'] in agencySpecificList[1]['CRN'].tolist() and row['JaCo'] == 'Yes':
Filed.append("Yes")
elif row['CRN'] not in agencySpecificList[1]['CRN'].tolist() and row['JaCo'] == 'Yes':
Filed.append("No")
else:
Filed.append("NoJuris")
#Check Disposed Cases
if row['CRN'] in agencySpecificList[2]['CRN'].tolist() and row['JaCo'] == 'Yes':
Disposed.append("Yes")
elif row['CRN'] not in agencySpecificList[2]['CRN'].tolist() and row['JaCo'] == 'Yes':
Disposed.append("No")
else:
Disposed.append("NoJuris")
#Check Declined Cases
if row['CRN'] in agencySpecificList[3]['CRN'].tolist() and row['JaCo'] == 'Yes':
Declined.append("Yes")
elif row['CRN'] not in agencySpecificList[3]['CRN'].tolist() and row['JaCo'] == 'Yes':
Declined.append("No")
else:
Declined.append("NoJuris")
if Declined[i]=='No' and Filed[i]=='No' and Referred[i] == 'Yes':
Review.append("Yes")
elif Declined[i]=='NoJuris' or Filed[i]=='NoJuris' or Referred[i] == 'NoJuris':
Review.append("NoJuris")
else:
Review.append("No")
shootingDataFrame = shootingDataFrame.reset_index(drop=True)
shootingDataFrame['Ref'] = Referred
shootingDataFrame['Filed'] = Filed
shootingDataFrame['Disposed'] = Disposed
shootingDataFrame['Declined'] = Declined
shootingDataFrame['Review'] = Review
return shootingDataFrame
def prepareData(shootingDataFrame, agencyList):
#Location to Shooting Data
shootingFolderLocation = "ShootingData\\"
#Remove Old Analysis from DataForDashboard
#Deletes and Remakes Data For Dashboard Folder
if os.path.exists("DataForDashboard"):
shutil.rmtree("DataForDashboard")
os.makedirs("DataForDashboard")
#Location to Daily Karpel Data Update
weeklyUpload = "H:\\Units Attorneys and Staff\\01 - Units\\DT Crime Strategies Unit\\Weekly Update\\"
#Calling loadKarpelCases using the newest potential data
karpelCases = loadKarpelCases(weeklyUpload, getNewestFile(weeklyUpload))
#For Each Year in Shooting Data Location (2017 through the present)
for year in getYearList():
tempShootingDF = shootingDataFrame[shootingDataFrame['DateTime'].dt.year == year]
tempShootingDF = tempShootingDF.reset_index(drop=True)
#Geocode Cases
tempShootingDF = geocodeCases(tempShootingDF)
#Within
tempShootingDF = withinFunction(tempShootingDF)
#Check Referrals, Filings, Declines, and Disposals
tempShootingDF['CRN']
tempShootingDF = checkReferrals(tempShootingDF, karpelCases, agencyList)
#Export Each Updated Dataframe to Excel
tempShootingDF.to_excel(shootingFolderLocation+str(year) + " Shooting Data.xlsx", index= False)
return karpelCases