NRWAL.handlers.groups.VariableGroup
- class VariableGroup(group, name=None, interp_extrap_power=False, use_nearest_power=False, interp_extrap_year=False, use_nearest_year=False)[source]
Bases:
AbstractGroup
Class to handle a single json or yaml file with multiple numerical variable definitions from variables.yaml files.
- Parameters:
group (str | dict) – String filepath to a yaml or json file containing one or more equation strings OR a pre-extracted dictionary from a yaml or json file with equation strings as values.
name (str | None) – Optional name for identification and debugging if this AbstractGroup is being initialized with the “group” input argument as a pre-extracted dictionary.
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
Find power-based (MW) equations in this EquationGroup that match the request (by regex pattern "_[0-9]*MW$") and sort them by difference in equation power.
Find year-based (*_YYYY) equations in this EquationGroup that match the request difference in equation year.
Attempt to get a key from the EquationGroup, return default_value if the key could not be retrieved
Return the first n lines of the group string representation
Determine if an equation key is power-based by looking for the case-insensitive regex pattern "_[0-9]*MW$"
Determine if an equation key is year-based by looking for *_YYYY in the key
Get the 1st level of equation (keys, values), same as dict.items().
Get the 1st level of equation group keys, same as dict.keys()
Set default variables available to this object and all sub-groups and equations within this object.
Return the last n lines of the group string representation
Get the 1st level of equation values, same as dict.values()
Attributes
List of all Equation objects from this object.
Get a dictionary of the variable namespace where keys are variable names and values are single numeric variable values.
- __add__(other)
Add another equation group to this instance of EquationGroup (self) and return a new EquationGroup object that updates this instance with the new input. Note that overlapping sub EquationGroups in the original EquationGroup may be overwritten by the new input if a duplicate key exists.
- Parameters:
other (EquationGroup | str | dict) – Another EquationGroup object or filepath to an EquationGroup to add to this instance of EquationGroup (self).
- Returns:
out (EquationGroup) – A new EquationGroup instance with this instance of EquationGroup (self) updated with the input EquationGroup. Note that overlapping sub EquationGroups in the original EquationGroup may be overwritten by the new input if a duplicate key exists.
- property all_equations
List of all Equation objects from this object.
- find_nearest_power_eqns(request, group=None)
Find power-based (MW) equations in this EquationGroup that match the request (by regex pattern “_[0-9]*MW$”) and sort them by difference in equation power.
For example, if the request is “eqn_a_7MW” and there are “eqn_a_4MW”, “eqn_a_6MW”, and “eqn_a_10MW” in this group, this method will return [eqn_a_6MW, eqn_a_10MW, eqn_a_4MW], [6, 10, 4]
- Parameters:
request (str) – A key to retrieve an equation from this EquationGroup. Should contain the case-insensitive regex pattern “_[0-9]*MW$”. Otherwise, empty lists will be returned.
group (EquationGroup) – Group to be looking in for equations adjacent to the requested equation. Defaults to the top level self._group attribute.
- Returns:
eqns (list) – List of Equation objects that match the request key and are sorted by difference in the _*MW specification to the input request key. If the request key does not have the _*MW specification or if no other keys in this EquationGroup match the request then this will return an empty list.
eqn_powers (list) – List of float power MW values corresponding to eqns and sorted by difference in the _*MW specification to the input request key. If the request key does not have the _*MW specification or if no other keys in this EquationGroup match the request then this will return an empty list.
- find_nearest_year_eqns(request, group=None)
Find year-based (*_YYYY) equations in this EquationGroup that match the request difference in equation year.
- Parameters:
request (str) – A key to retrieve an equation from this EquationGroup. Should have the *_YYYY pattern. Otherwise, None will be returned.
group (EquationGroup) – Group to be looking in for equations adjacent to the requested equation. Defaults to the top level self._group attribute.
- Returns:
eqns (list) – List of Equation objects that match the request key and are sorted by difference in the YYYY specification to the input request key. If the request key does not have the YYYY specification or if no other keys in this EquationGroup match the request then this will return an empty list.
eqn_years (list) – List of integer year YYYY values corresponding to eqns and sorted by difference in the YYYY specification to the input request key. If the request key does not have the YYYY specification or if no other keys in this EquationGroup match the request then this will return an empty list.
- get(key, default_value)
Attempt to get a key from the EquationGroup, return default_value if the key could not be retrieved
- head(n=5)
Return the first n lines of the group string representation
- classmethod is_power_eqn(key)
Determine if an equation key is power-based by looking for the case-insensitive regex pattern “_[0-9]*MW$”
- Parameters:
key (str) – An equation key/name.
- Returns:
out (bool) – True if the regex pattern “_[0-9]*MW$” was found in key
- classmethod is_year_eqn(key)
Determine if an equation key is year-based by looking for *_YYYY in the key
- Parameters:
key (str) – An equation key/name.
- Returns:
out (bool) – True if a year string *_YYYY is found in key
- items()
Get the 1st level of equation (keys, values), same as dict.items().
- keys()
Get the 1st level of equation group keys, same as dict.keys()
- set_default_variables(var_dict)
Set default variables available to this object and all sub-groups and equations within this object.
- Parameters:
var_dict (dict | None) – Default variables namespace. Variables from this input will be passed to all Equation objects in this EquationGroup. These variables can always be overwritten when Equation.evaluate() is called.
- tail(n=5)
Return the last n lines of the group string representation
- values()
Get the 1st level of equation values, same as dict.values()
- property var_dict
Get a dictionary of the variable namespace where keys are variable names and values are single numeric variable values.