add error

This commit is contained in:
Aron Petau 2025-12-15 17:25:51 +01:00
parent 4845d5d0dc
commit d39453d688
2 changed files with 164 additions and 5 deletions

View file

@ -288,22 +288,58 @@ When autopilot is ON, I will automatically apply to new listings."""
grouped['failures'] = grouped['total'] - grouped['successes']
grouped['error_rate'] = grouped['failures'] / grouped['total']
# Prepare plot
# Ensure index is sorted by date for plotting
grouped = grouped.sort_index()
# Prepare plot: convert dates to matplotlib numeric x-values so bars and line align
import matplotlib.dates as mdates
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), sharex=True)
grouped[['successes','failures']].plot(kind='bar', stacked=True, ax=ax1, color=['#2E8B57','#C44A4A'])
dates = pd.to_datetime(grouped.index).to_pydatetime()
x = mdates.date2num(dates)
width = 0.6 # width in days for bars
successes = grouped['successes'].values
failures = grouped['failures'].values
ax1.bar(x, successes, width=width, color='#2E8B57', align='center')
ax1.bar(x, failures, bottom=successes, width=width, color='#C44A4A', align='center')
ax1.set_ylabel('Count')
ax1.set_title('Autopilot: Successes vs Failures (by day)')
ax1.set_xticks(x)
ax1.set_xlim(min(x) - 1, max(x) + 1)
ax1.xaxis.set_major_locator(mdates.AutoDateLocator())
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax2.plot(grouped.index, grouped['error_rate'], marker='o', color='#3333AA')
ax2.set_ylim(0,1)
# Plot error rate line on same x (date) axis
ax2.plot(x, grouped['error_rate'].values, marker='o', color='#3333AA', linewidth=2)
ax2.set_ylim(-0.02, 1.02)
ax2.set_ylabel('Error rate')
ax2.set_xlabel('Date')
ax2.set_title('Daily Error Rate (failures / total)')
ax2.grid(True, alpha=0.3)
ax2.set_xticks(x)
ax2.set_xlim(min(x) - 1, max(x) + 1)
ax2.xaxis.set_major_locator(mdates.AutoDateLocator())
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
fig.autofmt_xdate()
plt.tight_layout()
plot_path = DATA_DIR / 'error_rate.png'
fig.savefig(plot_path)
tmp_path = DATA_DIR / 'error_rate.tmp.png'
# Save to a temp file first and atomically replace to ensure overwrite
fig.savefig(tmp_path, format='png')
plt.close(fig)
try:
tmp_path.replace(plot_path)
except Exception:
# Fallback: try removing existing and renaming
try:
if plot_path.exists():
plot_path.unlink()
tmp_path.rename(plot_path)
except Exception:
logger.exception(f"Failed to write plot to {plot_path}")
# Summary
total_attempts = int(grouped['total'].sum())