retuve.classes.metrics

Metric Classes for all Retuve Models.

  1# Copyright 2024 Adam McArthur
  2#
  3# Licensed under the Apache License, Version 2.0 (the "License");
  4# you may not use this file except in compliance with the License.
  5# You may obtain a copy of the License at
  6#
  7#     http://www.apache.org/licenses/LICENSE-2.0
  8#
  9# Unless required by applicable law or agreed to in writing, software
 10# distributed under the License is distributed on an "AS IS" BASIS,
 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12# See the License for the specific language governing permissions and
 13# limitations under the License.
 14
 15"""
 16Metric Classes for all Retuve Models.
 17"""
 18
 19from typing import List, Union
 20
 21
 22class Metric2D:
 23    """
 24    2D Metrics just have a name and a value.
 25    """
 26
 27    def __init__(self, name: str, value: Union[int, float]):
 28        """
 29        Initialize a 2D Metric.
 30
 31        :param name: Name of the metric.
 32        :param value: Value of the metric.
 33        """
 34        self.name = name
 35        self.value = value
 36
 37    def __str__(self) -> str:
 38        return f"Metric2D(name={self.name}, value={self.value})"
 39
 40    def __repr__(self):
 41        return self.__str__()
 42
 43
 44class Metric3D:
 45    """
 46    3D Metrics have a name and 4 values: Post, Graf, Ant, Full.
 47    """
 48
 49    def __init__(
 50        self,
 51        name: str,
 52        post: Union[float, str] = 0,
 53        graf: Union[float, str] = 0,
 54        ant: Union[float, str] = 0,
 55        full: Union[float, None] = None,
 56    ):
 57        """
 58        If full is not not provided, it is calculated as the average of Post, Graf, and Ant.
 59        """
 60        if full is None:
 61            self.name = name
 62            self.post = post
 63            self.graf = graf
 64            self.ant = ant
 65
 66            average_calc = []
 67            for val in [post, graf, ant]:
 68                if val > 0:
 69                    average_calc.append(val)
 70
 71            if len(average_calc) == 0:
 72                self.full = 0
 73            else:
 74                self.full = round(sum(average_calc) / len(average_calc), 2)
 75
 76        else:
 77            self.name = name
 78            self.post, self.graf, self.ant = "N/A", "N/A", "N/A"
 79            self.full = full
 80
 81    def dump(self) -> List[Union[str, float]]:
 82        """
 83        Dump the values of the metric into a list.
 84
 85        Useful for writing to CSV.
 86        """
 87        return [
 88            self.name.capitalize(),
 89            self.post,
 90            self.graf,
 91            self.ant,
 92            self.full,
 93        ]
 94
 95    def names(self) -> List[str]:
 96        """
 97        Return the names of the values in the metric. (In order)
 98        """
 99
100        return ["Post", "Graf", "Ant", "Full"]
101
102    def __str__(self) -> str:
103        return f"Metric3D(name={self.name}, value={self.full})"
104
105    def __repr__(self):
106        return self.__str__()
class Metric2D:
23class Metric2D:
24    """
25    2D Metrics just have a name and a value.
26    """
27
28    def __init__(self, name: str, value: Union[int, float]):
29        """
30        Initialize a 2D Metric.
31
32        :param name: Name of the metric.
33        :param value: Value of the metric.
34        """
35        self.name = name
36        self.value = value
37
38    def __str__(self) -> str:
39        return f"Metric2D(name={self.name}, value={self.value})"
40
41    def __repr__(self):
42        return self.__str__()

2D Metrics just have a name and a value.

Metric2D(name: str, value: Union[int, float])
28    def __init__(self, name: str, value: Union[int, float]):
29        """
30        Initialize a 2D Metric.
31
32        :param name: Name of the metric.
33        :param value: Value of the metric.
34        """
35        self.name = name
36        self.value = value

Initialize a 2D Metric.

Parameters
  • name: Name of the metric.
  • value: Value of the metric.
name
value
class Metric3D:
 45class Metric3D:
 46    """
 47    3D Metrics have a name and 4 values: Post, Graf, Ant, Full.
 48    """
 49
 50    def __init__(
 51        self,
 52        name: str,
 53        post: Union[float, str] = 0,
 54        graf: Union[float, str] = 0,
 55        ant: Union[float, str] = 0,
 56        full: Union[float, None] = None,
 57    ):
 58        """
 59        If full is not not provided, it is calculated as the average of Post, Graf, and Ant.
 60        """
 61        if full is None:
 62            self.name = name
 63            self.post = post
 64            self.graf = graf
 65            self.ant = ant
 66
 67            average_calc = []
 68            for val in [post, graf, ant]:
 69                if val > 0:
 70                    average_calc.append(val)
 71
 72            if len(average_calc) == 0:
 73                self.full = 0
 74            else:
 75                self.full = round(sum(average_calc) / len(average_calc), 2)
 76
 77        else:
 78            self.name = name
 79            self.post, self.graf, self.ant = "N/A", "N/A", "N/A"
 80            self.full = full
 81
 82    def dump(self) -> List[Union[str, float]]:
 83        """
 84        Dump the values of the metric into a list.
 85
 86        Useful for writing to CSV.
 87        """
 88        return [
 89            self.name.capitalize(),
 90            self.post,
 91            self.graf,
 92            self.ant,
 93            self.full,
 94        ]
 95
 96    def names(self) -> List[str]:
 97        """
 98        Return the names of the values in the metric. (In order)
 99        """
100
101        return ["Post", "Graf", "Ant", "Full"]
102
103    def __str__(self) -> str:
104        return f"Metric3D(name={self.name}, value={self.full})"
105
106    def __repr__(self):
107        return self.__str__()

3D Metrics have a name and 4 values: Post, Graf, Ant, Full.

Metric3D( name: str, post: Union[float, str] = 0, graf: Union[float, str] = 0, ant: Union[float, str] = 0, full: Optional[float] = None)
50    def __init__(
51        self,
52        name: str,
53        post: Union[float, str] = 0,
54        graf: Union[float, str] = 0,
55        ant: Union[float, str] = 0,
56        full: Union[float, None] = None,
57    ):
58        """
59        If full is not not provided, it is calculated as the average of Post, Graf, and Ant.
60        """
61        if full is None:
62            self.name = name
63            self.post = post
64            self.graf = graf
65            self.ant = ant
66
67            average_calc = []
68            for val in [post, graf, ant]:
69                if val > 0:
70                    average_calc.append(val)
71
72            if len(average_calc) == 0:
73                self.full = 0
74            else:
75                self.full = round(sum(average_calc) / len(average_calc), 2)
76
77        else:
78            self.name = name
79            self.post, self.graf, self.ant = "N/A", "N/A", "N/A"
80            self.full = full

If full is not not provided, it is calculated as the average of Post, Graf, and Ant.

def dump(self) -> List[Union[str, float]]:
82    def dump(self) -> List[Union[str, float]]:
83        """
84        Dump the values of the metric into a list.
85
86        Useful for writing to CSV.
87        """
88        return [
89            self.name.capitalize(),
90            self.post,
91            self.graf,
92            self.ant,
93            self.full,
94        ]

Dump the values of the metric into a list.

Useful for writing to CSV.

def names(self) -> List[str]:
 96    def names(self) -> List[str]:
 97        """
 98        Return the names of the values in the metric. (In order)
 99        """
100
101        return ["Post", "Graf", "Ant", "Full"]

Return the names of the values in the metric. (In order)