wk_LinProg/LinProg_Scripts/ResultHeatmap.py

94 lines
No EOL
3.2 KiB
Python

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import statsmodels.api as sm
# Read the CSV file
csv_path = "../optimization_results/inception70/Opt_noChange_5x_fasterLink/"
file_name="resultsLinProg.csv"
df = pd.read_csv(csv_path+file_name)
pearson_corr = df[['noc_factor', 'comp_factor', 'objective_value']].corr(method='pearson')
spearman_corr = df[['noc_factor', 'comp_factor', 'objective_value']].corr(method='spearman')
print("Pearson correlation:\n", pearson_corr['objective_value'])
print("\nSpearman correlation:\n", spearman_corr['objective_value'])
X = df[['noc_factor', 'comp_factor']]
y = df['objective_value']
# Add constant (intercept term) to the model
X = sm.add_constant(X)
# Fit the model using Ordinary Least Squares (OLS)
model = sm.OLS(y, X).fit()
# Show the regression results
print(model.summary())
print(f"Effect of noc_factor : {model.params['noc_factor']:.3f}")
print(f"Effect of comp_factor : {model.params['comp_factor']:.3f}")
# Print R-squared to show how good the regression fits
print(f"R-squared (fit quality) : {model.rsquared:.4f}")
# df['objective_value']=df['objective_value']*1000
# ...existing code...
effect_noc = abs(model.params['noc_factor'])
effect_comp = abs(model.params['comp_factor'])
if effect_noc > effect_comp:
more_factor = "noc_factor"
less_factor = "comp_factor"
ratio = effect_noc / effect_comp if effect_comp != 0 else float('inf')
else:
more_factor = "comp_factor"
less_factor = "noc_factor"
ratio = effect_comp / effect_noc if effect_noc != 0 else float('inf')
print(f"{more_factor} impacts the objective value {ratio:.2f} times more than {less_factor}.")
# ...existing code...
impact_summary = f"{more_factor} x{ratio:.2f} > {less_factor}"
# Pivot the data to create a matrix for the heatmap
heatmap_data = df.pivot_table(
index="noc_factor",
columns="comp_factor",
values="objective_value",
aggfunc="mean"
)
# Sort the axes for better visualization
heatmap_data = heatmap_data.sort_index(ascending=False) # noc_factor descending (y-axis)
heatmap_data = heatmap_data[sorted(heatmap_data.columns, reverse=True)] # comp_factor descending (x-axis)
# Create the heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(
heatmap_data,
annot=True, # Show values in each cell
fmt=".1f", # Format for the numbers
cmap="viridis",
cbar_kws={'label': 'Max Delay'} # Add colorbar label
)
plt.title(
"Max Delay Heatmap\n"
"Pearson: noc={:.2f}, comp={:.2f} | Spearman: noc={:.2f}, comp={:.2f}\n"
"Regression: noc={:.2f}{} , comp={:.2f}{} | R²={:.2f}".format(
pearson_corr['objective_value']['noc_factor'],
pearson_corr['objective_value']['comp_factor'],
spearman_corr['objective_value']['noc_factor'],
spearman_corr['objective_value']['comp_factor'],
model.params['noc_factor'],
f" ← ({ratio:.2f})" if more_factor == "noc_factor" else "",
model.params['comp_factor'],
f" ← ({ratio:.2f})" if more_factor == "comp_factor" else "",
model.rsquared
)
)
plt.xlabel("comp_factor")
plt.ylabel("noc_factor")
plt.tight_layout()
plt.savefig(csv_path+"Opt_noChange_5x_fastLink_heatmap.png")
plt.savefig(csv_path+"Opt_noChange_5x_fastLink_heatmap.pdf")
plt.show()