plot with seaborn

This commit is contained in:
2025-02-28 20:34:32 +01:00
parent 6998598f29
commit f4e29c18e5

View File

@@ -1,5 +1,7 @@
import seaborn as sns
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import yaml import yaml
import pandas as pd
# Function to read and parse the file # Function to read and parse the file
def read_metrics(file_path): def read_metrics(file_path):
@@ -8,47 +10,47 @@ def read_metrics(file_path):
return data['runs'] return data['runs']
# Read metrics from the file # Read metrics from the file
file_path = 'project/metrics.yaml' # Replace with the correct file path file_path = 'project/metrics.yaml' # Replace with your file path
runs = read_metrics(file_path) runs = read_metrics(file_path)
# Extracting values #Flatten data into a list of dictionaries
n_tasks = [run['n_task'] for run in runs] data = []
build_times = [float(run['build']['components_build_time']) for run in runs] for run in runs:
gen_times = [float(run['code_gen']['gen_time']) for run in runs] if 'build' in run:
deploy_times = [float(run['deploy']['components_deploy_time']) for run in runs] data.append({'Task': run['n_task'], 'Type': 'Build Time', 'Time': float(run['build']['components_build_time'])})
# Plotting Build Time if 'code_gen' in run:
plt.figure(figsize=(8, 6)) data.append({'Task': run['n_task'], 'Type': 'Generation Time', 'Time': float(run['code_gen']['gen_time'])})
ax = plt.gca() # Get current axis
p = ax.bar(n_tasks, build_times, color='b') if 'deploy' in run:
ax.bar_label(p, label_type='edge') data.append({'Task': run['n_task'], 'Type': 'Deployment Time', 'Time': float(run['deploy']['components_deploy_time'])})
ax.set_title('Build Time')
ax.set_xlabel('Number of Tasks')
ax.set_ylabel('Build Time (seconds)')
plt.tight_layout()
plt.savefig('build_time_plot.png') # Save the plot as an image
plt.close()
# Plotting Code Generation Time # Convert to DataFrame
plt.figure(figsize=(8, 6)) df = pd.DataFrame(data)
ax = plt.gca() # Get current axis
p = ax.bar(n_tasks, gen_times, color='g')
ax.bar_label(p, label_type='edge')
ax.set_title('Code Generation Time')
ax.set_xlabel('Number of Tasks')
ax.set_ylabel('Generation Time (seconds)')
plt.tight_layout()
plt.savefig('gen_time_plot.png') # Save the plot as an image
plt.close()
# Plotting Deployment Time # Function to plot and add median labels
plt.figure(figsize=(8, 6)) def plot_metric(metric, filename, color):
ax = plt.gca() # Get current axis subset = df[df['Type'] == metric]
p = ax.bar(n_tasks, deploy_times, color='r') plt.figure(figsize=(8, 6))
ax.bar_label(p, label_type='edge') ax = sns.boxplot(x='Task', y='Time', data=subset, color=color)
ax.set_title('Deployment Time')
ax.set_xlabel('Number of Tasks') # Add median labels
ax.set_ylabel('Deployment Time (seconds)') medians = subset.groupby('Task')['Time'].median()
plt.tight_layout() for i, task in enumerate(medians.index):
plt.savefig('deploy_time_plot.png') # Save the plot as an image median_value = medians[task]
plt.close() ax.text(i, median_value, f'{median_value:.3f}', ha='center', va='center',
fontsize=10, color='white', bbox=dict(facecolor='black', alpha=0.6, boxstyle='round,pad=0.3'))
plt.title(f'{metric} by Number of Tasks')
plt.xlabel('Number of Tasks')
plt.ylabel('Time (seconds)')
plt.tight_layout()
plt.savefig(filename)
plt.close()
# Plot each metric separately
plot_metric('Build Time', 'res/build_time_boxplot.png', 'skyblue')
plot_metric('Generation Time', 'res/gen_time_boxplot.png', 'lightgreen')
plot_metric('Deployment Time', 'res/deploy_time_boxplot.png', 'salmon')
print('Plots saved successfully!')