class MonitorsTable(object):
    def __init__(self, model_name: str, max_width):
        self.model_name = model_name
        self._training_monitors = {}
        self._validation_monitors = {}
        self._test_monitors = {}
        self._max_width = max_width
        self.table = BeautifulTable(max_width)

    def append(self, values: dict, old_values: dict):
        self.table.rows.append(
            list(
                map(
                    lambda key: self.color(values[key],
                                           old_values.get(key, None)),
                    values.keys())))

    def update(self,
               training_monitors: dict,
               validation_monitors: dict,
               test_monitors: Optional[dict] = None):
        self.table.clear()
        self.append(training_monitors, self._training_monitors)
        self.append(validation_monitors, self._validation_monitors)

        if test_monitors is not None:
            self.append(test_monitors, self._test_monitors)
            self.table.rows.header = ["Training", "Validation", "Test"]
        else:
            self.table.rows.header = ["Training", "Validation"]

        self.table.columns.header = training_monitors.keys()

        self._training_monitors = dict(training_monitors)
        self._validation_monitors = dict(validation_monitors)
        self._test_monitors = dict(test_monitors)

    def show(self):
        self.table._compute_width()
        width = self.table._width - 2 if self.table._width == self._max_width else self.table._width + 11
        topline = "".join(["+", "-" * width, "+"])
        print(topline)
        spc = (len(topline) - 2 - len(self.model_name)) / 2
        print("%s%s%s%s%s" % ("|", " " * math.ceil(spc), self.model_name,
                              " " * math.floor(spc), "|"))
        print(self.table)

    def color(self, value, old_value):
        if old_value is not None:
            if value > old_value:
                return crayons.green("{} \u2197".format(value), bold=True)
            if value < old_value:
                return crayons.red("{} \u2198".format(value), bold=True)

        return crayons.white("{} \u2192".format(value), bold=True)
Exemple #2
0
class MonitorsTable(object):
    def __init__(self, model_name: str):
        self.model_name = model_name
        self._monitors = {}
        self._new_monitors = {}

        self.table = BeautifulTable(200)

    def append(self, values: dict, old_values: dict):
        self.table.rows.append(
            list(
                map(
                    lambda key: self.color(values[key],
                                           old_values.get(key, None)),
                    values.keys())))

    def update(self, monitors: dict, phase: Phase):
        self.table.clear()

        if phase not in self._monitors:
            self._monitors[phase] = monitors
        self._new_monitors[phase] = monitors

        for key, value in self._monitors.items():
            self.append(self._new_monitors[key], self._monitors[key])

        self.table.columns.header = monitors.keys()
        self.table.rows.header = list(
            map(lambda key: str(key), self._monitors.keys()))

    def show(self):
        self.table._compute_width()
        topline = "".join([
            "+", "-" *
            (self.table._width + 1 +
             max(list(map(lambda key: len(str(key)), self._monitors.keys())))),
            "+"
        ])
        print(topline)
        spc = (len(topline) - 2 - len(self.model_name)) / 2
        print("%s%s%s%s%s" % ("|", " " * math.ceil(spc), self.model_name,
                              " " * math.floor(spc), "|"))
        print(self.table)

    def color(self, value, old_value):
        if old_value is not None:
            if value > old_value:
                return crayons.green("{} \u2197".format(value), bold=True)
            if value < old_value:
                return crayons.red("{} \u2198".format(value), bold=True)

        return crayons.white("{} \u2192".format(value), bold=True)