python/scripts/homework_1/benchmark.py
def measure_lsh_recall(
    n_docs=512,
    n_rows=1000003,
    minhash_size=100,
    lsh_nbands=20,
    jsim_threshold=0.7
):
    """Computes the recall of the LSH algorithm by comparing to "brute force"
    calculation"""
    job_name = 'ndocs={}_nrows={}_minhash_size={}_lsh_nbands={}'.format(
        n_docs,
        n_rows,
        minhash_size,
        lsh_nbands
    )
    job_path = os.path.join(OUTPUT_PATH, job_name)
    mkdirp(job_path)

    top_sim_path = os.path.join(job_path, 'top_similarities_index.json')
    if not os.path.exists(top_sim_path):
        run_timing_job(n_docs, n_rows, minhash_size, lsh_nbands)
    with open(top_sim_path) as f:
        top_similarities_index = json.load(f)
    top_similar_set_index = {
        int(doc_idx): set([sim_doc[0] for sim_doc in sim_docs])
        for doc_idx, sim_docs in top_similarities_index.items()
    }
    jsim_matrix = get_jsim_matrix(n_docs, n_rows)
    tps = 0
    fns = 0
    true_similar_index = {}
    for i in range(n_docs):
        similar_docs = set(np.where(jsim_matrix[i] > jsim_threshold)[0]) - {i}
        lsh_similar_docs = top_similar_set_index.get(i, set([]))
        intersection = similar_docs.intersection(lsh_similar_docs)
        missing = similar_docs - intersection
        tps += len(intersection)
        fns += len(missing)
        if len(similar_docs) > 0:
            true_similar_index[i] = similar_docs

    recall = tps / (tps + fns) if (tps + fns) > 0 else np.nan
    p_candidate_at_threshold = get_p_lsh_candidate(
        jsim=jsim_threshold,
        n_bands=lsh_nbands,
        n_rows_per_band=minhash_size / lsh_nbands
    )
    result = {
        'ndocs': n_docs,
        'nrows': n_rows,
        'minhash_size': minhash_size,
        'lsh_nbands': lsh_nbands,
        'nrows_per_band': minhash_size / lsh_nbands,
        'jsim_threshold': jsim_threshold,
        'p_candidate_at_threshold': p_candidate_at_threshold,
        'support': tps + fns,
        'recall': recall,
    }
    print(result)

    return result
Computes the recall of the LSH algorithm by comparing to "brute force"
    calculation
Computes the recall of the LSH algorithm by comparing to "brute force"
calculation
Computes the recall of the LSH algorithm by comparing to " brute force " calculation
def measure_lsh_recall(
    n_docs=512,
    n_rows=1000003,
    minhash_size=100,
    lsh_nbands=20,
    jsim_threshold=0.7
):
    job_name = 'ndocs={}_nrows={}_minhash_size={}_lsh_nbands={}'.format(
        n_docs,
        n_rows,
        minhash_size,
        lsh_nbands
    )
    job_path = os.path.join(OUTPUT_PATH, job_name)
    mkdirp(job_path)
    top_sim_path = os.path.join(job_path, 'top_similarities_index.json')
    if not os.path.exists(top_sim_path):
        run_timing_job(n_docs, n_rows, minhash_size, lsh_nbands)
    with open(top_sim_path) as f:
        top_similarities_index = json.load(f)
    top_similar_set_index = {
        int(doc_idx): set([sim_doc[0] for sim_doc in sim_docs])
        for doc_idx, sim_docs in top_similarities_index.items()
    }
    jsim_matrix = get_jsim_matrix(n_docs, n_rows)
    tps = 0
    fns = 0
    true_similar_index = {}
    for i in range(n_docs):
        similar_docs = set(np.where(jsim_matrix[i] > jsim_threshold)[0]) - {i}
        lsh_similar_docs = top_similar_set_index.get(i, set([]))
        intersection = similar_docs.intersection(lsh_similar_docs)
        missing = similar_docs - intersection
        tps += len(intersection)
        fns += len(missing)
        if len(similar_docs) > 0:
            true_similar_index[i] = similar_docs
    recall = tps / (tps + fns) if (tps + fns) > 0 else np.nan
    p_candidate_at_threshold = get_p_lsh_candidate(
        jsim=jsim_threshold,
        n_bands=lsh_nbands,
        n_rows_per_band=minhash_size / lsh_nbands
    )
    result = {
        'ndocs': n_docs,
        'nrows': n_rows,
        'minhash_size': minhash_size,
        'lsh_nbands': lsh_nbands,
        'nrows_per_band': minhash_size / lsh_nbands,
        'jsim_threshold': jsim_threshold,
        'p_candidate_at_threshold': p_candidate_at_threshold,
        'support': tps + fns,
        'recall': recall,
    }
    print(result)
    return result
def measure_lsh_recall ( n_docs = 512 , n_rows = 1000003 , minhash_size = 100 , lsh_nbands = 20 , jsim_threshold = 0.7 ) : job_name = 'ndocs={}_nrows={}_minhash_size={}_lsh_nbands={}' . format ( n_docs , n_rows , minhash_size , lsh_nbands ) job_path = os . path . join ( OUTPUT_PATH , job_name ) mkdirp ( job_path ) top_sim_path = os . path . join ( job_path , 'top_similarities_index.json' ) if not os . path . exists ( top_sim_path ) : run_timing_job ( n_docs , n_rows , minhash_size , lsh_nbands ) with open ( top_sim_path ) as f : top_similarities_index = json . load ( f ) top_similar_set_index = { int ( doc_idx ) : set ( [ sim_doc [ 0 ] for sim_doc in sim_docs ] ) for doc_idx , sim_docs in top_similarities_index . items ( ) } jsim_matrix = get_jsim_matrix ( n_docs , n_rows ) tps = 0 fns = 0 true_similar_index = { } for i in range ( n_docs ) : similar_docs = set ( np . where ( jsim_matrix [ i ] > jsim_threshold ) [ 0 ] ) - { i } lsh_similar_docs = top_similar_set_index . get ( i , set ( [ ] ) ) intersection = similar_docs . intersection ( lsh_similar_docs ) missing = similar_docs - intersection tps += len ( intersection ) fns += len ( missing ) if len ( similar_docs ) > 0 : true_similar_index [ i ] = similar_docs recall = tps / ( tps + fns ) if ( tps + fns ) > 0 else np . nan p_candidate_at_threshold = get_p_lsh_candidate ( jsim = jsim_threshold , n_bands = lsh_nbands , n_rows_per_band = minhash_size / lsh_nbands ) result = { 'ndocs' : n_docs , 'nrows' : n_rows , 'minhash_size' : minhash_size , 'lsh_nbands' : lsh_nbands , 'nrows_per_band' : minhash_size / lsh_nbands , 'jsim_threshold' : jsim_threshold , 'p_candidate_at_threshold' : p_candidate_at_threshold , 'support' : tps + fns , 'recall' : recall , } print ( result ) return result
Computes the recall of the LSH algorithm by comparing to "brute force"
calculation
Computes the recall of the LSH algorithm by comparing to " brute force " calculation
"""Computes the recall of the LSH algorithm by comparing to "brute force"
    calculation"""
n_docs
n_rows
minhash_size
lsh_nbands
jsim_threshold