Skip to content

Utils Module

SRToolkit.utils

The module containing the utils.

The utils module provides a set of utilities used in the package and for expression compilation.

Modules:

Name Description
symbol_library

The module containing the symbol library data structure for managing symbols that can occur in expressions and their properties.

expression_tree

The module containing the expression tree data structure and functions for transforming expressions into trees and back.

expression_compiler

The module containing functions that transform expressions in the infix notation (represented as lists of tokens) to executable python functions.

Node

Source code in SRToolkit/utils/expression_tree.py
class Node:
    def __init__(self, symbol: str = None, right: "Node" = None, left: "Node" = None):
        """
        Initializes a Node object. We assume that nodes containing functions have only one child node, i.e. right is None.

        Examples:
            >>> node = Node("+", Node("x"), Node("1"))
            >>> len(node)
            3

        Args:
            symbol: The symbol string stored in this node.
            right: The right child of this node.
            left: The left child of this node.

        Methods:
            __len__(self):
                Returns the number of nodes in the tree rooted at this node.
            __str__(self):
                Returns a string representation of the tree rooted at this node.
            to_list(self, notation: str = "infix", symbol_library: SymbolLibrary = None):
                Returns a list representation of the tree rooted at this node.

        """
        self.symbol = symbol
        self.right = right
        self.left = left

    def __len__(self) -> int:
        """
        Returns the number of nodes in the tree rooted at this node.

        Examples:
            >>> node = Node("+", Node("x"), Node("1"))
            >>> len(node)
            3

        Returns:
            The number of nodes in the tree rooted at this node.
        """
        return (
            1
            + (len(self.left) if self.left is not None else 0)
            + (len(self.right) if self.right is not None else 0)
        )

    def __str__(self) -> str:
        """
        Returns a string representation of the tree rooted at this node.

        Examples:
            >>> node = Node("+", Node("x"), Node("1"))
            >>> str(node)
            'x+1'

        Returns:
            A string representation of the tree rooted at this node.
        """
        return "".join(self.to_list())

    def to_list(self, notation: str = "infix", symbol_library: SymbolLibrary = None) -> List[str]:
        """
        Transforms the tree rooted at this node into a list of tokens.

        Examples:
            >>> node = Node("+", Node("X_0"), Node("1"))
            >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
            ['1', '+', 'X_0']
            >>> node.to_list(notation="postfix")
            ['1', 'X_0', '+']
            >>> node.to_list(notation="prefix")
            ['+', '1', 'X_0']
            >>> node = Node("+", Node("*", Node("X_0"), Node("X_1")), Node("1"))
            >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
            ['1', '+', 'X_1', '*', 'X_0']
            >>> node.to_list(notation="infix")
            ['1', '+', '(', 'X_1', '*', 'X_0', ')']
            >>> node = Node("sin", None, Node("X_0"))
            >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
            ['sin', '(', 'X_0', ')']
            >>> node = Node("^2", None, Node("X_0"))
            >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
            ['X_0', '^2']
            >>> node.to_list()
            ['(', 'X_0', ')', '^2']

        Args:
            notation: The notation to use for the resulting list of tokens. One of "prefix", "postfix", or "infix".
            symbol_library: The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree.

        Returns:
            A list of tokens representing the tree rooted at this node in the specified notation.

        Raises:
             Exception: If the notation is not one of "prefix", "postfix", or "infix" or if a symbol is not in the symbol library.

        Notes:
            If the notation is "infix" and the symbol library is not provided, then the resulting list of tokens may contain unnecessary parentheses or have other issues.
        """
        left = [] if self.left is None else self.left.to_list(notation, symbol_library)
        right = [] if self.right is None else self.right.to_list(notation, symbol_library)

        if notation == "prefix":
            return [self.symbol] + left + right

        elif notation == "postfix":
            return left + right + [self.symbol]

        elif notation == "infix" and symbol_library is None:
            warnings.warn("Symbol library not provided. Generated expression may contain unnecessary parentheses and"
                          " have other issues.")
            if self.left is None and self.right is None:
                return [self.symbol]
            if self.right is None and self.left is not None:
                if self.symbol[0] == "^":
                    return ["("] + left + [")", self.symbol]
                else:
                    return [self.symbol, "("] + left + [")"]
            else:
                if len(left) > 1:
                    left = ["("] + left + [")"]
                if len(right) > 1:
                    right = ["("] + right + [")"]
                return left + [self.symbol] + right

        elif notation == "infix":
            if is_float(self.symbol):
                return [self.symbol]
            if symbol_library.get_type(self.symbol) in ["var", "const", "lit"]:
                return [self.symbol]
            elif symbol_library.get_type(self.symbol) == "fn":
                if symbol_library.get_precedence(self.symbol) > 0:
                    return [self.symbol, "("] + left + [")"]
                else:
                    if len(left) > 1:
                        left = ["("] + left + [")"]
                    return left + [self.symbol]
            elif symbol_library.get_type(self.symbol) == "op":
                if not is_float(self.left.symbol) and -1 < symbol_library.get_precedence(self.left.symbol) <= symbol_library.get_precedence(self.symbol):
                    left = ["("] + left + [")"]
                if not is_float(self.right.symbol) and -1 < symbol_library.get_precedence(self.right.symbol) <= symbol_library.get_precedence(self.symbol):
                    right = ["("] + right + [")"]
                return left + [self.symbol] + right
            else:
                raise Exception(f"Invalid symbol type for symbol {self.symbol}.")
        else:
            raise Exception("Invalid notation selected. Use 'infix', 'prefix', 'postfix', or leave blank (defaults to 'infix').")

__init__(symbol=None, right=None, left=None)

Initializes a Node object. We assume that nodes containing functions have only one child node, i.e. right is None.

Examples:

>>> node = Node("+", Node("x"), Node("1"))
>>> len(node)
3

Parameters:

Name Type Description Default
symbol str

The symbol string stored in this node.

None
right Node

The right child of this node.

None
left Node

The left child of this node.

None

Functions:

Name Description
__len__

Returns the number of nodes in the tree rooted at this node.

__str__

Returns a string representation of the tree rooted at this node.

to_list

str = "infix", symbol_library: SymbolLibrary = None): Returns a list representation of the tree rooted at this node.

Source code in SRToolkit/utils/expression_tree.py
def __init__(self, symbol: str = None, right: "Node" = None, left: "Node" = None):
    """
    Initializes a Node object. We assume that nodes containing functions have only one child node, i.e. right is None.

    Examples:
        >>> node = Node("+", Node("x"), Node("1"))
        >>> len(node)
        3

    Args:
        symbol: The symbol string stored in this node.
        right: The right child of this node.
        left: The left child of this node.

    Methods:
        __len__(self):
            Returns the number of nodes in the tree rooted at this node.
        __str__(self):
            Returns a string representation of the tree rooted at this node.
        to_list(self, notation: str = "infix", symbol_library: SymbolLibrary = None):
            Returns a list representation of the tree rooted at this node.

    """
    self.symbol = symbol
    self.right = right
    self.left = left

__len__()

Returns the number of nodes in the tree rooted at this node.

Examples:

>>> node = Node("+", Node("x"), Node("1"))
>>> len(node)
3

Returns:

Type Description
int

The number of nodes in the tree rooted at this node.

Source code in SRToolkit/utils/expression_tree.py
def __len__(self) -> int:
    """
    Returns the number of nodes in the tree rooted at this node.

    Examples:
        >>> node = Node("+", Node("x"), Node("1"))
        >>> len(node)
        3

    Returns:
        The number of nodes in the tree rooted at this node.
    """
    return (
        1
        + (len(self.left) if self.left is not None else 0)
        + (len(self.right) if self.right is not None else 0)
    )

__str__()

Returns a string representation of the tree rooted at this node.

Examples:

>>> node = Node("+", Node("x"), Node("1"))
>>> str(node)
'x+1'

Returns:

Type Description
str

A string representation of the tree rooted at this node.

Source code in SRToolkit/utils/expression_tree.py
def __str__(self) -> str:
    """
    Returns a string representation of the tree rooted at this node.

    Examples:
        >>> node = Node("+", Node("x"), Node("1"))
        >>> str(node)
        'x+1'

    Returns:
        A string representation of the tree rooted at this node.
    """
    return "".join(self.to_list())

to_list(notation='infix', symbol_library=None)

Transforms the tree rooted at this node into a list of tokens.

Examples:

>>> node = Node("+", Node("X_0"), Node("1"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['1', '+', 'X_0']
>>> node.to_list(notation="postfix")
['1', 'X_0', '+']
>>> node.to_list(notation="prefix")
['+', '1', 'X_0']
>>> node = Node("+", Node("*", Node("X_0"), Node("X_1")), Node("1"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['1', '+', 'X_1', '*', 'X_0']
>>> node.to_list(notation="infix")
['1', '+', '(', 'X_1', '*', 'X_0', ')']
>>> node = Node("sin", None, Node("X_0"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['sin', '(', 'X_0', ')']
>>> node = Node("^2", None, Node("X_0"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['X_0', '^2']
>>> node.to_list()
['(', 'X_0', ')', '^2']

Parameters:

Name Type Description Default
notation str

The notation to use for the resulting list of tokens. One of "prefix", "postfix", or "infix".

'infix'
symbol_library SymbolLibrary

The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree.

None

Returns:

Type Description
List[str]

A list of tokens representing the tree rooted at this node in the specified notation.

Raises:

Type Description
Exception

If the notation is not one of "prefix", "postfix", or "infix" or if a symbol is not in the symbol library.

Notes

If the notation is "infix" and the symbol library is not provided, then the resulting list of tokens may contain unnecessary parentheses or have other issues.

Source code in SRToolkit/utils/expression_tree.py
def to_list(self, notation: str = "infix", symbol_library: SymbolLibrary = None) -> List[str]:
    """
    Transforms the tree rooted at this node into a list of tokens.

    Examples:
        >>> node = Node("+", Node("X_0"), Node("1"))
        >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
        ['1', '+', 'X_0']
        >>> node.to_list(notation="postfix")
        ['1', 'X_0', '+']
        >>> node.to_list(notation="prefix")
        ['+', '1', 'X_0']
        >>> node = Node("+", Node("*", Node("X_0"), Node("X_1")), Node("1"))
        >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
        ['1', '+', 'X_1', '*', 'X_0']
        >>> node.to_list(notation="infix")
        ['1', '+', '(', 'X_1', '*', 'X_0', ')']
        >>> node = Node("sin", None, Node("X_0"))
        >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
        ['sin', '(', 'X_0', ')']
        >>> node = Node("^2", None, Node("X_0"))
        >>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
        ['X_0', '^2']
        >>> node.to_list()
        ['(', 'X_0', ')', '^2']

    Args:
        notation: The notation to use for the resulting list of tokens. One of "prefix", "postfix", or "infix".
        symbol_library: The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree.

    Returns:
        A list of tokens representing the tree rooted at this node in the specified notation.

    Raises:
         Exception: If the notation is not one of "prefix", "postfix", or "infix" or if a symbol is not in the symbol library.

    Notes:
        If the notation is "infix" and the symbol library is not provided, then the resulting list of tokens may contain unnecessary parentheses or have other issues.
    """
    left = [] if self.left is None else self.left.to_list(notation, symbol_library)
    right = [] if self.right is None else self.right.to_list(notation, symbol_library)

    if notation == "prefix":
        return [self.symbol] + left + right

    elif notation == "postfix":
        return left + right + [self.symbol]

    elif notation == "infix" and symbol_library is None:
        warnings.warn("Symbol library not provided. Generated expression may contain unnecessary parentheses and"
                      " have other issues.")
        if self.left is None and self.right is None:
            return [self.symbol]
        if self.right is None and self.left is not None:
            if self.symbol[0] == "^":
                return ["("] + left + [")", self.symbol]
            else:
                return [self.symbol, "("] + left + [")"]
        else:
            if len(left) > 1:
                left = ["("] + left + [")"]
            if len(right) > 1:
                right = ["("] + right + [")"]
            return left + [self.symbol] + right

    elif notation == "infix":
        if is_float(self.symbol):
            return [self.symbol]
        if symbol_library.get_type(self.symbol) in ["var", "const", "lit"]:
            return [self.symbol]
        elif symbol_library.get_type(self.symbol) == "fn":
            if symbol_library.get_precedence(self.symbol) > 0:
                return [self.symbol, "("] + left + [")"]
            else:
                if len(left) > 1:
                    left = ["("] + left + [")"]
                return left + [self.symbol]
        elif symbol_library.get_type(self.symbol) == "op":
            if not is_float(self.left.symbol) and -1 < symbol_library.get_precedence(self.left.symbol) <= symbol_library.get_precedence(self.symbol):
                left = ["("] + left + [")"]
            if not is_float(self.right.symbol) and -1 < symbol_library.get_precedence(self.right.symbol) <= symbol_library.get_precedence(self.symbol):
                right = ["("] + right + [")"]
            return left + [self.symbol] + right
        else:
            raise Exception(f"Invalid symbol type for symbol {self.symbol}.")
    else:
        raise Exception("Invalid notation selected. Use 'infix', 'prefix', 'postfix', or leave blank (defaults to 'infix').")

SymbolLibrary

Source code in SRToolkit/utils/symbol_library.py
class SymbolLibrary:
    def __init__(self):
        """
        Initializes an instance of the SymbolLibrary class. This class is used for managing symbols and their
        properties for other functionality in this package.

        Examples:
            >>> library = SymbolLibrary()
            >>> library.add_symbol("x", "var", 0, "x")
            >>> library.get_type("x")
            'var'
            >>> library.get_precedence("x")
            0
            >>> library.get_np_fn("x")
            'x'
            >>> library.remove_symbol("x")
            >>> library = SymbolLibrary.default_symbols()

        Attributes:
            symbols : dict
                A dictionary mapping symbols to their properties (type, precedence, numpy function).

        Methods:
            add_symbol(symbol, symbol_type, precedence, np_fn):
                Adds a symbol to the library.
            remove_symbol(symbol):
                Removes a symbol from the library.
            get_type(symbol):
                Retrieves the type of a symbol from the library.
            get_precedence(symbol):
                Returns the precedence of the given symbol.
            get_np_fn(symbol):
                Returns the numpy function corresponding to the given symbol.
            default_symbols():
                Returns a SymbolLibrary with the default symbols.
        """
        self.symbols = dict()

    def __str__(self) -> str:
        """
        Returns a string representation of the SymbolLibrary instance.

        This method provides a comma-separated string of all the symbol keys
        currently stored in the SymbolLibrary.

        Examples:
            >>> library = SymbolLibrary()
            >>> library.add_symbol("x", "var", 0, "x")
            >>> str(library)
            'x'
            >>> library.add_symbol("sin", "fn", 5, "{} = np.sin({})")
            >>> str(library)
            'x, sin'

        Returns:
            A string containing all symbols in the library, separated by commas.
        """
        return ", ".join(self.symbols.keys())

    def __copy__(self) -> "SymbolLibrary":
        """
        Creates a copy of the SymbolLibrary instance.

        Examples:
            >>> old_symbols = SymbolLibrary()
            >>> old_symbols.add_symbol("x", "var", 0, "x")
            >>> print(old_symbols)
            x
            >>> new_symbols = copy.copy(old_symbols)
            >>> new_symbols.add_symbol("sin", "fn", 5, "{} = np.sin({})")
            >>> print(old_symbols)
            x
            >>> print(new_symbols)
            x, sin

        Returns:
            A copy of the SymbolLibrary instance.
        """
        sl = SymbolLibrary()
        sl.symbols = copy.deepcopy(self.symbols)
        return sl

    def add_symbol(self, symbol: str, symbol_type: str, precedence: int, np_fn: str):
        """
        Adds a symbol to the library. A symbol should have a type, precedence, and numpy function associated with it.
        Type "op" should be used for symbols operating on two operands, "fn" for symbols operating on one operand,
        "lit" for constants with a known value (such as pi or e), "const" for constants/parameters without a value that
        need to be optimized, and "var" for variables whose values are provided as input data.

        For example, look at the default_symbols function for the SymbolLibrary class.

        Examples:
            >>> library = SymbolLibrary()
            >>> library.add_symbol("x", "var", 0, "x")
            >>> library.add_symbol("sin", "fn", 5, "np.sin({})")
            >>> library.add_symbol("C", "const", 5, "C[{}]")
            >>> library.add_symbol("X", "var", 5, "X[:, 0]")
            >>> library.add_symbol("pi", "lit", 5, "np.pi")

        Args:
            symbol: The symbol to be added to the library.
            symbol_type: The type of the symbol, one of "op" (operator), "fn" (function), "lit" (literal), "const" (constant), or "var" (variable).
            precedence: The precedence of the symbol, used to determine the order of operations.
            np_fn: A string representing the numpy function associated with this symbol.
        """
        self.symbols[symbol] = {
            "symbol": symbol,
            "type": symbol_type,
            "precedence": precedence,
            "np_fn": np_fn,
        }

    def remove_symbol(self, symbol: str):
        """
        Removes a symbol from the library.

        Examples:
            >>> library = SymbolLibrary()
            >>> library.add_symbol("x", "var", 0, "x")
            >>> len(library.symbols)
            1
            >>> library.remove_symbol("x")
            >>> len(library.symbols)
            0

        Args:
            symbol: The symbol to be removed from the library.

        Raises:
            KeyError: If the symbol does not exist in the library.
        """
        del self.symbols[symbol]

    def get_type(self, symbol: str) -> str:
        """
        Retrieves the type of a symbol from the library.

        Examples:
            >>> library = SymbolLibrary()
            >>> library.add_symbol("x", "var", 0, "x")
            >>> library.get_type("x")
            'var'

        Args:
            symbol: The symbol whose type is to be retrieved.

        Returns:
            The type of the symbol if it exists in the library, otherwise an empty string.
        """
        if symbol in self.symbols:
            return self.symbols[symbol]["type"]
        else:
            return ""

    def get_precedence(self, symbol: str) -> int:
        """
        Retrieves the precedence of the given symbol.

        Examples:
            >>> library = SymbolLibrary()
            >>> library.add_symbol("x", "var", 0, "x")
            >>> library.get_precedence("x")
            0

        Args:
            symbol: The symbol whose precedence is to be retrieved.

        Returns:
            The precedence of the symbol if it exists in the library, otherwise -1.
        """
        if symbol in self.symbols:
            return self.symbols[symbol]["precedence"]
        else:
            return -1

    def get_np_fn(self, symbol: str) -> str:
        """
        Returns the numpy function corresponding to the given symbol.

        Examples:
            >>> library = SymbolLibrary()
            >>> library.add_symbol("x", "var", 0, "x")
            >>> library.get_np_fn("x")
            'x'

        Args:
            symbol: The symbol to look up.

        Returns:
            The numpy function corresponding to the given symbol, or an empty string if the symbol was not found.
        """
        if symbol in self.symbols:
            return self.symbols[symbol]["np_fn"]
        else:
            return ""

    @staticmethod
    def default_symbols(num_variables: int = 25) -> "SymbolLibrary":
        """
        Creates a SymbolLibrary instance populated with default mathematical symbols.

        This method adds a set of predefined symbols to a SymbolLibrary instance,
        representing common mathematical operations, functions, constants, and optional
        variables. The symbols include basic arithmetic operations, trigonometric and
        exponential functions, and mathematical constants like pi and e.

        If num_variables is greater than 0, it adds variables labeled 'X_0' to 'X_{num_variables-1}', each
         associated with a column in a data array X.

        Note: The variables in the default_symbols function are added in the predefined order,
        which is the same order as the columns in the data array X.

        Examples:
            >>> library = SymbolLibrary.default_symbols()
            >>> len(library.symbols)
            44

        Args:
            num_variables: The number of variables to add to the library (default is 25).

        Returns:
            A SymbolLibrary instance populated with default mathematical symbols.
        """
        sl = SymbolLibrary()
        sl.add_symbol("+", symbol_type="op", precedence=0, np_fn="{} = {} + {}")
        sl.add_symbol("-", symbol_type="op", precedence=0, np_fn="{} = {} - {}")
        sl.add_symbol("*", symbol_type="op", precedence=1, np_fn="{} = {} * {}")
        sl.add_symbol("/", symbol_type="op", precedence=1, np_fn="{} = {} / {}")
        sl.add_symbol("^", symbol_type="op", precedence=2, np_fn="{} = np.pow({},{})")
        sl.add_symbol("u-", symbol_type="fn", precedence=5, np_fn="{} = -{}")
        sl.add_symbol("sqrt", symbol_type="fn", precedence=5, np_fn="{} = np.sqrt({})")
        sl.add_symbol("sin", symbol_type="fn", precedence=5, np_fn="{} = np.sin({})")
        sl.add_symbol("cos", symbol_type="fn", precedence=5, np_fn="{} = np.cos({})")
        sl.add_symbol("exp", symbol_type="fn", precedence=5, np_fn="{} = np.exp({})")
        sl.add_symbol("ln", symbol_type="fn", precedence=5, np_fn="{} = np.log({})")
        sl.add_symbol("log", symbol_type="fn", precedence=5, np_fn="{} = np.log10({})")
        sl.add_symbol("^-1", symbol_type="fn", precedence=-1, np_fn="{} = 1/{}")
        sl.add_symbol("^2", symbol_type="fn", precedence=-1, np_fn="{} = {}**2")
        sl.add_symbol("^3", symbol_type="fn", precedence=-1, np_fn="{} = {}**3")
        sl.add_symbol("^4", symbol_type="fn", precedence=-1, np_fn="{} = {}**4")
        sl.add_symbol("^5", symbol_type="fn", precedence=-1, np_fn="{} = {}**5")
        sl.add_symbol("pi", symbol_type="lit", precedence=5, np_fn="np.pi")
        sl.add_symbol("e", symbol_type="lit", precedence=5, np_fn="np.e")
        sl.add_symbol("C", symbol_type="const", precedence=5, np_fn="C[{}]")

        if num_variables > 0:
            for i in range(num_variables):
                sl.add_symbol(f"X_{i}", "var", 5, "X[:, {}]".format(i))

        return sl

__init__()

Initializes an instance of the SymbolLibrary class. This class is used for managing symbols and their properties for other functionality in this package.

Examples:

>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_type("x")
'var'
>>> library.get_precedence("x")
0
>>> library.get_np_fn("x")
'x'
>>> library.remove_symbol("x")
>>> library = SymbolLibrary.default_symbols()

Attributes:

Name Type Description
symbols

dict A dictionary mapping symbols to their properties (type, precedence, numpy function).

Functions:

Name Description
add_symbol

Adds a symbol to the library.

remove_symbol

Removes a symbol from the library.

get_type

Retrieves the type of a symbol from the library.

get_precedence

Returns the precedence of the given symbol.

get_np_fn

Returns the numpy function corresponding to the given symbol.

default_symbols

Returns a SymbolLibrary with the default symbols.

Source code in SRToolkit/utils/symbol_library.py
def __init__(self):
    """
    Initializes an instance of the SymbolLibrary class. This class is used for managing symbols and their
    properties for other functionality in this package.

    Examples:
        >>> library = SymbolLibrary()
        >>> library.add_symbol("x", "var", 0, "x")
        >>> library.get_type("x")
        'var'
        >>> library.get_precedence("x")
        0
        >>> library.get_np_fn("x")
        'x'
        >>> library.remove_symbol("x")
        >>> library = SymbolLibrary.default_symbols()

    Attributes:
        symbols : dict
            A dictionary mapping symbols to their properties (type, precedence, numpy function).

    Methods:
        add_symbol(symbol, symbol_type, precedence, np_fn):
            Adds a symbol to the library.
        remove_symbol(symbol):
            Removes a symbol from the library.
        get_type(symbol):
            Retrieves the type of a symbol from the library.
        get_precedence(symbol):
            Returns the precedence of the given symbol.
        get_np_fn(symbol):
            Returns the numpy function corresponding to the given symbol.
        default_symbols():
            Returns a SymbolLibrary with the default symbols.
    """
    self.symbols = dict()

__str__()

Returns a string representation of the SymbolLibrary instance.

This method provides a comma-separated string of all the symbol keys currently stored in the SymbolLibrary.

Examples:

>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> str(library)
'x'
>>> library.add_symbol("sin", "fn", 5, "{} = np.sin({})")
>>> str(library)
'x, sin'

Returns:

Type Description
str

A string containing all symbols in the library, separated by commas.

Source code in SRToolkit/utils/symbol_library.py
def __str__(self) -> str:
    """
    Returns a string representation of the SymbolLibrary instance.

    This method provides a comma-separated string of all the symbol keys
    currently stored in the SymbolLibrary.

    Examples:
        >>> library = SymbolLibrary()
        >>> library.add_symbol("x", "var", 0, "x")
        >>> str(library)
        'x'
        >>> library.add_symbol("sin", "fn", 5, "{} = np.sin({})")
        >>> str(library)
        'x, sin'

    Returns:
        A string containing all symbols in the library, separated by commas.
    """
    return ", ".join(self.symbols.keys())

__copy__()

Creates a copy of the SymbolLibrary instance.

Examples:

>>> old_symbols = SymbolLibrary()
>>> old_symbols.add_symbol("x", "var", 0, "x")
>>> print(old_symbols)
x
>>> new_symbols = copy.copy(old_symbols)
>>> new_symbols.add_symbol("sin", "fn", 5, "{} = np.sin({})")
>>> print(old_symbols)
x
>>> print(new_symbols)
x, sin

Returns:

Type Description
SymbolLibrary

A copy of the SymbolLibrary instance.

Source code in SRToolkit/utils/symbol_library.py
def __copy__(self) -> "SymbolLibrary":
    """
    Creates a copy of the SymbolLibrary instance.

    Examples:
        >>> old_symbols = SymbolLibrary()
        >>> old_symbols.add_symbol("x", "var", 0, "x")
        >>> print(old_symbols)
        x
        >>> new_symbols = copy.copy(old_symbols)
        >>> new_symbols.add_symbol("sin", "fn", 5, "{} = np.sin({})")
        >>> print(old_symbols)
        x
        >>> print(new_symbols)
        x, sin

    Returns:
        A copy of the SymbolLibrary instance.
    """
    sl = SymbolLibrary()
    sl.symbols = copy.deepcopy(self.symbols)
    return sl

add_symbol(symbol, symbol_type, precedence, np_fn)

Adds a symbol to the library. A symbol should have a type, precedence, and numpy function associated with it. Type "op" should be used for symbols operating on two operands, "fn" for symbols operating on one operand, "lit" for constants with a known value (such as pi or e), "const" for constants/parameters without a value that need to be optimized, and "var" for variables whose values are provided as input data.

For example, look at the default_symbols function for the SymbolLibrary class.

Examples:

>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.add_symbol("sin", "fn", 5, "np.sin({})")
>>> library.add_symbol("C", "const", 5, "C[{}]")
>>> library.add_symbol("X", "var", 5, "X[:, 0]")
>>> library.add_symbol("pi", "lit", 5, "np.pi")

Parameters:

Name Type Description Default
symbol str

The symbol to be added to the library.

required
symbol_type str

The type of the symbol, one of "op" (operator), "fn" (function), "lit" (literal), "const" (constant), or "var" (variable).

required
precedence int

The precedence of the symbol, used to determine the order of operations.

required
np_fn str

A string representing the numpy function associated with this symbol.

required
Source code in SRToolkit/utils/symbol_library.py
def add_symbol(self, symbol: str, symbol_type: str, precedence: int, np_fn: str):
    """
    Adds a symbol to the library. A symbol should have a type, precedence, and numpy function associated with it.
    Type "op" should be used for symbols operating on two operands, "fn" for symbols operating on one operand,
    "lit" for constants with a known value (such as pi or e), "const" for constants/parameters without a value that
    need to be optimized, and "var" for variables whose values are provided as input data.

    For example, look at the default_symbols function for the SymbolLibrary class.

    Examples:
        >>> library = SymbolLibrary()
        >>> library.add_symbol("x", "var", 0, "x")
        >>> library.add_symbol("sin", "fn", 5, "np.sin({})")
        >>> library.add_symbol("C", "const", 5, "C[{}]")
        >>> library.add_symbol("X", "var", 5, "X[:, 0]")
        >>> library.add_symbol("pi", "lit", 5, "np.pi")

    Args:
        symbol: The symbol to be added to the library.
        symbol_type: The type of the symbol, one of "op" (operator), "fn" (function), "lit" (literal), "const" (constant), or "var" (variable).
        precedence: The precedence of the symbol, used to determine the order of operations.
        np_fn: A string representing the numpy function associated with this symbol.
    """
    self.symbols[symbol] = {
        "symbol": symbol,
        "type": symbol_type,
        "precedence": precedence,
        "np_fn": np_fn,
    }

remove_symbol(symbol)

Removes a symbol from the library.

Examples:

>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> len(library.symbols)
1
>>> library.remove_symbol("x")
>>> len(library.symbols)
0

Parameters:

Name Type Description Default
symbol str

The symbol to be removed from the library.

required

Raises:

Type Description
KeyError

If the symbol does not exist in the library.

Source code in SRToolkit/utils/symbol_library.py
def remove_symbol(self, symbol: str):
    """
    Removes a symbol from the library.

    Examples:
        >>> library = SymbolLibrary()
        >>> library.add_symbol("x", "var", 0, "x")
        >>> len(library.symbols)
        1
        >>> library.remove_symbol("x")
        >>> len(library.symbols)
        0

    Args:
        symbol: The symbol to be removed from the library.

    Raises:
        KeyError: If the symbol does not exist in the library.
    """
    del self.symbols[symbol]

get_type(symbol)

Retrieves the type of a symbol from the library.

Examples:

>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_type("x")
'var'

Parameters:

Name Type Description Default
symbol str

The symbol whose type is to be retrieved.

required

Returns:

Type Description
str

The type of the symbol if it exists in the library, otherwise an empty string.

Source code in SRToolkit/utils/symbol_library.py
def get_type(self, symbol: str) -> str:
    """
    Retrieves the type of a symbol from the library.

    Examples:
        >>> library = SymbolLibrary()
        >>> library.add_symbol("x", "var", 0, "x")
        >>> library.get_type("x")
        'var'

    Args:
        symbol: The symbol whose type is to be retrieved.

    Returns:
        The type of the symbol if it exists in the library, otherwise an empty string.
    """
    if symbol in self.symbols:
        return self.symbols[symbol]["type"]
    else:
        return ""

get_precedence(symbol)

Retrieves the precedence of the given symbol.

Examples:

>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_precedence("x")
0

Parameters:

Name Type Description Default
symbol str

The symbol whose precedence is to be retrieved.

required

Returns:

Type Description
int

The precedence of the symbol if it exists in the library, otherwise -1.

Source code in SRToolkit/utils/symbol_library.py
def get_precedence(self, symbol: str) -> int:
    """
    Retrieves the precedence of the given symbol.

    Examples:
        >>> library = SymbolLibrary()
        >>> library.add_symbol("x", "var", 0, "x")
        >>> library.get_precedence("x")
        0

    Args:
        symbol: The symbol whose precedence is to be retrieved.

    Returns:
        The precedence of the symbol if it exists in the library, otherwise -1.
    """
    if symbol in self.symbols:
        return self.symbols[symbol]["precedence"]
    else:
        return -1

get_np_fn(symbol)

Returns the numpy function corresponding to the given symbol.

Examples:

>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_np_fn("x")
'x'

Parameters:

Name Type Description Default
symbol str

The symbol to look up.

required

Returns:

Type Description
str

The numpy function corresponding to the given symbol, or an empty string if the symbol was not found.

Source code in SRToolkit/utils/symbol_library.py
def get_np_fn(self, symbol: str) -> str:
    """
    Returns the numpy function corresponding to the given symbol.

    Examples:
        >>> library = SymbolLibrary()
        >>> library.add_symbol("x", "var", 0, "x")
        >>> library.get_np_fn("x")
        'x'

    Args:
        symbol: The symbol to look up.

    Returns:
        The numpy function corresponding to the given symbol, or an empty string if the symbol was not found.
    """
    if symbol in self.symbols:
        return self.symbols[symbol]["np_fn"]
    else:
        return ""

default_symbols(num_variables=25) staticmethod

Creates a SymbolLibrary instance populated with default mathematical symbols.

This method adds a set of predefined symbols to a SymbolLibrary instance, representing common mathematical operations, functions, constants, and optional variables. The symbols include basic arithmetic operations, trigonometric and exponential functions, and mathematical constants like pi and e.

If num_variables is greater than 0, it adds variables labeled 'X_0' to 'X_{num_variables-1}', each associated with a column in a data array X.

Note: The variables in the default_symbols function are added in the predefined order, which is the same order as the columns in the data array X.

Examples:

>>> library = SymbolLibrary.default_symbols()
>>> len(library.symbols)
44

Parameters:

Name Type Description Default
num_variables int

The number of variables to add to the library (default is 25).

25

Returns:

Type Description
SymbolLibrary

A SymbolLibrary instance populated with default mathematical symbols.

Source code in SRToolkit/utils/symbol_library.py
@staticmethod
def default_symbols(num_variables: int = 25) -> "SymbolLibrary":
    """
    Creates a SymbolLibrary instance populated with default mathematical symbols.

    This method adds a set of predefined symbols to a SymbolLibrary instance,
    representing common mathematical operations, functions, constants, and optional
    variables. The symbols include basic arithmetic operations, trigonometric and
    exponential functions, and mathematical constants like pi and e.

    If num_variables is greater than 0, it adds variables labeled 'X_0' to 'X_{num_variables-1}', each
     associated with a column in a data array X.

    Note: The variables in the default_symbols function are added in the predefined order,
    which is the same order as the columns in the data array X.

    Examples:
        >>> library = SymbolLibrary.default_symbols()
        >>> len(library.symbols)
        44

    Args:
        num_variables: The number of variables to add to the library (default is 25).

    Returns:
        A SymbolLibrary instance populated with default mathematical symbols.
    """
    sl = SymbolLibrary()
    sl.add_symbol("+", symbol_type="op", precedence=0, np_fn="{} = {} + {}")
    sl.add_symbol("-", symbol_type="op", precedence=0, np_fn="{} = {} - {}")
    sl.add_symbol("*", symbol_type="op", precedence=1, np_fn="{} = {} * {}")
    sl.add_symbol("/", symbol_type="op", precedence=1, np_fn="{} = {} / {}")
    sl.add_symbol("^", symbol_type="op", precedence=2, np_fn="{} = np.pow({},{})")
    sl.add_symbol("u-", symbol_type="fn", precedence=5, np_fn="{} = -{}")
    sl.add_symbol("sqrt", symbol_type="fn", precedence=5, np_fn="{} = np.sqrt({})")
    sl.add_symbol("sin", symbol_type="fn", precedence=5, np_fn="{} = np.sin({})")
    sl.add_symbol("cos", symbol_type="fn", precedence=5, np_fn="{} = np.cos({})")
    sl.add_symbol("exp", symbol_type="fn", precedence=5, np_fn="{} = np.exp({})")
    sl.add_symbol("ln", symbol_type="fn", precedence=5, np_fn="{} = np.log({})")
    sl.add_symbol("log", symbol_type="fn", precedence=5, np_fn="{} = np.log10({})")
    sl.add_symbol("^-1", symbol_type="fn", precedence=-1, np_fn="{} = 1/{}")
    sl.add_symbol("^2", symbol_type="fn", precedence=-1, np_fn="{} = {}**2")
    sl.add_symbol("^3", symbol_type="fn", precedence=-1, np_fn="{} = {}**3")
    sl.add_symbol("^4", symbol_type="fn", precedence=-1, np_fn="{} = {}**4")
    sl.add_symbol("^5", symbol_type="fn", precedence=-1, np_fn="{} = {}**5")
    sl.add_symbol("pi", symbol_type="lit", precedence=5, np_fn="np.pi")
    sl.add_symbol("e", symbol_type="lit", precedence=5, np_fn="np.e")
    sl.add_symbol("C", symbol_type="const", precedence=5, np_fn="C[{}]")

    if num_variables > 0:
        for i in range(num_variables):
            sl.add_symbol(f"X_{i}", "var", 5, "X[:, {}]".format(i))

    return sl

tokens_to_tree(tokens, sl)

Converts a list of tokens to a tree data structure. Throws an exception if the expression is invalid (check syntax and that all symbols are in the symbol library correctly defined).

Examples:

>>> tree = tokens_to_tree(["(", "x", "+", "y", ")"], SymbolLibrary.default_symbols())
>>> len(tree)
3

Parameters:

Name Type Description Default
tokens List[str]

The list of tokens to convert.

required
sl SymbolLibrary

The symbol library to use when parsing the tokens.

required

Returns:

Type Description
Node

The root of the expression tree data structure.

Raises:

Type Description
Exception

If the expression is invalid. Usually this means that a symbol is not in the symbol library or that there is a syntactic error in the expression.

Source code in SRToolkit/utils/expression_tree.py
def tokens_to_tree(tokens: List[str], sl: SymbolLibrary) -> Node:
    """
    Converts a list of tokens to a tree data structure. Throws an exception if the expression is invalid (check syntax
    and that all symbols are in the symbol library correctly defined).

    Examples:
        >>> tree = tokens_to_tree(["(", "x", "+", "y", ")"], SymbolLibrary.default_symbols())
        >>> len(tree)
        3

    Args:
        tokens: The list of tokens to convert.
        sl: The symbol library to use when parsing the tokens.

    Returns:
        The root of the expression tree data structure.

    Raises:
        Exception: If the expression is invalid. Usually this means that a symbol is not in the symbol library or that
                   there is a syntactic error in the expression.
    """
    num_tokens = len([t for t in tokens if t != "(" and t != ")"])
    expr_str = "".join(tokens)
    tokens = ["("] + tokens + [")"]
    operator_stack = []
    out_stack = []
    for token in tokens:
        if token == "(":
            operator_stack.append(token)
        elif sl.get_type(token) in ["var", "const", "lit"] or is_float(token):
            out_stack.append(Node(token))
        elif sl.get_type(token) == "fn":
            if token[0] == "^":
                out_stack.append(Node(token, left=out_stack.pop()))
            else:
                operator_stack.append(token)
        elif sl.get_type(token) == "op":
            while (
                len(operator_stack) > 0
                and operator_stack[-1] != "("
                and sl.get_precedence(operator_stack[-1]) > sl.get_precedence(token)
            ):
                if sl.get_type(operator_stack[-1]) == "fn":
                    out_stack.append(Node(operator_stack.pop(), left=out_stack.pop()))
                else:
                    out_stack.append(
                        Node(operator_stack.pop(), out_stack.pop(), out_stack.pop())
                    )
            operator_stack.append(token)
        else:
            while len(operator_stack) > 0 and operator_stack[-1] != "(":
                if sl.get_type(operator_stack[-1]) == "fn":
                    out_stack.append(Node(operator_stack.pop(), left=out_stack.pop()))
                else:
                    out_stack.append(
                        Node(operator_stack.pop(), out_stack.pop(), out_stack.pop())
                    )
            operator_stack.pop()
            if len(operator_stack) > 0 and sl.get_type(operator_stack[-1]) == "fn":
                out_stack.append(Node(operator_stack.pop(), left=out_stack.pop()))
    if len(out_stack[-1]) == num_tokens:
        return out_stack[-1]
    else:
        raise Exception(f"Error while parsing expression {expr_str}.")

is_float(element)

Checks if a given element is a float.

Examples:

>>> is_float(1.0)
True
>>> is_float("1.0")
True
>>> is_float("1")
True
>>> is_float(None)
False

Parameters:

Name Type Description Default
element any

The element to check.

required

Returns:

Type Description
bool

True if the element is a float, False otherwise.

Source code in SRToolkit/utils/expression_tree.py
def is_float(element: any) -> bool:
    """
    Checks if a given element is a float.

    Examples:
        >>> is_float(1.0)
        True
        >>> is_float("1.0")
        True
        >>> is_float("1")
        True
        >>> is_float(None)
        False


    Args:
        element: The element to check.

    Returns:
        True if the element is a float, False otherwise.
    """
    if element is None:
        return False
    try:
        float(element)
        return True
    except ValueError:
        return False

tree_to_function_rec(tree, symbol_library, var_counter=0, const_counter=0)

Recursively converts a parse tree into a string of Python code that can be executed to evaluate the expression represented by the tree.

Parameters:

Name Type Description Default
tree Node

The root of the parse tree to convert.

required
symbol_library SymbolLibrary

The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree.

required
var_counter int

The number of variables encountered so far. This is used to create a unique variable name for each variable.

0
const_counter int

The number of constants encountered so far. This is used to select the correct constant value from the constant array.

0

Returns:

Type Description
List[str]

A list of strings, where each string contains a line of Python code to execute to evaluate the expression represented by the tree.

str

The name of the variable that represents the output of the expression.

int

The updated value of var_counter.

int

The updated value of const_counter.

Raises:

Type Description
Exception

If the parse tree contains an invalid symbol.

Notes

This function is a helper function for expr_to_executable_function and similar and should not be called directly unless you want to customize the way the expression is defined. For examples, see the code of expr_to_executable_function and expr_to_error_function in this module.

Source code in SRToolkit/utils/expression_compiler.py
def tree_to_function_rec(tree: Node, symbol_library: SymbolLibrary, var_counter: int=0, const_counter: int=0) -> Tuple[List[str], str, int, int]:
    """
    Recursively converts a parse tree into a string of Python code that can be executed to evaluate the expression
    represented by the tree.

    Args:
        tree: The root of the parse tree to convert.
        symbol_library: The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree.
        var_counter: The number of variables encountered so far. This is used to create a unique variable name for each variable.
        const_counter: The number of constants encountered so far. This is used to select the correct constant value from the constant array.

    Returns:
        A list of strings, where each string contains a line of Python code to execute to evaluate the expression represented by the tree.
        The name of the variable that represents the output of the expression.
        The updated value of `var_counter`.
        The updated value of `const_counter`.

    Raises:
        Exception: If the parse tree contains an invalid symbol.

    Notes:
        This function is a helper function for `expr_to_executable_function` and similar and should not be called directly
        unless you want to customize the way the expression is defined. For examples, see the code of `expr_to_executable_function` and `expr_to_error_function` in this module.


    """
    if tree.left is None and tree.right is None:
        if symbol_library.get_type(tree.symbol) in ["var", "lit"]:
            return [], symbol_library.get_np_fn(tree.symbol), var_counter, const_counter
        elif symbol_library.get_type(tree.symbol) == "const":
            return [], symbol_library.get_np_fn(tree.symbol).format(const_counter), var_counter, const_counter + 1
        else:
            if is_float(tree.symbol):
                return [], tree.symbol, var_counter, const_counter
            else:
                raise Exception(f"Encountered invalid symbol {tree.symbol} while converting to function.")

    elif tree.left is not None and tree.right is None:
        code, symbol, var_counter, const_counter = tree_to_function_rec(tree.left, symbol_library, var_counter, const_counter)
        output_symbol = "y_{}".format(var_counter)
        code.append(symbol_library.get_np_fn(tree.symbol).format(output_symbol, symbol))
        return code, output_symbol, var_counter + 1, const_counter

    else:
        left_code, left_symbol, var_counter, const_counter = tree_to_function_rec(tree.left, symbol_library, var_counter, const_counter)
        right_code, right_symbol, var_counter, const_counter = tree_to_function_rec(tree.right, symbol_library, var_counter, const_counter)
        output_symbol = "y_{}".format(var_counter)
        code = left_code + right_code
        code.append(symbol_library.get_np_fn(tree.symbol).format(output_symbol, left_symbol, right_symbol))
        return code, output_symbol, var_counter + 1, const_counter

expr_to_executable_function(expr, symbol_library=SymbolLibrary.default_symbols())

Converts an expression in infix notation to an executable function.

Examples:

>>> executable_fun = expr_to_executable_function(["A", "+", "1"])
>>> executable_fun(np.array([[1], [2], [3], [4]]), np.array([]))
array([2, 3, 4, 5])

Parameters:

Name Type Description Default
expr

The expression in infix notation.

required
symbol_library

The symbol library to use. Defaults to SymbolLibrary.default_symbols().

default_symbols()

Returns:

Type Description
callable

An executable function that takes in a 2D array of input values and a 1D array of constant values and returns the output of the expression.

Source code in SRToolkit/utils/expression_compiler.py
def expr_to_executable_function(expr: List[str], symbol_library: SymbolLibrary=SymbolLibrary.default_symbols()) -> callable:
    """
    Converts an expression in infix notation to an executable function.

    Examples:
        >>> executable_fun = expr_to_executable_function(["A", "+", "1"])
        >>> executable_fun(np.array([[1], [2], [3], [4]]), np.array([]))
        array([2, 3, 4, 5])

    Args:
        expr : The expression in infix notation.
        symbol_library : The symbol library to use. Defaults to SymbolLibrary.default_symbols().

    Returns:
        An executable function that takes in a 2D array of input values and a 1D array of constant values and returns the output of the expression.
    """
    tree = tokens_to_tree(expr, symbol_library)
    code, symbol, var_counter, const_counter = tree_to_function_rec(tree, symbol_library)

    fun_string = "def _executable_expression_(X, C):\n"
    for c in code:
        fun_string += "\t" + c + "\n"
    fun_string += "\treturn " + symbol

    exec(fun_string)
    return locals()["_executable_expression_"]

expr_to_error_function(expr, symbol_library=SymbolLibrary.default_symbols())

Converts an expression in infix notation to an executable function that returns the root mean squared error between the output of the expression and the target values.

Examples:

>>> executable_fun = expr_to_error_function(["X_0", "+", "1"])
>>> executable_fun(np.array([[1], [2], [3], [4]]), np.array([]), np.array([2, 3, 4, 5]))
0.0

Parameters:

Name Type Description Default
expr

The expression in infix notation.

required
symbol_library

The symbol library to use. Defaults to SymbolLibrary.default_symbols().

default_symbols()

Returns:

Type Description
callable

An executable function that takes in a 2D array of input values X, a 1D array of constant values C, and a 1D array of target values y. It returns the root mean squared error between the output of the expression and the target values.

Source code in SRToolkit/utils/expression_compiler.py
def expr_to_error_function(expr: List[str], symbol_library: SymbolLibrary=SymbolLibrary.default_symbols()) -> callable:
    """
    Converts an expression in infix notation to an executable function that returns the root mean squared error between
    the output of the expression and the target values.

    Examples:
        >>> executable_fun = expr_to_error_function(["X_0", "+", "1"])
        >>> executable_fun(np.array([[1], [2], [3], [4]]), np.array([]), np.array([2, 3, 4, 5]))
        0.0

    Args:
        expr : The expression in infix notation.
        symbol_library : The symbol library to use. Defaults to SymbolLibrary.default_symbols().

    Returns:
        An executable function that takes in a 2D array of input values `X`, a 1D array of constant values `C`, and a 1D array of target values `y`. It returns the root mean squared error between the output of the expression and the target values.
    """
    tree = tokens_to_tree(expr, symbol_library)
    code, symbol, var_counter, const_counter = tree_to_function_rec(tree, symbol_library)

    fun_string = "def _executable_expression_(X, C, y):\n"
    for c in code:
        fun_string += "\t" + c + "\n"
    fun_string += f"\treturn np.sqrt(np.mean(({symbol}-y)**2))"

    exec(fun_string)
    return locals()["_executable_expression_"]