Dataset References¶
datasets
¶
Dataset module providing the core data access layer for MESQUAL.
This module implements the foundational "Everything is a Dataset" principle,
where all data sources—individual scenarios, merged scenarios, collections,
and comparisons—share a unified interface through the .fetch(flag) pattern.
Core Classes
Dataset: Abstract base class defining the universal data access interface.
All dataset types inherit from this class and implement the fetch() method.
DatasetCollection: Base class for grouping related datasets together. Provides iteration and batch operations across multiple datasets.
DatasetLinkCollection: Collection maintaining parent-child relationships. Used when datasets need to reference back to their container.
DatasetMergeCollection: Combines multiple datasets by merging their data. Useful for aggregating results from different simulation runs.
DatasetSumCollection: Aggregates datasets by summing numeric values. Commonly used for capacity or production totals across scenarios.
DatasetConcatCollection: Concatenates datasets along a specified axis. Creates MultiIndex structures preserving scenario identities.
DatasetComparison: Computes differences between scenario pairs. Enables delta analysis and comparative studies.
DatasetConcatCollectionOfComparisons: Specialized collection for comparisons. Facilitates systematic comparison across multiple scenario pairs.
PlatformDataset: Dataset subclass for platform-specific implementations. Extended by platform interfaces (e.g., PyPSA, PLEXOS) to provide platform-aware data access.
DatasetConfig: Configuration class controlling dataset behavior. Manages caching, post-processing, and platform-specific options.
Example
Basic usage pattern::
from mesqual.datasets import Dataset, DatasetConfig
# Fetch data from a dataset
prices = dataset.fetch('buses_t.marginal_price')
# Configure dataset behavior
config = DatasetConfig(use_database=True)
dataset.set_instance_config(config)
# Work with collections
for scenario in collection:
data = scenario.fetch('generators_t.p')
See Also
- :mod:
mesqual.flag: Flag types and flag index implementations - :mod:
mesqual.kpis: KPI calculation framework - :mod:
mesqual.databases: Caching backends for datasets
Dataset
¶
Bases: Generic[DatasetConfigType, FlagType, FlagIndexType], ABC
Abstract base class for all datasets in the MESQUAL framework.
The Dataset class provides the fundamental interface for data access and manipulation in MESQUAL. It implements the core principle "Everything is a Dataset" where individual scenarios, scenarios merged from multiple simulation runs or data sources, collections of scenarios, and scenario comparisons all share the same unified interface.
Key Features
- Unified
.fetch(flag)interface for data access - Attribute management for scenario metadata
- KPI calculation integrations
- Database caching support
- Dot notation fetching via
dotfetch() - Type-safe generic implementation
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
DatasetConfigType
|
Configuration class for dataset behavior |
required | |
FlagType
|
Type used for data flag identification (typically str) |
required | |
FlagIndexType
|
Flag index implementation for flag mapping |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable identifier for the dataset |
kpi_collection |
KPICollection
|
Collection of KPIs associated with this dataset |
Example:
>>> # Basic usage pattern
>>> data = dataset.fetch('buses_t.marginal_price')
>>> flags = dataset.accepted_flags
>>> if dataset.flag_is_accepted('generators_t.p'):
... gen_data = dataset.fetch('generators_t.p')
Source code in submodules/mesqual/mesqual/datasets/dataset.py
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 580 581 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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 | |
accepted_flags
abstractmethod
property
¶
accepted_flags: set[FlagType]
Set of all flags accepted by this dataset.
This abstract property must be implemented by all concrete dataset classes to define which data flags can be fetched from the dataset.
Returns:
| Type | Description |
|---|---|
set[FlagType]
|
Set of flags that can be used with the fetch() method |
Example:
>>> print(dataset.accepted_flags)
{'buses', 'buses_t.marginal_price', 'generators', 'generators_t.p', ...}
flag_index
property
¶
flag_index: FlagIndexType
Access the flag index for this dataset.
The flag index provides flag mapping, validation, and metadata lookup capabilities. It enables features like dot notation fetching, flag-to-model mapping, and flag categorization.
If no flag index was configured, returns an EmptyFlagIndex and logs an informational message when accessed.
Returns:
| Name | Type | Description |
|---|---|---|
FlagIndexType |
FlagIndexType
|
The configured flag index or EmptyFlagIndex if none set |
Note
For full flag index functionality (model mapping, flag categorization), ensure a proper flag index is set during dataset initialization.
See Also
- Flag System - Flag index implementations and usage
database
property
¶
database: Database | None
Access the caching database for this dataset.
The database provides persistent caching for expensive fetch operations. When configured, the fetch() method automatically checks the database before computing data and stores results for future access.
Returns:
| Type | Description |
|---|---|
Database | None
|
Database | None: The configured database instance, or None if caching is not enabled for this dataset |
See Also
- Database configuration and caching behavior
- Uses database for automatic caching when available (see
fetch()method)
attributes
property
¶
attributes: dict
Access the metadata attributes dictionary for this dataset.
Attributes store scenario-level metadata such as configuration parameters, simulation settings, or descriptive labels. These are useful for filtering, grouping, and annotating datasets in collections.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Dictionary of attribute key-value pairs. Keys are strings, values are primitive types (bool, int, float, str). |
Example
Accessing and using attributes::
>>> dataset.attributes
{'year': 2030, 'scenario_type': 'high_renewable', 'carbon_price': 50.0}
>>> # Filter datasets in a collection by attribute
>>> high_re_scenarios = [d for d in collection if d.attributes.get('scenario_type') == 'high_renewable']
See Also
set_attributes()- Set attribute valuesget_attributes_series()- Convert attributes to pandas Series
parent_dataset
property
writable
¶
parent_dataset: 'DatasetLinkCollection'
Access the parent collection linking this interpreter to sibling interpreters.
The parent_dataset provides the bridge between specialized flag interpreters within a single platform dataset or study. It is NOT used to link scenarios together, but rather to enable modular interpreter architectures where each interpreter handles a specific subset of flags and can access flags from sibling interpreters through the shared parent.
Architecture Pattern
A typical platform dataset (e.g., PyPSADataset, PlexosDataset) is implemented as a DatasetLinkCollection containing multiple specialized interpreters:
- ModelInterpreter: Provides static model data (e.g., 'generators', 'buses')
- TimeSeriesInterpreter: Provides time-series data (e.g., 'generators_t.p')
- ObjectiveInterpreter: Provides objective function values
- Custom Interpreters: Study-specific derived or corrected variables
Each interpreter is a child dataset within the parent DatasetLinkCollection. Through parent_dataset, any interpreter can fetch flags from siblings without needing direct references or circular dependencies.
Why This Pattern
- Separation of Concerns: Each interpreter focuses on one data type
- Modularity: Add/remove/replace interpreters independently
- Dependency Resolution: Interpreters can depend on each other's flags
- Study Customization: Override or extend specific interpreters per study
- Maintainability: Changes to one interpreter don't affect others
Returns:
| Name | Type | Description |
|---|---|---|
DatasetLinkCollection |
'DatasetLinkCollection'
|
The parent collection that orchestrates flag routing between this interpreter and its siblings |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If accessed before the parent has been assigned (typically happens if an interpreter is used standalone instead of within a DatasetLinkCollection) |
Example
Custom interpreter combining flags from sibling interpreters:
>>> # Study-specific interpreter for renewable generation per bidding zone
>>> class RESGenerationPerBZInterpreter(PlatformBaseInterpreterDataset):
... @property
... def accepted_flags(self):
... return {'generators_t.res_generation_per_bz'}
...
... def _fetch(self, flag, config, **kwargs):
... # Fetch time series from TimeSeriesInterpreter sibling
... generation = self.parent_dataset.fetch('generators_t.p')
...
... # Fetch model data from ModelInterpreter sibling
... gen_model = self.parent_dataset.fetch('generators.model')
...
... # Filter to RES generators and aggregate by bidding zone
... res_gens = gen_model[gen_model['carrier'].isin(['solar', 'wind'])]
... res_generation = generation[res_gens.index]
... return res_generation.groupby(gen_model['bidding_zone'], axis=1).sum()
Accessing specific sibling interpreter by type:
>>> class SomeCustomPTDFMatrixFormat(PlexosImporterBase):
... def _fetch(self, flag, config, **kwargs):
... # Get specific sibling interpreter
... ptdf_ds = self.parent_dataset.get_dataset_by_type(
... PlexosPTDFInterpreter
... )
...
... # Or fetch through parent (automatically routes to correct sibling)
... headers = self.parent_dataset.fetch('PTDF.Headers')
... factors = self.parent_dataset.fetch('PTDF.Factors')
...
... # Process and return derived flag
... return self._custom_ptdf_process(headers, factors)
Study-specific correction of platform variables:
>>> class LineFlows(MyStudyVariables):
... '''Replaces specific flows with external data.'''
...
... def _fetch(self, flag, config, **kwargs):
... # Get reference dataset (sibling interpreter) for this flag
... reference_ds = self._get_reference_dataset_for_flag(flag)
...
... # Fetch original data from sibling
... df = reference_ds.fetch(flag, config, **kwargs)
...
... # Apply study-specific corrections
... if self.parent_dataset.attributes['manual_line_flow_correction']:
... df = self._apply_historical_corrections(df)
...
... return df
See Also
DatasetLinkCollection- Parent collection class that orchestrates routingget_dataset_by_type()- Method to access specific sibling by type
instance_config
property
¶
instance_config: DatasetConfigType
Get the effective configuration for this dataset instance.
Computes the merged configuration by combining: 1. Base config defaults 2. Class-level config (set via set_class_config) 3. Instance-level config (passed to init or set via set_instance_config)
Later settings override earlier ones. This is the configuration used by fetch() unless overridden by a fetch-time config parameter.
Returns:
| Name | Type | Description |
|---|---|---|
DatasetConfigType |
DatasetConfigType
|
The fully resolved configuration for this instance |
Example
Inspecting current configuration::
>>> config = dataset.instance_config
>>> print(config.use_database)
True
>>> print(config.auto_sort_datetime_index)
True
See Also
set_instance_config()- Replace instance configurationset_class_config()- Set class-level defaults- DatasetConfigManager - Configuration management system
__init__
¶
__init__(name: str = None, parent_dataset: Dataset = None, flag_index: FlagIndexType = None, attributes: dict = None, database: Database = None, config: DatasetConfigType = None)
Initialize a new Dataset instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable identifier. If None, auto-generates from class name |
None
|
parent_dataset
|
Dataset
|
Optional parent dataset for hierarchical relationships |
None
|
flag_index
|
FlagIndexType
|
Index for mapping and validating data flags |
None
|
attributes
|
dict
|
Dictionary of metadata attributes for the dataset |
None
|
database
|
Database
|
Optional database for caching expensive computations |
None
|
config
|
DatasetConfigType
|
Configuration object controlling dataset behavior |
None
|
Source code in submodules/mesqual/mesqual/datasets/dataset.py
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 | |
fetch
¶
fetch(flag: FlagType, config: dict | DatasetConfigType = None, **kwargs) -> Series | DataFrame
Fetch data associated with a specific flag.
This is the primary method for data access in MESQUAL datasets. It provides a unified interface for retrieving data regardless of the underlying source or dataset type. The method includes automatic caching, post-processing, and configuration management.
Configuration Override Behavior:
The ``config`` parameter allows fetch-time overrides of dataset behavior.
These overrides are merged with the dataset's effective configuration
(which combines class-level and instance-level settings). Only non-None
values in the override will replace the existing settings.
The configuration resolution hierarchy (later overrides earlier):
1. Base config defaults
2. Class config (via DatasetConfigManager)
3. Instance config (passed to Dataset.__init__)
4. **Fetch-time config (this parameter)**
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
Data identifier flag (must be in accepted_flags) |
required |
config
|
dict | DatasetConfigType
|
Optional configuration to override dataset defaults. Can be either:
|
None
|
**kwargs
|
Additional keyword arguments passed to the underlying data fetching implementation |
{}
|
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
DataFrame or Series containing the requested data |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the flag is not accepted by this dataset |
Examples:
Basic usage::
>>> prices = dataset.fetch('buses_t.marginal_price')
Override base config options with a dict::
>>> # Skip database cache for this fetch
>>> prices = dataset.fetch(
... 'buses_t.marginal_price',
... config=dict(use_database=False)
... )
>>>
>>> # Disable datetime sorting
>>> prices = dataset.fetch(
... 'generators_t.p',
... config=dict(auto_sort_datetime_index=False)
... )
Override platform-specific options::
>>> # Platform configs may have additional options
>>> # e.g., a config with timestamp conversion setting
>>> data = dataset.fetch(
... 'some_flag',
... config=dict(convert_period_enum_to_datetime_index=False)
... )
Override study-specific options::
>>> # Study-specific configs can add custom behavior
>>> # e.g., toggle custom data corrections
>>> data = dataset.fetch(
... 'some_flag',
... config=dict(apply_custom_correction=False)
... )
Using a config object::
>>> from mesqual.datasets import DatasetConfig
>>> custom_config = DatasetConfig(
... use_database=False,
... auto_sort_datetime_index=False
... )
>>> prices = dataset.fetch('buses_t.marginal_price', config=custom_config)
Source code in submodules/mesqual/mesqual/datasets/dataset.py
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 | |
get_accepted_flags_containing_x
¶
get_accepted_flags_containing_x(x: str, match_case: bool = False) -> set[FlagType]
Find all accepted flags containing a specific substring.
Useful for discovering related data flags or filtering flags by category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str
|
Substring to search for in flag names |
required |
match_case
|
bool
|
If True, performs case-sensitive search. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
set[FlagType]
|
Set of accepted flags containing the substring |
Example:
>>> ds = PyPSADataset()
>>> ds.get_accepted_flags_containing_x('generators')
{'generators', 'generators_t.p', 'generators_t.efficiency', ...}
>>> ds.get_accepted_flags_containing_x('BUSES', match_case=True)
set() # Empty because case doesn't match
Source code in submodules/mesqual/mesqual/datasets/dataset.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 | |
flag_is_accepted
¶
flag_is_accepted(flag: FlagType) -> bool
Boolean check whether a flag is accepted by the Dataset.
This method can be optionally overridden in any child-class in case you want to follow logic instead of the explicit set of accepted_flags.
Source code in submodules/mesqual/mesqual/datasets/dataset.py
287 288 289 290 291 292 293 294 | |
dotfetch
¶
dotfetch() -> _DotNotationFetcher
Create a dot notation fetcher for intuitive flag access.
Returns a helper object that allows accessing nested data flags using Python attribute syntax instead of string-based flags. The fetcher accumulates attribute accesses and converts them to the appropriate flag when called.
Returns:
| Name | Type | Description |
|---|---|---|
_DotNotationFetcher |
_DotNotationFetcher
|
Helper object enabling chained attribute access |
Example
Using dot notation instead of string flags::
>>> # Traditional string-based fetch
>>> prices = dataset.fetch('buses_t.marginal_price')
>>> # Equivalent dot notation fetch
>>> prices = dataset.dotfetch().buses_t.marginal_price()
>>> # Multi-level flag access
>>> gen_power = dataset.dotfetch().generators_t.p()
Source code in submodules/mesqual/mesqual/datasets/dataset.py
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 | |
add_kpis_from_definitions
¶
add_kpis_from_definitions(kpi_definitions: KPIDefinition | list[KPIDefinition])
Generate and add KPIs from one or more KPI definitions.
KPI definitions are templates that generate concrete KPI instances based on the dataset's structure. This method processes definitions and adds the resulting KPIs to the dataset's KPI collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpi_definitions
|
KPIDefinition | list[KPIDefinition]
|
Single KPIDefinition or list of definitions. Each definition's generate_kpis() method is called with this dataset to produce KPI instances. |
required |
Example
Adding KPIs from definitions::
>>> from mesqual.kpis.definitions import TotalGenerationKPIDefinition
>>> dataset.add_kpis_from_definitions(TotalGenerationKPIDefinition())
>>> # Add multiple definitions at once
>>> definitions = [
... TotalGenerationKPIDefinition(),
... MarginalPriceKPIDefinition(),
... ]
>>> dataset.add_kpis_from_definitions(definitions)
See Also
add_kpi()- Add a single KPI directlyadd_kpis()- Add multiple KPI instances- KPI Definitions - Base KPI definition class
Source code in submodules/mesqual/mesqual/datasets/dataset.py
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 | |
add_kpis
¶
add_kpis(kpis: Iterable[KPI])
Add multiple KPIs to this dataset's KPI collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpis
|
Iterable[KPI]
|
Iterable of KPI instances, factories, or classes to add |
required |
Source code in submodules/mesqual/mesqual/datasets/dataset.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
add_kpi
¶
add_kpi(kpi: KPI)
Add a single KPI to this dataset's KPI collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpi
|
KPI
|
KPI instance, factory, or class to add |
required |
Source code in submodules/mesqual/mesqual/datasets/dataset.py
429 430 431 432 433 434 435 436 | |
clear_kpi_collection
¶
clear_kpi_collection()
Clear the KPI collection.
Source code in submodules/mesqual/mesqual/datasets/dataset.py
438 439 440 441 | |
get_attributes_series
¶
get_attributes_series() -> Series
Convert dataset attributes to a pandas Series.
Creates a Series with attribute names as the index and attribute values as data. The Series name is set to the dataset name, making it suitable for concatenation with other datasets' attribute series.
Returns:
| Type | Description |
|---|---|
Series
|
pd.Series: Series containing attribute values, indexed by attribute names, with the dataset name as the Series name |
Example
Converting attributes and combining across datasets::
>>> dataset.set_attributes(year=2030, carbon_price=50.0)
>>> series = dataset.get_attributes_series()
>>> series
year 2030
carbon_price 50.0
Name: Scenario_A, dtype: object
>>> # Combine attributes from multiple datasets
>>> attr_df = pd.concat([d.get_attributes_series() for d in collection], axis=1).T
See Also
attributes- Access raw attributes dictionaryset_attributes()- Set attribute values
Source code in submodules/mesqual/mesqual/datasets/dataset.py
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 | |
set_attributes
¶
set_attributes(**kwargs)
Set one or more metadata attributes on this dataset.
Attributes are key-value pairs that store scenario metadata. They must use string keys and primitive values (bool, int, float, str) to ensure serializability and consistent comparison behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Attribute key-value pairs to set. Keys must be strings, values must be bool, int, float, or str. |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If any key is not a string |
TypeError
|
If any value is not bool, int, float, or str |
Example
Setting scenario metadata::
>>> dataset.set_attributes(
... year=2030,
... scenario_type='high_renewable',
... carbon_price=50.0,
... includes_nuclear=True
... )
>>> # Access the attributes
>>> dataset.attributes['year']
2030
See Also
attributes- Access attributes dictionaryget_attributes_series()- Convert to pandas Series
Source code in submodules/mesqual/mesqual/datasets/dataset.py
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 | |
required_flags_for_flag
¶
required_flags_for_flag(flag: FlagType) -> set[FlagType]
Get the set of flags required to compute a given flag.
For derived or computed flags, this method returns the set of source flags that must be available to produce the requested data. This is useful for understanding data dependencies and ensuring prerequisite data exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to check requirements for. Must be in accepted_flags. |
required |
Returns:
| Type | Description |
|---|---|
set[FlagType]
|
set[FlagType]: Set of flags that are required to compute the given flag. Returns an empty set if the flag has no dependencies. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the flag is not accepted by this dataset |
Example
Checking data dependencies::
>>> # A derived flag might depend on multiple source flags
>>> deps = dataset.required_flags_for_flag('total_generation')
>>> deps
{'generators_t.p', 'generators'}
See Also
_required_flags_for_flag()- Abstract method to implementflag_is_accepted()- Check if a flag is valid
Source code in submodules/mesqual/mesqual/datasets/dataset.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
fetch_multiple_flags_and_concat
¶
fetch_multiple_flags_and_concat(flags: Iterable[FlagType], concat_axis: int = 1, concat_level_name: str = 'variable', concat_level_at_top: bool = True, config: dict | DatasetConfigType = None, **kwargs) -> Union[Series, DataFrame]
Fetch multiple flags and concatenate results into a single DataFrame.
Convenience method for retrieving data from multiple flags and combining them into a single DataFrame with a MultiIndex. Useful for comparative analysis of multiple variables or creating wide-format data structures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flags
|
Iterable[FlagType]
|
Iterable of flags to fetch and concatenate |
required |
concat_axis
|
int
|
Axis along which to concatenate (0=rows, 1=columns). Default is 1 (columns). |
1
|
concat_level_name
|
str
|
Name for the new MultiIndex level identifying the source flag. Default is 'variable'. |
'variable'
|
concat_level_at_top
|
bool
|
If True, the flag level is the outermost level in the MultiIndex. If False, it's moved to the innermost level. Default is True. |
True
|
config
|
dict | DatasetConfigType
|
Optional configuration override (see fetch() for details) |
None
|
**kwargs
|
Additional arguments passed to each fetch() call |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Series, DataFrame]
|
DataFrame with concatenated data and a MultiIndex identifying the |
Union[Series, DataFrame]
|
source flag for each section |
Example
Fetching and comparing multiple variables::
>>> # Fetch power output and efficiency for generators
>>> combined = dataset.fetch_multiple_flags_and_concat(
... flags=['generators_t.p', 'generators_t.efficiency'],
... concat_level_name='metric'
... )
>>> # Result has MultiIndex columns: (metric, generator_name)
>>> # Row-wise concatenation
>>> stacked = dataset.fetch_multiple_flags_and_concat(
... flags=['bus_A_prices', 'bus_B_prices'],
... concat_axis=0,
... concat_level_name='bus'
... )
See Also
fetch()- Single flag data retrievalfetch_filter_groupby_agg()- Fetch with filtering and aggregation
Source code in submodules/mesqual/mesqual/datasets/dataset.py
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 | |
fetch_filter_groupby_agg
¶
fetch_filter_groupby_agg(flag: FlagType, model_filter_query: str = None, prop_groupby: str | list[str] = None, prop_groupby_agg: str = None, config: dict | DatasetConfigType = None, **kwargs) -> Series | DataFrame
Fetch data with model-based filtering, grouping, and aggregation.
Provides a powerful one-line method for common data analysis patterns: filter time series by model properties, group by categories, and aggregate results. Requires a flag index with model mappings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
Data flag to fetch (must have a linked model flag) |
required |
model_filter_query
|
str
|
Pandas query string to filter based on model properties. Applied to the linked model DataFrame. Example: "carrier == 'solar'" or "p_nom > 100" |
None
|
prop_groupby
|
str | list[str]
|
Model property or list of properties to group by. Adds these as MultiIndex levels and groups the data. Example: 'carrier' or ['carrier', 'bus'] |
None
|
prop_groupby_agg
|
str
|
Aggregation function to apply after grouping. Standard pandas aggregation strings like 'sum', 'mean', 'max'. Only used if prop_groupby is specified. |
None
|
config
|
dict | DatasetConfigType
|
Optional configuration override (see fetch() for details) |
None
|
**kwargs
|
Additional arguments passed to fetch() |
{}
|
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
Filtered and/or aggregated data. If prop_groupby is specified without |
Series | DataFrame
|
prop_groupby_agg, returns a DataFrameGroupBy object. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the flag has no linked model flag in the flag index |
Example
Common analysis patterns::
>>> # Filter generators to only solar, sum by carrier
>>> solar_gen = dataset.fetch_filter_groupby_agg(
... 'generators_t.p',
... model_filter_query="carrier == 'solar'",
... prop_groupby='carrier',
... prop_groupby_agg='sum'
... )
>>> # Group all generation by carrier and bus
>>> by_carrier_bus = dataset.fetch_filter_groupby_agg(
... 'generators_t.p',
... prop_groupby=['carrier', 'bus'],
... prop_groupby_agg='sum'
... )
>>> # Filter to large generators only
>>> large_gens = dataset.fetch_filter_groupby_agg(
... 'generators_t.p',
... model_filter_query="p_nom >= 500"
... )
See Also
fetch()- Basic data retrieval- Pandas Utils - Underlying filter/group utilities
Source code in submodules/mesqual/mesqual/datasets/dataset.py
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 | |
get_flag_type
classmethod
¶
get_flag_type() -> Type[FlagType]
Get the flag type class for this dataset type.
Returns the type used for data flags in this dataset class. Subclasses can override to specify a custom flag type for type checking and validation.
Returns:
| Type | Description |
|---|---|
Type[FlagType]
|
Type[FlagType]: The flag type class (default: FlagTypeProtocol) |
Note
Override in subclasses that use custom flag types.
Source code in submodules/mesqual/mesqual/datasets/dataset.py
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 | |
get_flag_index_type
classmethod
¶
get_flag_index_type() -> Type[FlagIndexType]
Get the flag index type class for this dataset type.
Returns the type used for the flag index in this dataset class. Subclasses can override to specify a custom flag index implementation.
Returns:
| Type | Description |
|---|---|
Type[FlagIndexType]
|
Type[FlagIndexType]: The flag index type class (default: FlagIndex) |
Note
Override in subclasses that use platform-specific flag indices.
Source code in submodules/mesqual/mesqual/datasets/dataset.py
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 | |
get_config_type
classmethod
¶
get_config_type() -> Type[DatasetConfigType]
Get the configuration type class for this dataset type.
Returns the DatasetConfig subclass used by this dataset. Platform interfaces typically override this to return their extended config class with platform-specific options.
Returns:
| Type | Description |
|---|---|
Type[DatasetConfigType]
|
Type[DatasetConfigType]: The config type class (default: DatasetConfig) |
Example
Creating a config instance for this dataset type::
>>> ConfigClass = MyDataset.get_config_type()
>>> config = ConfigClass(use_database=True)
Note
Override in platform dataset subclasses to return platform-specific config types with additional options.
Source code in submodules/mesqual/mesqual/datasets/dataset.py
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 | |
set_instance_config
¶
set_instance_config(config: DatasetConfigType) -> None
Replace the instance-level configuration for this dataset.
Sets the configuration that will be merged with class-level defaults to produce the effective configuration used by fetch().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DatasetConfigType
|
New configuration object to use for this instance |
required |
Example
Setting a custom configuration::
>>> from mesqual.datasets import DatasetConfig
>>> config = DatasetConfig(use_database=False, auto_sort_datetime_index=False)
>>> dataset.set_instance_config(config)
See Also
instance_config- Get the effective configurationset_instance_config_kwargs()- Update individual settingsset_class_config()- Set class-level defaults
Source code in submodules/mesqual/mesqual/datasets/dataset.py
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 | |
set_instance_config_kwargs
¶
set_instance_config_kwargs(**kwargs) -> None
Update individual configuration settings on this instance.
Modifies specific attributes of the existing instance configuration without replacing the entire config object. Useful for tweaking individual settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Configuration attribute names and values to set |
{}
|
Example
Adjusting specific settings::
>>> dataset.set_instance_config_kwargs(
... use_database=True,
... auto_sort_datetime_index=False
... )
Warning
Raises AttributeError if the config attribute doesn't exist.
See Also
set_instance_config()- Replace entire configurationinstance_config- Get the effective configuration
Source code in submodules/mesqual/mesqual/datasets/dataset.py
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 | |
set_class_config
classmethod
¶
set_class_config(config: DatasetConfigType) -> None
Set the class-level configuration for all instances of this dataset type.
Class-level configuration serves as the default for all instances of this class. Instance-level configuration (set via set_instance_config) can override these defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DatasetConfigType
|
Configuration object to use as class-level defaults |
required |
Example
Setting defaults for all instances::
>>> from mesqual.datasets import DatasetConfig
>>> config = DatasetConfig(use_database=True)
>>> MyDataset.set_class_config(config)
>>>
>>> # All new instances will use database by default
>>> ds1 = MyDataset() # uses database
>>> ds2 = MyDataset() # uses database
Note
This affects all instances of the class, including existing ones that haven't overridden the setting at instance level.
See Also
set_instance_config()- Override for specific instances- DatasetConfigManager - Configuration management system
Source code in submodules/mesqual/mesqual/datasets/dataset.py
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 | |
DatasetCollection
¶
Bases: Generic[DatasetType, DatasetConfigType, FlagType, FlagIndexType], Dataset[DatasetConfigType, FlagType, FlagIndexType], ABC
Abstract base class for collections of datasets.
DatasetCollection extends the Dataset interface to handle multiple child datasets while maintaining the same unified API. This enables complex hierarchical structures where collections themselves can be treated as datasets.
Key Features
- Inherits all Dataset functionality
- Manages collections of child datasets
- Provides iteration and access methods
- Aggregates accepted flags from all children
- Supports KPI operations across all sub-datasets
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
DatasetType
|
Type of datasets that can be collected |
required | |
DatasetConfigType
|
Configuration class for dataset behavior |
required | |
FlagType
|
Type used for data flag identification |
required | |
FlagIndexType
|
Flag index implementation for flag mapping |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
datasets |
list[DatasetType]
|
List of child datasets in this collection |
Note
This class follows the "Everything is a Dataset" principle, allowing collections to be used anywhere a Dataset is expected.
Source code in submodules/mesqual/mesqual/datasets/dataset_collection.py
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 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 | |
get_merged_kpi_collection
¶
get_merged_kpi_collection(deep: bool = True) -> KPICollection
Merge KPI collections from all child datasets.
This method collects KPIs from all child datasets' kpi_collection properties and returns a unified collection. Optionally recurses into nested DatasetCollections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
If True, recursively merge from nested DatasetCollections |
True
|
Returns:
| Type | Description |
|---|---|
KPICollection
|
KPICollection containing all KPIs from all child datasets |
Example:
>>> # Create KPIs for all scenarios
>>> study.scen: DatasetConcatCollection
>>> study.scen.add_kpis_from_definitions_to_all_child_datasets(kpi_defs)
>>>
>>> # Get merged collection across all scenarios
>>> all_kpis = study.scen.get_merged_kpi_collection()
>>>
>>> # Filter and export
>>> mean_prices = all_kpis.filter_by(aggregation=Aggregations.Mean)
>>> df = mean_prices.to_dataframe(unit_handling='auto_convert')
Source code in submodules/mesqual/mesqual/datasets/dataset_collection.py
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 | |
fetch_merged
¶
fetch_merged(flag: FlagType, config: dict | DatasetConfigType = None, keep_first: bool = True, **kwargs) -> Series | DataFrame
Fetch method that merges dataframes from all child datasets, similar to DatasetMergeCollection.
Source code in submodules/mesqual/mesqual/datasets/dataset_collection.py
203 204 205 206 207 208 209 210 211 212 | |
DatasetLinkCollection
¶
Bases: Generic[DatasetType, DatasetConfigType, FlagType, FlagIndexType], DatasetCollection[DatasetType, DatasetConfigType, FlagType, FlagIndexType]
Links specialized flag interpreters into a unified platform dataset interface.
DatasetLinkCollection is the foundation for modular platform dataset architectures. It orchestrates multiple specialized interpreter datasets, each handling a specific subset of flags, and automatically routes fetch requests to the appropriate interpreter. This is NOT used for linking scenarios (use DatasetConcatCollection for that), but for linking interpreters within a single scenario/platform.
Architecture Pattern
Platform datasets (PyPSADataset, PlexosDataset, etc.) are typically implemented as DatasetLinkCollections containing specialized interpreters:
-
Core Platform Interpreters: Handle standard platform data
- ModelInterpreter: Static model data (generators, buses, lines, etc.)
- TimeSeriesInterpreter: Time-varying data (generators_t.p, buses_t.marginal_price)
- ObjectiveInterpreter: Optimization objective values
- ConstraintInterpreters: Shadow prices, binding constraints
-
Study-Specific Interpreters: Extend or override platform behavior
- Custom variable interpreters: Derived metrics specific to the study
- Correction interpreters: Override platform data with corrections
- Integration interpreters: Combine external data sources
Key Features
- Automatic Flag Routing: Fetches are routed to the interpreter that accepts the flag
- Bidirectional Relationships: Each interpreter can access siblings via parent_dataset
- Separation of Concerns: Each interpreter specializes in one aspect of the data
- Study Extensibility: Add custom interpreters without modifying platform code
- First-Match Routing: First interpreter accepting a flag handles it
- Overlap Detection: Warns if multiple interpreters accept the same flag
Routing Logic
- User calls
platform_dataset.fetch('some_flag') - DatasetLinkCollection iterates through child interpreters in order
- Returns data from first interpreter where
interpreter.flag_is_accepted('some_flag') - If no interpreter accepts the flag, raises KeyError
Interpreter Communication
Interpreters access sibling data through the parent_dataset property:
self.parent_dataset.fetch('other_flag')- Fetch from any siblingself.parent_dataset.get_dataset_by_type(InterpreterClass)- Access specific siblingself.parent_dataset.attributes- Access shared dataset attributes
Example
Building a platform dataset with modular interpreters:
>>> # Standard platform dataset structure
>>> class PyPSADataset(DatasetLinkCollection):
... def __init__(self, network, name=None):
... interpreters = [
... PyPSAModelInterpreter(network), # Handles: 'generators', 'buses', 'lines'
... PyPSATimeSeriesInterpreter(network), # Handles: 'generators_t.p', 'buses_t.marginal_price'
... PyPSAObjectiveInterpreter(network), # Handles: 'objective', 'total_cost'
... ]
... super().__init__(datasets=interpreters, name=name)
...
... # Set bidirectional parent-child links
... for interpreter in interpreters:
... interpreter.parent_dataset = self
>>>
>>> # Usage: transparent routing to correct interpreter
>>> dataset = PyPSADataset(network, name='base_case')
>>> buses = dataset.fetch('buses') # -> PyPSAModelInterpreter
>>> gen_p = dataset.fetch('generators_t.p') # -> PyPSATimeSeriesInterpreter
>>> cost = dataset.fetch('objective') # -> PyPSAObjectiveInterpreter
Study-specific extension with custom interpreter:
>>> # Study extends platform dataset with custom variables
>>> class StudyDataset(PyPSADataset):
... def __init__(self, network, name=None):
... super().__init__(network, name)
...
... # Add study-specific interpreter for derived metrics
... custom_interpreter = RESGenerationInterpreter()
... custom_interpreter.parent_dataset = self
... self.add_dataset(custom_interpreter)
...
>>> # Custom interpreter accesses platform interpreters via parent
>>> class RESGenerationInterpreter(Dataset):
... @property
... def accepted_flags(self):
... return {'generators_t.res_generation_total'}
...
... def _fetch(self, flag, config, **kwargs):
... # Access sibling interpreters through parent
... gen_p = self.parent_dataset.fetch('generators_t.p') # From TimeSeriesInterpreter
... gen_model = self.parent_dataset.fetch('generators') # From ModelInterpreter
...
... # Calculate derived metric
... res_gens = gen_model[gen_model['carrier'].isin(['solar', 'wind'])]
... return gen_p[res_gens.index].sum(axis=1)
Study-specific override of platform variable:
>>> # Study corrects platform data for specific scenarios
>>> class CorrectedLineFlowsInterpreter(Dataset):
... '''Override platform line flows with corrected external data.'''
...
... @property
... def accepted_flags(self):
... return {'Line.flow_net'} # Same flag as platform interpreter
...
... def _fetch(self, flag, config, **kwargs):
... # This interpreter is added BEFORE the previous platform interpreter,
... # so it gets priority due to first-match routing
...
... # Get original platform data from sibling
... platform_interpreter = self.parent_dataset.get_dataset_by_type(
... PlatformLineFlowInterpreter
... )
... flows = platform_interpreter.fetch(flag, config, **kwargs)
...
... # Apply corrections for historical scenarios
... if self.parent_dataset.attributes.get('replace_line_flow_with_custom_data'):
... flows = self._replace_line_flow_with_custom_data(flows)
...
... return flows
...
>>> # Add correction interpreter FIRST to override platform behavior
>>> study_dataset = StudyDataset(network)
>>> study_dataset.datasets.insert(0, CorrectedLineFlowsInterpreter())
Warning
If multiple child interpreters accept the same flag, only the FIRST one in the datasets list will handle it. The constructor logs warnings for such overlaps. This can be intentional (override pattern) or accidental.
To override a flag, add the overriding interpreter BEFORE the original interpreter in the datasets list.
See Also
Dataset.parent_dataset- Property that child interpreters use to access parentDatasetConcatCollection- For linking multiple scenarios (different use case)get_dataset_by_type()- Method to access specific child interpreter by type
Source code in submodules/mesqual/mesqual/datasets/dataset_collection.py
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 | |
get_dataset_by_type
¶
get_dataset_by_type(ds_type: type[Dataset]) -> DatasetType
Returns instance of child dataset that matches the ds_type.
Source code in submodules/mesqual/mesqual/datasets/dataset_collection.py
409 410 411 412 413 414 | |
DatasetMergeCollection
¶
Bases: Generic[DatasetType, DatasetConfigType, FlagType, FlagIndexType], DatasetCollection[DatasetType, DatasetConfigType, FlagType, FlagIndexType]
Fetch method will merge fragmented Datasets for same flag, e.g.: - fragmented simulation runs, e.g. CW1, CW2, CW3, CWn. - fragmented data sources, e.g. mapping from Excel file with model from simulation platform.
Source code in submodules/mesqual/mesqual/datasets/dataset_collection.py
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 | |
DatasetConcatCollection
¶
Bases: Generic[DatasetType, DatasetConfigType, FlagType, FlagIndexType], DatasetCollection[DatasetType, DatasetConfigType, FlagType, FlagIndexType]
Concatenates data from multiple datasets with MultiIndex structure.
DatasetConcatCollection is fundamental to MESQUAL's multi-scenario analysis capabilities. It fetches the same flag from multiple child datasets and concatenates the results into a single DataFrame/Series with an additional index level identifying the source dataset.
Key Features
- Automatic MultiIndex creation with dataset names
- Configurable concatenation axis and level positioning
- Preserves all dimensional relationships
- Supports scenario and comparison collections
- Enables unified analysis across multiple datasets
MultiIndex Structure
The resulting data structure includes an additional index level (typically named 'dataset') that identifies the source dataset for each data point.
Example:
>>> # Collection of scenario datasets
>>> scenarios = DatasetConcatCollection([
... PyPSADataset(base_network, name='base'),
... PyPSADataset(high_res_network, name='high_res'),
... PyPSADataset(low_gas_network, name='low_gas')
... ])
>>>
>>> # Fetch creates MultiIndex DataFrame
>>> prices = scenarios.fetch('buses_t.marginal_price')
>>> print(prices.columns.names)
['dataset', 'Bus'] # Original Bus index + dataset level
>>>
>>> # Access specific scenario data
>>> base_prices = prices['base']
>>>
>>> # Analyze across scenarios
>>> mean_prices = prices.mean() # Mean across all scenarios
Source code in submodules/mesqual/mesqual/datasets/dataset_collection.py
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 580 581 582 583 584 585 | |
DatasetComparison
¶
Bases: Generic[DatasetType, DatasetConfigType, FlagType, FlagIndexType], DatasetCollection[DatasetType, DatasetConfigType, FlagType, FlagIndexType]
Computes and provides access to differences between two datasets.
DatasetComparison is a core component of MESQUAL's scenario comparison capabilities. It automatically calculates deltas, ratios, or side-by-side comparisons between a variation dataset and a reference dataset, enabling systematic analysis of scenario differences.
Key Features
- Automatic delta computation between datasets
- Multiple comparison types (DELTA, VARIATION, BOTH)
- Handles numeric and non-numeric data appropriately
- Preserves data structure and index relationships
- Configurable unchanged value handling
- Inherits full Dataset interface
Comparison Types
- DELTA: Variation - Reference (default)
- VARIATION: Returns variation data with optional NaN for unchanged values
- BOTH: Side-by-side variation and reference data
Attributes:
| Name | Type | Description |
|---|---|---|
variation_dataset |
The dataset representing the scenario being compared |
|
reference_dataset |
The dataset representing the baseline for comparison |
Example:
>>> # Compare high renewable scenario to base case
>>> comparison = DatasetComparison(
... variation_dataset=high_res_dataset,
... reference_dataset=base_dataset
... )
>>>
>>> # Get price differences
>>> price_deltas = comparison.fetch('buses_t.marginal_price')
>>>
>>> # Get both datasets side-by-side (often used to show model changes)
>>> price_both = comparison.fetch('buses', comparison_type=ComparisonTypeEnum.BOTH)
>>>
>>> # Highlight only changes (often used to show model changes)
>>> price_changes = comparison.fetch('buses', replace_unchanged_values_by_nan=True)
Source code in submodules/mesqual/mesqual/datasets/dataset_comparison.py
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 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 | |
fetch
¶
fetch(flag: FlagType, config: dict | DatasetConfigType = None, comparison_type: ComparisonTypeEnum = DELTA, replace_unchanged_values_by_nan: bool = False, fill_value: float | int | None = None, **kwargs) -> Series | DataFrame
Fetch comparison data between variation and reference datasets.
Extends the base Dataset.fetch() method with comparison-specific parameters for controlling how the comparison is computed and formatted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
Data identifier flag to fetch from both datasets |
required |
config
|
dict | DatasetConfigType
|
Optional configuration overrides |
None
|
comparison_type
|
ComparisonTypeEnum
|
How to compare the datasets: - DELTA: variation - reference (default) - VARIATION: variation data only, optionally with NaN for unchanged - BOTH: concatenated variation and reference data |
DELTA
|
replace_unchanged_values_by_nan
|
bool
|
If True, replaces values that are identical between datasets with NaN (useful for highlighting changes) |
False
|
fill_value
|
float | int | None
|
Value to use for missing data in subtraction operations |
None
|
**kwargs
|
Additional arguments passed to child dataset fetch methods |
{}
|
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
DataFrame or Series with comparison results |
Example:
>>> # Basic delta comparison
>>> deltas = comparison.fetch('buses_t.marginal_price')
>>>
>>> # Highlight only changed values
>>> changes_only = comparison.fetch(
... 'buses_t.marginal_price',
... replace_unchanged_values_by_nan=True
... )
>>>
>>> # Side-by-side comparison
>>> both = comparison.fetch(
... 'buses_t.marginal_price',
... comparison_type=ComparisonTypeEnum.BOTH
... )
Source code in submodules/mesqual/mesqual/datasets/dataset_comparison.py
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 | |
PlatformDataset
¶
Bases: Generic[DatasetType, DatasetConfigType, FlagType, FlagIndexType], DatasetLinkCollection[DatasetType, DatasetConfigType, FlagType, FlagIndexType], ABC
Base class for platform-specific datasets with automatic interpreter management.
PlatformDataset provides the foundation for integrating MESQUAL with specific energy modeling platforms (PyPSA, PLEXOS, etc.). It manages a registry of data interpreters and automatically instantiates them to handle different types of platform data.
Key Features
- Automatic interpreter registration and instantiation
- Type-safe interpreter management through generics
- Flexible argument passing to interpreter constructors
- Support for study-specific interpreter extensions
- Unified data access through DatasetLinkCollection routing
Architecture
- Uses DatasetLinkCollection for automatic flag routing
- Manages interpreter registry at class level
- Auto-instantiates all registered interpreters on construction
- Supports inheritance and interpreter registration on subclasses
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
DatasetType
|
Base type for all interpreters (must be Dataset subclass) |
required | |
DatasetConfigType
|
Configuration class for dataset behavior |
required | |
FlagType
|
Type used for data flag identification |
required | |
FlagIndexType
|
Flag index implementation for flag mapping |
required |
Class Attributes
_interpreter_registry: List of registered interpreter classes
Usage Pattern
- Create platform dataset class inheriting from PlatformDataset
- Define get_child_dataset_type() to specify interpreter base class
- Create interpreter classes inheriting from the base interpreter
- Register interpreters using @PlatformDataset.register_interpreter
- Instantiate platform dataset - interpreters are auto-created
Example:
>>> # Define platform dataset
>>> class PyPSADataset(PlatformDataset[PyPSAInterpreter, ...]):
... @classmethod
... def get_child_dataset_type(cls):
... return PyPSAInterpreter
...
>>> # Register core interpreters
>>> @PyPSADataset.register_interpreter
... class PyPSAModelInterpreter(PyPSAInterpreter):
... @property
... def accepted_flags(self):
... return {'buses', 'generators', 'lines'}
...
>>> @PyPSADataset.register_interpreter
... class PyPSATimeSeriesInterpreter(PyPSAInterpreter):
... @property
... def accepted_flags(self):
... return {'buses_t.marginal_price', 'generators_t.p'}
...
>>> # Register study-specific interpreter
>>> @PyPSADataset.register_interpreter
... class CustomVariableInterpreter(PyPSAInterpreter):
... @property
... def accepted_flags(self):
... return {'custom_metric'}
...
>>> # Use platform dataset
>>> dataset = PyPSADataset(network=my_network)
>>> buses = dataset.fetch('buses') # Routes to ModelInterpreter
>>> prices = dataset.fetch('buses_t.marginal_price') # Routes to TimeSeriesInterpreter
>>> custom = dataset.fetch('custom_metric') # Routes to CustomVariableInterpreter
Notes
- Interpreters are registered at class level and shared across instances
- Registration order affects routing (last registered = first checked)
- All registered interpreters are instantiated for each platform dataset
- Constructor arguments are automatically extracted and passed to interpreters
Source code in submodules/mesqual/mesqual/datasets/platform_dataset.py
43 44 45 46 47 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 | |
register_interpreter
classmethod
¶
register_interpreter(interpreter: Type[DatasetType]) -> Type['DatasetType']
Register a data interpreter class with this platform dataset.
This method is typically used as a decorator to register interpreter classes that handle specific types of platform data. Registered interpreters are automatically instantiated when the platform dataset is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interpreter
|
Type[DatasetType]
|
Interpreter class that must inherit from get_child_dataset_type() |
required |
Returns:
| Type | Description |
|---|---|
Type['DatasetType']
|
The interpreter class (unchanged) to support decorator usage |
Raises:
| Type | Description |
|---|---|
TypeError
|
If interpreter doesn't inherit from the required base class |
Example:
>>> @PyPSADataset.register_interpreter
... class CustomInterpreter(PyPSAInterpreter):
... @property
... def accepted_flags(self):
... return {'custom_flag'}
...
... def _fetch(self, flag, config, **kwargs):
... return compute_custom_data()
Source code in submodules/mesqual/mesqual/datasets/platform_dataset.py
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 | |
DatasetConfig
dataclass
¶
Base configuration class for controlling Dataset behavior.
DatasetConfig provides common configuration options that apply to all datasets in the MESQUAL framework. Platform-specific and study-specific configurations should extend this class to add additional options.
The configuration system uses a merge-based hierarchy where each level can
override settings from the previous level. The :meth:merge method combines
configurations, with later values taking precedence over earlier ones.
Attributes:
| Name | Type | Description |
|---|---|---|
use_database |
bool
|
If True, enables database caching for expensive fetch operations. When a database is configured on the dataset, fetched data will be cached and retrieved from cache on subsequent calls. Set to False to bypass caching. Default: True. |
auto_sort_datetime_index |
bool
|
If True, automatically sorts the returned DataFrame/Series by its DatetimeIndex after fetching. This ensures time-series data is always in chronological order regardless of the source data ordering. Default: True. |
remove_duplicate_indices |
bool
|
If True, automatically removes duplicate index entries from fetched data, keeping the first occurrence. A warning is logged when duplicates are found. This protects against data quality issues in source data. Default: True. |
Example
Creating a custom configuration::
>>> config = DatasetConfig(use_database=False)
>>> dataset = MyDataset(config=config)
Extending for platform-specific options::
>>> @dataclass
... class MyPlatformConfig(DatasetConfig):
... custom_option: bool = True
... date_filter: list = None
Source code in submodules/mesqual/mesqual/datasets/dataset_config.py
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 | |
merge
¶
merge(other: Optional[DatasetConfigType | dict]) -> DatasetConfigType
Merge this configuration with another, returning a new combined config.
Creates a new configuration instance that combines settings from both
configurations. Values from other override values from self,
but only for non-None values. This allows partial overrides where you
only specify the settings you want to change.
The merge creates a new instance of the same type as self, ensuring
that subclass-specific attributes are preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Optional[DatasetConfigType | dict]
|
Configuration to merge with. Can be: - None: Returns self unchanged - dict: Keys map to config attribute names - DatasetConfig: Another config instance (same or subclass) |
required |
Returns:
| Type | Description |
|---|---|
DatasetConfigType
|
A new configuration instance combining both configs. The return |
DatasetConfigType
|
type matches the type of |
Example:
>>> base = DatasetConfig(use_database=True, auto_sort_datetime_index=True)
>>> override = DatasetConfig(use_database=False)
>>> merged = base.merge(override)
>>> merged.use_database
False
>>> merged.auto_sort_datetime_index # Preserved from base
True
Using a dict for quick overrides:
>>> merged = base.merge({'use_database': False})
Source code in submodules/mesqual/mesqual/datasets/dataset_config.py
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 | |
__repr__
¶
__repr__() -> str
Return a string representation showing all config attributes.
Source code in submodules/mesqual/mesqual/datasets/dataset_config.py
193 194 195 196 197 198 199 200 | |