das-research/DAS/results.py

53 lines
1.7 KiB
Python
Raw Normal View History

#!/bin/python3
2023-01-25 20:51:59 +00:00
import os
import bisect
2023-01-25 20:51:59 +00:00
from xml.dom import minidom
from dicttoxml import dicttoxml
class Result:
"""This class stores and process/store the results of a simulation."""
def __init__(self, shape, execID):
"""It initializes the instance with a specific shape."""
2023-01-25 20:51:59 +00:00
self.shape = shape
self.execID = execID
self.blockAvailable = -1
2023-01-25 20:51:59 +00:00
self.tta = -1
self.missingVector = []
self.metrics = {}
def populate(self, shape, config, missingVector):
"""It populates part of the result data inside a vector."""
2023-01-25 20:51:59 +00:00
self.shape = shape
self.missingVector = missingVector
v = self.metrics["progress"]["validators ready"]
tta = bisect.bisect(v, config.successCondition)
if v[-1] >= config.successCondition:
2023-01-25 20:51:59 +00:00
self.blockAvailable = 1
self.tta = tta * (config.stepDuration)
2023-01-25 20:51:59 +00:00
else:
self.blockAvailable = 0
self.tta = -1
def addMetric(self, name, metric):
"""Generic function to add a metric to the results."""
self.metrics[name] = metric
def dump(self):
"""It dumps the results of the simulation in an XML file."""
2023-01-25 20:51:59 +00:00
if not os.path.exists("results"):
os.makedirs("results")
if not os.path.exists("results/"+self.execID):
os.makedirs("results/"+self.execID)
2023-01-25 20:51:59 +00:00
resd1 = self.shape.__dict__
resd2 = self.__dict__.copy()
resd2.pop("shape")
resd1.update(resd2)
resXml = dicttoxml(resd1)
xmlstr = minidom.parseString(resXml)
xmlPretty = xmlstr.toprettyxml()
filePath = "results/"+self.execID+"/"+str(self.shape)+".xml"
2023-01-25 20:51:59 +00:00
with open(filePath, "w") as f:
f.write(xmlPretty)