Ejemplo n.º 1
0
def find_match():
    side = int(f.request.form['side'])
    assert side in [0, 1]
    _id = f.request.form['_id']
    first = DEST if side else ORIGIN
    second = ORIGIN if side else DEST
    query, res_ids, answers, dsts, among = cn.find_closest(_id, first, second)
    _ = cn.interpret(first['features'][query, :],
                     second['features'][answers[0], :])
    query_info, first_answer, feature_order = _
    answers_info = [first_answer]
    answers_info.extend([
        cn.interpret(first['features'][query, :],
                     second['features'][answer, :], feature_order)[1]
        for answer in answers[1:]
    ])
    sendf = lambda x, p: ('{:.' + str(p) + 'f}').format(float(x))
    res = {
        'query': query_info,
        'answers_id': list(res_ids),
        'distances': [sendf(d, 5) for d in dsts],
        'explanations': answers_info,
        'among': among
    }
    return f.jsonify(r=res)
Ejemplo n.º 2
0
def find_match():
    side = int(f.request.form['side'])
    assert side in [0, 1]
    _id = f.request.form['_id']
    first = DEST if side else ORIGIN
    second = ORIGIN if side else DEST
    query, res_ids, answers, dsts, among = cn.find_closest(_id, first, second)
    _ = cn.interpret(first['features'][query, :],
                     second['features'][answers[0], :])
    query_info, first_answer, feature_order = _
    answers_info = [first_answer]
    answers_info.extend([cn.interpret(first['features'][query, :],
                                      second['features'][answer, :],
                                      feature_order)[1]
                         for answer in answers[1:]])
    sendf = lambda x, p: ('{:.'+str(p)+'f}').format(float(x))
    res = {'query': query_info, 'answers_id': list(res_ids),
           'distances': [sendf(d, 5) for d in dsts],
           'explanations': answers_info, 'among': among}
    return f.jsonify(r=res)
Ejemplo n.º 3
0
def get_knn_candidates(vids, left_knn, right_knn, at_least, at_most=None):
    """Return between `at_least` and `at_most` venue in right that are close (in
    the sense of euclidean distance) of the `vids` in left. Namely, it return
    their row number and their ids."""
    import heapq
    candidates = []
    candidates_id = []
    knn = right_knn['knn']
    at_most = int(at_most) or 50000
    nb_venues = min(at_most, max(len(vids)*knn, at_least))
    for idx, vid in enumerate(vids):
        _, rid, ridx, dst, _ = cn.find_closest(vid, left_knn, right_knn)
        for dst_, rid_, ridx_, idx_ in zip(dst, rid, ridx, range(knn)):
            if rid_ not in candidates_id:
                candidates_id.append(rid_)
                heapq.heappush(candidates, (dst_, idx*knn+idx_,
                                            (rid_, ridx_)))
    nb_venues = min(len(candidates), int(nb_venues))
    closest = heapq.nsmallest(nb_venues, candidates)
    mask = np.array([v[2][1] for v in closest])
    r_vids = np.array([v[2][0] for v in closest])
    return mask, r_vids
Ejemplo n.º 4
0
def get_knn_candidates(vids, left_knn, right_knn, at_least, at_most=None):
    """Return between `at_least` and `at_most` venue in right that are close (in
    the sense of euclidean distance) of the `vids` in left. Namely, it return
    their row number and their ids."""
    import heapq
    candidates = []
    candidates_id = []
    knn = right_knn['knn']
    at_most = int(at_most) or 50000
    nb_venues = min(at_most, max(len(vids) * knn, at_least))
    for idx, vid in enumerate(vids):
        _, rid, ridx, dst, _ = cn.find_closest(vid, left_knn, right_knn)
        for dst_, rid_, ridx_, idx_ in zip(dst, rid, ridx, range(knn)):
            if rid_ not in candidates_id:
                candidates_id.append(rid_)
                heapq.heappush(candidates,
                               (dst_, idx * knn + idx_, (rid_, ridx_)))
    nb_venues = min(len(candidates), int(nb_venues))
    closest = heapq.nsmallest(nb_venues, candidates)
    mask = np.array([v[2][1] for v in closest])
    r_vids = np.array([v[2][0] for v in closest])
    return mask, r_vids