diff --git a/DAS/visualizer.py b/DAS/visualizer.py index 047e513..bb8c527 100644 --- a/DAS/visualizer.py +++ b/DAS/visualizer.py @@ -16,11 +16,11 @@ class Visualizer: self.minimumDataPoints = 2 def plottingData(self): - #Store data with a unique key for each params combination + """Store data with a unique key for each params combination""" data = {} - #Loop over the xml files in the folder + """Loop over the xml files in the folder""" for filename in os.listdir(self.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'): tree = ET.parse(os.path.join(self.folderPath, filename)) root = tree.getroot() @@ -32,24 +32,24 @@ class Visualizer: chi = int(root.find('chi').text) tta = int(root.find('tta').text) - # Loop over all possible combinations of length 4 of the parameters + """Loop over all possible combinations of length 4 of the parameters""" for combination in combinations(self.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 = [self.parameters.index(element) for element in combination] selectedValues = [run, blockSize, failureRate, numberValidators, netDegree, chi] values = [selectedValues[index] for index in indices] names = [self.parameters[i] for i in indices] keyComponents = [f"{name}_{value}" for name, value in zip(names, values)] 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""" otherParams = [self.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""" otherIndices = [i for i in range(len(self.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: 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][otherParams[0]] = [] data[key][otherParams[1]] = [] data[key]['ttas'] = [] @@ -65,9 +65,42 @@ class Visualizer: data[key]['ttas'].append(tta) print("Getting data from the folder...") return data + + def averageRuns(self, data): + """Get the average of run 0 and run 1 for each key""" + newData = {} + for key, value in data.items(): + runExists = False + """Check if the key contains 'run_' with a numerical value""" + for item in key: + if item.startswith('run_0'): + runExists = True + break + if runExists: + for item in key: + """Create a new key with the other items in the tuple""" + if item.startswith('run_'): + newKey = tuple([x for x in key if x != item]) + key0 = ('run_0',) + newKey + data0 = data[key0] + key1 = ('run_1',) + newKey + data1 = data[key1] + averages = {} + for key in data0.keys(): + if key in data1: + values = [] + for i in range(len(data0[key])): + value = (data0[key][i] + data1[key][i]) / 2.0 + if isinstance(data0[key][i], int) and isinstance(data1[key][i], int) and key != 'ttas': + value = int(value) + values.append(value) + averages[key] = values + newData[newKey] = averages + print("Getting the average of the runs...") + return newData def similarKeys(self, 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""" filteredKeys = {} for key1, value1 in data.items(): subKeys1 = list(value1.keys()) @@ -83,28 +116,29 @@ class Visualizer: return filteredKeys def formatLabel(self, label): - #Label formatting for the figures + """Label formatting for the figures""" result = ''.join([f" {char}" if char.isupper() else char for char in label]) return result.title() def formatTitle(self, key): - #Title formatting for the figures + """Title formatting for the figures""" name = ''.join([f" {char}" if char.isupper() else char for char in key.split('_')[0]]) number = key.split('_')[1] return f"{name.title()}: {number} " def plotHeatmaps(self): - #Plot and store the 2D heatmaps in subfolders + """Plot and store the 2D heatmaps in subfolders""" data = self.plottingData() + data = self.averageRuns(data) filteredKeys = self.similarKeys(data) print("Plotting heatmaps...") - #Create the directory if it doesn't exist already + """Create the directory if it doesn't exist already""" heatmapsFolder = self.folderPath + '/heatmaps' if not os.path.exists(heatmapsFolder): os.makedirs(heatmapsFolder) - #Plot + """Plot""" for labels, keys in filteredKeys.items(): for key in keys: xlabels = np.sort(np.unique(data[key][labels[0]])) @@ -121,7 +155,7 @@ class Visualizer: title = "" paramValueCnt = 0 for param in self.parameters: - if param != labels[0] and param != labels[1]: + if param != labels[0] and param != labels[1] and param != 'run': filename += f"{key[paramValueCnt]}" formattedTitle = self.formatTitle(key[paramValueCnt]) title += formattedTitle