-
Notifications
You must be signed in to change notification settings - Fork 2
/
go_webinterface.py
executable file
·277 lines (222 loc) · 9.02 KB
/
go_webinterface.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
#!/usr/bin/env python3
from tsdb import *
from timeseries import TimeSeries
import numpy as np
import asyncio
import requests
import json
from webserver import *
import matplotlib.pyplot as plt
from scipy.stats import norm
########################################
#
# This file can be used to test the basic REST API functionality.
# For it to work, you will need to first run go_server.py to set up the
# server and go_webserver.py to set up the webserver.
#
# NOTE: This file does not incorporate the persistent database behavior.
#
########################################
def tsmaker(m, s, j):
'''
Helper function: randomly generates a time series for testing.
Parameters
----------
m : float
Mean value for generating time series data
s : float
Standard deviation value for generating time series data
j : float
Quantifies the "jitter" to add to the time series data
Returns
-------
A time series and associated meta data.
'''
# generate metadata
meta = {}
meta['order'] = int(np.random.choice([-5, -4, -3, -2, -1, 0,
1, 2, 3, 4, 5]))
meta['blarg'] = int(np.random.choice([1, 2]))
meta['vp'] = False # initialize vantage point indicator as negative
# generate time series data
t = np.arange(0.0, 1.0, 0.01)
v = norm.pdf(t, m, s) + j * np.random.randn(100)
# return time series and metadata
return meta, TimeSeries(t, v)
def main():
print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&')
# initialize web interface
web_interface = WebInterface()
########################################
#
# create dummy data
#
########################################
# parameters for testing
num_ts = 25
num_vps = 5
# time series parameters
mus = np.random.uniform(low=0.0, high=1.0, size=num_ts)
sigs = np.random.uniform(low=0.05, high=0.4, size=num_ts)
jits = np.random.uniform(low=0.05, high=0.2, size=num_ts)
# initialize dictionaries for time series and their metadata
tsdict = {}
metadict = {}
# fill dictionaries with randomly generated entries for database
for i, m, s, j in zip(range(50), mus, sigs, jits):
meta, tsrs = tsmaker(m, s, j) # generate data
pk = "ts-{}".format(i) # generate primary key
tsdict[pk] = tsrs # store time series data
metadict[pk] = meta # store metadata
########################################
#
# trigger operations
#
########################################
# add dummy trigger
web_interface.add_trigger('junk', 'insert_ts', None, None)
# add stats trigger
web_interface.add_trigger(
'stats', 'insert_ts', ['mean', 'std'], None)
########################################
#
# time series insertion & metadata upsertion
#
########################################
print("\nSTARTING UPSERTS")
print('\n---------------------')
# insert the time series
for k in tsdict:
web_interface.insert_ts(k, tsdict[k])
# upsert the metadata
for k in tsdict:
web_interface.upsert_meta(k, metadict[k])
print("\nUPSERTS FINISHED")
print('\n---------------------')
########################################
#
# selects
#
########################################
print("\nSTARTING SELECTS")
print('\n---------------------')
# select all database entries; no metadata fields
print('\n---------DEFAULT------------')
results = web_interface.select()
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all database entries; sort by 'order'; no metadata fields
print('\n---------ADDITIONAL------------')
results = web_interface.select(additional={'sort_by': '-order'})
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all database entries; return 'order' metadata
print('\n----------ORDER FIELD-----------')
results = web_interface.select(fields=['order'])
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all database entries; return all metadata fields
print('\n---------ALL FIELDS------------')
results = web_interface.select(fields=[])
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all entries with order = 1; return 'ts' metadata
print('\n------------TS with order 1---------')
results = web_interface.select({'order': 1}, fields=['ts'])
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all entries with blarg = 1; return all metadata fields
print('\n------------All fields, blarg 1 ---------')
results = web_interface.select({'blarg': 1}, fields=[])
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all entries with order = 1 AND blarg = 2; no metadata fields
print('\n------------order 1 blarg 2 no fields---------')
results = web_interface.select({'order': 1, 'blarg': 2})
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all entries with order >= 4; return 'order', 'blarg', 'mean'
# metadata fields
print('\n------------order >= 4 order, blarg and mean sent back, '
'also sorted---------')
results = web_interface.select({'order': {'>=': 4}},
fields=['order', 'blarg', 'mean'],
additional={'sort_by': '-order'})
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
# select all entries with blarg >= 1 AND order = 1; return 'blarg' and
# 'std' metadata fields
print('\n------------order 1 blarg >= 1 fields blarg and std---------')
results = web_interface.select({'blarg': {'>=': 1}, 'order': 1},
fields=['blarg', 'std'])
if len(results) > 0:
print('C> metadata fields:',
list(results[list(results.keys())[0]].keys()))
print("\nSELECTS FINISHED")
print('\n---------------------')
########################################
#
# time series similarity search
#
########################################
########################################
print("\nSTARTING TIME SERIES SIMILARITY SEARCH")
print('\n---------------------')
# randomly choose time series as vantage points
random_vps = np.random.choice(
range(num_ts), size=num_vps, replace=False)
vpkeys = ['ts-{}'.format(i) for i in random_vps]
# add the time series as vantage points
for i in range(num_vps):
web_interface.insert_vp(vpkeys[i])
# primary keys of vantage points
print("VPS", vpkeys)
# first create a query time series
_, query = tsmaker(np.random.uniform(low=0.0, high=1.0),
np.random.uniform(low=0.05, high=0.4),
np.random.uniform(low=0.05, high=0.2))
# get distance from query time series to the vantage point
result_distance = web_interface.augmented_select(
'corr', ['vpdist'], query, {'vp': {'==': True}})
vpdist = {v: result_distance[v]['vpdist'] for v in vpkeys}
print(vpdist)
# pick the closest vantage point
nearest_vp_to_query = min(vpkeys, key=lambda v: vpdist[v])
# define circle radius as 2 x distance to closest vantage point
radius = 2 * vpdist[nearest_vp_to_query]
print('Radius: {:.2f}'.format(radius))
# find relative index of nearest vantage point
relative_index_vp = vpkeys.index(nearest_vp_to_query)
# calculate distance to all time series within the circle radius
results = web_interface.augmented_select(
'corr', ['towantedvp'], query,
{'d_vp-{}'.format(relative_index_vp): {'<=': radius}})
# find the closest time series
nearestwanted1 = min(results.keys(),
key=lambda k: results[k]['towantedvp'])
print('Nearest time series (manual): {}; distance: {:.2f}'.
format(nearestwanted1, results[nearestwanted1]['towantedvp']))
# compare to database similarity search
nearestwanted2 = web_interface.vp_similarity_search(query, 1)
print('Nearest time series (query): {}; distance: {:.2f}'.
format(list(nearestwanted2.keys())[0],
list(nearestwanted2.values())[0]))
# visualize results
plt.plot(query, label='Input TS')
plt.plot(tsdict[nearestwanted1], label='Closest TS (manual)')
plt.plot(tsdict[list(nearestwanted2.keys())[0]],
label='Closest TS (DB operation)')
plt.legend(loc='best')
plt.show()
print("\nTIME SERIES SIMILARITY SEARCH FINISHED")
print('\n---------------------')
if __name__ == '__main__':
main()