Exemplo n.º 1
0
 def test_rank_fusion(self):
     treestr = '{"$and": [{"reading": {"$gte": 0.1}}, {"teaching_or_training": {"$gte": 0.1}}, {"$or": [{"research": {"$gte": 0.05}}, {"eating": {"$gte": 0.2}}]}]}'
     tree = json.loads(treestr)
     print tree
     qt = RankFusionQueryTree(tree, debug=True)
     results = qt.sql()
     print "Result = ", results
     self.assertEqual(results,"SELECT v_id, reading, teaching_or_training, research, eating, MAX( ( RANK() OVER (ORDER BY reading), RANK() OVER (ORDER BY teaching_or_training), RANK() OVER (ORDER BY MAX(research,eating)) ) ) as result FROM clip_calib_scores  ORDER BY result ASC LIMIT 0,50;")
Exemplo n.º 2
0
def query_clip():
    """
    On submisison gets the query
    "algo" is avg by default
    "rank_fusion" to be supported soon

    """
    algo = flask.request.args.get("algo", "avg")
    tree = flask.request.args.get('tree', None)

    skip = int(flask.request.args.get('skip', '0'))
    limit = int(flask.request.args.get("limit", '30'))

    # Unescape the incoming tree information
    query = json.loads(urllib2.unquote(tree).decode('utf-8'))

    # Parse the query
    obj = {}
    obj["query"] = {}
    obj["query"]["algo"] = algo
    obj["query"]["tree"] = query
    obj["query"]["treestr"] = json.dumps(query)

    tstart = time.time()
    # Process the tree here
    if algo=="avg":
        # Browse through the tree

        # TODO: Work with trees with depth higher than one

        pass
    elif algo=="rank_fusion":

        pass
    else:
        obj["error"] = "Unknown algo"
        return flask.jsonify(obj)

    engine = RankFusionQueryTree(query)
    obj["clips"] = list(engine.process())[:limit]
    obj["count"] = len(obj["clips"])

    # Send the results back
    tend = time.time()
    obj["query"]["time"] = (tend - tstart)
    return flask.jsonify(obj)

    cur = db[dataset].find(query, { "_id" : 1 }, skip=skip, limit=20)
    obj["count"] = cur.count()

    imgs = {}

    count = 0
    iter = 0
    while 1:
        cur = db[dataset].find(query, { "thumb" : 0, "scores" : { "$slice" : [attr_id, 1]}}, skip=skip, limit=limit).sort("scores.%d" % (attr_id), pymongo.DESCENDING)
        iter = iter + 1
        if count >= limit or iter > 5:
            break;

        for animage in cur:
            # Append to the images
            v_id = "Ground truth"
            if "v_id" in animage:
                v_id = animage["v_id"]

            if v_id in imgs:
                imgs[v_id].append([ str(animage["_id"]), animage["scores"][0]])
            else:
                imgs[v_id] = [[ str(animage["_id"]), animage["scores"][0]], ]
                count = count + 1
                skip = skip + limit

    cliplist = []
    for akey in imgs.keys():
        cliplist.append({'clip_id' : akey, 'high_score' : imgs[akey][0][1], 'images' : imgs[akey]})

    obj["clips"] = sorted(cliplist, key=lambda k: k['high_score'], reverse=True)

    tend = time.time()
    obj["query"] = { 'dataset' : dataset, 'skip' : skip, "time" : (tend - tstart), "tree" : query, "sort" : sort }
    return jsonify(obj)