forked from typedb/typedb-driver-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.py
258 lines (192 loc) · 7.71 KB
/
statistics.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
# Copyright 2020 Grakn Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from grakn.client import GraknClient
def print_to_log(title, content):
print(title)
print("")
print(content)
print("\n")
# How many stations do exist?
def query_station_count(question, transaction):
print_to_log("Question: ", question)
query = 'compute count in station;'
print_to_log("Query:", query)
answer = list(transaction.query(query))[0]
number_of_stations = answer.number()
print("Number of stations: " + str(number_of_stations))
return number_of_stations
# How long is the shortest trip between two stations?
def query_shortest_trip(question, transaction):
print_to_log("Question: ", question)
query = 'compute min of duration, in route-section;'
print_to_log("Query:", query)
answer = list(transaction.query(query))[0]
min_duration = answer.number()
print("Shortest Trip: " + str(min_duration))
return min_duration
# Which is the west most station in London?
def query_northernmost_station(question, transaction):
print_to_log("Question: ", question)
query = 'compute min of lat, in station;'
print_to_log("Query:", query)
answer = list(transaction.query(query))[0]
lat = answer.number()
query = [
'match',
' $sta isa station, has lat $lat, has name $nam;',
' $lat ' + str(lat) + ';',
'get $nam;'
]
print_to_log("Query:", "\n".join(query))
query = "".join(query)
answers = [ans.get("nam") for ans in transaction.query(query)]
result = [answer.value() for answer in answers]
print_to_log("Northmost stations with " + str(lat) + " are: ", result)
return [lat, result]
# How long is the longest trip between two stations?
def query_longest_trip(question, transaction):
print_to_log("Question: ", question)
query = 'compute max of duration, in route-section;'
print_to_log("Query:", query)
answer = list(transaction.query(query))[0]
max_duration = answer.number()
print(max_duration)
query = [
'match',
' $rou (section: $sec, origin: $ori, destination: $des, route-operator: $tul) isa route;',
' $sec isa route-section, has duration ' + str(max_duration) + ';',
' $tul isa tube-line, has name $tul-nam;',
' $tun (beginning: $sta1, end: $sta2, service: $sec) isa tunnel;',
' $sta1 isa station, has name $sta1-nam;',
' $sta2 isa station, has name $sta2-nam;',
' $ori isa station, has name $ori-nam;',
' $des isa station, has name $des-nam;',
'get;'
]
print_to_log("Query:", "\n".join(query))
query = "".join(query)
answers = transaction.query(query)
result = []
for answer in answers:
answer = answer.map()
print_to_log("Longest trip is found in: ", "Tunnel from " +
answer.get("sta1-nam").value() +
" to " + answer.get("sta2-nam").value() +
", via " + answer.get("tul-nam").value() +
", on the route going from " +
answer.get("ori-nam").value() +
" to " + answer.get("des-nam").value())
result.append([
answer.get("sta1-nam").value(),
answer.get("sta2-nam").value(),
answer.get("tul-nam").value(),
answer.get("ori-nam").value(),
answer.get("des-nam").value()
])
return [max_duration, result]
# What's the average duration of all trips?
def query_avg_duration(question, transaction):
print_to_log("Question: ", question)
query = 'compute mean of duration, in route-section;'
print_to_log("Query:", query)
answer = list(transaction.query(query))[0]
mean_duration = answer.number()
print("Average duration: " + str(mean_duration))
return mean_duration
# What's the median duration among all trips?
def query_median_duration(question, transaction):
print_to_log("Question: ", question)
query = 'compute median of duration, in route-section;'
print_to_log("Query:", query)
answer = list(transaction.query(query))[0]
median_duration = answer.number()
print("Median of durations: " + str(median_duration))
return median_duration
# What's the standard deviation of trip durations?
def query_std_duration(question, transaction):
print_to_log("Question: ", question)
query = 'compute std of duration, in route-section;'
print_to_log("Query:", query)
answer = list(transaction.query(query))[0]
std_duration = answer.number()
print("Standard deviation of durations: " + str(std_duration))
return std_duration
def execute_query_all(transaction):
for qs_func in query_examples:
question = qs_func["question"]
query_function = qs_func["query_function"]
query_function(question, transaction)
print("\n - - - - - - - - - - - - \n")
query_examples = [
{
"question": "How many stations do exist?",
"query_function": query_station_count
},
{
"question": "How long is the shortest trip between two stations?",
"query_function": query_shortest_trip
},
{
"question": "Which is the northernmost station in London?",
"query_function": query_northernmost_station
},
{
"question": "How long is the longest trip between two stations?",
"query_function": query_longest_trip
},
{
"question": "What's the average duration of all trips?",
"query_function": query_avg_duration
},
{
"question": "What's the median duration among all trips?",
"query_function": query_median_duration
},
{
"question": "What's the standard deviation of trip durations?",
"query_function": query_std_duration
}
]
def init(qs_number):
# create a transaction to talk to the keyspace
with GraknClient(uri="localhost:48555") as client:
with client.session(keyspace="tube_network") as session:
with session.transaction().read() as transaction:
# execute the query for the selected question
if qs_number == 0:
execute_query_all(transaction)
else:
question = query_examples[qs_number - 1]["question"]
query_function = query_examples[qs_number - 1]["query_function"]
query_function(question, transaction)
if __name__ == "__main__":
"""
The code below:
- gets user's selection wrt the queries to be executed
- creates a Grakn client > session > transaction connected to the keyspace
- runs the right function based on the user's selection
- closes the session and transaction
"""
# ask user which question to execute the query for
print("")
print("For which of these questions, on the tube knowledge graph, do you want to execute the query?\n")
for index, qs_func in enumerate(query_examples):
print(str(index + 1) + ". " + qs_func["question"])
print("")
# get user's question selection
qs_number = -1
while qs_number < 0 or qs_number > len(query_examples):
qs_number = int(input("choose a number (0 for to answer all questions): "))
print("")
init(qs_number)