Skip to content

Result Augmentation Module

SRToolkit.evaluation.result_augmentation

This module contains the ResultAugmenter class and the result augmentation implementations that inherit from it.

ResultAugmenter

Source code in SRToolkit/evaluation/result_augmentation.py
class ResultAugmenter:
    def __init__(self):
        """
        Generic class that defines the interface for result augmentation.
        """
        pass

    def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
        """
        Augments the results dictionary with additional information. The model variable contains all models, for only
        top models, results["top_models"] should be used.

        Args:
            results: The dictionary containing the results to augment.
            models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i,
                models[i]["error"] contains the error for model i, ...
            evaluator: The evaluator used to evaluate the models.

        Returns:
            The augmented results dictionary.
        """
        pass

__init__()

Generic class that defines the interface for result augmentation.

Source code in SRToolkit/evaluation/result_augmentation.py
def __init__(self):
    """
    Generic class that defines the interface for result augmentation.
    """
    pass

augment_results(results, models, evaluator)

Augments the results dictionary with additional information. The model variable contains all models, for only top models, results["top_models"] should be used.

Parameters:

Name Type Description Default
results dict

The dictionary containing the results to augment.

required
models List[dict]

The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, models[i]["error"] contains the error for model i, ...

required
evaluator SR_evaluator

The evaluator used to evaluate the models.

required

Returns:

Type Description
dict

The augmented results dictionary.

Source code in SRToolkit/evaluation/result_augmentation.py
def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
    """
    Augments the results dictionary with additional information. The model variable contains all models, for only
    top models, results["top_models"] should be used.

    Args:
        results: The dictionary containing the results to augment.
        models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i,
            models[i]["error"] contains the error for model i, ...
        evaluator: The evaluator used to evaluate the models.

    Returns:
        The augmented results dictionary.
    """
    pass

ExpressionToLatex

Bases: ResultAugmenter

Source code in SRToolkit/evaluation/result_augmentation.py
class ExpressionToLatex(ResultAugmenter):
    def __init__(self, only_best_expression: bool=False):
        """
        Transforms the expressions inside the results dictionary into LaTeX strings.

        Args:
            only_best_expression: If True, only the best expression is transformed. If False, all top expressions are
        """
        super().__init__()
        self.only_best_expression = only_best_expression

    def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
        """
        Transforms the expressions inside the results dictionary into LaTeX strings.

        Args:
            results: The dictionary containing the results to augment.
            models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
            evaluator: The evaluator used to evaluate the models.

        Returns:
            The augmented results dictionary. The results dictionary contains an additional key "best_expr_latex" with
                the LaTeX representation of the best expression, and similarly keys "expr_latex" for expressions inside
                the top_models list if only_best_expression is False.
        """
        results["best_expr_latex"] = tokens_to_tree(models[0]["expr"], evaluator.symbol_library).to_latex(evaluator.symbol_library)
        if not self.only_best_expression:
            for model in results["top_models"]:
                model["expr_latex"] = tokens_to_tree(model["expr"], evaluator.symbol_library).to_latex(evaluator.symbol_library)

        return results

__init__(only_best_expression=False)

Transforms the expressions inside the results dictionary into LaTeX strings.

Parameters:

Name Type Description Default
only_best_expression bool

If True, only the best expression is transformed. If False, all top expressions are

False
Source code in SRToolkit/evaluation/result_augmentation.py
def __init__(self, only_best_expression: bool=False):
    """
    Transforms the expressions inside the results dictionary into LaTeX strings.

    Args:
        only_best_expression: If True, only the best expression is transformed. If False, all top expressions are
    """
    super().__init__()
    self.only_best_expression = only_best_expression

augment_results(results, models, evaluator)

Transforms the expressions inside the results dictionary into LaTeX strings.

Parameters:

Name Type Description Default
results dict

The dictionary containing the results to augment.

required
models List[dict]

The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...

required
evaluator SR_evaluator

The evaluator used to evaluate the models.

required

Returns:

Type Description
dict

The augmented results dictionary. The results dictionary contains an additional key "best_expr_latex" with the LaTeX representation of the best expression, and similarly keys "expr_latex" for expressions inside the top_models list if only_best_expression is False.

Source code in SRToolkit/evaluation/result_augmentation.py
def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
    """
    Transforms the expressions inside the results dictionary into LaTeX strings.

    Args:
        results: The dictionary containing the results to augment.
        models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
        evaluator: The evaluator used to evaluate the models.

    Returns:
        The augmented results dictionary. The results dictionary contains an additional key "best_expr_latex" with
            the LaTeX representation of the best expression, and similarly keys "expr_latex" for expressions inside
            the top_models list if only_best_expression is False.
    """
    results["best_expr_latex"] = tokens_to_tree(models[0]["expr"], evaluator.symbol_library).to_latex(evaluator.symbol_library)
    if not self.only_best_expression:
        for model in results["top_models"]:
            model["expr_latex"] = tokens_to_tree(model["expr"], evaluator.symbol_library).to_latex(evaluator.symbol_library)

    return results

ExpressionSimplifier

Bases: ResultAugmenter

Source code in SRToolkit/evaluation/result_augmentation.py
class ExpressionSimplifier(ResultAugmenter):
    def __init__(self, only_best_expression: bool=False, verbose: bool=False):
        """
        Simplifies the expressions inside the results dictionary if possible.

        Args:
            only_best_expression: If True, only the best expression is simplified. If False, all top expressions are
                simplified.
            verbose: If True, warns the user if simplification fails for a given expression.
        """
        super().__init__()
        self.only_best_expression = only_best_expression
        self.verbose = verbose

    def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
        """
        Simplifies the expressions inside the results dictionary if possible.

        Args:
            results: The dictionary containing the results to augment.
            models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
            evaluator: The evaluator used to evaluate the models.

        Returns:
            The augmented results dictionary. The results dictionary contains an additional key "simplified_best_expr"
            if simplification was successful for the best expression, and similarly keys "simplified_expr" inside the
            top_models list if only_best_expression is False.
        """
        try:
            simplified_expr = simplify(models[0]["expr"], evaluator.symbol_library)
            results["simplified_best_expr"] = "".join(simplified_expr)
        except Exception as e:
            if self.verbose:
                print(f"Unable to simplify {results['best_expr']}: {e}")

        for model in results["top_models"]:
            try:
                simplified_expr = simplify(model["expr"], evaluator.symbol_library)
                model["simplified_expr"] = "".join(simplified_expr)
            except Exception as e:
                if self.verbose:
                    print(f"Unable to simplify {model['expr']}: {e}")

        return results

__init__(only_best_expression=False, verbose=False)

Simplifies the expressions inside the results dictionary if possible.

Parameters:

Name Type Description Default
only_best_expression bool

If True, only the best expression is simplified. If False, all top expressions are simplified.

False
verbose bool

If True, warns the user if simplification fails for a given expression.

False
Source code in SRToolkit/evaluation/result_augmentation.py
def __init__(self, only_best_expression: bool=False, verbose: bool=False):
    """
    Simplifies the expressions inside the results dictionary if possible.

    Args:
        only_best_expression: If True, only the best expression is simplified. If False, all top expressions are
            simplified.
        verbose: If True, warns the user if simplification fails for a given expression.
    """
    super().__init__()
    self.only_best_expression = only_best_expression
    self.verbose = verbose

augment_results(results, models, evaluator)

Simplifies the expressions inside the results dictionary if possible.

Parameters:

Name Type Description Default
results dict

The dictionary containing the results to augment.

required
models List[dict]

The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...

required
evaluator SR_evaluator

The evaluator used to evaluate the models.

required

Returns:

Type Description
dict

The augmented results dictionary. The results dictionary contains an additional key "simplified_best_expr"

dict

if simplification was successful for the best expression, and similarly keys "simplified_expr" inside the

dict

top_models list if only_best_expression is False.

Source code in SRToolkit/evaluation/result_augmentation.py
def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
    """
    Simplifies the expressions inside the results dictionary if possible.

    Args:
        results: The dictionary containing the results to augment.
        models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
        evaluator: The evaluator used to evaluate the models.

    Returns:
        The augmented results dictionary. The results dictionary contains an additional key "simplified_best_expr"
        if simplification was successful for the best expression, and similarly keys "simplified_expr" inside the
        top_models list if only_best_expression is False.
    """
    try:
        simplified_expr = simplify(models[0]["expr"], evaluator.symbol_library)
        results["simplified_best_expr"] = "".join(simplified_expr)
    except Exception as e:
        if self.verbose:
            print(f"Unable to simplify {results['best_expr']}: {e}")

    for model in results["top_models"]:
        try:
            simplified_expr = simplify(model["expr"], evaluator.symbol_library)
            model["simplified_expr"] = "".join(simplified_expr)
        except Exception as e:
            if self.verbose:
                print(f"Unable to simplify {model['expr']}: {e}")

    return results

RMSE

Bases: ResultAugmenter

Source code in SRToolkit/evaluation/result_augmentation.py
class RMSE(ResultAugmenter):
    def __init__(self, evaluator: "SR_evaluator"):
        """
        Computes the RMSE for the top models in the results dictionary.

        Args:
             evaluator: The evaluator used to evaluate the models (e.g., evaluator defined with test set data). This
                evaluator must be initialized with ranking_function = "rmse"

        Raises:
            Exception: If the evaluator is not initialized with ranking_function = "rmse" or if y in the evaluator is None.
        """
        super().__init__()
        self.evaluator = evaluator
        if self.evaluator.ranking_function != "rmse":
            raise Exception("[RMSE augmenter] Ranking function of the evaluator must be set to 'rmse' to compute RMSE.")
        if self.evaluator.y is None:
            raise Exception("[RMSE augmenter] y in the evaluator must not be None to compute RMSE.")

    def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
        """
        Computes the RMSE for the top models in the results dictionary.

        Args:
            results: The dictionary containing the results to augment.
            models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
            evaluator: The evaluator used to evaluate the models.

        Returns:
            The augmented results dictionary. The results dictionary contains an additional key "best_expr_rmse" with the
            RMSE of the best expression, and keys "rmse" and "parameters_rmse" for each of the top_models inside the
            results["top_models"] list.
        """
        expr = models[0]["expr"]
        error = self.evaluator.evaluate_expr(expr)
        results["best_expr_rmse"] = error
        for model in results["top_models"]:
            error = self.evaluator.evaluate_expr(model["expr"])
            model["rmse"] = error
            model["parameters_rmse"] = self.evaluator.models["".join(model["expr"])]["parameters"]
        return results

__init__(evaluator)

Computes the RMSE for the top models in the results dictionary.

Parameters:

Name Type Description Default
evaluator SR_evaluator

The evaluator used to evaluate the models (e.g., evaluator defined with test set data). This evaluator must be initialized with ranking_function = "rmse"

required

Raises:

Type Description
Exception

If the evaluator is not initialized with ranking_function = "rmse" or if y in the evaluator is None.

Source code in SRToolkit/evaluation/result_augmentation.py
def __init__(self, evaluator: "SR_evaluator"):
    """
    Computes the RMSE for the top models in the results dictionary.

    Args:
         evaluator: The evaluator used to evaluate the models (e.g., evaluator defined with test set data). This
            evaluator must be initialized with ranking_function = "rmse"

    Raises:
        Exception: If the evaluator is not initialized with ranking_function = "rmse" or if y in the evaluator is None.
    """
    super().__init__()
    self.evaluator = evaluator
    if self.evaluator.ranking_function != "rmse":
        raise Exception("[RMSE augmenter] Ranking function of the evaluator must be set to 'rmse' to compute RMSE.")
    if self.evaluator.y is None:
        raise Exception("[RMSE augmenter] y in the evaluator must not be None to compute RMSE.")

augment_results(results, models, evaluator)

Computes the RMSE for the top models in the results dictionary.

Parameters:

Name Type Description Default
results dict

The dictionary containing the results to augment.

required
models List[dict]

The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...

required
evaluator SR_evaluator

The evaluator used to evaluate the models.

required

Returns:

Type Description
dict

The augmented results dictionary. The results dictionary contains an additional key "best_expr_rmse" with the

dict

RMSE of the best expression, and keys "rmse" and "parameters_rmse" for each of the top_models inside the

dict

results["top_models"] list.

Source code in SRToolkit/evaluation/result_augmentation.py
def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
    """
    Computes the RMSE for the top models in the results dictionary.

    Args:
        results: The dictionary containing the results to augment.
        models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
        evaluator: The evaluator used to evaluate the models.

    Returns:
        The augmented results dictionary. The results dictionary contains an additional key "best_expr_rmse" with the
        RMSE of the best expression, and keys "rmse" and "parameters_rmse" for each of the top_models inside the
        results["top_models"] list.
    """
    expr = models[0]["expr"]
    error = self.evaluator.evaluate_expr(expr)
    results["best_expr_rmse"] = error
    for model in results["top_models"]:
        error = self.evaluator.evaluate_expr(model["expr"])
        model["rmse"] = error
        model["parameters_rmse"] = self.evaluator.models["".join(model["expr"])]["parameters"]
    return results

BED

Bases: ResultAugmenter

Source code in SRToolkit/evaluation/result_augmentation.py
class BED(ResultAugmenter):
    def __init__(self, evaluator: "SR_evaluator"):
        """
        Computes BED for the top models in the results dictionary.

        Args:
             evaluator: The evaluator used to evaluate the models. This evaluator must be initialized with
             ranking_function = "bed"

        Raises:
            Exception: If the evaluator is not initialized with ranking_function = "bed".
        """
        super().__init__()
        self.evaluator = evaluator
        if self.evaluator.ranking_function != "bed":
            raise Exception("[BED augmenter] Ranking function of the evaluator must be set to 'bed' to compute BED.")

    def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
        """
        Computes BED for the top models in the results dictionary.

        Args:
            results: The dictionary containing the results to augment.
            models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
            evaluator: The evaluator used to evaluate the models.

        Returns:
            The augmented results dictionary. The results dictionary contains an additional key "best_expr_bed" with
            BED of the best expression, and key "bed" for each of the top_models inside the results["top_models"] list.
        """
        expr = models[0]["expr"]
        error = self.evaluator.evaluate_expr(expr)
        results["best_expr_bed"] = error
        for model in results["top_models"]:
            model["bed"] = self.evaluator.evaluate_expr(model["expr"])
        return results

__init__(evaluator)

Computes BED for the top models in the results dictionary.

Parameters:

Name Type Description Default
evaluator SR_evaluator

The evaluator used to evaluate the models. This evaluator must be initialized with

required

Raises:

Type Description
Exception

If the evaluator is not initialized with ranking_function = "bed".

Source code in SRToolkit/evaluation/result_augmentation.py
def __init__(self, evaluator: "SR_evaluator"):
    """
    Computes BED for the top models in the results dictionary.

    Args:
         evaluator: The evaluator used to evaluate the models. This evaluator must be initialized with
         ranking_function = "bed"

    Raises:
        Exception: If the evaluator is not initialized with ranking_function = "bed".
    """
    super().__init__()
    self.evaluator = evaluator
    if self.evaluator.ranking_function != "bed":
        raise Exception("[BED augmenter] Ranking function of the evaluator must be set to 'bed' to compute BED.")

augment_results(results, models, evaluator)

Computes BED for the top models in the results dictionary.

Parameters:

Name Type Description Default
results dict

The dictionary containing the results to augment.

required
models List[dict]

The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...

required
evaluator SR_evaluator

The evaluator used to evaluate the models.

required

Returns:

Type Description
dict

The augmented results dictionary. The results dictionary contains an additional key "best_expr_bed" with

dict

BED of the best expression, and key "bed" for each of the top_models inside the results["top_models"] list.

Source code in SRToolkit/evaluation/result_augmentation.py
def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
    """
    Computes BED for the top models in the results dictionary.

    Args:
        results: The dictionary containing the results to augment.
        models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
        evaluator: The evaluator used to evaluate the models.

    Returns:
        The augmented results dictionary. The results dictionary contains an additional key "best_expr_bed" with
        BED of the best expression, and key "bed" for each of the top_models inside the results["top_models"] list.
    """
    expr = models[0]["expr"]
    error = self.evaluator.evaluate_expr(expr)
    results["best_expr_bed"] = error
    for model in results["top_models"]:
        model["bed"] = self.evaluator.evaluate_expr(model["expr"])
    return results

R2

Bases: ResultAugmenter

Source code in SRToolkit/evaluation/result_augmentation.py
class R2(ResultAugmenter):
    def __init__(self, evaluator: "SR_evaluator"):
        """
        Computes the R^2 for the top models in the results dictionary. NOT IMPLEMENTED YET, CURRENTLY YOU GET RMSE.

        Args:
             evaluator: The evaluator used to evaluate the models (e.g., evaluator defined with test set data). This
                evaluator must be initialized with ranking_function = "rmse". If you're also using the RMSE augmenter,
                they the same one can be used for both.

        Raises:
            Exception: If the evaluator is not initialized with ranking_function = "rmse" or if y in the evaluator is None.
        """
        super().__init__()
        self.evaluator = evaluator
        if self.evaluator.ranking_function != "rmse":
            raise Exception("[R2 augmenter] Ranking function of the evaluator must be set to 'rmse' to compute R^2.")
        if self.evaluator.y is None:
            raise Exception("[R2 augmenter] y in the evaluator must not be None to compute R^2.")
        self.average_y = np.mean(self.evaluator.y)

    def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
        """
        Computes the R^2 for the top models in the results dictionary.

        Args:
            results: The dictionary containing the results to augment.
            models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
            evaluator: The evaluator used to evaluate the models.

        Returns:
            The augmented results dictionary. The results dictionary contains an additional key "best_expr_r^2" with the
            R^2 of the best expression, and keys "r^2" and "parameters_r^2" for each of the top_models inside the
            results["top_models"] list.
        """
        expr = models[0]["expr"]
        error = self.evaluator.evaluate_expr(expr)
        results["best_expr_r^2"] = error # TODO: Popravi
        for model in results["top_models"]:
            error = self.evaluator.evaluate_expr(model["expr"])
            model["r^2"] = error
            model["parameters_r^2"] = self.evaluator.models["".join(model["expr"])]["parameters"]
        return results

__init__(evaluator)

Computes the R^2 for the top models in the results dictionary. NOT IMPLEMENTED YET, CURRENTLY YOU GET RMSE.

Parameters:

Name Type Description Default
evaluator SR_evaluator

The evaluator used to evaluate the models (e.g., evaluator defined with test set data). This evaluator must be initialized with ranking_function = "rmse". If you're also using the RMSE augmenter, they the same one can be used for both.

required

Raises:

Type Description
Exception

If the evaluator is not initialized with ranking_function = "rmse" or if y in the evaluator is None.

Source code in SRToolkit/evaluation/result_augmentation.py
def __init__(self, evaluator: "SR_evaluator"):
    """
    Computes the R^2 for the top models in the results dictionary. NOT IMPLEMENTED YET, CURRENTLY YOU GET RMSE.

    Args:
         evaluator: The evaluator used to evaluate the models (e.g., evaluator defined with test set data). This
            evaluator must be initialized with ranking_function = "rmse". If you're also using the RMSE augmenter,
            they the same one can be used for both.

    Raises:
        Exception: If the evaluator is not initialized with ranking_function = "rmse" or if y in the evaluator is None.
    """
    super().__init__()
    self.evaluator = evaluator
    if self.evaluator.ranking_function != "rmse":
        raise Exception("[R2 augmenter] Ranking function of the evaluator must be set to 'rmse' to compute R^2.")
    if self.evaluator.y is None:
        raise Exception("[R2 augmenter] y in the evaluator must not be None to compute R^2.")
    self.average_y = np.mean(self.evaluator.y)

augment_results(results, models, evaluator)

Computes the R^2 for the top models in the results dictionary.

Parameters:

Name Type Description Default
results dict

The dictionary containing the results to augment.

required
models List[dict]

The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...

required
evaluator SR_evaluator

The evaluator used to evaluate the models.

required

Returns:

Type Description
dict

The augmented results dictionary. The results dictionary contains an additional key "best_expr_r^2" with the

dict

R^2 of the best expression, and keys "r^2" and "parameters_r^2" for each of the top_models inside the

dict

results["top_models"] list.

Source code in SRToolkit/evaluation/result_augmentation.py
def augment_results(self, results: dict, models: List[dict], evaluator: "SR_evaluator") -> dict:
    """
    Computes the R^2 for the top models in the results dictionary.

    Args:
        results: The dictionary containing the results to augment.
        models: The list of models that were evaluated. Variable models[i]["expr"] contains the expression model i, ...
        evaluator: The evaluator used to evaluate the models.

    Returns:
        The augmented results dictionary. The results dictionary contains an additional key "best_expr_r^2" with the
        R^2 of the best expression, and keys "r^2" and "parameters_r^2" for each of the top_models inside the
        results["top_models"] list.
    """
    expr = models[0]["expr"]
    error = self.evaluator.evaluate_expr(expr)
    results["best_expr_r^2"] = error # TODO: Popravi
    for model in results["top_models"]:
        error = self.evaluator.evaluate_expr(model["expr"])
        model["r^2"] = error
        model["parameters_r^2"] = self.evaluator.models["".join(model["expr"])]["parameters"]
    return results