Repository navigation
Expand file tree
/
Copy path3_Data_Checks.py
More file actions
133 lines (90 loc) · 3.21 KB
/
Copy path3_Data_Checks.py
File metadata and controls
133 lines (90 loc) · 3.21 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
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
# coding: utf-8
# In[92]:
import pprint
import datetime
import time
from pymongo import MongoClient
client = MongoClient('192.168.0.29:27017')
db = client['test']
def do_aggregate(collection, query):
cursor = collection.aggregate(query)
return [x for x in cursor]
# In[ ]:
# 1. Total record count
print 'Record count'
print db.liverpool.count()
print '\n'
# In[ ]:
# 2. Count of nodes/ways
print 'Node/way element frequency'
pipeline = [{'$match':{'$or':[{'type':"way"},{'type':"node"}]}},
{'$group': {'_id': '$type', 'count': {'$sum':1}}},
{"$limit": 2}]
agg1 = do_aggregate(db.liverpool, pipeline)
for x in agg1:
print x['_id'], '\t', x['count']
print '\n'
# In[ ]:
# 3. Unique users
print 'Unique users:'
print len(db.liverpool.distinct('created.user'))
print '\n'
# In[ ]:
# 4. Top 5 contributors as percentage of contributions
totalUserPosts=db.liverpool.count({"created.user": {"$exists": True}})
pipeline = [{"$match": {"created.user": { "$exists": True }}},
{"$group":{"_id":"$created.user","count":{"$sum":1}}},
{"$project":
{"count":1,"percentage":{"$multiply":[{"$divide":[100,totalUserPosts]},"$count"]}}
},
{"$sort" : {"count": -1}},
{"$limit": 5}]
agg2 = do_aggregate(db.liverpool, pipeline)
print 'Users by contribution percentage'
for x in agg2:
print x['_id'], x['count'], "%.2f" % x['percentage']
print '\n'
# In[131]:
# 5. Top 10 months for contributions
pipeline = [{"$match": {"created.user": { "$exists": True }}},
{'$project':
{'username': '$created.user',
'year': { '$year': "$created.timestamp" },
'month': { '$month': "$created.timestamp" }
}},
{'$group':{'_id': {'year':'$year', 'month':'$month', 'username':'$username'}, 'count':{'$sum':1}}},
{'$sort': {'count':-1}},
{'$limit': 10}]
agg3 = do_aggregate(db.liverpool, pipeline)
print 'Top 10 months for contributions'
for x in agg3:
print '%s\t%s\t%s\t%d' % (x['_id']['year'], x['_id']['month'], x['_id']['username'], x['count'])
# In[157]:
# Postcode re-assessment
import re
postcode_re = re.compile(r'^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$')
pipeline = [{'$match': {'address.postcode':{'$exists':True}, }},
{'$match': {'address.postcode':{'$not': postcode_re}}},
{'$project': {'postcode':'$address.postcode'}},
{'$group': {'_id': '$postcode', 'count':{'$sum':1}}},
{'$project': {'postcode':'$_id', 'count':'$count'}},
{'$limit': 20}]
res = do_aggregate(db.liverpool,pipeline)
print 'Invalid postcodes'
for x in res:
print x['postcode'], x['count']
# In[159]:
res = do_aggregate(db.liverpool,
[{'$match':{'address.postcode':{'$exists':True}}},
{'$project':{'postcode':'$address.postcode'}}])
# In[174]:
prefix = set()
print 'Total postcodes: %d' % len(res)
for x in res:
prefix.add( x['postcode'].split(' ')[0])
print 'Unique postcode prefixes'
pprint.pprint(prefix)
# In[182]:
bob='colin.'
print bob[::-1][1:][::-1]
# In[ ]: