-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathprojectStationData.py
127 lines (99 loc) · 5.37 KB
/
projectStationData.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
import urllib.request
import json
import dml
import prov.model
import datetime
import uuid
class projectDestinationData(dml.Algorithm):
contributor = 'cma4_lliu_saragl_tsuen'
reads = ['cma4_lliu_saragl_tsuen.mbta', 'cma4_lliu_saragl_tsuen.hubway']
writes = ['cma4_lliu_saragl_tsuen.stationsProjected']
@staticmethod
def execute(trial = False):
'''Retrieve some data sets (not using the API here for the sake of simplicity).'''
startTime = datetime.datetime.now()
# Set up the database connection.
client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('cma4_lliu_saragl_tsuen', 'cma4_lliu_saragl_tsuen')
dataSet = []
collection = []
if trial:
collection = repo['cma4_lliu_saragl_tsuen.hubway'].aggregate([{'$sample': {'size': 25}}], allowDiskUse=True)
else:
collection = repo['cma4_lliu_saragl_tsuen.hubway'].find()
# projection
dataSet = [
{'key': ('hubway', row["s"]),
'coords': (row["la"], row["lo"])}
for row in collection
]
collection2 = None
if trial:
collection2 = repo['cma4_lliu_saragl_tsuen.mbta'].aggregate([{'$sample': {'size': 1000}}], allowDiskUse=True)
else:
collection2 = repo['cma4_lliu_saragl_tsuen.mbta'].find()
mbta_dataset = []
# more projection
mbta_dataset = [{'key': ('mbta', row['Stop_Name']),'coords': (row['Coords:'][0], row['Coords:'][1])} for row in collection2]
for x in range(len(mbta_dataset)):
dataSet.append(mbta_dataset[x])
final = []
for entry in dataSet:
if entry not in final:
final.append(entry)
print(final)
repo.dropCollection("cma4_lliu_saragl_tsuen.stationsProjected")
repo.createCollection("cma4_lliu_saragl_tsuen.stationsProjected")
repo['cma4_lliu_saragl_tsuen.stationsProjected'].insert_many(final)
repo['cma4_lliu_saragl_tsuen.stationsProjected'].metadata({'complete':True})
print(repo['cma4_lliu_saragl_tsuen.stationsProjected'].metadata())
repo.logout()
endTime = datetime.datetime.now()
return {"start":startTime, "end":endTime}
@staticmethod
def provenance(doc = prov.model.ProvDocument(), startTime = None, endTime = None):
'''
Create the provenance document describing everything happening
in this script. Each run of the script will generate a new
document describing that invocation event.
'''
# Set up the database connection.
client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('cma4_lliu_saragl_tsuen', 'cma4_lliu_saragl_tsuen')
doc.add_namespace('alg', 'http://datamechanics.io/algorithm/') # The scripts are in <folder>#<filename> format.
doc.add_namespace('dat', 'http://datamechanics.io/data/') # The data sets are in <user>#<collection> format.
doc.add_namespace('ont', 'http://datamechanics.io/ontology#') # 'Extension', 'DataResource', 'DataSet', 'Retrieval', 'Query', or 'Computation'.
doc.add_namespace('log', 'http://datamechanics.io/log/') # The event log.
doc.add_namespace('stations', 'http://datamechanics.io/')
this_script = doc.agent('alg:cma4_lliu_saragl_tsuen#stations', {prov.model.PROV_TYPE:prov.model.PROV['SoftwareAgent'], 'ont:Extension':'py'})
resource = doc.entity('dat:mbta', {'prov:label':'MBTA Station Names and Coords Data', prov.model.PROV_TYPE:'ont:DataResource', 'ont:Extension':'json'})
get_mbta_stations = doc.activity('log:uuid'+str(uuid.uuid4()), startTime, endTime)
doc.wasAssociatedWith(get_mbta_stations, this_script)
doc.usage(get_mbta_stations, resource, startTime, None,
{prov.model.PROV_TYPE:'ont:Retrieval'
}
)
mbta_stations = doc.entity('dat:cma4_lliu_saragl_tsuen#mbta', {prov.model.PROV_LABEL:'MBTA Stations', prov.model.PROV_TYPE:'ont:DataSet'})
doc.wasAttributedTo(mbta_stations, this_script)
doc.wasGeneratedBy(mbta_stations, get_mbta_stations, endTime)
doc.wasDerivedFrom(mbta_stations, resource, get_mbta_stations, get_mbta_stations, get_mbta_stations)
resource2 = doc.entity('dat:hubway', {'prov:label':'Hubway Stations Names and Coords Data', prov.model.PROV_TYPE:'ont:DataResource', 'ont:Extension':'json'})
get_hubway_stations = doc.activity('log:uuid'+str(uuid.uuid4()), startTime, endTime)
doc.wasAssociatedWith(get_hubway_stations, this_script)
doc.usage(get_hubway_stations, resource2, startTime, None,
{prov.model.PROV_TYPE:'ont:Retrieval'
}
)
hubway_stations = doc.entity('dat:cma4_lliu_saragl_tsuen#hubway', {prov.model.PROV_LABEL:'Hubway Stations', prov.model.PROV_TYPE:'ont:DataSet'})
doc.wasAttributedTo(hubway_stations, this_script)
doc.wasGeneratedBy(hubway_stations, get_hubway_stations, endTime)
doc.wasDerivedFrom(hubway_stations, resource, get_hubway_stations, get_hubway_stations, get_hubway_stations)
repo.logout()
return doc
projectDestinationData.execute()
doc = projectDestinationData.provenance()
print(doc.get_provn())
print(json.dumps(json.loads(doc.serialize()), indent=4))
## eof