Retrieve Top-k trajectory

Description: Geneate top-k trajectory
URI: /algo/BT/<name>
http method: GET
paremater
    r = <range>
    d = <density>
    type = <search bype: b,d>   default: d
    k = <top k threshold>   default: 5
  • Code

    def test3(name):
      east = request.args.get('east', '-8.648355')
      west = request.args.get('west', '-8.701432')
      north = request.args.get('north', '41.260813')
      south = request.args.get('south', '41.208771')
      r = str(east)+','+str(north)+','+str(west)+','+str(south)
      type = request.args.get('type','b')
      k = int(request.args.get('k',5))
    
      density = request.args.get('d',50000)
    
      tStart = time.time()
      payload = {'r': r, 'd':density}
      r = requests.get('http://127.0.0.1:5566/datasets/ROI/'+name, params = payload)
      ROIs = r.json()['ROI']
    
      tStop = time.time()
    
      t_roi = tStop - tStart
      tStart = time.time()
    
      ROI_DB = {}
      ROI_rank = []
    
      for roi in ROIs:
          ROI_DB[roi['rid']]=[]
          ROI_rank.append((roi['rid'],roi['score']))
    
      ROI_rank.sort(key = lambda tup: tup[1],reverse=True) #sort by score
    
      if len(ROI_rank) ==0: return jsonify({
          "candidate_tid": [],
          "range":{
              "east": float(east),
              "west": float(west),
              "north": float(north),
              "south": float(south)
          }
          }) 
      ######## Convert Tra to TR  ############
      '''
          TRD:
              Save tid: rid1, rid2, rid3...
      '''
      TRD = {}
    
      tStart = time.time()
    
      ROI_list = [str(roi[0]) for roi in ROI_rank]
    
      import redis
    
      r = redis.StrictRedis(host='localhost', port=6379, db=0)
      pipeline=r.pipeline()
    
      for rid in ROI_list:
          tid_set = r.lrange("rid:"+str(rid),0,-1)
          ROI_DB[int(rid)] = tid_set
    
          for tid in tid_set:
              if int(tid) in TRD:
                  TRD[int(tid)].append(int(rid))
              else:
                  TRD[int(tid)] = [int(rid)]
    
      tid_sets = pipeline.execute()
    
      tStop = time.time()
      t_TRA = tStop - tStart
      ##############  BTS  ###########################
      print "BTS"
    
      LBS = float("infinity")
      SBR = ROI_rank[-1]
    
      tStart = time.time()
    
      candidate = []
    
      complete_tid = set()    # the calculated tid
    
      for ROI in ROI_rank:
          if ROI[0] == SBR[0]:
              logging.info("EARLY STOP")
              break
    
          logging.info('begin getting tid_list')
    
          candidate_id_set = set(map(lambda can: can[0], candidate))
          tid_list = set(ROI_DB[ROI[0]]) - complete_tid #skip calculated tra
          complete_tid = complete_tid | set(ROI_DB[ROI[0]]) # add tids in ROI_DB
    
          logging.info('end getting tid_list')
    
          logging.info('begin Calculate TRD')
          Calculate_TRD_Score(tid_list, ROI_rank, TRD, candidate,type)
          logging.info('end Calculate TRD')
    
          logging.info('begin sort')
          candidate.sort(key = lambda tup: tup[1],reverse=True)
          candidate = candidate[:k]
    
          logging.info('end sort')
    
          if len(candidate) < k:
              LBS = float("infinity")
          else:
              LBS = float(candidate[k-1][1])  #LBS = last score of (tid, score) in candidates
    
          logging.info('begin getting SBR')
          if type == 'd':
              try:
                  SBR = ROI_rank[max( [ i for i in range(len(ROI_rank)) if float(ROI_rank[i][1])>=LBS] )]
              except ValueError:
                  SBR = ROI_rank[-1]
          else:
              try:
                  SBR = ROI_rank[max([i for i in range(len(ROI_rank)) if sum( [ROI_rank[j][1] for j in range(len(ROI_rank)) if j >= i]) >= LBS])]
              except ValueError:
                  SBR = ROI_rank[-1]
    
          logging.info('end getting SBR')
    
      tStop = time.time()
      t_LBS = tStop - tStart
      candidate_set = []
    
      for candidate_tra in candidate:
          r = requests.get('http://127.0.0.1:5566/datasets/trajectory/'+name+'?tid='+str(candidate_tra[0]))
          if len(r.json()['trajectory']) > 0:
              trajectory = r.json()['trajectory'][0]
    
              trajectory['score'] = candidate_tra[1]
    
              candidate_set.append(trajectory)
    
      return jsonify({
              "candidate_tid": [ candidate_set[i] for i in range(len(candidate_set)) if i < k],
              "range":{
                  "east": float(east),
                  "west": float(west),
                  "north": float(north),
                  "south": float(south)
              },
              "time":{
                  "ROI":t_roi,
                  "ROI_DB":t_ROI_DB,
                  "TRD": t_TRA,
                  "LBS": t_LBS
              }
          })
    
  • ver 3


@app.route('/algo/BTS/<name>', methods=['GET'])
def test3(name):
    # paramater
    #           r=0,0,1,1 MUST
    #           type = "b" or "d" / default 'b'
    #           k = 10
    if 'r' not in request.args:
        jsonify({"trajectory": [-1]})
    type = request.args.get('type','b')
    k = int(request.args.get('k',5))
    r = request.args.get('r')
    density = request.args.get('d',0)

    tStart = time.time()
    # get roi in range
    payload = {'r': r, 'd':density}
    r = requests.get('http://127.0.0.1:5566/datasets/ROI/'+name, params = payload)
    ROIs = r.json()['ROI']

    tStop = time.time()

    t_roi = tStop - tStart

    # build ROI database: ROI_DB
    # build ROI rank list :ROI_rank
    '''
        ROI_DB: 紀錄 invert list, rid -> tid1 tid2 tid3... 
        ROI_rank: 紀錄 ROI score,並依分數排列 (rid, score)
    '''
    tStart = time.time()

    ROI_DB = {}
    ROI_rank = []

    for roi in ROIs:
        ROI_DB[roi['rid']]=[]
        ROI_rank.append((roi['rid'],roi['score']))

    ROI_rank.sort(key = lambda tup: tup[1],reverse=True) #依照score排序

    if len(ROI_rank) ==0: return jsonify({'candidate_tid':[]})

    tStop = time.time()
    t_index = tStop - tStart

    # get trajectory in range


    tStart = time.time()


    tStop = time.time()
    t_tid = tStop - tStart


######## Convert Tra to TR  ############
    TRD = {}
    cur = conn.cursor()
    TRD_name = name + "_trd" 
    tStart = time.time()

    ROI_list = [str(roi[0]) for roi in ROI_rank]


    query = "SELECT * from roi." + TRD_name + " where rid in (" + ', '.join(ROI_list) + ") ORDER BY tid, index;"


    try:
        cur.execute(query)
    except psycopg2.Error as e:
        print query
        conn.rollback()
        cur.close()
        return jsonify({"ERROR": query})

    seq = []
    previous_tid = -1
    for points in cur:
        tid, index, rid = points

        if tid != previous_tid: #complete trajectory
            if len(seq)>0:
                TRD[int(tid)] = seq
                for rid in set(seq):
                    ROI_DB[int(rid)].append(int(tid))
            seq = []
            previous_tid = tid

        if len(seq) == 0:
            seq.append(int(rid))
        elif seq[-1] != rid:
            seq.append(int(rid))


    tStop = time.time()
    t_TRA = tStop - tStart

##############  BTS  ###########################
    print "BTS"



    LBS = float("infinity")
    SBR = ROI_rank[-1]

    tStart = time.time()

    candidate = []

    for ROI in ROI_rank:
        if ROI[0] == SBR[0]: break

        #tid_list = ROI_DB[ROI[0]]
        logging.info('begin getting tid_list')

        candidate_id_set = set(map(lambda can: can[0], candidate))

        tid_list = set(ROI_DB[ROI[0]]) - candidate_id_set
        logging.info('end getting tid_list')

        print ROI[0]

        tid_que = Queue.Queue()

        logging.info('put in queue')
        for tid in tid_list:
            tid_que.put(int(tid))
        logging.info('end putting in queue')

        NUM_THREADS = 1


        threads = map(lambda i: threading.Thread(target=Calculate_TRD_Score, args=(tid_que, ROI_rank, TRD, candidate,type)), xrange(NUM_THREADS))

        map(lambda th: th.start(), threads)
        map(lambda th: th.join(), threads)


        logging.info('begin sort')
        candidate.sort(key = lambda tup: tup[1],reverse=True)
        logging.info('end sort')


        if len(candidate) < k:
            LBS = float("infinity")
        else:
            LBS = float(candidate[k-1][1])

        logging.info('begin getting SBR')
        if type == 'd':
            try:
                SBR = ROI_rank[max( [ i for i in range(len(ROI_rank)) if float(ROI_rank[i][1])>=LBS] )]
            except ValueError:
                SBR = ROI_rank[-1]
        else:
            try:
                SBR = ROI_rank[max([i for i in range(len(ROI_rank)) if sum( [ROI_rank[j][1] for j in range(len(ROI_rank)) if j >= i]) >= LBS])]
            except ValueError:
                SBR = ROI_rank[-1]
        logging.info('end getting SBR')
    tStop = time.time()

    t_LBS = tStop - tStart

    candidate_set = []

    for candidate_tra in candidate:
        r = requests.get('http://127.0.0.1:5566/datasets/trajectory/'+name+'?tid='+str(candidate_tra[0]))
        if len(r.json()['trajectory']) > 0:
            trajectory = r.json()['trajectory'][0]

            trajectory['score'] = candidate_tra[1]

            candidate_set.append(trajectory)

    return jsonify({"candidate_tid": [ candidate_set[i] for i in range(len(candidate_set)) if i < k]})
  • ver2

@app.route('/algo/BTS/<name>', methods=['GET'])
def BTS(name):
    # paramater
    #           r=0,0,1,1 MUST
    #           type = "b" or "d" / default 'b'
    #           k = 10
    if 'r' not in request.args:
        jsonify({"trajectory": [-1]})
    type = request.args.get('type','b')
    k = int(request.args.get('k',5))
    r = request.args.get('r')
    density = request.args.get('d',0)

    # get roi in range
    payload = {'r': r, 'd':density}
    r = requests.get('http://127.0.0.1:5566/datasets/ROI/'+name, params = payload)
    ROIs = r.json()['ROI']


    # build ROI database: ROI_DB
    # build ROI rank list :ROI_rank
    '''
        idx: ROI rtree
        ROI_DB: 紀錄 invert list, rid -> tid1 tid2 tid3... 
        ROI_rank: 紀錄 ROI score,並依分數排列 (rid, score)
    '''
    idx = index.Index()
    ROI_DB = {}
    ROI_rank = []
    for roi in ROIs:
        ROI_DB[roi['rid']]=[]
        ROI_rank.append((roi['rid'],roi['score']))

        left, bottom, right, top = (roi['range']['west'],roi['range']['south'],roi['range']['east'],roi['range']['north'])
        idx.insert(int(roi['rid']), (float(left), float(bottom), float(right), float(top)))


    ROI_rank.sort(key = lambda tup: tup[1],reverse=True) #依照score排序

    if len(ROI_rank) ==0: return jsonify({'candidate_tid':[]})

    # get trajectory in range
    cur = conn.cursor()

    if not re.match('[-+]?[0-9]*\.?[0-9]+\,[-+]?[0-9]*\.?[0-9]+,[-+]?[0-9]*\.?[0-9]+\,[-+]?[0-9]*\.?[0-9]+',request.args['r']):
        return error_request()

    bounds = request.args['r'].split(",")
    east = max(float(bounds[0]), float(bounds[2]))
    west = min(float(bounds[0]), float(bounds[2]))
    north = max(float(bounds[1]), float(bounds[3]))
    south = min(float(bounds[1]), float(bounds[3]))

    query = "select distinct(tid) from trajectory." + name + " where (lon between " + str(west) + " and " + str(east) + ") and (lat between " + str(south) +" and "+ str(north) +") limit 100000;"



    try:
        cur.execute(query)
    except psycopg2.Error as e:
        print query
        conn.rollback()
        cur.close()
        return error_trajectory()

    rows = [r for r in cur]

    # Convert Tra to TR
    TRD = {}

    #return jsonify({'tid': rows})

    for tids in rows:
        tid = tids[0]
        r = requests.get('http://127.0.0.1:5566/datasets/trajectory/'+name+'?tid='+str(tid))


        if len(r.json()['trajectory']) > 0:

            trajectory = r.json()['trajectory'][0]

            payload_params = {}

            # fixed density: 5000
            # Todo: 預先算TRD? 刪除ROI?
            '''
            payload_params['d'] = density
            r = requests.get('http://127.0.0.1:5566/algo/tra2seq/'+name,json = trajectory, params = payload_params)
            seq = r.json()['seq']
            '''
            seq = []
            for points in trajectory['points']:
                rid_list = list(idx.intersection((float(points['lon']), float(points['lat']))))

                if len(rid_list) == 0:
                    continue
                rid = rid_list[0]

                if len(seq) == 0:
                    seq.append(rid)
                elif seq[-1] != rid:
                    seq.append(rid)

            print seq

            if len(seq) > 0:
                TRD[int(tid)] = seq

                temp_DB = []

                for rid in seq:
                    temp_DB.append(int(rid))
                for rid in set(temp_DB):
                    ROI_DB[int(rid)].append(int(tid))
    #BTS



    LBS = float("infinity")
    SBR = ROI_rank[-1]




    candidate = []

    for ROI in ROI_rank:
        if ROI[0] == SBR[0]: break

        tid_list = ROI_DB[ROI[0]]

        for tid in tid_list:
            '''
            payload = {'seq':TRD[tid]}
            if type == 'd':
                r = requests.get('http://127.0.0.1:5566/algo/DT/'+name,json = payload)
            else: r = requests.get('http://127.0.0.1:5566/algo/BT/'+name,json = payload)

            score = float(r.json()['score'])
            '''

            seq = TRD[tid]

            size = len(seq)

            score_list = []

            for rid in seq:

                ROI_list = [ROI for ROI in ROI_rank if ROI[0]==rid]

                if len(ROI_list) > 0:
                    score_list.append(ROI_list[0][1])

            if type == 'd':
                score = sum(score_list)/len(seq)
            else:
                score = sum(score_list)

            if (tid, score) not in candidate:
                candidate.append((tid, score))

        candidate.sort(key = lambda tup: tup[1],reverse=True)

        if len(candidate) < k:
            LBS = float("infinity")
        else:
            LBS = float(candidate[k-1][1])


        if type == 'd':
            try:
                SBR = ROI_rank[max( [ i for i in range(len(ROI_rank)) if float(ROI_rank[i][1])>=LBS] )]
            except ValueError:
                SBR = ROI_rank[-1]
        else:
            try:
                SBR = ROI_rank[max([i for i in range(len(ROI_rank)) if sum( [ROI_rank[j][1] for j in range(len(ROI_rank)) if j >= i]) >= LBS])]
            except ValueError:
                SBR = ROI_rank[-1]

    return jsonify({"candidate_tid": [ candidate[i] for i in range(len(candidate)) if i < k]})
@app.route('/algo/BTS/<name>', methods=['GET'])
def BTS(name):
    # paramater
    #           r=0,0,1,1 MUST
    #           type = "b" or "d" / default 'b'
    #           k = 10
    if 'r' not in request.args:
        jsonify({"trajectory": [-1]})
    type = request.args.get('type','b')
    k = request.args.get('k',5)
    r = request.args.get('r')
    density = request.args.get('d',0)

    # get roi in range
    payload = {'r': r, 'd':density}
    r = requests.get('http://127.0.0.1:5566/datasets/ROI/'+name, params = payload)
    ROIs = r.json()['ROI']




    # build ROI database: ROI_DB
    # build ROI rank list :ROI_rank
    ROI_DB = {}
    ROI_rank = []
    for roi in ROIs:
        ROI_DB[roi['rid']]=[]
        ROI_rank.append((roi['rid'],roi['score']))

    ROI_rank.sort(key = lambda tup: tup[1],reverse=True) #依照score排序

    if len(ROI_rank) ==0: return jsonify({'candidate_tid':[]})

    # get trajectory in range
    cur = conn.cursor()

    if not re.match('[-+]?[0-9]*\.?[0-9]+\,[-+]?[0-9]*\.?[0-9]+,[-+]?[0-9]*\.?[0-9]+\,[-+]?[0-9]*\.?[0-9]+',request.args['r']):
        return error_request()

    bounds = request.args['r'].split(",")
    east = max(bounds[0], bounds[2])
    west = min(bounds[0], bounds[2])
    north = max(bounds[1], bounds[3])
    south = min(bounds[1], bounds[3])

    query = "select distinct(tid) from trajectory." + name + " where (lon between " + west + " and " + east + ") and (lat between " + south +" and "+ north +");"
    try:
        cur.execute(query)
    except psycopg2.Error as e:
        print query
        conn.rollback()
        cur.close()
        return error_trajectory()
    rows = [r for r in cur]

    # Convert Tra to TR
    TRD = {}



    for tids in rows:
        tid = tids[0]
        r = requests.get('http://127.0.0.1:5566/datasets/trajectory/'+name+'?tid='+str(tid))


        if len(r.json()['trajectory']) > 0:

            trajectory = r.json()['trajectory'][0]

            payload_params = {}

            # fixed density: 5000
            # Todo: 預先算TRD? 刪除ROI?
            payload_params['d'] = density
            r = requests.get('http://127.0.0.1:5566/algo/tra2seq/'+name,json = trajectory, params = payload_params)
            seq = r.json()['seq']

            print seq

            if len(seq) > 0:
                TRD[int(tid)] = seq

                temp_DB = []

                for rid in seq:
                    temp_DB.append(int(rid))
                for rid in set(temp_DB):
                    ROI_DB[int(rid)].append(int(tid))
    #BTS



    LBS = float("infinity")
    SBR = ROI_rank[-1]




    candidate = []

    for ROI in ROI_rank:
        if ROI[0] == SBR[0]: break

        tid_list = ROI_DB[ROI[0]]

        for tid in tid_list:
            payload = {'seq':TRD[tid]}
            if type == 'd':
                r = requests.get('http://127.0.0.1:5566/algo/DT/'+name,json = payload)
            else: r = requests.get('http://127.0.0.1:5566/algo/BT/'+name,json = payload)


            score = float(r.json()['score'])

            if (tid, score) not in candidate:
                candidate.append((tid, score))

        candidate.sort(key = lambda tup: tup[1],reverse=True)

        if len(candidate) <= k: continue

        LBS = float(candidate[k-1][1])

        if type == 'd':
            try:
                SBR = ROI_rank[max( [ i for i in range(len(ROI_rank)) if float(ROI_rank[i][1])>=LBS] )]
            except ValueError:
                SBR = ROI_rank[-1][0]
        else:
            try:
                SBR = ROI_rank[max([i for i in range(len(ROI_rank)) if sum( [ROI_rank[j][1] for j in range(len(ROI_rank)) if j >= i]) >= LBS])]
            except ValueError:
                SBR = ROI_rank[-1][0]

    return jsonify({"candidate_tid": [ candidate[i] for i in range(len(candidate)) if i < k]})

results matching ""

    No results matching ""