-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis.py
More file actions
60 lines (48 loc) · 1.81 KB
/
analysis.py
File metadata and controls
60 lines (48 loc) · 1.81 KB
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
import csv
from pymongo import MongoClient
from datetime import datetime
import json
client = MongoClient()
incidents = client.acab_db.incidents
def incident_types(year):
return incidents.aggregate([
{ '$match' : {'ZIP':'94110', 'Date': {'$gte':datetime(year,1,1),'$lte':datetime(year,12,31)} } },
{ '$group' : {"_id":"$Resolution", "count":{"$sum":1} } },
{ '$sort' : {'count':-1} }
])
def danger(year):
return incidents.find({'Date': {'$gte':datetime(year,1,1),'$lte':datetime(year,12,31)}, 'Descript':'DANGER OF LEADING IMMORAL LIFE', 'Resolution':'JUVENILE BOOKED' })
def juveniles(year):
return incidents.aggregate([
{ '$match' : {'Date': {'$gte':datetime(year,1,1),'$lte':datetime(year,12,31)}, 'Resolution':'JUVENILE BOOKED' } },
{ '$group' : {"_id":"$Descript", "count":{"$sum":1} } },
{ '$sort' : {'count':-1} }
])
def incidentnums(year):
thing = incidents.aggregate([
{ '$match' : {'Date': {'$gte':datetime(year,1,1),'$lte':datetime(year,12,31)} } },
{ '$group' : {"_id":"$IncidntNum", "count":{"$sum":1} } },
{ '$sort' : {'count':-1} }
])
return len(thing['result'])
def resolution(year):
return incidents.aggregate([
{ '$match' : {'Date': {'$gte':datetime(year,1,1),'$lte':datetime(year,12,31)} } },
{ '$group' : {"_id":"$Resolution", "count":{"$sum":1} } },
{ '$sort' : {'count':-1} }
])
def write_to_file(filename,data):
outfile = open('analysis/%s.json' % filename,'w+')
outjson = json.dumps(data)
outfile.write(outjson)
outfile.close()
def do_stuff(func,filename):
outdata = {}
for year in range(2003,2015):
outdata[year] = globals()[func](year)['result']
write_to_file(filename, outdata)
"""
Quality of life crimes
-Drunk in
Number of officers
"""