NRWAL.handlers.config.NrwalConfig

class NrwalConfig(config, inputs=None, interp_extrap_power=False, use_nearest_power=False, interp_extrap_year=False, use_nearest_year=False)[source]

Bases: object

Config framework for NRWAL.

Examples

The NrwalConfig object can be instantiated from a config yaml file for from a python dictionary. The config is based on a NRWAL equation directory that can be specified by the “equation_directory” key in the config or will be defaulted to the NRWAL repository equation directory. Please note that the test config used in this example is completely fictitious and is for demonstration purposes only.

>>> from NRWAL import NrwalConfig
>>> obj = NrwalConfig('./tests/data/test_configs/test_config_00_good.yml')
>>> obj
NrwalConfig object with equation directory: /home/gbuster/code/NRWAL/NRWAL
num_turbines
        6.0
fixed_charge_rate
        0.096
array
        fixed(depth, num_turbines=6.0)
export
        fixed(dist_s_to_l)
grid
        grid_connection(dist_l_to_ts, transmission_multi, turbine_capacity,
                        num_turbines=6.0, transmission_cost=6536.67)
monopile
        EquationGroup object from "monopile.yaml" with heirarchy:
        pslt_3MW(depth, dist_p_to_s)
        pslt_6MW(depth, dist_p_to_s)
        pslt_10MW(depth, dist_p_to_s)
        pslt_12MW(depth, dist_p_to_s)
        pslt_15MW(depth, dist_p_to_s)
        install_3MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
        install_6MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
        install_10MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
        install_12MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
        install_15MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
monopile_costs
        (pslt_12MW(depth, dist_p_to_s) + install_12MW(depth, dist_p_to_s,
         fixed_downtime, turbine_capacity))
electrical
        (fixed(depth, num_turbines=6.0) * (fixed(dist_s_to_l)
         + grid_connection(dist_l_to_ts, transmission_multi,
         turbine_capacity, num_turbines=6.0, transmission_cost=6536.67)))
electrical_duplicate
        (fixed(depth, num_turbines=6.0) * (fixed(dist_s_to_l)
         + grid_connection(dist_l_to_ts, transmission_multi,
         turbine_capacity, num_turbines=6.0, transmission_cost=6536.67)))
capex
        (fixed(depth, num_turbines=6.0) * (fixed(dist_s_to_l)
         + grid_connection(dist_l_to_ts, transmission_multi,
         turbine_capacity, num_turbines=6.0, transmission_cost=6536.67)))
lcoe
        ((fixed(depth, num_turbines=6.0) * (fixed(dist_s_to_l)
         + grid_connection(dist_l_to_ts, transmission_multi,
         turbine_capacity, num_turbines=6.0, transmission_cost=6536.67)))
         * 0.096)

Objects can be retrieved from the NrwalConfig object using the python bracket syntax similar to a python dictionary. You can see here how values in the config object correspond to: global variables defined in the config (e.g. ‘num_turbines’), NRWAL EquationGroup objects that organize the equations found in the equation directory (e.g. ‘monopile’), or single NRWAL Equation objects that will output numerical data when evaluated (e.g. ‘monopile_costs’).

>>> type(obj['num_turbines'])
NRWAL.handlers.equations.Equation
>>> obj['num_turbines']
6.0
>>> type(obj['monopile'])
NRWAL.handlers.groups.EquationGroup
>>> obj['monopile']
EquationGroup object from "monopile.yaml" with heirarchy:
pslt_3MW(depth, dist_p_to_s)
pslt_6MW(depth, dist_p_to_s)
pslt_10MW(depth, dist_p_to_s)
pslt_12MW(depth, dist_p_to_s)
pslt_15MW(depth, dist_p_to_s)
install_3MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
install_6MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
install_10MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
install_12MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
install_15MW(depth, dist_p_to_s, fixed_downtime, turbine_capacity)
>>> type(obj['monopile_costs'])
NRWAL.handlers.equations.Equation
>>> obj['monopile_costs']
(pslt_12MW(depth, dist_p_to_s) + install_12MW(depth, dist_p_to_s, fixed_...

There are a number of helpful properties that show what variables are available or required for all of the Equation objects in the config. Note that the global variables are passed into each of the Equation objects defined in the NrwalConfig, although these can always be overwritten by the NrwalConfig inputs.

>>> obj.global_variables
{'num_turbines': 6.0, 'fixed_charge_rate': 0.096}
>>> obj.all_variables
['depth',
 'dist_l_to_ts',
 'dist_p_to_s',
 'dist_s_to_l',
 'fixed_charge_rate',
 'fixed_downtime',
 'num_turbines',
 'transmission_cost',
 'transmission_multi',
 'turbine_capacity']
>>> obj.required_inputs
['depth',
 'dist_l_to_ts',
 'dist_p_to_s',
 'dist_s_to_l',
 'fixed_downtime',
 'transmission_multi',
 'turbine_capacity']
>>> obj.solvable
False

The NrwalConfig object can take a dictionary or DataFrame of inputs that will be provided to all of the Equation objects. Inputs can be passed to the NrwalConfig constructor or later to the NrwalConfig inputs property. Note that passing new data to the inputs property will update the inputs dictionary, not overwrite it. Inputs can also be passed to the NrwalConfig object at runtime via the evaluate() method.

>>> obj.inputs
{}
>>> obj.inputs = {'depth': np.ones(3)}
>>> obj.inputs
{'depth': array([1., 1., 1.])}
>>> obj.inputs['depth'] = 2 * np.ones(3)
>>> obj.inputs
{'depth': array([2., 2., 2.])}
>>> obj.inputs = {'dist_p_to_s': np.ones(3)}
>>> obj.inputs
{'depth': array([2., 2., 2.]), 'dist_p_to_s': array([1., 1., 1.])}
>>> obj.missing_inputs
['dist_l_to_ts',
 'dist_s_to_l',
 'fixed_downtime',
 'transmission_multi',
 'turbine_capacity']
>>> obj.solvable
False

Finally, the NrwalConfig object can be evaluated, which really means that every Equation object in the NrwalConfig will be evaluated. Here you can see the evaluate function being called with all remaining missing inputs defined in the input argument (all the inputs that were not already passed into the config inputs). The output is a dictionary of data with the keys from the input config that map directly to Equations. Note that keys from the input config like “num_turbines” and “monopile” map to global variables and EquationGroups respectively, and so are not included in the outputs. The outputs will also be saved to the NrwalConfig.outputs property.

>>> obj.evaluate(inputs={k: np.ones(3) for k in obj.missing_inputs})
{'array': array([1.52304188e+08, 1.52304188e+08, 1.52304188e+08]),
 'export': array([92471016.757, 92471016.757, 92471016.757]),
 'grid': array([39220., 39220., 39220.]),
 'monopile_costs': array([inf, inf, inf]),
 'electrical': array([1.40896965e+16, 1.40896965e+16, 1.40896965e+16]),
 'electrical_duplicate': array([1.40896965e+16, 1.40896965e+16,
                                1.40896965e+16]),
 'capex': array([1.40896965e+16, 1.40896965e+16, 1.40896965e+16]),
 'lcoe': array([1.35261086e+15, 1.35261086e+15, 1.35261086e+15])}
Parameters:
  • config (dict | str) – NRWAL config input. Can be a string filepath to a json or yml file or an extracted dictionary.

  • inputs (dict | pd.DataFrame | None) – Optional namespace of input data to make available for the evaluation of the NrwalConfig. This can be set at any time after config initialization by setting the .inputs attribute.

  • interp_extrap_power (bool) – Flag to interpolate and extrapolate power (MW) dependent equations based on the case-insensitive regex pattern: “_[0-9]*MW$” This takes preference over the use_nearest_power flag. If both interp_extrap_power & use_nearest_power are False, a KeyError will be raised if the exact equation name request is not found.

  • use_nearest_power (bool) – Flag to use the nearest valid power (MW) dependent equation based on the case-insensitive regex pattern: “_[0-9]*MW$” This is second priority to the interp_extrap_power flag. If both interp_extrap_power & use_nearest_power are False, a KeyError will be raised if the exact equation name request is not found.

  • interp_extrap_year (bool) – Flag to interpolate and extrapolate equations keyed by year. This takes preference over the use_nearest_year flag. If both interp_extrap_year & use_nearest_year are False, a KeyError will be raised if the exact equation name request is not found.

  • use_nearest_year (bool) – Flag to use the nearest valid equation keyed by year. This is second priority to the interp_extrap_year flag. If both interp_extrap_year & use_nearest_year are False, a KeyError will be raised if the exact equation name request is not found.

Methods

eval

Alias for evaluate().

evaluate

Evaluate the equations in the NrwalConfig, set the output to the outputs attribute, and return the output.

get

Attempt to get a key from the NrwalConfig, return default_value if the key could not be retrieved

head

Return the first n lines of the config string representation

items

Get the 1st level of config (keys, values), same as dict.items().

keys

Get the 1st level of config keys, same as dict.keys()

reset_output

Reset the output dictionary of the NrwalConfig object.

tail

Return the last n lines of the config string representation

values

Get the 1st level of config values, same as dict.values()

Attributes

DEFAULT_DIR

all_variables

Get a sorted list of unique names of the input variables for all equations in this config.

global_variables

Get a dictionary of global variables (constant numerical values) available within this config object.

inputs

Get the inputs dictionary.

missing_inputs

Get a list of unique variables names that are required to evaluate the equations in the config but are still missing from the inputs.

outputs

Get the outputs dictionary.

required_inputs

Get a list of unique variable names required in the input namespace.

solvable

Are all required inputs defined?

solved

Have all the config equations been solved?

to_be_solved

NRWAL config keys that have not yet been solved but need to be.

head(n=5)[source]

Return the first n lines of the config string representation

tail(n=5)[source]

Return the last n lines of the config string representation

property inputs

Get the inputs dictionary.

Returns:

dict

property outputs

Get the outputs dictionary.

Returns:

dict

property global_variables

Get a dictionary of global variables (constant numerical values) available within this config object.

Returns:

dict

property all_variables

Get a sorted list of unique names of the input variables for all equations in this config. This will include global variables defined in this config and default variables defined in the equation directories.

Returns:

list

property required_inputs

Get a list of unique variable names required in the input namespace. This considers variables set in the config or in variables.yaml files to not be required, although these can still be overwritten in the config “inputs” attribute.

Returns:

list

property missing_inputs

Get a list of unique variables names that are required to evaluate the equations in the config but are still missing from the inputs.

Returns:

list

property solvable

Are all required inputs defined?

Returns:

bool

property to_be_solved

NRWAL config keys that have not yet been solved but need to be.

Returns:

list

property solved

Have all the config equations been solved?

Returns:

bool

get(key, default_value)[source]

Attempt to get a key from the NrwalConfig, return default_value if the key could not be retrieved

reset_output(key=None)[source]

Reset the output dictionary of the NrwalConfig object.

Parameters:

key (str) – Optioinal key to reset. Defaults to None which will reset all outputs.

keys()[source]

Get the 1st level of config keys, same as dict.keys()

items()[source]

Get the 1st level of config (keys, values), same as dict.items().

values()[source]

Get the 1st level of config values, same as dict.values()

eval(inputs=None)[source]

Alias for evaluate().

evaluate(inputs=None)[source]

Evaluate the equations in the NrwalConfig, set the output to the outputs attribute, and return the output.

Parameters:

inputs (dict | pd.DataFrame | None) – Optional namespace of input data to make available for the evaluation of the NrwalConfig. This will update any previously set inputs.

Returns:

outputs (dict) – Dictionary of outputs with the same keys as the input config but with int, float, or np.ndarray outputs as values.