import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('res.csv')

grouped = df.groupby(['Structure', 'Mode']).agg({
    'Insert (sec)': ['mean', 'std'],
    'Search (sec)': ['mean', 'std'],
    'Delete (sec)': ['mean', 'std']
}).reset_index()

grouped.columns = ['Structure', 'Mode', 'Insert_mean', 'Insert_std',
                   'Search_mean', 'Search_std', 'Delete_mean', 'Delete_std']

structures = grouped['Structure'].unique()
modes = grouped['Mode'].unique()

fig, axes = plt.subplots(1, 3, figsize=(15, 5))
operations = ['Insert', 'Search', 'Delete']
colors = {'random': 'skyblue', 'sorted': 'lightcoral'}

for i, op in enumerate(operations):
    ax = axes[i]
    mean_col = f'{op}_mean'
    std_col = f'{op}_std'
    x = np.arange(len(structures))
    width = 0.35
    for j, mode in enumerate(modes):
        data = grouped[grouped['Mode'] == mode]
        means = [data[data['Structure'] == s][mean_col].values[0] for s in structures]
        stds = [data[data['Structure'] == s][std_col].values[0] for s in structures]
        offset = (j - 0.5) * width
        bars = ax.bar(x + offset, means, width, yerr=stds, capsize=3,
                      label=mode.capitalize(), color=colors[mode])
    
    ax.set_xticks(x)
    ax.set_xticklabels(structures)
    ax.set_ylabel('Time (seconds)')
    ax.set_title(f'{op} Time')
    ax.legend()
    
    if op == 'Insert':
        ax.set_yscale('log')
        ax.set_ylabel('Time (seconds) [log scale]')

plt.tight_layout()
plt.savefig('performance_plots.png', dpi=150)
plt.show()

print("Графики сохранены в файл performance_plots.png")