python/scripts/homework_1/benchmark.py
def run_timing_job(
    n_docs,
    n_rows,
    minhash_size,
    lsh_nbands
):
    """Times the execution of computing Min Hashes and running LSH.
    """
    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)
    result_path = os.path.join(job_path, 'result.json')

    if os.path.exists(result_path):
        with open(result_path) as f:
            result = json.load(f)

        return result

    def primary_hash(s):
        """Pythons default hash(str) returns value in range
            [-(sys.maxint + 1):sys.maxint]
        """
        return hash(s) % n_rows

    print('loading documents')
    documents = list(get_news_groups_documents(first_n=n_docs))

    print('extracting shingle hashes')
    with timer() as prep_timer:
        shingle_sequences = [
            get_shingles(doc, k=SHINGLES_SIZE) for doc in documents
        ]
        hash_sets = [
            get_hash_set(ss, hash_f=primary_hash) for ss in shingle_sequences
        ]

    print('computing minhash signatures')
    mh = MinHashing(n_rows=n_rows, n_hash_fs=minhash_size)
    with timer() as minhash_timer:
        signatures = mh(hash_sets)

    print('finding similar documents using LSH')
    lsh = LocalitySensitiveHashing(n_bands=lsh_nbands)
    with timer() as lsh_timer:
        top_similar_index = lsh.get_top_similar_index(signatures)

    top_similarities_index = get_jsim_and_approx_jsim(
        top_similar_index,
        hash_sets,
        signatures
    )
    pprint(top_similarities_index)
    with open(os.path.join(job_path, 'top_similarities_index.json'), 'w') as f:
        json.dump(top_similarities_index, f)

    diffs = get_approximation_diffs(top_similarities_index)
    diff_mean = np.mean(diffs)
    diff_std = np.std(diffs)
    f, ax = plt.subplots(1, 1)
    ax.hist(diffs, bins=np.arange(-0.3, 0.31, 0.025))
    ax.set_xlim(-0.3, 0.3)
    f.set_size_inches((8, 6))
    f.savefig(os.path.join(job_path, 'error_dist.png'), format='png')

    result = {
        'ndocs': n_docs,
        'nrows': n_rows,
        'minhash_size': minhash_size,
        'lsh_nbands': lsh_nbands,
        'diff_mean': diff_mean,
        'diff_std': diff_std,
        'n_matches': len(diffs),
        't_prep': prep_timer.elapsed,
        't_minhash': minhash_timer.elapsed,
        't_lsh': lsh_timer.elapsed
    }
    with open(result_path, 'w') as f:
        json.dump(result, f)

    return result
Times the execution of computing Min Hashes and running LSH.
    
Times the execution of computing Min Hashes and running LSH.
Times the execution of computing Min Hashes and running LSH .
def run_timing_job(
    n_docs,
    n_rows,
    minhash_size,
    lsh_nbands
):
    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)
    result_path = os.path.join(job_path, 'result.json')
    if os.path.exists(result_path):
        with open(result_path) as f:
            result = json.load(f)
        return result
    def primary_hash(s):
        return hash(s) % n_rows
    print('loading documents')
    documents = list(get_news_groups_documents(first_n=n_docs))
    print('extracting shingle hashes')
    with timer() as prep_timer:
        shingle_sequences = [
            get_shingles(doc, k=SHINGLES_SIZE) for doc in documents
        ]
        hash_sets = [
            get_hash_set(ss, hash_f=primary_hash) for ss in shingle_sequences
        ]
    print('computing minhash signatures')
    mh = MinHashing(n_rows=n_rows, n_hash_fs=minhash_size)
    with timer() as minhash_timer:
        signatures = mh(hash_sets)
    print('finding similar documents using LSH')
    lsh = LocalitySensitiveHashing(n_bands=lsh_nbands)
    with timer() as lsh_timer:
        top_similar_index = lsh.get_top_similar_index(signatures)
    top_similarities_index = get_jsim_and_approx_jsim(
        top_similar_index,
        hash_sets,
        signatures
    )
    pprint(top_similarities_index)
    with open(os.path.join(job_path, 'top_similarities_index.json'), 'w') as f:
        json.dump(top_similarities_index, f)
    diffs = get_approximation_diffs(top_similarities_index)
    diff_mean = np.mean(diffs)
    diff_std = np.std(diffs)
    f, ax = plt.subplots(1, 1)
    ax.hist(diffs, bins=np.arange(-0.3, 0.31, 0.025))
    ax.set_xlim(-0.3, 0.3)
    f.set_size_inches((8, 6))
    f.savefig(os.path.join(job_path, 'error_dist.png'), format='png')
    result = {
        'ndocs': n_docs,
        'nrows': n_rows,
        'minhash_size': minhash_size,
        'lsh_nbands': lsh_nbands,
        'diff_mean': diff_mean,
        'diff_std': diff_std,
        'n_matches': len(diffs),
        't_prep': prep_timer.elapsed,
        't_minhash': minhash_timer.elapsed,
        't_lsh': lsh_timer.elapsed
    }
    with open(result_path, 'w') as f:
        json.dump(result, f)
    return result
def run_timing_job ( n_docs , n_rows , minhash_size , lsh_nbands ) : 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 ) result_path = os . path . join ( job_path , 'result.json' ) if os . path . exists ( result_path ) : with open ( result_path ) as f : result = json . load ( f ) return result def primary_hash ( s ) : """Pythons default hash(str) returns value in range
            [-(sys.maxint + 1):sys.maxint]
        """ return hash ( s ) % n_rows print ( 'loading documents' ) documents = list ( get_news_groups_documents ( first_n = n_docs ) ) print ( 'extracting shingle hashes' ) with timer ( ) as prep_timer : shingle_sequences = [ get_shingles ( doc , k = SHINGLES_SIZE ) for doc in documents ] hash_sets = [ get_hash_set ( ss , hash_f = primary_hash ) for ss in shingle_sequences ] print ( 'computing minhash signatures' ) mh = MinHashing ( n_rows = n_rows , n_hash_fs = minhash_size ) with timer ( ) as minhash_timer : signatures = mh ( hash_sets ) print ( 'finding similar documents using LSH' ) lsh = LocalitySensitiveHashing ( n_bands = lsh_nbands ) with timer ( ) as lsh_timer : top_similar_index = lsh . get_top_similar_index ( signatures ) top_similarities_index = get_jsim_and_approx_jsim ( top_similar_index , hash_sets , signatures ) pprint ( top_similarities_index ) with open ( os . path . join ( job_path , 'top_similarities_index.json' ) , 'w' ) as f : json . dump ( top_similarities_index , f ) diffs = get_approximation_diffs ( top_similarities_index ) diff_mean = np . mean ( diffs ) diff_std = np . std ( diffs ) f , ax = plt . subplots ( 1 , 1 ) ax . hist ( diffs , bins = np . arange ( - 0.3 , 0.31 , 0.025 ) ) ax . set_xlim ( - 0.3 , 0.3 ) f . set_size_inches ( ( 8 , 6 ) ) f . savefig ( os . path . join ( job_path , 'error_dist.png' ) , format = 'png' ) result = { 'ndocs' : n_docs , 'nrows' : n_rows , 'minhash_size' : minhash_size , 'lsh_nbands' : lsh_nbands , 'diff_mean' : diff_mean , 'diff_std' : diff_std , 'n_matches' : len ( diffs ) , 't_prep' : prep_timer . elapsed , 't_minhash' : minhash_timer . elapsed , 't_lsh' : lsh_timer . elapsed } with open ( result_path , 'w' ) as f : json . dump ( result , f ) return result
Times the execution of computing Min Hashes and running LSH.
Times the execution of computing Min Hashes and running LSH .
"""Times the execution of computing Min Hashes and running LSH.
    """ """Pythons default hash(str) returns value in range
            [-(sys.maxint + 1):sys.maxint]
        """
n_docs
n_rows
minhash_size
lsh_nbands