Flag Aggregation KPI Builder¶
FlagAggKPIBuilder
¶
Bases: KPIBuilder[FlagAggKPIDefinition]
Fluent builder for creating FlagAggregation KPI definitions in bulk.
Provides a clean, declarative API for specifying multiple KPI definitions at once. Particularly useful for creating large numbers of KPIs with different combinations of flags and aggregations.
Example:
>>> builder = FlagAggKPIBuilder()
>>> definitions = (
... builder
... .for_flags(['BZ.market_price', 'BZ.net_position'])
... .with_aggregations([Aggregations.Mean, Aggregations.Max, Aggregations.Min])
... .for_all_objects()
... .build()
... )
>>> len(definitions) # 2 flags × 3 aggs = 6
All methods return self for chaining.
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
__init__
¶
__init__()
Initialize builder with default values.
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
87 88 89 90 91 92 93 | |
for_flag
¶
for_flag(flag: FlagTypeProtocol) -> FlagAggKPIBuilder
Set a single flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagTypeProtocol
|
Flag (e.g., 'BZ.Results.market_price') |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
95 96 97 98 99 100 101 102 103 104 105 106 | |
for_flags
¶
for_flags(flags: list[FlagTypeProtocol]) -> FlagAggKPIBuilder
Set multiple flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flags
|
list[FlagTypeProtocol]
|
List of flags |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
108 109 110 111 112 113 114 115 116 117 118 119 | |
with_aggregation
¶
with_aggregation(agg: Aggregation) -> FlagAggKPIBuilder
Set a single aggregation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agg
|
Aggregation
|
Aggregation function |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
121 122 123 124 125 126 127 128 129 130 131 132 | |
with_aggregations
¶
with_aggregations(aggs: list[Aggregation]) -> FlagAggKPIBuilder
Set multiple aggregations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggs
|
list[Aggregation]
|
List of aggregation functions |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
134 135 136 137 138 139 140 141 142 143 144 145 | |
for_all_objects
¶
for_all_objects() -> FlagAggKPIBuilder
Auto-discover objects from data.
Objects will be detected from DataFrame columns when generate_kpis() is called.
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
147 148 149 150 151 152 153 154 155 156 157 158 | |
for_objects_with_model_properties
¶
for_objects_with_model_properties(properties: dict[str, Any] | None = None, query_expr: str | None = None, filter_funcs: dict[str, Callable[[Any], bool]] | None = None) -> 'FlagAggKPIBuilder'
Filter objects based on model properties during KPI generation.
During KPI generation, the model DataFrame is fetched and filtered using the specified conditions. Only objects that: 1. Pass all filter conditions, AND 2. Exist in both model and flag DataFrames will be included in the generated KPIs.
Three modes of operation (can be combined with AND logic): 1. Property filters: Exact match or list membership 2. Query expression: Pandas query string evaluated on model DataFrame 3. Filter functions: Custom functions applied to property columns
All conditions across all modes are combined with AND logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
properties
|
dict[str, Any] | None
|
Dict of property names to values. Scalars for exact match, lists/sets for membership checks. |
None
|
query_expr
|
str | None
|
Pandas query expression (uses engine="python") |
None
|
filter_funcs
|
dict[str, Callable[[Any], bool]] | None
|
Dict of property names to filter functions applied column-wise |
None
|
Returns:
| Type | Description |
|---|---|
'FlagAggKPIBuilder'
|
Self for chaining |
Examples:
# Property filter - exact match and list membership
builder.for_objects_with_model_properties(
properties={'country': 'DE', 'type': ['wind', 'solar']}
)
# Query expression
builder.for_objects_with_model_properties(
query_expr='country == "DE" and voltage_kV > 200'
)
# Custom filter function
builder.for_objects_with_model_properties(
filter_funcs={'voltage_kV': lambda x: x > 200}
)
# Combined - properties AND query
builder.for_objects_with_model_properties(
properties={'country': 'DE'},
query_expr='voltage_kV > 200'
)
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 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 | |
for_object
¶
for_object(object: Hashable) -> FlagAggKPIBuilder
Specify explicit object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
Hashable
|
object name / ID |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
221 222 223 224 225 226 227 228 229 230 231 232 | |
for_objects
¶
for_objects(objects: list[Hashable]) -> FlagAggKPIBuilder
Specify explicit object list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list[Hashable]
|
List of object names |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
234 235 236 237 238 239 240 241 242 243 244 245 | |
with_model_flag
¶
with_model_flag(flag: FlagTypeProtocol, model_flag: FlagTypeProtocol) -> FlagAggKPIBuilder
Set explicit model flag for a specific flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagTypeProtocol
|
Variable flag |
required |
model_flag
|
FlagTypeProtocol
|
Corresponding model flag |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
247 248 249 250 251 252 253 254 255 256 257 258 259 | |
build
¶
build() -> list[FlagAggKPIDefinition]
Generate all KPI definitions from builder configuration.
Creates the Cartesian product of flags × aggregations.
Returns:
| Type | Description |
|---|---|
list[FlagAggKPIDefinition]
|
List of FlagAggKPIDefinition instances |
Example:
>>> builder = FlagAggKPIBuilder()
>>> definitions = (
... builder
... .for_flags(['BZ.market_price', 'BZ.net_position'])
... .with_aggregations([Aggregations.Mean, Aggregations.Max, Aggregations.Min])
... .build()
... )
>>> len(definitions) # 2 flags × 3 aggs = 6
Source code in submodules/mesqual/mesqual/kpis/builders/flag_agg_builder.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |