Skanislav commited on
Commit ·
7bea63a
1
Parent(s): d1bdad3
feat: collect top3 tools accuracy
Browse files
scripts/update_tools_accuracy.py
CHANGED
|
@@ -501,9 +501,13 @@ def compute_global_weekly_accuracy(clean_tools_df):
|
|
| 501 |
Compute accuracy following version 5.0 of spec"""
|
| 502 |
# get the information in clean_tools_df from last two weeks only, timestamp column is request_time
|
| 503 |
|
| 504 |
-
three_weeks_ago = pd.Timestamp.now(tz="UTC") - pd.Timedelta(days=
|
| 505 |
recent_df = clean_tools_df[clean_tools_df["request_time"] >= three_weeks_ago]
|
| 506 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
# compute at the tool level (using "tool" column) the volume of requests per tool
|
| 508 |
tool_volumes = (
|
| 509 |
recent_df.groupby("tool")["request_id"].count().reset_index(name="volume")
|
|
@@ -525,13 +529,86 @@ def compute_global_weekly_accuracy(clean_tools_df):
|
|
| 525 |
sampling_size = int(avg_volume / 2)
|
| 526 |
print(f"Sampling size = {sampling_size}")
|
| 527 |
|
| 528 |
-
|
| 529 |
tools_df=recent_df,
|
| 530 |
recent_samples_size=avg_volume,
|
| 531 |
sample_size=sampling_size,
|
| 532 |
n_subsets=50,
|
| 533 |
)
|
| 534 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
def get_accuracy_info(clean_tools_df: pd.DataFrame) -> [pd.DataFrame, bool, List]:
|
| 537 |
"""
|
|
@@ -552,7 +629,7 @@ def get_accuracy_info(clean_tools_df: pd.DataFrame) -> [pd.DataFrame, bool, List
|
|
| 552 |
"tool": tool,
|
| 553 |
"tool_accuracy": global_accuracies[tool]["mean"],
|
| 554 |
"std_accuracy": global_accuracies[tool]["std"],
|
| 555 |
-
"
|
| 556 |
}
|
| 557 |
for tool in global_accuracies.keys()
|
| 558 |
]
|
|
@@ -614,7 +691,7 @@ def update_tools_accuracy_same_model(
|
|
| 614 |
new_max_timeline = acc_info[acc_info["tool"] == tool]["max"].values[0]
|
| 615 |
|
| 616 |
old_accuracy = tools_acc[tools_acc["tool"] == tool]["tool_accuracy"].values[0]
|
| 617 |
-
old_volume = tools_acc[tools_acc["tool"] == tool]["
|
| 618 |
# update the accuracy information
|
| 619 |
print(f"Updating tool {tool} with new accuracy {new_accuracy}")
|
| 620 |
print(f"Old volume: {old_volume}, New volume: {new_volume}")
|
|
@@ -623,7 +700,7 @@ def update_tools_accuracy_same_model(
|
|
| 623 |
new_row = {
|
| 624 |
"tool": tool,
|
| 625 |
"tool_accuracy": new_accuracy,
|
| 626 |
-
"
|
| 627 |
"min": new_min_timeline,
|
| 628 |
"max": new_max_timeline,
|
| 629 |
}
|
|
@@ -659,7 +736,7 @@ def update_tools_accuracy_same_model(
|
|
| 659 |
new_row = {
|
| 660 |
"tool": tool,
|
| 661 |
"tool_accuracy": avg_accuracy,
|
| 662 |
-
"
|
| 663 |
"min": new_min_timeline,
|
| 664 |
"max": new_max_timeline,
|
| 665 |
}
|
|
|
|
| 501 |
Compute accuracy following version 5.0 of spec"""
|
| 502 |
# get the information in clean_tools_df from last two weeks only, timestamp column is request_time
|
| 503 |
|
| 504 |
+
three_weeks_ago = pd.Timestamp.now(tz="UTC") - pd.Timedelta(days=7)
|
| 505 |
recent_df = clean_tools_df[clean_tools_df["request_time"] >= three_weeks_ago]
|
| 506 |
|
| 507 |
+
daily_requests = recent_df.groupby(clean_tools_df["request_time"].dt.date)["request_id"].count()
|
| 508 |
+
print("\nDaily request counts:")
|
| 509 |
+
print(daily_requests)
|
| 510 |
+
|
| 511 |
# compute at the tool level (using "tool" column) the volume of requests per tool
|
| 512 |
tool_volumes = (
|
| 513 |
recent_df.groupby("tool")["request_id"].count().reset_index(name="volume")
|
|
|
|
| 529 |
sampling_size = int(avg_volume / 2)
|
| 530 |
print(f"Sampling size = {sampling_size}")
|
| 531 |
|
| 532 |
+
global_accuracies, new_tools = compute_global_accuracy_same_population(
|
| 533 |
tools_df=recent_df,
|
| 534 |
recent_samples_size=avg_volume,
|
| 535 |
sample_size=sampling_size,
|
| 536 |
n_subsets=50,
|
| 537 |
)
|
| 538 |
|
| 539 |
+
def save_weekly_top_3_metric(recent_df):
|
| 540 |
+
"""Save weekly top 3 tools percentage metric"""
|
| 541 |
+
|
| 542 |
+
tool_volumes = (
|
| 543 |
+
recent_df.groupby("tool")["request_id"].count().reset_index(name="volume")
|
| 544 |
+
)
|
| 545 |
+
|
| 546 |
+
global_accuracies, _ = compute_global_accuracy_same_population(
|
| 547 |
+
tools_df=recent_df,
|
| 548 |
+
recent_samples_size=len(recent_df) // len(recent_df['tool'].unique()) if len(recent_df) > 0 else 1000,
|
| 549 |
+
sample_size=sampling_size,
|
| 550 |
+
n_subsets=50
|
| 551 |
+
)
|
| 552 |
+
|
| 553 |
+
tools_data = []
|
| 554 |
+
for _, row in tool_volumes.iterrows():
|
| 555 |
+
tool = row['tool']
|
| 556 |
+
volume = row['volume']
|
| 557 |
+
accuracy = global_accuracies.get(tool, {}).get('mean', 0)
|
| 558 |
+
tools_data.append({
|
| 559 |
+
'tool': tool,
|
| 560 |
+
'volume': volume,
|
| 561 |
+
'accuracy': accuracy
|
| 562 |
+
})
|
| 563 |
+
|
| 564 |
+
tools_df = pd.DataFrame(tools_data)
|
| 565 |
+
|
| 566 |
+
# sort by accuracy descending and get top 3
|
| 567 |
+
top_3_tools = tools_df.nlargest(3, 'accuracy')
|
| 568 |
+
|
| 569 |
+
top_3_requests = top_3_tools['volume'].sum()
|
| 570 |
+
total_requests = tools_df['volume'].sum()
|
| 571 |
+
top_3_percentage = (top_3_requests / total_requests) * 100 if total_requests > 0 else 0
|
| 572 |
+
|
| 573 |
+
# Create weekly report
|
| 574 |
+
weekly_report = {
|
| 575 |
+
'timestamp': pd.Timestamp.now(tz="UTC").strftime("%Y-%m-%d %H:%M:%S"),
|
| 576 |
+
'calculation_period': '3_weeks',
|
| 577 |
+
'top_3_percentage': round(top_3_percentage, 2),
|
| 578 |
+
'total_requests': int(total_requests),
|
| 579 |
+
'top_3_total_requests': int(top_3_requests),
|
| 580 |
+
'top_1_tool': top_3_tools.iloc[0]['tool'] if len(top_3_tools) > 0 else None,
|
| 581 |
+
'top_1_accuracy': round(top_3_tools.iloc[0]['accuracy'], 2) if len(top_3_tools) > 0 else None,
|
| 582 |
+
'top_1_volume': int(top_3_tools.iloc[0]['volume']) if len(top_3_tools) > 0 else None,
|
| 583 |
+
'top_2_tool': top_3_tools.iloc[1]['tool'] if len(top_3_tools) > 1 else None,
|
| 584 |
+
'top_2_accuracy': round(top_3_tools.iloc[1]['accuracy'], 2) if len(top_3_tools) > 1 else None,
|
| 585 |
+
'top_2_volume': int(top_3_tools.iloc[1]['volume']) if len(top_3_tools) > 1 else None,
|
| 586 |
+
'top_3_tool': top_3_tools.iloc[2]['tool'] if len(top_3_tools) > 2 else None,
|
| 587 |
+
'top_3_accuracy': round(top_3_tools.iloc[2]['accuracy'], 2) if len(top_3_tools) > 2 else None,
|
| 588 |
+
'top_3_volume': int(top_3_tools.iloc[2]['volume']) if len(top_3_tools) > 2 else None,
|
| 589 |
+
}
|
| 590 |
+
|
| 591 |
+
# Save to CSV
|
| 592 |
+
report_df = pd.DataFrame([weekly_report])
|
| 593 |
+
filename = ROOT_DIR / "weekly_top_3_tools_report.csv"
|
| 594 |
+
|
| 595 |
+
# Append to existing file or create new one
|
| 596 |
+
if os.path.exists(filename):
|
| 597 |
+
existing_df = pd.read_csv(filename)
|
| 598 |
+
combined_df = pd.concat([existing_df, report_df], ignore_index=True)
|
| 599 |
+
combined_df.to_csv(filename, index=False)
|
| 600 |
+
else:
|
| 601 |
+
report_df.to_csv(filename, index=False)
|
| 602 |
+
|
| 603 |
+
print(f"Weekly top 3 tools report saved: {top_3_percentage:.2f}% requests served by top 3 most accurate tools")
|
| 604 |
+
print(f"Report saved to: {filename}")
|
| 605 |
+
|
| 606 |
+
return weekly_report
|
| 607 |
+
|
| 608 |
+
save_weekly_top_3_metric(recent_df)
|
| 609 |
+
|
| 610 |
+
return global_accuracies, new_tools
|
| 611 |
+
|
| 612 |
|
| 613 |
def get_accuracy_info(clean_tools_df: pd.DataFrame) -> [pd.DataFrame, bool, List]:
|
| 614 |
"""
|
|
|
|
| 629 |
"tool": tool,
|
| 630 |
"tool_accuracy": global_accuracies[tool]["mean"],
|
| 631 |
"std_accuracy": global_accuracies[tool]["std"],
|
| 632 |
+
"nr_responses": SAMPLING_POPULATION_SIZE,
|
| 633 |
}
|
| 634 |
for tool in global_accuracies.keys()
|
| 635 |
]
|
|
|
|
| 691 |
new_max_timeline = acc_info[acc_info["tool"] == tool]["max"].values[0]
|
| 692 |
|
| 693 |
old_accuracy = tools_acc[tools_acc["tool"] == tool]["tool_accuracy"].values[0]
|
| 694 |
+
old_volume = tools_acc[tools_acc["tool"] == tool]["nr_responses"].values[0]
|
| 695 |
# update the accuracy information
|
| 696 |
print(f"Updating tool {tool} with new accuracy {new_accuracy}")
|
| 697 |
print(f"Old volume: {old_volume}, New volume: {new_volume}")
|
|
|
|
| 700 |
new_row = {
|
| 701 |
"tool": tool,
|
| 702 |
"tool_accuracy": new_accuracy,
|
| 703 |
+
"nr_responses": SAMPLING_POPULATION_SIZE,
|
| 704 |
"min": new_min_timeline,
|
| 705 |
"max": new_max_timeline,
|
| 706 |
}
|
|
|
|
| 736 |
new_row = {
|
| 737 |
"tool": tool,
|
| 738 |
"tool_accuracy": avg_accuracy,
|
| 739 |
+
"nr_responses": SAMPLING_POPULATION_SIZE,
|
| 740 |
"min": new_min_timeline,
|
| 741 |
"max": new_max_timeline,
|
| 742 |
}
|