<s> from __future__ import division , print_function <EOL> from tribeflow import _learn <EOL> from tribeflow . mycollections . stamp_lists import StampLists <EOL> import tribeflow <EOL> import pandas as pd <EOL> import plac <EOL> import numpy as np <EOL> def main ( model , out_fpath ) : <EOL> store = pd . HDFStore ( model ) <EOL> from_ = store [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> to = store [ '<STR_LIT:to>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert from_ == <NUM_LIT:0> <EOL> trace_fpath = store [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> kernel_class = store [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> kernel_class = eval ( kernel_class ) <EOL> Theta_zh = store [ '<STR_LIT>' ] . values <EOL> Psi_sz = store [ '<STR_LIT>' ] . values <EOL> count_z = store [ '<STR_LIT>' ] . values [ : , <NUM_LIT:0> ] <EOL> P = store [ '<STR_LIT:P>' ] . values <EOL> residency_priors = store [ '<STR_LIT>' ] . values [ : , <NUM_LIT:0> ] <EOL> previous_stamps = StampLists ( count_z . shape [ <NUM_LIT:0> ] ) <EOL> mem_size = store [ '<STR_LIT>' ] . values . shape [ <NUM_LIT:1> ] <EOL> tstamps = store [ '<STR_LIT>' ] . values [ : , <NUM_LIT:0> ] <EOL> assign = store [ '<STR_LIT>' ] . values [ : , <NUM_LIT:0> ] <EOL> for z in xrange ( count_z . shape [ <NUM_LIT:0> ] ) : <EOL> idx = assign == z <EOL> previous_stamps . _extend ( z , tstamps [ idx ] ) <EOL> hyper2id = dict ( store [ '<STR_LIT>' ] . values ) <EOL> obj2id = dict ( store [ '<STR_LIT>' ] . values ) <EOL> HSDs = [ ] <EOL> Dts = [ ] <EOL> with open ( trace_fpath ) as trace_file : <EOL> for i , l in enumerate ( trace_file ) : <EOL> if i < to : <EOL> continue <EOL> spl = l . strip ( ) . split ( '<STR_LIT:\t>' ) <EOL> dts_line = [ float ( x ) for x in spl [ : mem_size ] ] <EOL> h = spl [ mem_size ] <EOL> d = spl [ - <NUM_LIT:1> ] <EOL> sources = spl [ mem_size + <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> all_in = h in hyper2id and d in obj2id <EOL> for s in sources : <EOL> all_in = all_in and s in obj2id <EOL> if all_in : <EOL> trace_line = [ hyper2id [ h ] ] + [ obj2id [ s ] for s in sources ] + [ obj2id [ d ] ] <EOL> HSDs . append ( trace_line ) <EOL> Dts . append ( dts_line ) <EOL> trace_size = sum ( count_z ) <EOL> kernel = kernel_class ( ) <EOL> kernel . build ( trace_size , count_z . shape [ <NUM_LIT:0> ] , residency_priors ) <EOL> kernel . update_state ( P ) <EOL> num_queries = min ( <NUM_LIT> , len ( HSDs ) ) <EOL> queries = np . random . choice ( len ( HSDs ) , size = num_queries ) <EOL> HSDs = np . array ( HSDs , dtype = '<STR_LIT>' ) [ queries ] . copy ( ) <EOL> Dts = np . array ( Dts , dtype = '<STR_LIT:d>' ) [ queries ] . copy ( ) <EOL> rrs = _learn . reciprocal_rank ( Dts , HSDs , previous_stamps , Theta_zh , Psi_sz , count_z , kernel ) <EOL> np . savetxt ( out_fpath , rrs ) <EOL> print ( rrs . mean ( axis = <NUM_LIT:0> ) ) <EOL> store . close ( ) <EOL> plac . call ( main ) </s>
