<s> import time <EOL> import numpy as np <EOL> def compute_all_neighbors ( data1 , data2 = None , just_nn = True ) : <EOL> """<STR_LIT>""" <EOL> from scipy . spatial . distance import cdist <EOL> if data2 is None : <EOL> data2 = data1 <EOL> dists = cdist ( data1 , data2 ) <EOL> if just_nn : <EOL> nns = np . zeros ( ( data1 . shape [ <NUM_LIT:0> ] ) , dtype = int ) <EOL> else : <EOL> nns = np . zeros ( dists . shape , dtype = int ) <EOL> for i in xrange ( dists . shape [ <NUM_LIT:0> ] ) : <EOL> if just_nn : <EOL> nns [ i ] = np . argmin ( dists [ i ] ) <EOL> else : <EOL> nns [ i ] = np . argsort ( dists [ i ] ) <EOL> return nns <EOL> def get_proportion_nns_with_same_coarse_codes ( data , model , nns = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> N = data . shape [ <NUM_LIT:0> ] <EOL> if nns is None : <EOL> nns = compute_all_neighbors ( data ) <EOL> coarse_codes = [ ] <EOL> for d in data : <EOL> c = model . predict_coarse ( d ) <EOL> coarse_codes . append ( c ) <EOL> count = <NUM_LIT:0> <EOL> for i in xrange ( N ) : <EOL> nn = nns [ i ] <EOL> if coarse_codes [ i ] == coarse_codes [ nn ] : <EOL> count += <NUM_LIT:1> <EOL> return float ( count ) / N <EOL> def get_cell_histogram ( data , model ) : <EOL> cells = [ ] <EOL> for d in data : <EOL> c = model . predict_coarse ( d ) <EOL> cell = model . get_cell_id_for_coarse_codes ( c ) <EOL> cells . append ( cell ) <EOL> return np . histogram ( cells , bins = range ( model . V ** <NUM_LIT:2> ) ) [ <NUM_LIT:0> ] <EOL> def get_proportion_of_reconstructions_with_same_codes ( data , model ) : <EOL> N = data . shape [ <NUM_LIT:0> ] <EOL> count = <NUM_LIT:0> <EOL> for d in data : <EOL> c1 = model . predict ( d ) <EOL> r = model . reconstruct ( c1 ) <EOL> c2 = model . predict ( r ) <EOL> if c1 == c2 : <EOL> count += <NUM_LIT:1> <EOL> return float ( count ) / N <EOL> def get_recall ( searcher , queries , nns , thresholds = [ <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:100> , <NUM_LIT:1000> ] , normalize = True , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> recall = np . zeros ( len ( thresholds ) ) <EOL> query_time = <NUM_LIT:0.0> <EOL> for i , d in enumerate ( queries ) : <EOL> nn = nns [ i ] <EOL> start = time . clock ( ) <EOL> results , cells_visited = searcher . search ( d , thresholds [ - <NUM_LIT:1> ] ) <EOL> query_time += time . clock ( ) - start <EOL> if verbose and i % <NUM_LIT:50> == <NUM_LIT:0> : <EOL> print '<STR_LIT>' % ( cells_visited , i ) <EOL> for j , res in enumerate ( results ) : <EOL> rid , code = res <EOL> if rid == nn : <EOL> for k , t in enumerate ( thresholds ) : <EOL> if j < t : <EOL> recall [ k ] += <NUM_LIT:1> <EOL> if normalize : <EOL> N = queries . shape [ <NUM_LIT:0> ] <EOL> return recall / N , query_time / N <EOL> else : <EOL> return recall , query_time <EOL> def get_subquantizer_distortion ( data , model ) : <EOL> from . model import compute_residuals , project_residuals_to_local <EOL> first_half , second_half = np . split ( data , <NUM_LIT:2> , axis = <NUM_LIT:1> ) <EOL> r1 , a1 = compute_residuals ( first_half , model . Cs [ <NUM_LIT:0> ] ) <EOL> r2 , a2 = compute_residuals ( second_half , model . Cs [ <NUM_LIT:1> ] ) <EOL> p1 = project_residuals_to_local ( r1 , a1 , model . Rs [ <NUM_LIT:0> ] , model . mus [ <NUM_LIT:0> ] ) <EOL> p2 = project_residuals_to_local ( r2 , a2 , model . Rs [ <NUM_LIT:1> ] , model . mus [ <NUM_LIT:1> ] ) <EOL> pall = np . concatenate ( ( p1 , p2 ) , axis = <NUM_LIT:1> ) <EOL> suball = model . subquantizers [ <NUM_LIT:0> ] + model . subquantizers [ <NUM_LIT:1> ] <EOL> dists = np . array ( [ sum ( np . linalg . norm ( compute_residuals ( d , c ) [ <NUM_LIT:0> ] , ord = <NUM_LIT:2> , axis = <NUM_LIT:1> ) ** <NUM_LIT:2> ) for c , d in zip ( suball , np . split ( pall , <NUM_LIT:8> , axis = <NUM_LIT:1> ) ) ] ) <EOL> return dists / data . shape [ <NUM_LIT:0> ] </s>
