Example #1
0
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)
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():
    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()
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():
    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()