"""Turn DIAMOND hits into one row per (query, candidate EC) with features for the reranker.""" import numpy as np import pandas as pd FEATURES = ['best_bits', 'bits_ratio', 'best_pid', 'pid_gap', 'first_rank', 'n_hits', 'hit_frac', 'share1', 'share4', 'l3_share', 'vote_rank', 'best_qcov', 'best_scov', 'log_ev', 'q_best_bits', 'q_best_pid', 'q_hits', 'n_cands', 'log_qlen', 'log_freq'] # Extra support and hierarchy features (added in v5) EXTRA_FEATURES = ['share8', 'top3_hits', 'top10_hits', 'is_top_hit', 'l2_share', 'l1_share', 'mean_pid', 'bits_per_len', 'min_cov', 'inv_labels'] def candidate_features(hits, ecs_of, log_freq): """hits: columns q, s, pid, alen, qlen, slen, ev, bits (any order). Returns one row per candidate EC.""" h = hits.sort_values(['q', 'bits'], ascending=[True, False]).copy() h['rank'] = h.groupby('q').cumcount() + 1 h['ec'] = h.s.map(ecs_of) h['n_labels'] = h.ec.str.len() h = h.explode('ec') # Query-level numbers: how good is the best hit at all, and how many hits are there per_query = h.groupby('q').agg(q_best_bits=('bits', 'max'), q_best_pid=('pid', 'max'), q_hits=('rank', 'max')) h = h.join(per_query, on='q') # Two vote weights: plain bitscore, and the v1 weight (relative bitscore to the 4th power) h['w1'] = h.bits / h.n_labels h['w4'] = (h.bits / h.q_best_bits) ** 4 / h.n_labels h['w8'] = (h.bits / h.q_best_bits) ** 8 / h.n_labels # even sharper: almost only the closest hits count h['qcov'] = h.alen / h.qlen h['scov'] = h.alen / h.slen h['in_top3'] = (h['rank'] <= 3).astype(int) h['in_top10'] = (h['rank'] <= 10).astype(int) c = h.groupby(['q', 'ec']).agg( best_bits=('bits', 'max'), best_pid=('pid', 'max'), first_rank=('rank', 'min'), n_hits=('rank', 'size'), w1=('w1', 'sum'), w4=('w4', 'sum'), best_qcov=('qcov', 'max'), best_scov=('scov', 'max'), min_ev=('ev', 'min'), q_best_bits=('q_best_bits', 'first'), q_best_pid=('q_best_pid', 'first'), q_hits=('q_hits', 'first'), qlen=('qlen', 'first'), w8=('w8', 'sum'), top3_hits=('in_top3', 'sum'), top10_hits=('in_top10', 'sum'), mean_pid=('pid', 'mean'), min_labels=('n_labels', 'min')).reset_index() # Share of the query's total vote that each candidate receives c['share1'] = c.w1 / c.groupby('q').w1.transform('sum') c['share4'] = c.w4 / c.groupby('q').w4.transform('sum') # How much of the vote agrees with this candidate's first three EC digits (family consensus) c['l3'] = c.ec.str.rsplit('.', n=1).str[0] c['l3_share'] = c.groupby(['q', 'l3']).share4.transform('sum') c['vote_rank'] = c.groupby('q').share4.rank(ascending=False, method='first') c['share8'] = c.w8 / c.groupby('q').w8.transform('sum') # Agreement at the first two digits and the first digit, like l3_share c['l2'] = c.ec.str.split('.').str[:2].str.join('.') c['l1'] = c.ec.str.split('.').str[0] c['l2_share'] = c.groupby(['q', 'l2']).share4.transform('sum') c['l1_share'] = c.groupby(['q', 'l1']).share4.transform('sum') c['is_top_hit'] = (c.first_rank == 1).astype(int) c['bits_per_len'] = c.best_bits / c.qlen c['min_cov'] = np.minimum(c.best_qcov.clip(upper=1), c.best_scov.clip(upper=1)) c['inv_labels'] = 1.0 / c.min_labels c['bits_ratio'] = c.best_bits / c.q_best_bits c['pid_gap'] = c.q_best_pid - c.best_pid c['hit_frac'] = c.n_hits / c.q_hits c['n_cands'] = c.groupby('q').ec.transform('size') c['log_ev'] = -np.log10(c.min_ev + 1e-300) c['log_qlen'] = np.log(c.qlen) c['log_freq'] = c.ec.map(log_freq).fillna(0.0) return c CLASSIFIER_FEATURES = ['clf_prob', 'clf_top'] def add_classifier_features(cand, clf_top10): """clf_top10: {query: (ten ECs, ten probabilities)} for queries the classifier was run on. Queries without classifier output keep NaN, which the gradient-boosted model handles as 'missing'.""" probs = {(q, e): p for q, (ecs, ps) in clf_top10.items() for e, p in zip(ecs, ps)} ran = cand.q.isin(set(clf_top10)) cand['clf_prob'] = [probs.get((q, e), 0.0) for q, e in zip(cand.q, cand.ec)] cand['clf_top'] = cand.q.map({q: ps[0] for q, (ecs, ps) in clf_top10.items()}) cand.loc[~ran, 'clf_prob'] = np.nan return cand RELIABILITY_FEATURES = ['ec_reliability', 'ec_reliability_n'] def add_reliability(cand, reliability, overall_rate): """reliability: {EC: (credit earned when proposed, number of times proposed)}, counted on other folds. The rate is smoothed towards the overall rate so rare ECs do not get extreme values.""" earned = cand.ec.map({e: v[0] for e, v in reliability.items()}).fillna(0.0) proposed = cand.ec.map({e: v[1] for e, v in reliability.items()}).fillna(0.0) cand['ec_reliability'] = (earned + 5 * overall_rate) / (proposed + 5) cand['ec_reliability_n'] = np.log1p(proposed) return cand