Fixed Plots

This commit is contained in:
HajarZaiz 2023-02-22 13:47:52 +01:00
parent 2b7302de64
commit a6896a60dd

View File

@ -6,34 +6,37 @@ import numpy as np
import seaborn as sns import seaborn as sns
from itertools import combinations from itertools import combinations
parameters = ['run', 'blockSize', 'failureRate', 'numberValidators', 'netDegree', 'chi']
#Title formatting for the figures #Title formatting for the figures
def formatTitle(key): def formatTitle(key):
name = ''.join([f" {char}" if char.isupper() else char for char in key.split('_')[0]]) name = ''.join([f" {char}" if char.isupper() else char for char in key.split('_')[0]])
number = key.split('_')[1] number = key.split('_')[1]
return f"{name.title()}: {number} " return f"{name.title()}: {number} "
results_folder = os.getcwd() def getLatestDirectory():
resultsFolder = os.getcwd()
#Get all folders and store their time info and sort #Get all folders and store their time info and sort
directories = [d for d in os.listdir(results_folder) if os.path.isdir(os.path.join(results_folder, d))] directories = [d for d in os.listdir(resultsFolder) if os.path.isdir(os.path.join(resultsFolder, d))]
directories_ctime = [(d, os.path.getctime(os.path.join(results_folder, d))) for d in directories] directoriesTime = [(d, os.path.getctime(os.path.join(resultsFolder, d))) for d in directories]
directories_ctime.sort(key=lambda x: x[1], reverse=True) directoriesTime.sort(key=lambda x: x[1], reverse=True)
#Get the path of the latest created folder #Get the path of the latest created folder
latest_directory = directories_ctime[0][0] latestDirectory = directoriesTime[0][0]
folder_path = os.path.join(results_folder, latest_directory) folderPath = os.path.join(resultsFolder, latestDirectory)
return folderPath
def plottingData(folderPath):
#Store data with a unique key for each params combination #Store data with a unique key for each params combination
data = {} data = {}
plotInfo = {} plotInfo = {}
parameters = ['run', 'blockSize', 'failureRate', 'numberValidators', 'netDegree', 'chi']
#Loop over the xml files in the folder #Loop over the xml files in the folder
for filename in os.listdir(folder_path): for filename in os.listdir(folderPath):
#Loop over the xmls and store the data in variables #Loop over the xmls and store the data in variables
if filename.endswith('.xml'): if filename.endswith('.xml'):
tree = ET.parse(os.path.join(folder_path, filename)) tree = ET.parse(os.path.join(folderPath, filename))
root = tree.getroot() root = tree.getroot()
run = int(root.find('run').text) run = int(root.find('run').text)
blockSize = int(root.find('blockSize').text) blockSize = int(root.find('blockSize').text)
@ -47,60 +50,64 @@ for filename in os.listdir(folder_path):
for combination in combinations(parameters, 4): for combination in combinations(parameters, 4):
# Get the indices and values of the parameters in the combination # Get the indices and values of the parameters in the combination
indices = [parameters.index(element) for element in combination] indices = [parameters.index(element) for element in combination]
selected_values = [run, blockSize, failureRate, numberValidators, netDegree, chi] selectedValues = [run, blockSize, failureRate, numberValidators, netDegree, chi]
values = [selected_values[index] for index in indices] values = [selectedValues[index] for index in indices]
names = [parameters[i] for i in indices] names = [parameters[i] for i in indices]
keyComponents = [f"{name}_{value}" for name, value in zip(names, values)] keyComponents = [f"{name}_{value}" for name, value in zip(names, values)]
key = tuple(keyComponents[:4]) key = tuple(keyComponents[:4])
#Get the names of the other 2 parameters that are not included in the key #Get the names of the other 2 parameters that are not included in the key
other_params = [parameters[i] for i in range(6) if i not in indices] otherParams = [parameters[i] for i in range(6) if i not in indices]
#Append the values of the other 2 parameters and the ttas to the lists for the key #Append the values of the other 2 parameters and the ttas to the lists for the key
other_indices = [i for i in range(len(parameters)) if i not in indices] otherIndices = [i for i in range(len(parameters)) if i not in indices]
#Initialize the dictionary for the key if it doesn't exist yet #Initialize the dictionary for the key if it doesn't exist yet
if key not in data: if key not in data:
data[key] = {} data[key] = {}
#Initialize lists for the other 2 parameters and the ttas with the key #Initialize lists for the other 2 parameters and the ttas with the key
data[key][other_params[0]] = [] data[key][otherParams[0]] = []
data[key][other_params[1]] = [] data[key][otherParams[1]] = []
data[key]['ttas'] = [] data[key]['ttas'] = []
if other_params[0] in data[key]: if otherParams[0] in data[key]:
data[key][other_params[0]].append(selected_values[other_indices[0]]) data[key][otherParams[0]].append(selectedValues[otherIndices[0]])
else: else:
data[key][other_params[0]] = [selected_values[other_indices[0]]] data[key][otherParams[0]] = [selectedValues[otherIndices[0]]]
if other_params[1] in data[key]: if otherParams[1] in data[key]:
data[key][other_params[1]].append(selected_values[other_indices[1]]) data[key][otherParams[1]].append(selectedValues[otherIndices[1]])
else: else:
data[key][other_params[1]] = [selected_values[other_indices[1]]] data[key][otherParams[1]] = [selectedValues[otherIndices[1]]]
data[key]['ttas'].append(tta) data[key]['ttas'].append(tta)
return data
def similarKeys(data):
#Get the keys for all data with the same x and y labels #Get the keys for all data with the same x and y labels
filtered_keys = {} filteredKeys = {}
for key1, value1 in data.items(): for key1, value1 in data.items():
sub_keys1 = list(value1.keys()) subKeys1 = list(value1.keys())
filtered_keys[(sub_keys1[0], sub_keys1[1])] = [key1] filteredKeys[(subKeys1[0], subKeys1[1])] = [key1]
for key2, value2 in data.items(): for key2, value2 in data.items():
sub_keys2 = list(value2.keys()) subKeys2 = list(value2.keys())
if key1 != key2 and sub_keys1[0] == sub_keys2[0] and sub_keys1[1] == sub_keys2[1]: if key1 != key2 and subKeys1[0] == subKeys2[0] and subKeys1[1] == subKeys2[1]:
try: try:
filtered_keys[(sub_keys1[0], sub_keys1[1])].append(key2) filteredKeys[(subKeys1[0], subKeys1[1])].append(key2)
except KeyError: except KeyError:
filtered_keys[(sub_keys1[0], sub_keys1[1])] = [key2] filteredKeys[(subKeys1[0], subKeys1[1])] = [key2]
return filteredKeys
def plotHeatmaps(filteredKeys, data):
#Store the 2D heatmaps in a folder #Store the 2D heatmaps in a folder
heatmaps_folder = 'heatmaps' heatmapsFolder = 'heatmaps'
if not os.path.exists(heatmaps_folder): if not os.path.exists(heatmapsFolder):
os.makedirs(heatmaps_folder) os.makedirs(heatmapsFolder)
for labels, keys in filtered_keys.items(): for labels, keys in filteredKeys.items():
for key in keys: for key in keys:
hist, xedges, yedges = np.histogram2d(data[key][labels[0]], data[key][labels[1]], bins=(3, 3), weights=data[key]['ttas']) xlabels = np.sort(np.unique(data[key][labels[0]]))
ylabels = np.sort(np.unique(data[key][labels[1]]))
hist, xedges, yedges = np.histogram2d(data[key][labels[0]], data[key][labels[1]], bins=(len(xlabels), len(ylabels)), weights=data[key]['ttas'])
hist = hist.T hist = hist.T
xlabels = [f'{val:.2f}' for val in xedges[::2]] + [f'{xedges[-1]:.2f}'] fig, ax = plt.subplots(figsize=(10, 6))
ylabels = [f'{val:.2f}' for val in yedges[::2]] + [f'{yedges[-1]:.2f}'] sns.heatmap(hist, xticklabels=xlabels, yticklabels=ylabels, cmap='Purples', cbar_kws={'label': 'Time to block availability'}, linecolor='black', linewidths=0.3, annot=True, fmt=".2f", ax=ax)
sns.heatmap(hist, xticklabels=xlabels, yticklabels=ylabels, cmap='Purples', cbar_kws={'label': 'Time to block availability'}, linecolor='black', linewidths=0.3, annot=True, fmt=".2f")
plt.xlabel(labels[0]) plt.xlabel(labels[0])
plt.ylabel(labels[1]) plt.ylabel(labels[1])
filename = "" filename = ""
@ -111,10 +118,21 @@ for labels, keys in filtered_keys.items():
filename += f"{key[paramValueCnt]}" filename += f"{key[paramValueCnt]}"
title += formatTitle(key[paramValueCnt]) title += formatTitle(key[paramValueCnt])
paramValueCnt += 1 paramValueCnt += 1
plt.title(title) title_obj = plt.title(title)
font_size = 16 * fig.get_size_inches()[0] / 10
title_obj.set_fontsize(font_size)
filename = filename + ".png" filename = filename + ".png"
target_folder = os.path.join(heatmaps_folder, f"{labels[0]}Vs{labels[1]}") targetFolder = os.path.join(heatmapsFolder, f"{labels[0]}Vs{labels[1]}")
if not os.path.exists(target_folder): if not os.path.exists(targetFolder):
os.makedirs(target_folder) os.makedirs(targetFolder)
plt.savefig(os.path.join(target_folder, filename)) plt.savefig(os.path.join(targetFolder, filename))
plt.close()
plt.clf() plt.clf()
def generateHeatmaps():
folderPath = getLatestDirectory()
data = plottingData(folderPath)
filteredKeys = similarKeys(data)
plotHeatmaps(filteredKeys, data)
generateHeatmaps()