calliope.backend.helper_functions
¶
Functions that can be used to process data in math where
and expression
strings.
NAME
is the function name to use in the math strings.
DefaultIfEmpty(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['expression']
class-attribute
instance-attribute
¶
NAME = 'default_if_empty'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(var, default)
¶
Get an array with filled NaNs if present in the model, or a single default value if not.
This function is useful for avoiding excessive sub expressions where it is a choice between an expression or a single numeric value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var |
DataArray
|
array of backend expression objects or an un-indexed "string" object array with the var name (if not present in the model). |
required |
default |
float | int
|
Numeric value with which to fill / replace |
required |
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray:
If var is an array of backend expression objects, NaNs will be filled with |
Examples:
>>> default_if_empty(flow_cap, 0)
[out] <xarray.DataArray (techs: 2)>
array(['ac_transmission:node1', 'ac_transmission:node2'], dtype=object)
>>> default_if_empty(flow_export, 0)
[out] <xarray.DataArray ()>
array(0, dtype=np.int)
Source code in src/calliope/backend/helper_functions.py
Defined(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['where']
class-attribute
instance-attribute
¶
NAME = 'defined'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(*, within, how, **dims)
¶
Find whether members of a model dimension are defined inside another.
For instance, whether a node defines a specific tech (or group of techs). Or, whether a tech defines a specific carrier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
within |
str
|
the model dimension to check. |
required |
how |
Literal[all, any]
|
Whether to return True for |
required |
Kwargs
dims (dict[str, str]):
key: dimension whose members will be searched for as being defined under the primary dimension (within
).
Must be one of the core model dimensions: [nodes, techs, carriers]
value: subset of the dimension members to find.
Transmission techs can be called using the base tech name (e.g., ac_transmission
) and all link techs will be collected (e.g., [ac_transmission:region1
, ac_transmission:region2
]).
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray:
For each member of |
Examples:
Check for any of a list of techs being defined at nodes. Assuming a YAML definition of:
Then: >>> defined(techs=[tech1, tech2], within=nodes, how=any)
[out] <xarray.DataArray (nodes: 2)>
array([ True, False])
Coordinates:
* nodes (nodes) <U5 'node1' 'node2'
>>> defined(techs=[tech1, tech2], within=nodes, how=all)
[out] <xarray.DataArray (nodes: 2)>
array([ False, False])
Coordinates:
* nodes (nodes) <U5 'node1' 'node2'
Source code in src/calliope/backend/helper_functions.py
as_math_string(*, within, how, **dims)
¶
Source code in src/calliope/backend/helper_functions.py
GetValAtIndex(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['expression', 'where']
class-attribute
instance-attribute
¶
NAME = 'get_val_at_index'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(**dim_idx_mapping)
¶
Get value of a model dimension at a given integer index.
This function is primarily useful for timeseries data.
Other Parameters:
Name | Type | Description |
---|---|---|
key |
str
|
Model dimension in which to extract value. |
value |
int
|
Integer index of the value to extract (assuming zero-indexing). |
Raises:
Type | Description |
---|---|
ValueError
|
Exactly one dimension:index mapping is expected. |
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray: Dimensionless array containing one value. |
Examples:
>>> coords = {"timesteps": ["2000-01-01 00:00", "2000-01-01 01:00", "2000-01-01 02:00"]}
>>> model_data = xr.Dataset(coords=coords)
>>> get_val_at_index = GetValAtIndex(model_data=model_data)
>>> get_val_at_index(model_data)(timesteps=0)
<xarray.DataArray 'timesteps' ()>
array('2000-01-01 00:00', dtype='<U16')
Coordinates:
timesteps <U16 '2000-01-01 00:00'
>>> get_val_at_index(model_data)(timesteps=-1)
<xarray.DataArray 'timesteps' ()>
array('2000-01-01 00:00', dtype='<U16')
Coordinates:
timesteps <U16 '2000-01-01 02:00'
Source code in src/calliope/backend/helper_functions.py
Inheritance(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['where']
class-attribute
instance-attribute
¶
NAME = 'inheritance'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(*, nodes=None, techs=None)
¶
Find all technologies and/or nodes which inherit from a particular technology or node group.
The group items being referenced must be defined by the user in node_groups
/tech_groups
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
Optional[str]
|
group name to search for inheritance of on the |
None
|
techs |
Optional[str]
|
group name to search for inheritance of on the |
None
|
Returns:
Type | Description |
---|---|
DataArray
|
xr.Dataset: Boolean array where values are True where the group is inherited, False otherwise. Array dimensions will equal the number of non-None inputs. |
Examples:
With:
node_groups:
foo:
available_area: 1
tech_groups:
bar:
flow_cap_max: 1
baz:
inherits: bar
flow_out_eff: 0.5
nodes:
node_1:
inherits: foo
techs: {tech_1, tech_2}
node_2:
techs: {tech_1, tech_2}
techs:
tech_1:
...
inherits: bar
tech_2:
...
inherits: baz
>>> inheritance(nodes=foo)
<xarray.DataArray (nodes: 2)>
array([True, False])
Coordinates:
* nodes (nodes) <U1 'node_1' 'node_2'
>>> inheritance(techs=bar) # tech_2 inherits `bar` via `baz`.
<xarray.DataArray (techs: 2)>
array([True, True])
Coordinates:
* techs (techs) <U1 'tech_1' 'tech_2'
>>> inheritance(techs=baz)
<xarray.DataArray (techs: 2)>
array([False, True])
Coordinates:
* techs (techs) <U1 'tech_1' 'tech_2'
>>> inheritance(nodes=foo, techs=baz)
<xarray.DataArray (nodes: 2, techs: 2)>
array([[False, False],
[True, False]])
Coordinates:
* nodes (nodes) <U1 'node_1' 'node_2'
* techs (techs) <U1 'tech_1' 'tech_2'
Source code in src/calliope/backend/helper_functions.py
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 |
|
as_math_string(nodes=None, techs=None)
¶
Source code in src/calliope/backend/helper_functions.py
ParsingHelperFunction(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ABC
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN: list[Literal['where', 'expression']]
abstractmethod
property
¶
List of parseable math strings that this function can be accessed from.
NAME: str
abstractmethod
property
¶
Helper function name that is used in the math expression/where string.
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(*args, **kwargs)
abstractmethod
¶
Method to apply the helper function to provide an n-dimensional array output.
This method is called when the class is initialised with return_type=array
.
Source code in src/calliope/backend/helper_functions.py
as_math_string(*args, **kwargs)
abstractmethod
¶
Method to update LaTeX math strings to include the action applied by the helper function.
This method is called when the class is initialised with return_type=math_string
.
Source code in src/calliope/backend/helper_functions.py
ReduceCarrierDim(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['expression']
class-attribute
instance-attribute
¶
NAME = 'reduce_carrier_dim'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(array, flow_direction)
¶
Reduce expression array data by selecting the carrier that corresponds to the given carrier tier and then dropping the carriers
dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
DataArray
|
Expression array. |
required |
flow_direction |
Literal['in', 'out']
|
Flow direction in which to check for the existence of carrier(s) for technologies defined in |
required |
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray: |
Source code in src/calliope/backend/helper_functions.py
Roll(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['expression']
class-attribute
instance-attribute
¶
NAME = 'roll'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
as_array(array, **roll_kwargs)
¶
Roll (a.k.a., shift) the array along the given dimension(s) by the given number of places. Rolling keeps the array index labels in the same position, but moves the data by the given number of places.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
DataArray
|
Array on which to roll data. |
required |
Keyword Args: key (str): name of dimension on which to roll. value (int): number of places to roll data.
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray: |
Examples:
>>> array = xr.DataArray([1, 2, 3], coords={"foo": ["A", "B", "C"]})
>>> model_data = xr.Dataset({"bar": array})
>>> roll = Roll()
>>> roll("bar", foo=1)
<xarray.DataArray 'bar' (foo: 3)>
array([3, 1, 2])
Coordinates:
* foo (foo) <U1 'A' 'B' 'C'
Source code in src/calliope/backend/helper_functions.py
as_math_string(array, **roll_kwargs)
¶
Source code in src/calliope/backend/helper_functions.py
SelectFromLookupArrays(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['expression']
class-attribute
instance-attribute
¶
NAME = 'select_from_lookup_arrays'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(array, **lookup_arrays)
¶
Apply vectorised indexing on an arbitrary number of an input array's dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
DataArray
|
Array on which to apply vectorised indexing. |
required |
Kwargs
lookup_arrays (dict[str, xr.DataArray]): key: dimension on which to apply vectorised indexing value: array whose values are either NaN or values from the dimension given in the key.
Raises:
BackendError: array
must be indexed over the dimensions given in the lookup_arrays
dict keys.
BackendError: All lookup_arrays
must be indexed over all the dimensions given in the lookup_arrays
dict keys.
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray:
|
Examples:
>>> coords = {"foo": ["A", "B", "C"]}
>>> array = xr.DataArray([1, 2, 3], coords=coords)
>>> lookup_array = xr.DataArray(np.array(["B", "A", np.nan], dtype="O"), coords=coords, name="bar")
>>> model_data = xr.Dataset({"bar": lookup_array})
>>> select_from_lookup_arrays = SelectFromLookupArrays(model_data=model_data)
>>> select_from_lookup_arrays(array, foo=lookup_array)
<xarray.DataArray 'bar' (foo: 3)>
array([ 2., 1., nan])
Coordinates:
* foo (foo) object 'A' 'B' 'C'
The lookup array assigns the value at "B" to "A" and vice versa. "C" is masked since the lookup array value is NaN.
Source code in src/calliope/backend/helper_functions.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
|
as_math_string(array, **lookup_arrays)
¶
Source code in src/calliope/backend/helper_functions.py
Sum(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['expression']
class-attribute
instance-attribute
¶
NAME = 'sum'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(array, *, over)
¶
Sum an expression array over the given dimension(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
DataArray
|
expression array |
required |
over |
Union[str, list[str]]
|
dimension(s) over which to apply |
required |
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray:
Array with dimensions reduced by applying a summation over the dimensions given in |
Source code in src/calliope/backend/helper_functions.py
as_math_string(array, *, over)
¶
Source code in src/calliope/backend/helper_functions.py
WhereAny(return_type, *, equation_name, input_data, backend_interface=None, **kwargs)
¶
Bases: ParsingHelperFunction
Abstract helper function class, which all helper functions must subclass.
The abstract properties and methods defined here must be defined by all helper functions.
Source code in src/calliope/backend/helper_functions.py
ALLOWED_IN = ['where']
class-attribute
instance-attribute
¶
NAME = 'any'
class-attribute
instance-attribute
¶
ignore_where: bool
property
¶
If True, where
arrays will not be applied to the incoming data variables (valid for expression helpers)
as_array(parameter, *, over)
¶
Reduce the boolean where array of a model parameter by applying any
over some dimension(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameter |
str
|
Reference to a model input parameter |
required |
over |
Union[str, list[str]]
|
dimension(s) over which to apply |
required |
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray:
If the parameter exists in the model, returns a boolean array with dimensions reduced by applying a boolean OR operation along the dimensions given in |