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
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
__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:
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
__len__()
Returns the number of nodes in the tree rooted at this node.
Examples:
Returns:
Type | Description |
---|---|
int
|
The number of nodes in the tree rooted at this node. |
Source code in SRToolkit/utils/expression_tree.py
__str__()
Returns a string representation of the tree rooted at this node.
Examples:
Returns:
Type | Description |
---|---|
str
|
A string representation of the tree rooted at this node. |
Source code in SRToolkit/utils/expression_tree.py
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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
SymbolLibrary
Source code in SRToolkit/utils/symbol_library.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
__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
__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
__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
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
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
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
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
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
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:
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
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
is_float(element)
Checks if a given element is a float.
Examples:
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
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 |
int
|
The updated value of |
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
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
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 |