Skip to content

Commit

Permalink
add recall metric qdrant#138
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasWestholt committed Oct 4, 2024
1 parent 6bab477 commit 9016ecb
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions engine/base_client/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ def _search_one(cls, query: Query, top: Optional[int] = None):
end = time.perf_counter()

precision = 1.0
recall = 1.0
if query.expected_result:
ids = set(x[0] for x in search_res)
precision = len(ids.intersection(query.expected_result[:top])) / top

return precision, end - start
# precision equals True Positives / (True Positives + False Positives)
# recall equals True Positives / (True Positives + False Negatives)
# See https://en.wikipedia.org/wiki/Precision_and_recall
true_positives = len(ids.intersection(query.expected_result[:top]))
precision = true_positives / top # 1 means that the results consist of expected elements.
recall = true_positives / len(query.expected_result) # 1 means that all expected elements are in the results.

return precision, recall, end - start

def search_all(
self,
Expand All @@ -71,7 +78,7 @@ def search_all(

if parallel == 1:
start = time.perf_counter()
precisions, latencies = list(
precisions, recalls, latencies = list(
zip(*[search_one(query) for query in tqdm.tqdm(queries)])
)
else:
Expand All @@ -90,7 +97,7 @@ def search_all(
if parallel > 10:
time.sleep(15) # Wait for all processes to start
start = time.perf_counter()
precisions, latencies = list(
precisions, recalls, latencies = list(
zip(*pool.imap_unordered(search_one, iterable=tqdm.tqdm(queries)))
)

Expand All @@ -109,6 +116,7 @@ def search_all(
"p95_time": np.percentile(latencies, 95),
"p99_time": np.percentile(latencies, 99),
"precisions": precisions,
"recalls": recalls,
"latencies": latencies,
}

Expand Down

0 comments on commit 9016ecb

Please sign in to comment.