import csv

import matplotlib.pyplot as plt


def plot_from_csv(filename, save_plot=False):
    durations = []
    sleep_errors = []
    busy_sleep_errors = []
    
    with open(filename, 'r') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            durations.append(float(row['duration']))
            sleep_errors.append(float(row['sleep_error']))
            busy_sleep_errors.append(float(row['busy_sleep_error']))
    
    plt.figure(figsize=(10, 5))
    plt.plot(durations, sleep_errors, label='time.sleep()', marker='o')
    plt.plot(durations, busy_sleep_errors, label='busy_sleep()', marker='x')
    plt.xlabel('Desired Delay (ms)')
    plt.ylabel('Average Error (ms)')
    plt.title('Sleep Accuracy: time.sleep() vs Busy-Wait Loop (macOS)')
    plt.legend()
    plt.grid(True)
    
    if save_plot:
        plt.savefig('sleep_accuracy_plot.png', dpi=300)
        print("Plot saved as sleep_accuracy_plot.png")
        
    plt.show()

plot_from_csv('sleep_data.csv', save_plot=True)
