MCPcopy Create free account
hub / github.com/cosdata/cosdata / compare_server_and_bruteforce

Function compare_server_and_bruteforce

tests/test_multiple_datasets.py:255–300  ·  view source on GitHub ↗
(bf_csv_path: str)

Source from the content-addressed store, hash-verified

253
254
255def compare_server_and_bruteforce(bf_csv_path: str):
256 bf = pl.read_csv(bf_csv_path)
257 base_dir, bf_file_name = os.path.split(bf_csv_path)
258 server_file_name = bf_file_name.replace("bf_", "server_")
259 server_path = os.path.join(base_dir, server_file_name)
260 sr = pl.read_csv(server_path)
261
262 total_queries = sr.shape[0]
263 hits = 0
264 recall_sum = 0.0
265
266 for row in sr.iter_rows(named=True):
267 qid = row["query_id"]
268 match = bf.filter(pl.col("query_id") == qid).row(0)
269
270 bf_top_ids = [match[1], match[3], match[5], match[7]]
271 bf_top_sims = [match[2], match[4], match[6], match[8]]
272 srv_top_ids = [row["top1_id"], row["top2_id"], row["top3_id"], row["top4_id"]]
273 srv_top_sims = [
274 row["top1_sim"],
275 row["top2_sim"],
276 row["top3_sim"],
277 row["top4_sim"],
278 ]
279
280 print(colored(f"Query ID: {qid}", "yellow"))
281 print(
282 colored(f"BF top4 IDs: {bf_top_ids}", "blue"),
283 colored(f"sims: {bf_top_sims}", "blue"),
284 )
285 print(
286 colored(f"Server top4 IDs: {srv_top_ids}", "magenta"),
287 colored(f"sims: {srv_top_sims}", "magenta"),
288 )
289 print("-" * 40)
290
291 top_srv = set(srv_top_ids)
292 top_bf = set(bf_top_ids)
293 common = top_srv.intersection(top_bf)
294 hits += len(common)
295 recall_sum += len(common) / 4
296
297 print(colored(f"Total queries compared: {total_queries}", "cyan"))
298 print(colored(f"Total matching embeddings in server top4: {hits}", "cyan"))
299 recall_percentage = (recall_sum / total_queries) * 100
300 print(colored(f"Mean recall: {recall_percentage:.2f}%", "green"))
301
302
303if __name__ == "__main__":

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected