def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() n_add = 50 mus = np.random.uniform(low=0.0, high=1.0, size=n_add) sigs = np.random.uniform(low=0.05, high=0.4, size=n_add) jits = np.random.uniform(low=0.05, high=0.2, size=n_add) for i, m, s, j in zip(range(n_add), mus, sigs, jits): client.insert_ts("ts-{}".format(i), tsmaker(m, s, j)) for i in range(5): client.add_vp() _, results = client.select(fields=[]) print("RESULT TYPE: ", type(results)) for k in results: print(k, results[k]) m = np.random.uniform(low=0.0, high=1.0) s = np.random.uniform(low=0.05, high=0.4) j = np.random.uniform(low=0.05, high=0.2) query = tsmaker(m, s, j) _, closest = client.simsearch(query) print("CLOSEST MATCH: ", closest) _, closest = client.sim_search_SAX(query) print("CLOSEST MATCH: ", closest)
def on_button(self): self.client = TSDBClient() self.client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') self.client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) if self.master2: self.master2.destroy() else: self.master.destroy() _, results = self.client.select() if not len(results): self.master1 = tk.Tk() self.label = tk.Label(self.master1,text=""" It appears you have not chosen to load any time series into the database so the database will be automatically populated for the search. """,justify= 'left') self.label2 = tk.Label(self.master1,text= "How many time series would you like in the database?") self.entry = tk.Entry(self.master1) self.button = tk.Button(self.master1, text="continue", command=self.on_button1) self.label.pack() self.label2.pack() self.entry.pack() self.button.pack() else: self.switch = 1 self.on_button1()
async def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() #ppl = open('standardize.ppl').read() await client.add_trigger('junk', 'insert_ts', None, 23) #client.add_trigger('junk', 'select', None, 23) await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) await client.insert_ts('one',ts.TimeSeries([1, 2, 3],[1, 4, 9])) await client.insert_ts('two',ts.TimeSeries([2, 3, 4],[4, 9, 16])) await client.insert_ts('three',ts.TimeSeries([9,3,4],[4,0,16])) await client.remove_trigger('junk', 'insert_ts') await client.insert_ts('four',ts.TimeSeries([0,0,4],[1,0,4])) await client.upsert_meta('one', {'order': 1, 'blarg': 1}) await client.upsert_meta('two', {'order': 2}) await client.upsert_meta('three', {'order': 1, 'blarg': 2}) await client.upsert_meta('four', {'order': 2, 'blarg': 2}) print("UPSERTS FINISHED") print('---------------------') await client.select() print('---------------------') await client.select(fields=['order']) print('---------------------') await client.select(fields=[]) print('---------------------') print('---------------------') await client.select({'order': 1}, fields=['ts']) print('{{{{{{{{{{{{{{}}}}}}}}}}}}}}') await client.select({'blarg': 1}, fields=[]) print('{{{{{{{{{{{{{{}}}}}}}}}}}}}}') bla = client.select({'order': 1, 'blarg': 2}) print("END", bla) await client.select({'blarg': {'>=': 2}}, fields=['blarg', 'mean']) await client.select({'blarg': {'>=': 2}, 'order': 1}, fields=['blarg', 'std']) await client.select({'order': {'>=': 2}}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) await client.select({'order': {'>=': 2}}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order', 'limit':2}) select = await client.select(fields=['order'], additional={'sort_by': '-order'}) for x in range(1, len(select[1])): print (list(select[1].keys())[x]) assert(select[1][list(select[1].keys())[x]]['order'] <= select[1][list(select[1].keys())[x-1]]['order'])
async def test_complex_run(self): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() # Add a trigger. notice the argument. It does not do anything here but # it could be used to save a shlep of data from client to server. await client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # Our stats trigger await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) # Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=50) sigs = np.random.uniform(low=0.05, high=0.4, size=50) jits = np.random.uniform(low=0.05, high=0.2, size=50) # Dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(50), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta[ 'vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # Choose 5 distinct vantage point time series vpkeys = [ "ts-{}".format(i) for i in np.random.choice(range(50), size=5, replace=False) ] for i in range(5): # add 5 triggers to upsert distances to these vantage points await client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp'] = True # Having set up the triggers, now insert the time series, and upsert the metadata for k in tsdict: await client.insert_ts(k, tsdict[k]) await client.upsert_meta(k, metadict[k]) select = await client.select() # Assert we received all pks assert (len(select[1]) == 50) # Assert the primary keys have empty dicts assert (select[1]['ts-1'] == {}) # In this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. select = await client.select(fields=['order'], additional={'sort_by': '-order'}) print(select) # TODO: DO NOT APPEAR TO BE IN ORDER for x in range(1, len(select[1])): assert (select[1][list(select[1].keys())[x]]['order'] <= select[1][list(select[1].keys())[x - 1]]['order'])
async def test_simple_run(self): # Data t = [0, 1, 2, 3, 4] v = [1.0, 2.0, 3.0, 2.0, 1.0] ats = ts.TimeSeries(t, v) # Setup Client client = TSDBClient() # Add Trigger await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) # Insert await client.insert_ts(1, ats) # Select status, payload = await client.select({'pk': { '==': 1 }}, ['ts', 'mean', 'std'], None) assert (status == 0) assert (ts.TimeSeries(payload['1']['ts'][0], payload['1']['ts'][1]) == ats) assert (payload['1']['std'] == 1.4142135623730951) assert (payload['1']['mean'] == 2.0) # FINALLY WORKING!!! YAY!!! # Upsert await client.upsert_meta(1, {'order': 1}) status, payload = await client.select({'order': { '==': 1 }}, ['pk', 'order'], None) assert (status == 0) assert (payload['1']['order'] == 1) # Remove Trigger await client.remove_trigger('stats', 'insert_ts') # Insert (No Trigger) await client.insert_ts(2, ats) status, payload = await client.select({'pk': { '==': 2 }}, ['ts', 'mean', 'std'], None) assert (ts.TimeSeries(payload['2']['ts'][0], payload['2']['ts'][1]) == ats) assert ('std' not in payload['2']) assert ('mean' not in payload['2']) # Delete await client.delete_ts(1) status, payload = await client.select({'pk': { '==': 1 }}, ['ts', 'mean', 'std'], None) assert (status == 0) assert (payload == {})
async def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() await client.add_trigger('period', 'insert_ts', ['period'], None) #['mean', 'std'], None)# sigeta = np.random.normal(0, 1, 2000) sigeps = np.random.normal(0, 10, 2000) mus = np.cumsum(sigeta) + 20 y = mus + sigeps await client.insert_ts('one', ts.TimeSeries(y, np.arange(2000))) await client.upsert_meta('one', {'order': 1, 'blarg': 1}) print('---------------------') await client.select(fields=[])
def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() # add a trigger. notice the argument. It does not do anything here but # could be used to save a shlep of data from client to server. client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # our stats trigger client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) #Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=50) sigs = np.random.uniform(low=0.05, high=0.4, size=50) jits = np.random.uniform(low=0.05, high=0.2, size=50) # dictionaries for time series and their metadata tsdict={} metadict={} for i, m, s, j in zip(range(50), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta['vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # choose 5 distinct vantage point time series vpkeys = ["ts-{}".format(i) for i in np.random.choice(range(50), size=5, replace=False)] for i in range(5): # add 5 triggers to upsert distances to these vantage points client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) print('lalala:', i, *tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp']=True # Having set up the triggers, now inser the time series, and upsert the metadata for k in tsdict: client.insert_ts(k, tsdict[k]) client.upsert_meta(k, metadict[k]) print("UPSERTS FINISHED") print('---------------------') print("STARTING SELECTS") print('---------DEFAULT------------') client.select() #in this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. print('---------ADDITIONAL------------') print(client.select(additional={'sort_by': '-order'})) print('----------ORDER FIELD-----------') _, results = client.select(fields=['order']) print(results) for k in results: print(k, results[k]) print('---------ALL FIELDS------------') client.select(fields=[]) print('------------TS with order 1---------') client.select({'order': 1}, fields=['ts']) print('------------All fields, blarg 1 ---------') client.select({'blarg': 1}, fields=[]) print('------------order 1 blarg 2 no fields---------') _, bla = client.select({'order': 1, 'blarg': 2}) print(bla) print('------------order >= 4 order, blarg and mean sent back, also sorted---------') _, results = client.select({'order': {'>=': 4}}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) for k in results: print(k, results[k]) print('------------order 1 blarg >= 1 fields blarg and std---------') _, results = client.select({'blarg': {'>=': 1}, 'order': 1}, fields=['blarg', 'std']) for k in results: print(k, results[k]) print('------now computing vantage point stuff---------------------') #we first create a query time series. _, query = tsmaker(0.5, 0.2, 0.1) # Step 1: in the vpdist key, get distances from query to vantage points # this is an augmented select client.insert_ts('ts-query',query) dists = client.select({'pk': 'ts-query'},fields=['d_vp-{}'.format(i) for i in range(5)])[1]['ts-query'] min_dist = np.inf nearest_vp_to_query = None for vp in dists: if dists[vp] < min_dist: min_dist = dists[vp] nearest_vp_to_query = vp print(nearest_vp_to_query,min_dist) #1b: choose the lowest distance vantage point # you can do this in local code # Step 2: find all time series within 2*d(query, nearest_vp_to_query) #this is an augmented select to the same proc in correlation # nearestwanted = client.augmented_select('corr', ['d_vp-1'], arg=query, # metadata_dict={'d_vp-1': {'<=': 2.0*min_dist}}, additional={'sort_by': '+d_vp-1', 'limit': 10}) #print(client.select(fields=['d_vp-1'])) #print(2*min_dist) nearestwanted = client.select(fields=['d_vp-1'], metadata_dict={'d_vp-1': {'<=': 2.0*min_dist}}, additional={'sort_by': '+d_vp-1', 'limit': 10}) print(nearestwanted) nearestwanted = list(nearestwanted[1].keys())[0] #2b: find the smallest distance amongst this ( or k smallest) #you can do this in local code #your code here ends # plot the timeseries to see visually how we did. import matplotlib.pyplot as plt for k in tsdict: plt.plot(tsdict[k], alpha=0.15) plt.plot(query) plt.plot(tsdict[nearestwanted]) plt.show()
def main(): client = TSDBClient() client.insert_ts('one',ts.TimeSeries([1, 2, 3],[1, 4, 9])) client.insert_ts('two',ts.TimeSeries([2, 3, 4],[4, 9, 16])) client.insert_ts('three',ts.TimeSeries([9,3,4],[4,0,16])) client.insert_ts('four',ts.TimeSeries([0,0,4],[1,0,4])) client.upsert_meta('one', {'order': 1, 'blarg': 1}) client.upsert_meta('two', {'order': 2}) client.upsert_meta('three', {'order': 1, 'blarg': 2}) client.upsert_meta('four', {'order': 2, 'blarg': 2}) client.select() client.select({'order': 1}) client.select({'blarg': 1}) bla = client.select({'order': 1, 'blarg': 2}) print("END", bla)
class SampleApp2(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.destroy() self.switch = 0 self.switch2 = 0 self.master3 = None self.master = tk.Tk() self.master2 = None self.label = tk.Label(self.master,text=""" I would like to take this opportunity to thank you for using similarity search for time series using the iSAX index. Please complete the prompts from the other window before moving on to this window. """,justify = 'left') self.button = tk.Button(self.master, text="continue", command=self.on_button) self.label.pack() self.button.pack() def on_button(self): self.client = TSDBClient() self.client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') self.client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) if self.master2: self.master2.destroy() else: self.master.destroy() _, results = self.client.select() if not len(results): self.master1 = tk.Tk() self.label = tk.Label(self.master1,text=""" It appears you have not chosen to load any time series into the database so the database will be automatically populated for the search. """,justify= 'left') self.label2 = tk.Label(self.master1,text= "How many time series would you like in the database?") self.entry = tk.Entry(self.master1) self.button = tk.Button(self.master1, text="continue", command=self.on_button1) self.label.pack() self.label2.pack() self.entry.pack() self.button.pack() else: self.switch = 1 self.on_button1() def on_button1(self): self.master2 = tk.Tk() if self.switch == 1: self.label_1 = tk.Label(self.master2, text='Do you wish to provide your own time series for the query?') self.button1 = tk.Button(self.master2, text="yes", command=self.on_button2) self.button2 = tk.Button(self.master2, text="no", command=self.on_button3) self.label_1.pack() self.button1.pack(side='right') self.button2.pack(side='right') else: self.number = None number = self.entry.get() if number: try: self.number = int(number) except: self.number = None if not self.number: self.label_1 = tk.Label(self.master2,text="Please enter a number.") self.button1 = tk.Button(self.master2, text="continue", command=self.on_button) self.master1.destroy() self.label_1.pack() self.button1.pack() else: self.master1.destroy() self.tsdict = tsgenerator(self.client,self.number) self.label_1 = tk.Label(self.master2, text='Do you wish to provide your own time series for the query?') self.button1 = tk.Button(self.master2, text="yes", command=self.on_button2) self.button2 = tk.Button(self.master2, text="no", command=self.on_button3) self.label_1.pack() self.button1.pack(side='right') self.button2.pack(side='right') def on_button2(self): self.switch2 = 0 if self.master3: self.master3 = tk.Tk() self.master4.destroy() else: self.master2.destroy() self.master3 = tk.Tk() self.label_1 = tk.Label(self.master3,text="Please enter the file name for the query (as .npy file): ") self.entry = tk.Entry(self.master3) self.button1 = tk.Button(self.master3, text="continue", command=self.on_button4) self.label_1.pack() self.entry.pack() self.button1.pack() def on_button3(self): self.master2.destroy() self.master4 = tk.Tk() self.switch2 = 1 self.label = tk.Label(self.master4,text="A time series will be automatically generated. ") self.button1 = tk.Button(self.master4, text="continue", command=self.on_button4) self.label.pack() self.button1.pack() def on_button4(self): if self.switch2 == 0: self.filename = self.entry.get() if self.filename: try: tsarray = np.load(self.filename) self.arg = ts.TimeSeries(tsarray[0,:], tsarray[1,:]) self.master3.destroy() except: self.master4 = tk.Tk() self.label = tk.Label(self.master4,text="Error encountered, please make sure the file name is correct.") self.button1 = tk.Button(self.master4, text="continue", command=self.on_button2) self.master3.destroy() self.label.pack() self.button1.pack() else: self.on_button2() else: mus = np.random.uniform(low=0.0, high=1.0, size=1) sigs = np.random.uniform(low=0.05, high=0.4, size=1) jits = np.random.uniform(low=0.05, high=0.2, size=1) self.master4.destroy() meta, tsrs = tsmaker(mus, sigs, jits) self.arg = tsrs import matplotlib.pyplot as plt _, res = self.client.sim_search_SAX(self.arg) if res: _, res2 = self.client.select(metadata_dict={'pk':res}, fields=['ts']) plt.plot(self.arg.times(), self.arg.values(),label='query') plt.plot(res2[res]['ts'][0], res2[res]['ts'][1],label='closest match') plt.legend(loc='best',fontsize=12) plt.show() else: print('Could not find matching time series')
def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient(30000) # add a trigger. notice the argument. It does not do anything here but # could be used to save a shlep of data from client to server. client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # our stats trigger client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) # Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=50) sigs = np.random.uniform(low=0.05, high=0.4, size=50) jits = np.random.uniform(low=0.05, high=0.2, size=50) # dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(50), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta['vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # choose 5 distinct vantage point time series vpkeys = ["ts-{}".format(i) for i in np.random.choice(range(50), size=5, replace=False)] for i in range(5): # add 5 triggers to upsert distances to these vantage points client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp'] = True # Having set up the triggers, now insert the time series, and upsert the metadata for k in tsdict: client.insert_ts(k, tsdict[k]) time.sleep(1) client.upsert_meta(k, metadict[k]) print("UPSERTS FINISHED") time.sleep(5) print('---------------------') print("STARTING SELECTS") print('---------DEFAULT------------') client.select() # in this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. print('---------ADDITIONAL------------') client.select(additional={'sort_by': '-order'}) print('----------ORDER FIELD-----------') _, results = client.select(fields=['order']) for k in results: print(k, results[k]) print('---------ALL FIELDS------------') client.select(fields=[]) print('------------TS with order 1---------') client.select({'order': 1}, fields=['ts']) print('------------All fields, blarg 1 ---------') client.select({'blarg': 1}, fields=[]) print('------------order 1 blarg 2 no fields---------') _, bla = client.select({'order': 1, 'blarg': 2}) print(bla) print('------------order >= 4 order, blarg and mean sent back, also sorted---------') _, results = client.select({'order': {'>=': 4}}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) for k in results: print(k, results[k]) print('------------order 1 blarg >= 1 fields blarg and std---------') _, results = client.select({'blarg': {'>=': 1}, 'order': 1}, fields=['blarg', 'std']) for k in results: print(k, results[k]) print('------now computing vantage point stuff---------------------') print("VPS", vpkeys) # # # we first create a query time series. _, query = tsmaker(0.5, 0.2, 0.1) # query = tsdict['ts-6'] # # # your code here begins # # # Step 1: in the vpdist key, get distances from query to vantage points # # this is an augmented select res = client.augmented_select('corr', ['dist'], query, {'vp': True}) print(res) # # 1b: choose the lowest distance vantage point # # you can do this in local code d_res = res[1] sorted_res = [] for k, v in d_res.items(): sorted_res.append((v['dist'], k)) sorted_res.sort() D = sorted_res[0][0] D_vp = vpkeys.index(sorted_res[0][1]) print (sorted_res, 'D = ', D) # # Step 2: find all time series within 2*d(query, nearest_vp_to_query) # # this is an augmented select to the same proc in correlation res2 = client.augmented_select('corr', ['dist'], query, {'d_vp-{}'.format(D_vp): {'<=': 2*D}}) print (res2) # # 2b: find the smallest distance amongst this ( or k smallest) d_res = res2[1] sorted_res = [] for k, v in d_res.items(): sorted_res.append( (v['dist'], k) ) sorted_res.sort() D = sorted_res[0][0] D_k = sorted_res[0][1] print (sorted_res, 'D = ', D, 'D_k = ', D_k) nearestwanted = D_k # # you can do this in local code # # your code here ends # # plot the timeseries to see visually how we did. import matplotlib matplotlib.use('qt4agg') import matplotlib.pyplot as plt plt.plot(query, label='') plt.plot(tsdict[nearestwanted]) plt.show()