Flag and FlagIndex References¶
flag
¶
Flag management system for MESQUAL energy analysis.
Provides flexible identification and metadata management for energy system variables and models. Flags serve as universal identifiers with associated units, visualization preferences, and model relationships.
Key features:
- Type-safe flag protocols
- Registry-based metadata management
- Hierarchical model relationships
- Energy-specific enum integration
FlagTypeProtocol
¶
Bases: Protocol
Protocol for MESQUAL flag types.
Flags must be hashable (usable as dict keys) and convertible to strings. Compatible types include strings, enums, and custom classes with hash and str.
Examples:
- "Generator.Model", "Node.p_nom_opt"
- Enum members
- Custom classes implementing the protocol
Source code in submodules/mesqual/mesqual/flag/flag.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
__hash__
¶
__hash__() -> int
Return stable hash value for use as dictionary key.
Source code in submodules/mesqual/mesqual/flag/flag.py
17 18 19 | |
__str__
¶
__str__() -> str
Return human-readable string representation.
Source code in submodules/mesqual/mesqual/flag/flag.py
21 22 23 | |
FlagIndex
¶
Bases: Generic[FlagType], ABC
Abstract base class for flag metadata management.
Central registry supporting explicit registration and implicit resolution. Manages hierarchical relationships (e.g., 'Generator.p_nom_opt' → 'Generator.Model').
Subclasses must implement flag parsing and metadata resolution methods.
Source code in submodules/mesqual/mesqual/flag/flag_index.py
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 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
register_new_flag
¶
register_new_flag(flag: FlagType, linked_model_flag: FlagType = None, item_type: ItemTypeEnum = None, visualization_type: VisualizationTypeEnum = None, topology_type: TopologyTypeEnum = None, unit: Unit = None)
Explicitly register a flag with its metadata in the registry.
This method allows for complete control over flag metadata by creating an explicit registry entry. Explicitly registered flags take precedence over implicit resolution methods, enabling customization and override of default behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to register |
required |
linked_model_flag
|
FlagType
|
Parent model flag for variable flags (e.g., 'Generator.Model' for 'Generator.p_nom_opt') |
None
|
item_type
|
ItemTypeEnum
|
Category of the flag (Model, TimeSeries, Parameter, etc.) |
None
|
visualization_type
|
VisualizationTypeEnum
|
Preferred visualization approach for this flag's data |
None
|
topology_type
|
TopologyTypeEnum
|
Energy system topology category (Node, Branch, Generator, etc.) |
None
|
unit
|
Unit
|
Physical unit for the flag's associated data |
None
|
Examples:
>>> # Register a custom efficiency parameter
>>> flag_index.register_new_flag(
... flag="CustomGenerator.efficiency",
... linked_model_flag="CustomGenerator.Model",
... item_type=ItemTypeEnum.Other,
... unit=Units.NaU
... )
Source code in submodules/mesqual/mesqual/flag/flag_index.py
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 | |
get_registry_entry
¶
get_registry_entry(flag: FlagType) -> RegistryEntry
Retrieve complete registry entry for a flag, using explicit or implicit resolution.
This method returns a complete RegistryEntry for the specified flag, either from the explicit registry or by constructing one using implicit resolution methods. This provides a unified interface for accessing all flag metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to retrieve metadata for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RegistryEntry |
RegistryEntry
|
Complete metadata entry for the flag |
Note
For implicitly resolved entries, the returned RegistryEntry is constructed on-demand and not stored in the registry. This ensures metadata consistency while avoiding memory overhead for large numbers of implicit flags.
Source code in submodules/mesqual/mesqual/flag/flag_index.py
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 | |
get_linked_model_flag
¶
get_linked_model_flag(flag: FlagType) -> FlagType
Get the parent model flag for a variable flag.
This method resolves the hierarchical relationship between variable flags and their parent model flags. For example, 'Generator.p_nom_opt' would return 'Generator.Model'. This relationship is essential for organizing energy system data and understanding which variables belong to which models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The variable flag to resolve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FlagType |
FlagType
|
The parent model flag, or the flag itself if it's already a model flag |
Examples:
>>> flag_index.get_linked_model_flag("Generator.Results.Generation")
"Generator.Model"
>>> flag_index.get_linked_model_flag("Node.Model")
"Node.Model" # Model flags return themselves
Source code in submodules/mesqual/mesqual/flag/flag_index.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
get_item_type
¶
get_item_type(flag: FlagType) -> ItemTypeEnum
Get the item type category for a flag.
Item types classify flags into categories such as Model (static model data), TimeSeries (time-dependent variables), Parameter (model parameters), etc. This classification is used by MESQUAL for appropriate data handling and visualization selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to classify |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ItemTypeEnum |
ItemTypeEnum
|
The item type category for the flag |
Examples:
>>> flag_index.get_item_type("Generator.Model")
ItemTypeEnum.Model
>>> flag_index.get_item_type("Generator.Results.Generation")
ItemTypeEnum.TimeSeries
Source code in submodules/mesqual/mesqual/flag/flag_index.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
get_visualization_type
¶
get_visualization_type(flag: FlagType) -> VisualizationTypeEnum
Get the preferred visualization type for a flag.
Visualization types suggest how data associated with a flag should be displayed, such as geographic areas for areas, point for nodal objects, lines for line objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to get visualization preferences for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
VisualizationTypeEnum |
VisualizationTypeEnum
|
The preferred visualization type |
Examples:
>>> flag_index.get_visualization_type("Node.x")
VisualizationTypeEnum.Point
>>> flag_index.get_visualization_type("BiddingZone.Results.Price")
VisualizationTypeEnum.Area
Source code in submodules/mesqual/mesqual/flag/flag_index.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
get_topology_type
¶
get_topology_type(flag: FlagType) -> TopologyTypeEnum
Get the energy system topology type for a flag.
Topology types classify flags according to their role in the energy system topology, such as Node (buses/nodes), Edges (transmission lines, converters), NodeConnectedElement (generation units), etc. This classification helps with network analysis and appropriate data organization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to classify |
required |
Returns:
| Name | Type | Description |
|---|---|---|
TopologyTypeEnum |
TopologyTypeEnum
|
The topology type category |
Examples:
>>> flag_index.get_topology_type("Generator.p_nom_opt")
TopologyTypeEnum.NodeConnectedElement
>>> flag_index.get_topology_type("Line.s_nom")
TopologyTypeEnum.Edge
Source code in submodules/mesqual/mesqual/flag/flag_index.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
get_unit
¶
get_unit(flag: FlagType) -> Unit
Get the physical unit for a flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to get units for |
required |
Returns:
| Type | Description |
|---|---|
Unit
|
Units.Unit: The physical unit for the flag's data |
Examples:
>>> flag_index.get_unit("Generator.p_nom_opt")
Units.MW
>>> flag_index.get_unit("Line.length")
Units.km
Source code in submodules/mesqual/mesqual/flag/flag_index.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
get_quantity_type_enum
¶
get_quantity_type_enum(flag: FlagType) -> QuantityTypeEnum
Get the quantity type classification for a flag based on its unit (INTENSIVE vs EXTENSIVE quantities).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to classify |
required |
Returns:
| Name | Type | Description |
|---|---|---|
QuantityTypeEnum |
QuantityTypeEnum
|
The quantity type category based on the flag's unit (INTENSIVE or EXTENSIVE). |
Examples:
>>> flag_index.get_quantity_type_enum("Generator.p_nom_opt") # MW
QuantityTypeEnum.INTENSIVE
>>> flag_index.get_quantity_type_enum("Storage.energy_nom") # MWh
QuantityTypeEnum.EXTENSIVE
Source code in submodules/mesqual/mesqual/flag/flag_index.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
get_all_timeseries_flags_for_model_flag
¶
get_all_timeseries_flags_for_model_flag(dataset: Dataset, flag: FlagType) -> Set[FlagType]
Find all time series flags associated with a specific model flag.
This method discovers all time series variables that belong to a particular model type by examining the dataset's accepted flags and checking their linked model relationships. This is useful for finding all variables associated with a particular component type (e.g., all Generator time-series variables).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The dataset to search for flags |
required |
flag
|
FlagType
|
The model flag to find associated time series for |
required |
Returns:
| Type | Description |
|---|---|
Set[FlagType]
|
Set[FlagType]: All time series flags linked to the specified model flag |
Examples:
>>> # Find all generator time series variables
>>> gen_vars = flag_index.get_all_timeseries_flags_for_model_flag(
... dataset, "Generator.Model"
... )
>>> # Result might include: {"Generator.p_nom_opt", "Generator.efficiency", ...}
Source code in submodules/mesqual/mesqual/flag/flag_index.py
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 298 299 300 | |
get_flag_from_string
abstractmethod
¶
get_flag_from_string(flag_string: str) -> FlagType
Convert a string representation to a flag object.
This abstract method must be implemented by concrete flag index classes to provide string-to-flag conversion. This is essential for parsing configuration files, user input, and serialized data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag_string
|
str
|
String representation of the flag |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FlagType |
FlagType
|
The flag object corresponding to the string |
Note
Implementation depends on the specific flag type used by the concrete flag index class.
Source code in submodules/mesqual/mesqual/flag/flag_index.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
get_linked_model_flag_for_membership_column
¶
get_linked_model_flag_for_membership_column(membership_column_name: str) -> FlagType
Get the model flag associated with a membership column name.
This method resolves membership relationships in energy system models, where one component type references another through a membership column. For example, generators might have a 'node' column that references the node they're connected to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
membership_column_name
|
str
|
Name of the membership column in model DataFrames |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FlagType |
FlagType
|
The model flag for the referenced component type |
Examples:
>>> # Generator model has 'node' column referencing Node components
>>> flag_index.get_linked_model_flag_for_membership_column('node')
'Node.Model'
>>> # Line model has 'bus0' column referencing Node components
>>> flag_index.get_linked_model_flag_for_membership_column('bus0')
'Node.Model'
Raises:
| Type | Description |
|---|---|
KeyError
|
If no model is linked to the membership column name |
Source code in submodules/mesqual/mesqual/flag/flag_index.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
get_membership_column_name_for_model_flag
¶
get_membership_column_name_for_model_flag(flag: FlagType) -> str
Get the membership column name used to reference a model flag.
This method provides the reverse lookup of get_linked_model_flag_for_membership_column, returning the column name used in other model DataFrames to reference components of the specified model type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The model flag to get the membership column name for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The column name used in other models to reference this model type |
Examples:
>>> # Nodes are referenced by 'node' column in other models
>>> flag_index.get_membership_column_name_for_model_flag('Node.Model')
'node'
>>> # Generators might be referenced by 'generator' column
>>> flag_index.get_membership_column_name_for_model_flag('Generator.Model')
'generator'
Raises:
| Type | Description |
|---|---|
ValueError
|
If flag is not a Model type |
KeyError
|
If no membership column is linked to the model flag |
Source code in submodules/mesqual/mesqual/flag/flag_index.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
column_name_in_model_describes_membership
¶
column_name_in_model_describes_membership(column_name: str) -> bool
Check if a column name represents a membership relationship.
This utility method determines whether a given column name in a model DataFrame represents a membership relationship to another model type. This is useful for automatic DataFrame processing and validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column_name
|
str
|
The column name to check |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the column represents a membership relationship, False otherwise |
Examples:
>>> flag_index.column_name_in_model_describes_membership('node')
True # 'node' references Node.Model
>>> flag_index.column_name_in_model_describes_membership('efficiency')
False # 'efficiency' is a regular parameter, not a membership
Source code in submodules/mesqual/mesqual/flag/flag_index.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | |
get_flag_type
classmethod
¶
get_flag_type()
Get the flag type protocol used by this flag index.
This class method returns the type/protocol that flags must implement to be compatible with this flag index. It's primarily used for type checking and validation purposes.
Returns:
| Type | Description |
|---|---|
|
The FlagTypeProtocol class |
Note
This method helps with type safety and documentation of the flag type requirements for the flag index system.
Source code in submodules/mesqual/mesqual/flag/flag_index.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
EmptyFlagIndex
¶
Bases: FlagIndex
Minimal flag index implementation for basic scenarios.
Treats flags as simple strings, returns generic defaults for metadata. Suitable for testing or when flag metadata is not required.
Source code in submodules/mesqual/mesqual/flag/flag_index.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
get_flag_from_string
¶
get_flag_from_string(flag_string: str) -> FlagType
Return string as-is as flag.
Source code in submodules/mesqual/mesqual/flag/flag_index.py
590 591 592 | |