formulas
frequenz.sdk.timeseries.formulas ¤
Provides a way for the SDK to apply formulas on resampled data streams.
Formulas¤
Formulas are used in the SDK to
calculate and stream metrics like
grid_power,
consumer_power, etc., which
are building blocks of the Frequenz SDK Microgrid
Model.
The SDK creates the formulas by analysing the configuration of components in the Component Graph.
Streaming Interface¤
The
Formula.new_receiver()
method can be used to create a Receiver that streams the
Samples calculated by the evaluation of the formula.
from frequenz.sdk import microgrid
battery_pool = microgrid.new_battery_pool(priority=5)
async for power in battery_pool.power.new_receiver():
print(f"{power=}")
Composition¤
Composite Formulas can be built using arithmetic operations on Formulas
streaming the same type of data.
For example, if you're interested in a particular composite metric that can be
calculated by subtracting
new_battery_pool().power
and
new_ev_charger_pool().power
from the grid().power, we can build
a Formula that provides a stream of this calculated metric as follows:
from frequenz.sdk import microgrid
battery_pool = microgrid.new_battery_pool(priority=5)
ev_charger_pool = microgrid.new_ev_charger_pool(priority=5)
grid = microgrid.grid()
# apply operations on formulas to create a new formula that would
# apply these operations on the corresponding data streams.
net_power = (
grid.power - (battery_pool.power + ev_charger_pool.power)
).build("net_power")
async for power in net_power.new_receiver():
print(f"{power=}")
3-Phase Formulas¤
A Formula3Phase is similar
to a Formula, except that it
streams 3-phase samples. All the
current formulas (like
Grid.current_per_phase,
EVChargerPool.current_per_phase,
etc.) are implemented as 3-phase formulas.
Streaming Interface¤
The
Formula3Phase.new_receiver()
method can be used to create a Receiver that
streams the Sample3Phase values
calculated by 3-phase formulas.
from frequenz.sdk import microgrid
ev_charger_pool = microgrid.new_ev_charger_pool(priority=5)
async for sample in ev_charger_pool.current_per_phase.new_receiver():
print(f"Current: {sample}")
Composition¤
Formula3Phase instances can be composed together, just like Formula
instances.
from frequenz.sdk import microgrid
ev_charger_pool = microgrid.new_ev_charger_pool(priority=5)
grid = microgrid.grid()
# Calculate grid consumption current that's not used by the EV chargers
other_current = (grid.current_per_phase - ev_charger_pool.current_per_phase).build(
"other_current"
)
async for sample in other_current.new_receiver():
print(f"Other current: {sample}")
Classes¤
frequenz.sdk.timeseries.formulas.Formula ¤
Bases: BackgroundService, Generic[QuantityT]
A formula represented as an AST.
Source code in src/frequenz/sdk/timeseries/formulas/_formula.py
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 | |
Attributes¤
is_running
property
¤
is_running: bool
Return whether this background service is running.
A service is considered running when at least one task is running.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether this background service is running. |
name
property
¤
name: str
The name of this background service.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The name of this background service. |
tasks
property
¤
Return the set of running tasks spawned by this background service.
Users typically should not modify the tasks in the returned set and only use them for informational purposes.
Danger
Changing the returned tasks may lead to unexpected behavior, don't do it unless the class explicitly documents it is safe to do so.
| RETURNS | DESCRIPTION |
|---|---|
Set[Task[Any]]
|
The set of running tasks spawned by this background service. |
Functions¤
__add__ ¤
__add__(
other: (
FormulaBuilder[QuantityT]
| QuantityT
| Formula[QuantityT]
),
) -> FormulaBuilder[QuantityT]
Create an addition operation node.
__aenter__
async
¤
__aenter__() -> Self
Enter an async context.
Start this background service.
| RETURNS | DESCRIPTION |
|---|---|
Self
|
This background service. |
__aexit__
async
¤
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Exit an async context.
Stop this background service.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type
|
The type of the exception raised, if any.
TYPE:
|
exc_val
|
The exception raised, if any.
TYPE:
|
exc_tb
|
The traceback of the exception raised, if any.
TYPE:
|
Source code in src/frequenz/sdk/actor/_background_service.py
__await__ ¤
__await__() -> Generator[None, None, None]
Await this background service.
An awaited background service will wait for all its tasks to finish.
| RETURNS | DESCRIPTION |
|---|---|
None
|
An implementation-specific generator for the awaitable. |
Source code in src/frequenz/sdk/actor/_background_service.py
__init__ ¤
__init__(
*,
name: str,
root: AstNode[QuantityT],
create_method: Callable[[float], QuantityT],
sub_formulas: list[Formula[QuantityT]] | None = None,
metric_fetcher: ResampledStreamFetcher | None = None
) -> None
Create a Formula instance.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the formula.
TYPE:
|
root
|
The root node of the formula AST.
TYPE:
|
create_method
|
A method to generate the output values with. If the
formula is for generating power values, this would be
|
sub_formulas
|
Any sub-formulas that this formula depends on. |
metric_fetcher
|
An optional metric fetcher that needs to be started before the formula can be evaluated.
TYPE:
|
Source code in src/frequenz/sdk/timeseries/formulas/_formula.py
__repr__ ¤
__repr__() -> str
Return a string representation of this instance.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string representation of this instance. |
__sub__ ¤
__sub__(
other: (
FormulaBuilder[QuantityT]
| QuantityT
| Formula[QuantityT]
),
) -> FormulaBuilder[QuantityT]
Create a subtraction operation node.
cancel ¤
cancel(msg: str | None = None) -> None
Cancel all running tasks spawned by this background service.
| PARAMETER | DESCRIPTION |
|---|---|
msg
|
The message to be passed to the tasks being cancelled.
TYPE:
|
Source code in src/frequenz/sdk/actor/_background_service.py
coalesce ¤
coalesce(
*other: FormulaBuilder[QuantityT]
| QuantityT
| Formula[QuantityT],
) -> FormulaBuilder[QuantityT]
Create a coalesce operation node.
Source code in src/frequenz/sdk/timeseries/formulas/_formula.py
max ¤
max(
*other: FormulaBuilder[QuantityT]
| QuantityT
| Formula[QuantityT],
) -> FormulaBuilder[QuantityT]
Create a max operation node.
min ¤
min(
*other: FormulaBuilder[QuantityT]
| QuantityT
| Formula[QuantityT],
) -> FormulaBuilder[QuantityT]
Create a min operation node.
new_receiver ¤
Subscribe to the formula evaluator to get evaluated samples.
Source code in src/frequenz/sdk/timeseries/formulas/_formula.py
start ¤
stop
async
¤
stop(msg: str | None = None) -> None
Stop the formula evaluator.
Source code in src/frequenz/sdk/timeseries/formulas/_formula.py
wait
async
¤
Wait this background service to finish.
Wait until all background service tasks are finished.
| RAISES | DESCRIPTION |
|---|---|
BaseExceptionGroup
|
If any of the tasks spawned by this service raised an
exception ( |
Source code in src/frequenz/sdk/actor/_background_service.py
frequenz.sdk.timeseries.formulas.Formula3Phase ¤
Bases: BackgroundService, Generic[QuantityT]
A composite formula for three-phase metrics.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
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 | |
Attributes¤
is_running
property
¤
is_running: bool
Return whether this background service is running.
A service is considered running when at least one task is running.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether this background service is running. |
name
property
¤
name: str
The name of this background service.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The name of this background service. |
tasks
property
¤
Return the set of running tasks spawned by this background service.
Users typically should not modify the tasks in the returned set and only use them for informational purposes.
Danger
Changing the returned tasks may lead to unexpected behavior, don't do it unless the class explicitly documents it is safe to do so.
| RETURNS | DESCRIPTION |
|---|---|
Set[Task[Any]]
|
The set of running tasks spawned by this background service. |
Functions¤
__add__ ¤
__add__(
other: (
Formula3PhaseBuilder[QuantityT]
| Formula3Phase[QuantityT]
),
) -> Formula3PhaseBuilder[QuantityT]
Add two three-phase formulas.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
__aenter__
async
¤
__aenter__() -> Self
Enter an async context.
Start this background service.
| RETURNS | DESCRIPTION |
|---|---|
Self
|
This background service. |
__aexit__
async
¤
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Exit an async context.
Stop this background service.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type
|
The type of the exception raised, if any.
TYPE:
|
exc_val
|
The exception raised, if any.
TYPE:
|
exc_tb
|
The traceback of the exception raised, if any.
TYPE:
|
Source code in src/frequenz/sdk/actor/_background_service.py
__await__ ¤
__await__() -> Generator[None, None, None]
Await this background service.
An awaited background service will wait for all its tasks to finish.
| RETURNS | DESCRIPTION |
|---|---|
None
|
An implementation-specific generator for the awaitable. |
Source code in src/frequenz/sdk/actor/_background_service.py
__init__ ¤
__init__(
*,
name: str,
phase_1: Formula[QuantityT],
phase_2: Formula[QuantityT],
phase_3: Formula[QuantityT],
sub_formulas: (
list[Formula3Phase[QuantityT]] | None
) = None
) -> None
Initialize this instance.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the formula.
TYPE:
|
phase_1
|
The formula for phase 1.
TYPE:
|
phase_2
|
The formula for phase 2.
TYPE:
|
phase_3
|
The formula for phase 3.
TYPE:
|
sub_formulas
|
Sub-formulas that need to be started before this formula.
TYPE:
|
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
__mul__ ¤
__mul__(scalar: float) -> Formula3PhaseBuilder[QuantityT]
Multiply the three-phase formula by a scalar.
__repr__ ¤
__repr__() -> str
Return a string representation of this instance.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string representation of this instance. |
__str__ ¤
__str__() -> str
Return a string representation of this instance.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string representation of this instance. |
__sub__ ¤
__sub__(
other: (
Formula3PhaseBuilder[QuantityT]
| Formula3Phase[QuantityT]
),
) -> Formula3PhaseBuilder[QuantityT]
Subtract two three-phase formulas.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
__truediv__ ¤
__truediv__(
scalar: float,
) -> Formula3PhaseBuilder[QuantityT]
Divide the three-phase formula by a scalar.
cancel ¤
cancel(msg: str | None = None) -> None
Cancel all running tasks spawned by this background service.
| PARAMETER | DESCRIPTION |
|---|---|
msg
|
The message to be passed to the tasks being cancelled.
TYPE:
|
Source code in src/frequenz/sdk/actor/_background_service.py
coalesce ¤
coalesce(
*other: Formula3PhaseBuilder[QuantityT]
| Formula3Phase[QuantityT]
| tuple[QuantityT, QuantityT, QuantityT]
) -> Formula3PhaseBuilder[QuantityT]
Coalesce the three-phase formula with a default value.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
max ¤
max(
*other: Formula3PhaseBuilder[QuantityT]
| Formula3Phase[QuantityT]
| tuple[QuantityT, QuantityT, QuantityT]
) -> Formula3PhaseBuilder[QuantityT]
Get the maximum of the three-phase formula with other formulas.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
min ¤
min(
*other: Formula3PhaseBuilder[QuantityT]
| Formula3Phase[QuantityT]
| tuple[QuantityT, QuantityT, QuantityT]
) -> Formula3PhaseBuilder[QuantityT]
Get the minimum of the three-phase formula with other formulas.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
new_receiver ¤
new_receiver(
*, max_size: int = 50
) -> Receiver[Sample3Phase[QuantityT]]
Subscribe to the output of this formula.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
start ¤
Start the per-phase and sub formulas.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
stop
async
¤
stop(msg: str | None = None) -> None
Stop the formula.
Source code in src/frequenz/sdk/timeseries/formulas/_formula_3_phase.py
wait
async
¤
Wait this background service to finish.
Wait until all background service tasks are finished.
| RAISES | DESCRIPTION |
|---|---|
BaseExceptionGroup
|
If any of the tasks spawned by this service raised an
exception ( |