NRWAL.handlers.equations.Equation

class Equation(eqn, name=None, default_variables=None)[source]

Bases: object

Class to handle and evaluate a single wind cost equation string.

Parameters:
  • eqn (str | int | float) – Cost equation in a string representation or a single number e.g.: “-34.80 * depth ** 2 + 207619.80 * depth + 221197699.89”

  • name (str | None) – Optional equation name / key for string formatting

  • default_variables (dict) – Optional dictionary of default variables accessible to this Equation object. These inputs can still be overwritten at runtime.

Methods

eval

Alias for evaluate().

evaluate

Evaluate the equation string and return the result

is_equation

Check if an expression is an equation to be handled by this framework.

is_method

Check if a string is a numpy/pandas or python builtin method

is_num

Check if a string is a number

is_variable

Check if a string is a variable name without any constants, operators, or arithmetic expressions

parse_variables

Parse variable names from an expression string.

replace_equation

Replace the expression of this equation with a new one.

set_default_variables

Set default variables available to this Equation object.

verify_no_self_reference

Verify that the equation does not reference itself.

Attributes

ILLEGAL

default_variables

Get a dictionary of default variables from a variables.yaml file accessible to this object

full

Get the full equation string without any pretty formatting.

variables

Get a unique sorted list names of all input variables that the Equation needs.

verify_no_self_reference()[source]

Verify that the equation does not reference itself.

Raises:

ValueError – If a reference to the equation name is found in its variables.

replace_equation(new_eqn)[source]

Replace the expression of this equation with a new one.

This method returns a new Equation instance that replaces the existing equation expression with the new one supplied by the user, keeping the equation name and default variables unchanged.

Parameters:

new_eqn (str) – String representation of the new Equation instance.

Returns:

Equation – A new Equation instance with the same name and default values as the old Equation but with the new equation expression.

__add__(other)[source]

Add another equation to this instance of Equation (self) and return a new Equation object that evaluates the sum of the two equations

Parameters:

other (Equation | str | int | float) – Another Equation object or simple string representation of an equation to add to this instance of Equation (self).

Returns:

out (Equation) – A new Equation instance with this instance of Equation (self) summed with the input Equation.

__mul__(other)[source]

Multiply another equation by this instance of Equation (self) and return a new Equation object that evaluates the product of the two equations

Parameters:

other (Equation | str | int | float) – Another Equation object or simple string representation of an equation to multiply by this instance of Equation (self).

Returns:

out (Equation) – A new Equation instance with this instance of Equation (self) multiplied by the input Equation.

set_default_variables(var_dict)[source]

Set default variables available to this Equation object.

Parameters:

var_dict (dict | None) – Default variables namespace. These variables can always be overwritten when Equation.evaluate() is called.

static is_num(s)[source]

Check if a string is a number

static is_method(s)[source]

Check if a string is a numpy/pandas or python builtin method

classmethod is_variable(s)[source]

Check if a string is a variable name without any constants, operators, or arithmetic expressions

property full

Get the full equation string without any pretty formatting.

property default_variables

Get a dictionary of default variables from a variables.yaml file accessible to this object

Returns:

dict

classmethod parse_variables(expression)[source]

Parse variable names from an expression string.

property variables

Get a unique sorted list names of all input variables that the Equation needs. This will return an empty list if the equation has no variables.

Returns:

list

classmethod is_equation(expression)[source]

Check if an expression is an equation to be handled by this framework.

Parameters:

expression (str | int | float) – Expression to be checked as an equation or not.

Returns:

check (bool) – True if the expression is an equation that can be handled by this framework.

eval(**kwargs)[source]

Alias for evaluate().

evaluate(**kwargs)[source]

Evaluate the equation string and return the result

Parameters:

kwargs (dict) – Keyword arguments setting variables of the equation. Note that this is **kwargs so this method can be run in either of these syntaxes:

Equation.evaluate(input1=10, input2=20) Equation.evaluate(**{‘input1’: 10, ‘input2’: 20})

Returns:

out (float | np.ndarray) – Evaluated output of this equation object.