src.slune.loggers.default

 1import pandas as pd
 2from slune.base import BaseLogger
 3
 4class LoggerDefault(BaseLogger):
 5    """ Logs metric/s in a data frame.
 6    
 7    Stores the metric/s in a data frame that we can later save in storage.
 8    Logs by creating data frame out of the metrics and then appending it to the current results data frame.
 9
10    Attributes:
11        - results (pd.DataFrame): Data frame containing all the metrics logged so far.
12            Each row stores all the metrics that were given in a call to the 'log' method,
13            each column title is a metric name.
14            The first column is always the time stamp at which 'log' is called.
15
16    """
17    
18    def __init__(self, *args, **kwargs):
19        """ Initialises the logger. """
20
21        super(LoggerDefault, self).__init__(*args, **kwargs)
22        # Raise warning if any arguments are given
23        if args or kwargs:
24            raise Warning(f"Arguments {args} and keyword arguments {kwargs} are ignored")
25        # Initialise results data frame
26        self.results = pd.DataFrame()
27    
28    def log(self, metrics: dict):
29        """ Logs the metric/s given.
30
31        Stores them in a data frame that we can later save in storage.
32        All metrics provided will be saved as a row in the results data frame,
33        the first column is always the time stamp at which log is called.
34
35        Args:
36            - metrics (dict): Metrics to be logged, keys are metric names and values are metric values.
37                Each metric should only have one value! So please log as soon as you get a metric.
38
39        """
40
41        # Get current time stamp
42        time_stamp = pd.Timestamp.now()
43        # Add time stamp to metrics dictionary
44        metrics['time_stamp'] = time_stamp
45        # Convert metrics dictionary to a dataframe
46        metrics_df = pd.DataFrame(metrics, index=[0])
47        # Append metrics dataframe to results dataframe
48        self.results = pd.concat([self.results, metrics_df], ignore_index=True)
49    
50    def read_log(self, data_frame: pd.DataFrame, metric_name: str, select_by: str ='max') -> float:
51        """ Reads log and returns value according to select_by.
52
53        Reads the values for given metric for given log and chooses metric value to return based on select_by.
54
55        Args:
56            - data_frame (pd.DataFrame): Data frame containing the metric to be read.
57            - metric_name (str): Name of the metric to be read.
58            - select_by (str, optional): How to select the 'best' metric, currently can select by 'min' or 'max'.
59
60        Returns:
61            - value (float): Minimum or maximum value of the metric.
62
63        TODO: 
64            - Add more options for select_by.
65            - Should be able to return other types than float?
66
67        """ 
68
69        # Get the metric column
70        metric_col = data_frame[metric_name]
71        # Get the index of the minimum or maximum value
72        if select_by == 'max':
73            index = metric_col.idxmax()
74        elif select_by == 'min':
75            index = metric_col.idxmin()
76        else:
77            raise ValueError(f"select_by must be 'min' or 'max', got {select_by}")
78        # Get the value of the metric
79        value = metric_col.iloc[index]
80        return value
class LoggerDefault(slune.base.BaseLogger):
 5class LoggerDefault(BaseLogger):
 6    """ Logs metric/s in a data frame.
 7    
 8    Stores the metric/s in a data frame that we can later save in storage.
 9    Logs by creating data frame out of the metrics and then appending it to the current results data frame.
10
11    Attributes:
12        - results (pd.DataFrame): Data frame containing all the metrics logged so far.
13            Each row stores all the metrics that were given in a call to the 'log' method,
14            each column title is a metric name.
15            The first column is always the time stamp at which 'log' is called.
16
17    """
18    
19    def __init__(self, *args, **kwargs):
20        """ Initialises the logger. """
21
22        super(LoggerDefault, self).__init__(*args, **kwargs)
23        # Raise warning if any arguments are given
24        if args or kwargs:
25            raise Warning(f"Arguments {args} and keyword arguments {kwargs} are ignored")
26        # Initialise results data frame
27        self.results = pd.DataFrame()
28    
29    def log(self, metrics: dict):
30        """ Logs the metric/s given.
31
32        Stores them in a data frame that we can later save in storage.
33        All metrics provided will be saved as a row in the results data frame,
34        the first column is always the time stamp at which log is called.
35
36        Args:
37            - metrics (dict): Metrics to be logged, keys are metric names and values are metric values.
38                Each metric should only have one value! So please log as soon as you get a metric.
39
40        """
41
42        # Get current time stamp
43        time_stamp = pd.Timestamp.now()
44        # Add time stamp to metrics dictionary
45        metrics['time_stamp'] = time_stamp
46        # Convert metrics dictionary to a dataframe
47        metrics_df = pd.DataFrame(metrics, index=[0])
48        # Append metrics dataframe to results dataframe
49        self.results = pd.concat([self.results, metrics_df], ignore_index=True)
50    
51    def read_log(self, data_frame: pd.DataFrame, metric_name: str, select_by: str ='max') -> float:
52        """ Reads log and returns value according to select_by.
53
54        Reads the values for given metric for given log and chooses metric value to return based on select_by.
55
56        Args:
57            - data_frame (pd.DataFrame): Data frame containing the metric to be read.
58            - metric_name (str): Name of the metric to be read.
59            - select_by (str, optional): How to select the 'best' metric, currently can select by 'min' or 'max'.
60
61        Returns:
62            - value (float): Minimum or maximum value of the metric.
63
64        TODO: 
65            - Add more options for select_by.
66            - Should be able to return other types than float?
67
68        """ 
69
70        # Get the metric column
71        metric_col = data_frame[metric_name]
72        # Get the index of the minimum or maximum value
73        if select_by == 'max':
74            index = metric_col.idxmax()
75        elif select_by == 'min':
76            index = metric_col.idxmin()
77        else:
78            raise ValueError(f"select_by must be 'min' or 'max', got {select_by}")
79        # Get the value of the metric
80        value = metric_col.iloc[index]
81        return value

Logs metric/s in a data frame.

Stores the metric/s in a data frame that we can later save in storage. Logs by creating data frame out of the metrics and then appending it to the current results data frame.

Attributes:
  • - results (pd.DataFrame): Data frame containing all the metrics logged so far. Each row stores all the metrics that were given in a call to the 'log' method, each column title is a metric name. The first column is always the time stamp at which 'log' is called.
LoggerDefault(*args, **kwargs)
19    def __init__(self, *args, **kwargs):
20        """ Initialises the logger. """
21
22        super(LoggerDefault, self).__init__(*args, **kwargs)
23        # Raise warning if any arguments are given
24        if args or kwargs:
25            raise Warning(f"Arguments {args} and keyword arguments {kwargs} are ignored")
26        # Initialise results data frame
27        self.results = pd.DataFrame()

Initialises the logger.

results
def log(self, metrics: dict):
29    def log(self, metrics: dict):
30        """ Logs the metric/s given.
31
32        Stores them in a data frame that we can later save in storage.
33        All metrics provided will be saved as a row in the results data frame,
34        the first column is always the time stamp at which log is called.
35
36        Args:
37            - metrics (dict): Metrics to be logged, keys are metric names and values are metric values.
38                Each metric should only have one value! So please log as soon as you get a metric.
39
40        """
41
42        # Get current time stamp
43        time_stamp = pd.Timestamp.now()
44        # Add time stamp to metrics dictionary
45        metrics['time_stamp'] = time_stamp
46        # Convert metrics dictionary to a dataframe
47        metrics_df = pd.DataFrame(metrics, index=[0])
48        # Append metrics dataframe to results dataframe
49        self.results = pd.concat([self.results, metrics_df], ignore_index=True)

Logs the metric/s given.

Stores them in a data frame that we can later save in storage. All metrics provided will be saved as a row in the results data frame, the first column is always the time stamp at which log is called.

Arguments:
  • - metrics (dict): Metrics to be logged, keys are metric names and values are metric values. Each metric should only have one value! So please log as soon as you get a metric.
def read_log( self, data_frame: pandas.core.frame.DataFrame, metric_name: str, select_by: str = 'max') -> float:
51    def read_log(self, data_frame: pd.DataFrame, metric_name: str, select_by: str ='max') -> float:
52        """ Reads log and returns value according to select_by.
53
54        Reads the values for given metric for given log and chooses metric value to return based on select_by.
55
56        Args:
57            - data_frame (pd.DataFrame): Data frame containing the metric to be read.
58            - metric_name (str): Name of the metric to be read.
59            - select_by (str, optional): How to select the 'best' metric, currently can select by 'min' or 'max'.
60
61        Returns:
62            - value (float): Minimum or maximum value of the metric.
63
64        TODO: 
65            - Add more options for select_by.
66            - Should be able to return other types than float?
67
68        """ 
69
70        # Get the metric column
71        metric_col = data_frame[metric_name]
72        # Get the index of the minimum or maximum value
73        if select_by == 'max':
74            index = metric_col.idxmax()
75        elif select_by == 'min':
76            index = metric_col.idxmin()
77        else:
78            raise ValueError(f"select_by must be 'min' or 'max', got {select_by}")
79        # Get the value of the metric
80        value = metric_col.iloc[index]
81        return value

Reads log and returns value according to select_by.

Reads the values for given metric for given log and chooses metric value to return based on select_by.

Arguments:
  • - data_frame (pd.DataFrame): Data frame containing the metric to be read.
  • - metric_name (str): Name of the metric to be read.
  • - select_by (str, optional): How to select the 'best' metric, currently can select by 'min' or 'max'.
Returns:
  • value (float): Minimum or maximum value of the metric.

TODO: - Add more options for select_by. - Should be able to return other types than float?