Skip to content

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
class FlagAggKPIBuilder(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.
    """

    def __init__(self):
        """Initialize builder with default values."""
        super().__init__()
        self._flags: list[FlagTypeProtocol] = []
        self._aggregations: list[Aggregation] = []
        self._objects: list[Hashable] | Literal['auto'] = 'auto'
        self._model_flags: dict[FlagTypeProtocol, FlagTypeProtocol] = {}

    def for_flag(self, flag: FlagTypeProtocol) -> FlagAggKPIBuilder:
        """
        Set a single flag.

        Args:
            flag: Flag (e.g., 'BZ.Results.market_price')

        Returns:
            Self for chaining
        """
        self._flags = [flag]
        return self

    def for_flags(self, flags: list[FlagTypeProtocol]) -> FlagAggKPIBuilder:
        """
        Set multiple flags.

        Args:
            flags: List of flags

        Returns:
            Self for chaining
        """
        self._flags = flags
        return self

    def with_aggregation(self, agg: Aggregation) -> FlagAggKPIBuilder:
        """
        Set a single aggregation.

        Args:
            agg: Aggregation function

        Returns:
            Self for chaining
        """
        self._aggregations = [agg]
        return self

    def with_aggregations(self, aggs: list[Aggregation]) -> FlagAggKPIBuilder:
        """
        Set multiple aggregations.

        Args:
            aggs: List of aggregation functions

        Returns:
            Self for chaining
        """
        self._aggregations = aggs
        return self

    def for_all_objects(self) -> FlagAggKPIBuilder:
        """
        Auto-discover objects from data.

        Objects will be detected from DataFrame columns when
        generate_kpis() is called.

        Returns:
            Self for chaining
        """
        self._objects = 'auto'
        return self

    def for_objects_with_model_properties(
            self,
            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.

        Args:
            properties: Dict of property names to values. Scalars for exact match,
                lists/sets for membership checks.
            query_expr: Pandas query expression (uses engine="python")
            filter_funcs: Dict of property names to filter functions applied column-wise

        Returns:
            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'
            )
        """
        self._objects = ModelPropertyFilter(
            properties=properties,
            query_expr=query_expr,
            filter_funcs=filter_funcs
        )
        return self

    def for_object(self, object: Hashable) -> FlagAggKPIBuilder:
        """
        Specify explicit object.

        Args:
            object: object name / ID

        Returns:
            Self for chaining
        """
        self._objects = [object]
        return self

    def for_objects(self, objects: list[Hashable]) -> FlagAggKPIBuilder:
        """
        Specify explicit object list.

        Args:
            objects: List of object names

        Returns:
            Self for chaining
        """
        self._objects = objects
        return self

    def with_model_flag(self, flag: FlagTypeProtocol, model_flag: FlagTypeProtocol) -> FlagAggKPIBuilder:
        """
        Set explicit model flag for a specific flag.

        Args:
            flag: Variable flag
            model_flag: Corresponding model flag

        Returns:
            Self for chaining
        """
        self._model_flags[flag] = model_flag
        return self

    def build(self) -> list[FlagAggKPIDefinition]:
        """
        Generate all KPI definitions from builder configuration.

        Creates the Cartesian product of flags × aggregations.

        Returns:
            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
        """
        definitions = []

        for flag in self._flags:
            for agg in self._aggregations:
                definition = FlagAggKPIDefinition(
                    flag=flag,
                    aggregation=agg,
                    model_flag=self._model_flags.get(flag),
                    objects=self._objects,
                    name_prefix=self._name_prefix,
                    name_suffix=self._name_suffix,
                    custom_name=self._custom_name,
                    extra_attributes=self._extra_attributes,
                )
                definitions.append(definition)

        return definitions

__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
def __init__(self):
    """Initialize builder with default values."""
    super().__init__()
    self._flags: list[FlagTypeProtocol] = []
    self._aggregations: list[Aggregation] = []
    self._objects: list[Hashable] | Literal['auto'] = 'auto'
    self._model_flags: dict[FlagTypeProtocol, FlagTypeProtocol] = {}

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
def for_flag(self, flag: FlagTypeProtocol) -> FlagAggKPIBuilder:
    """
    Set a single flag.

    Args:
        flag: Flag (e.g., 'BZ.Results.market_price')

    Returns:
        Self for chaining
    """
    self._flags = [flag]
    return self

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
def for_flags(self, flags: list[FlagTypeProtocol]) -> FlagAggKPIBuilder:
    """
    Set multiple flags.

    Args:
        flags: List of flags

    Returns:
        Self for chaining
    """
    self._flags = flags
    return self

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
def with_aggregation(self, agg: Aggregation) -> FlagAggKPIBuilder:
    """
    Set a single aggregation.

    Args:
        agg: Aggregation function

    Returns:
        Self for chaining
    """
    self._aggregations = [agg]
    return self

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
def with_aggregations(self, aggs: list[Aggregation]) -> FlagAggKPIBuilder:
    """
    Set multiple aggregations.

    Args:
        aggs: List of aggregation functions

    Returns:
        Self for chaining
    """
    self._aggregations = aggs
    return self

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
def for_all_objects(self) -> FlagAggKPIBuilder:
    """
    Auto-discover objects from data.

    Objects will be detected from DataFrame columns when
    generate_kpis() is called.

    Returns:
        Self for chaining
    """
    self._objects = 'auto'
    return self

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
def for_objects_with_model_properties(
        self,
        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.

    Args:
        properties: Dict of property names to values. Scalars for exact match,
            lists/sets for membership checks.
        query_expr: Pandas query expression (uses engine="python")
        filter_funcs: Dict of property names to filter functions applied column-wise

    Returns:
        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'
        )
    """
    self._objects = ModelPropertyFilter(
        properties=properties,
        query_expr=query_expr,
        filter_funcs=filter_funcs
    )
    return self

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
def for_object(self, object: Hashable) -> FlagAggKPIBuilder:
    """
    Specify explicit object.

    Args:
        object: object name / ID

    Returns:
        Self for chaining
    """
    self._objects = [object]
    return self

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
def for_objects(self, objects: list[Hashable]) -> FlagAggKPIBuilder:
    """
    Specify explicit object list.

    Args:
        objects: List of object names

    Returns:
        Self for chaining
    """
    self._objects = objects
    return self

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
def with_model_flag(self, flag: FlagTypeProtocol, model_flag: FlagTypeProtocol) -> FlagAggKPIBuilder:
    """
    Set explicit model flag for a specific flag.

    Args:
        flag: Variable flag
        model_flag: Corresponding model flag

    Returns:
        Self for chaining
    """
    self._model_flags[flag] = model_flag
    return self

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
def build(self) -> list[FlagAggKPIDefinition]:
    """
    Generate all KPI definitions from builder configuration.

    Creates the Cartesian product of flags × aggregations.

    Returns:
        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
    """
    definitions = []

    for flag in self._flags:
        for agg in self._aggregations:
            definition = FlagAggKPIDefinition(
                flag=flag,
                aggregation=agg,
                model_flag=self._model_flags.get(flag),
                objects=self._objects,
                name_prefix=self._name_prefix,
                name_suffix=self._name_suffix,
                custom_name=self._custom_name,
                extra_attributes=self._extra_attributes,
            )
            definitions.append(definition)

    return definitions