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.

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
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.

    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.
    """

    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, for example `'Country.Results.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. 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:

        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. The function takes
                the property value of one object. An object passes if the function returns a truthy value.

        Returns:
            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']
        """
        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
        """
        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 src/mesqual/kpis/builders/flag_agg_builder.py
89
90
91
92
93
94
95
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, for example 'Country.Results.price'.

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

    Args:
        flag: Flag, for example `'Country.Results.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 src/mesqual/kpis/builders/flag_agg_builder.py
110
111
112
113
114
115
116
117
118
119
120
121
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 src/mesqual/kpis/builders/flag_agg_builder.py
123
124
125
126
127
128
129
130
131
132
133
134
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 src/mesqual/kpis/builders/flag_agg_builder.py
136
137
138
139
140
141
142
143
144
145
146
147
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 src/mesqual/kpis/builders/flag_agg_builder.py
149
150
151
152
153
154
155
156
157
158
159
160
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. 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:

  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. 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
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. 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:

    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. The function takes
            the property value of one object. An object passes if the function returns a truthy value.

    Returns:
        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']
    """
    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 src/mesqual/kpis/builders/flag_agg_builder.py
219
220
221
222
223
224
225
226
227
228
229
230
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 src/mesqual/kpis/builders/flag_agg_builder.py
232
233
234
235
236
237
238
239
240
241
242
243
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 src/mesqual/kpis/builders/flag_agg_builder.py
245
246
247
248
249
250
251
252
253
254
255
256
257
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

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

    Creates the Cartesian product of flags × aggregations.

    Returns:
        List of FlagAggKPIDefinition instances
    """
    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

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
@dataclass
class ModelPropertyFilter:
    """
    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.
    """

    properties: dict[str, Any] | None = None
    query_expr: str | None = None
    filter_funcs: dict[str, Callable[[Any], bool]] | None = None

    def apply_filter(self, model_df: pd.DataFrame) -> list[Hashable]:
        """
        Apply all filter conditions to model DataFrame and return matching object names.

        Steps:

        1. Apply property filters (exact match or list membership) by row filtering
        2. Apply query expression using pandas.query()
        3. Keep the rows for which each filter function returns a truthy value for the value in its column
        4. Return index of remaining rows

        Args:
            model_df: DataFrame with objects as index and properties as columns

        Returns:
            List of object names (index values) that pass all filter conditions
        """
        model_dff = model_df.copy()
        if self.properties:
            for property, value in self.properties.items():
                if isinstance(value, (list, set)):
                    model_dff = model_dff[model_dff[property].isin(value)]
                else:
                    model_dff = model_dff[model_dff[property] == value]

        if self.query_expr:
            model_dff = model_dff.query(self.query_expr, engine='python')

        if self.filter_funcs:
            for prop_name, filter_func in self.filter_funcs.items():
                model_dff = model_dff[model_dff[prop_name].map(filter_func).astype(bool)]

        return list(model_dff.index)

apply_filter

apply_filter(model_df: DataFrame) -> list[Hashable]

Apply all filter conditions to model DataFrame and return matching object names.

Steps:

  1. Apply property filters (exact match or list membership) by row filtering
  2. Apply query expression using pandas.query()
  3. Keep the rows for which each filter function returns a truthy value for the value in its column
  4. 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
def apply_filter(self, model_df: pd.DataFrame) -> list[Hashable]:
    """
    Apply all filter conditions to model DataFrame and return matching object names.

    Steps:

    1. Apply property filters (exact match or list membership) by row filtering
    2. Apply query expression using pandas.query()
    3. Keep the rows for which each filter function returns a truthy value for the value in its column
    4. Return index of remaining rows

    Args:
        model_df: DataFrame with objects as index and properties as columns

    Returns:
        List of object names (index values) that pass all filter conditions
    """
    model_dff = model_df.copy()
    if self.properties:
        for property, value in self.properties.items():
            if isinstance(value, (list, set)):
                model_dff = model_dff[model_dff[property].isin(value)]
            else:
                model_dff = model_dff[model_dff[property] == value]

    if self.query_expr:
        model_dff = model_dff.query(self.query_expr, engine='python')

    if self.filter_funcs:
        for prop_name, filter_func in self.filter_funcs.items():
            model_dff = model_dff[model_dff[prop_name].map(filter_func).astype(bool)]

    return list(model_dff.index)