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.
Examples:
>>> from mesqual.kpis import Aggregations
>>> definitions = (
... FlagAggKPIBuilder()
... .for_flags(['Country.Results.price', 'Country.Results.demand_total'])
... .with_aggregations([Aggregations.Mean, Aggregations.Max, Aggregations.Min])
... .for_all_objects()
... .build()
... )
>>> len(definitions) # 2 flags x 3 aggregations
6
All methods return self for chaining.
Source code in src/mesqual/kpis/builders/flag_agg_builder.py
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 | |
__init__
¶
__init__()
Initialize builder with default values.
Source code in src/mesqual/kpis/builders/flag_agg_builder.py
89 90 91 92 93 94 95 | |
for_flag
¶
for_flag(flag: FlagTypeProtocol) -> FlagAggKPIBuilder
Set a single flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagTypeProtocol
|
Flag, for example |
required |
Returns:
| Type | Description |
|---|---|
FlagAggKPIBuilder
|
Self for chaining |
Source code in src/mesqual/kpis/builders/flag_agg_builder.py
97 98 99 100 101 102 103 104 105 106 107 108 | |
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 src/mesqual/kpis/builders/flag_agg_builder.py
110 111 112 113 114 115 116 117 118 119 120 121 | |
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 src/mesqual/kpis/builders/flag_agg_builder.py
123 124 125 126 127 128 129 130 131 132 133 134 | |
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 src/mesqual/kpis/builders/flag_agg_builder.py
136 137 138 139 140 141 142 143 144 145 146 147 | |
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 src/mesqual/kpis/builders/flag_agg_builder.py
149 150 151 152 153 154 155 156 157 158 159 160 | |
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. The KPIs include only the objects that pass all conditions and that exist in both the model and the flag DataFrames.
Three modes of operation:
- Property filters: Exact match or list membership
- Query expression: Pandas query string evaluated on model DataFrame
- 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. The function takes the property value of one object. An object passes if the function returns a truthy value. |
None
|
Returns:
| Type | Description |
|---|---|
'FlagAggKPIBuilder'
|
Self for chaining |
Examples:
The generators have the model properties node, fuel and capacity:
>>> from mesqual.kpis import Aggregations
>>> builder = FlagAggKPIBuilder().for_flag('Generator.Results.generation').with_aggregation(Aggregations.Mean)
>>> dataset.add_kpis_from_definitions(
... builder.for_objects_with_model_properties(properties={'fuel': ['wind', 'solar']}).build()
... )
>>> [kpi.attributes.object_name for kpi in dataset.kpi_collection]
['G4', 'G5']
>>> dataset.kpi_collection.clear()
>>> dataset.add_kpis_from_definitions(
... builder.for_objects_with_model_properties(query_expr='node == "N2" and capacity > 1050').build()
... )
>>> [kpi.attributes.object_name for kpi in dataset.kpi_collection]
['G4']
>>> dataset.kpi_collection.clear()
>>> dataset.add_kpis_from_definitions(
... builder.for_objects_with_model_properties(filter_funcs={'capacity': lambda x: x > 1050}).build()
... )
>>> [kpi.attributes.object_name for kpi in dataset.kpi_collection]
['G4', 'G5']
Source code in src/mesqual/kpis/builders/flag_agg_builder.py
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 | |
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 src/mesqual/kpis/builders/flag_agg_builder.py
219 220 221 222 223 224 225 226 227 228 229 230 | |
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 src/mesqual/kpis/builders/flag_agg_builder.py
232 233 234 235 236 237 238 239 240 241 242 243 | |
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 src/mesqual/kpis/builders/flag_agg_builder.py
245 246 247 248 249 250 251 252 253 254 255 256 257 | |
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 |
Source code in src/mesqual/kpis/builders/flag_agg_builder.py
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 | |
ModelPropertyFilter
dataclass
¶
Configuration for filtering objects based on model properties.
Filters a model DataFrame (with objects as index) by applying property constraints, query expressions, and custom filter functions. Returns the list of object names (index values) that pass all filters.
All conditions are combined with AND logic.
Source code in src/mesqual/kpis/builders/flag_agg_builder.py
15 16 17 18 19 20 21 22 23 24 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 | |
apply_filter
¶
apply_filter(model_df: DataFrame) -> list[Hashable]
Apply all filter conditions to model DataFrame and return matching object names.
Steps:
- Apply property filters (exact match or list membership) by row filtering
- Apply query expression using pandas.query()
- Keep the rows for which each filter function returns a truthy value for the value in its column
- Return index of remaining rows
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_df
|
DataFrame
|
DataFrame with objects as index and properties as columns |
required |
Returns:
| Type | Description |
|---|---|
list[Hashable]
|
List of object names (index values) that pass all filter conditions |
Source code in src/mesqual/kpis/builders/flag_agg_builder.py
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 | |