utils.py
Collect package helper functions.
add_grid_lines(buses, statistic)
Add a column with gridlines to a statistic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buses
|
pandas.DataFrame
|
The Bus component data frame from a pypsa network. |
required |
statistic
|
pandas.Series
|
A pandas object with a multiindex. There must be a "bus0" and a "bus1" multiindex level, that hold the node names. |
required |
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
A data frame with an additional "line" column that holds x/y coordinate pairs between the respective bus0 and bus1 locations. |
Source code in evals/utils.py
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 | |
align_edge_directions(df, lvl0='bus0', lvl1='bus1')
Align the directionality of edges between two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
The input data frame with a multiindex. |
required |
lvl0
|
str
|
The first MultiIndex level name to swap values. |
'bus0'
|
lvl1
|
str
|
The second MultiIndex level name to swap values. |
'bus1'
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
The input data frame with aligned edge directions between the nodes in lvl1 and lvl0. |
Source code in evals/utils.py
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 | |
apply_cutoff(df, limit, drop=True)
Replace small absolute values with NaN.
The limit boundary is not inclusive, i.e. the limit value itself will not be replaced by NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
The data frame to remove values from. |
required |
limit
|
float
|
Absolute values smaller than the limit will be dropped. |
required |
drop
|
bool
|
Whether to drop all NaN rows from the returned data frame. |
True
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
A data frame without values that are smaller than the limit. |
Source code in evals/utils.py
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 | |
build_plot_config(global_cfg)
Build a plot configuration namespace from the TOML global config dict.
All values are read directly from global_cfg without fallback defaults.
If a required key is missing, a :class:KeyError is raised immediately so
misconfigurations surface loudly rather than silently producing incorrect
output.
Complex values that cannot be expressed in TOML (chart class references,
colour/pattern dicts, empty per-view dicts) are set here using Python
constants. View-specific overrides (plotby, pivot_index, etc.) are
applied in the individual view functions after the namespace is constructed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_cfg
|
dict
|
The |
required |
Returns:
| Type | Description |
|---|---|
types.SimpleNamespace
|
A :class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a required key is absent from global_cfg. |
Source code in evals/utils.py
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 | |
calculate_input_share(df, bus_carrier, apply_scaling=True)
Calculate the withdrawal necessary to supply energy for requested bus_carrier.
Each technology's demand rows are weighted by the output share that lands
on the requested bus_carrier. An optional input/output scaling step
converts those input-side magnitudes into the equivalent output-side
magnitudes; see apply_scaling below.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame | pandas.Series
|
The input DataFrame or Series with a MultiIndex. |
required |
bus_carrier
|
str | list
|
Calculates the input energy for this bus_carrier. |
required |
apply_scaling
|
bool
|
Whether to rescale each demand row by the technology's
|
True
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame | pandas.Series
|
The withdrawal amounts necessary to produce energy of |
Source code in evals/utils.py
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 | |
combine_statistics(statistics, metric_name, is_unit, to_unit, keep_regions=('AT', 'GB', 'ES', 'FR', 'DE', 'IT'), region_nice_names=True)
Build the metric data frame from statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statistics
|
list
|
The statistics to combine. |
required |
metric_name
|
str
|
The metric name used in plot titles and column labels. |
required |
is_unit
|
str
|
The common unit of input statistics. |
required |
to_unit
|
str
|
The desired unit of the output metric. |
required |
keep_regions
|
tuple
|
A collection of country codes for which original input cluster codes will be included in the metric locations. |
('AT', 'GB', 'ES', 'FR', 'DE', 'IT')
|
region_nice_names
|
bool
|
Whether to replace location country codes with country/region names. |
True
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
The formatted metric in the desired unit and locations. |
Source code in evals/utils.py
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 | |
custom_sort(df, by, values, ascending=False)
Sort a data frame by the first appearance in values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
The dataframe to sort. |
required |
by
|
str
|
The column name to find values in. |
required |
values
|
tuple
|
The values to sort by. The order in this collection defines the sort result. |
required |
ascending
|
bool
|
Whether to reverse the result (Plotly inserts legend items from top down). |
False
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
The sorted data frame. |
Source code in evals/utils.py
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 | |
drop_from_multtindex_by_regex(df, pattern, level=DataModel.CARRIER)
Drop all rows that match the regex in the index level.
This function is needed, because pandas.DataFrame.filter cannot be applied to MultiIndexes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
The input data frame with a multi index. |
required |
pattern
|
str
|
The regular expression pattern as a raw string. |
required |
level
|
str
|
The multi index level to match the regex to. |
evals.constants.DataModel.CARRIER
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame | pandas.Series
|
The input data where the regular expression does not match. |
Source code in evals/utils.py
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 | |
filter_by(df, exclude=False, **kwargs)
Filter a data frame by key value pairs.
Constructs a pandas query using the pandas.Index.isin() method. Since the pandas query API is only available for data frames, any passed pandas Series is converted to frame and reset to series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame | pandas.Series
|
The data frame or Series to filter. |
required |
exclude
|
bool
|
Set to True to exclude the filter result from the original data set, and return the difference. |
False
|
**kwargs
|
object
|
Key=value pairs, used in the filter expression. Valid keys are index level names or column labels. |
{}
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame | pandas.Series
|
The filtered data frame in the same format as the input dataframe. |
Source code in evals/utils.py
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 | |
filter_for_carrier_connected_to(df, bus_carrier)
Return a subset with technologies connected to a bus carrier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
The input DataFrame or Series with a MultiIndex. |
required |
bus_carrier
|
str | list
|
The bus carrier to filter for. |
required |
Returns:
| Type | Description |
|---|---|
|
A subset of the input data that contains all location + carrier combinations that have at least one connection to the requested bus_carrier. |
Source code in evals/utils.py
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 | |
get_energy_totals_domestic_share(energy_totals, kind)
Return the domestic share of energy totals for a given kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy_totals
|
pandas.DataFrame
|
The energy totals data frame filtered to one energy year. |
required |
kind
|
str
|
The kind of energy totals to calculate the factor for. |
required |
Returns:
| Type | Description |
|---|---|
pandas.Series
|
The share of national aviation or navigation per country. |
Source code in evals/utils.py
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 | |
get_heat_loss_factor(nc)
Return the heat loss factor for district heating from the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nc
|
pypsa.NetworkCollection
|
The loaded networks. |
required |
Returns:
| Type | Description |
|---|---|
The heat loss factor for district heating networks.
|
|
Source code in evals/utils.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | |
get_latest_results_folder()
Find the results folder with the latest file system timestamp.
Source code in evals/utils.py
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 | |
get_location_alias(locations)
Return the location alias mapping depending on the clustering.
Constructs a mapping dictionary from location codes to human-readable names based on the detected clustering configuration. Automatically detects DE5/16 and AT10/35 clustering levels by counting the number of regional locations in the index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
locations
|
pandas.Index
|
Index containing location codes (e.g., 'DE1', 'AT211', 'EU'). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary mapping location codes to human-readable names. Includes country, region, and clustering-specific aliases. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of DE or AT regions doesn't match expected clustering configurations (DE5/16 or AT10/35). |
Source code in evals/utils.py
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 | |
get_storage_carriers(nc)
Get the storage carriers from the networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nc
|
pypsa.NetworkCollection
|
The loaded networks. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of storage carrier names. |
Source code in evals/utils.py
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | |
get_trade_type(bus_a, bus_b)
Determine the trade type between two buses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bus_a
|
str
|
1st string that should start with a region substring. |
required |
bus_b
|
str
|
2nd string that should start with a region substring. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The trade type. One of constants.TRADE_TYPES. |
Source code in evals/utils.py
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 | |
get_transmission_techs(nc, bus_carrier=None)
Get the transmission technologies from the networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nc
|
pypsa.NetworkCollection
|
The loaded networks. |
required |
bus_carrier
|
str | list
|
The bus carrier to filter for. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of transmission technology names. |
Source code in evals/utils.py
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 | |
get_unit(s, ignore_suffix=True)
Parse the unit from a string.
The unit must be inside round parentheses. If multiple parenthesis are found in the input string, returns the last one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The input string that should contain a unit. |
required |
ignore_suffix
|
bool
|
Whether to strip the suffix, e.g. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
All characters inside the last pair of parenthesis without the enclosing parenthesis, or an empty string. |
Source code in evals/utils.py
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 | |
insert_index_level(df, value, index_name, axis=0, pos=0)
Add an index level to the data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame | pandas.Series
|
The data frame that will receive the new outer level index. |
required |
value
|
str
|
The new index values. |
required |
index_name
|
str
|
The new index level name. |
required |
axis
|
optional
|
The index axis. Pass 0 for row index and 1 for column index. |
0
|
pos
|
optional
|
Move the new index name to this position. 0 is outer left, 1 is the second, and so on. |
0
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame | pandas.Series
|
The data frame with the new index level. |
Source code in evals/utils.py
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 | |
prettify_number(x)
Format a float for display on trace hover actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The imprecise value to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The formatted number as a string with 1 or 0 decimal places, depending on the magnitude of the input value. |
Source code in evals/utils.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
regionalize_statistics(supply, demand, bus_carrier)
Calculate regional balances for specific carriers.
Computes regional import/export balances by comparing supply and demand for specific bus carriers (e.g., oil, coal, lignite, NH3) across locations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
supply
|
pandas.Series
|
Supply statistics series. |
required |
demand
|
pandas.DataFrame
|
Demand statistics series. |
required |
bus_carrier
|
str | list
|
Bus carrier name(s) to analyze for regional trade. |
required |
Returns:
| Type | Description |
|---|---|
pandas.Series
|
List containing regional import and export series. Imports are negative balances (deficit), exports are positive (surplus). |
Source code in evals/utils.py
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 | |
rename_aggregate(df, mapper, level=DataModel.CARRIER, agg='sum')
Rename index values and aggregate duplicates.
In case the supplied mapper is a string, all values in the supplied level are replaced by this string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame | pandas.Series
|
The input data frame. |
required |
mapper
|
dict | str
|
A Dictionary with key-value pairs to rename index values, or a string used to replace all values in the given level. |
required |
level
|
str
|
The index level name. |
evals.constants.DataModel.CARRIER
|
agg
|
str
|
The aggregation method for duplicated index values after renaming. |
'sum'
|
Returns:
| Type | Description |
|---|---|
pandas.Series | pandas.DataFrame
|
A data frame with renamed index values and aggregated values. |
Notes
Support for column axis mapping was removed, because the groupby operation along axis=1 removes column level names and does not work correctly.
Source code in evals/utils.py
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 | |
scale(df, to_unit)
Scale metric values to the specified target unit.
Multiplies all columns in the metric by a scaling factor. The scaling factor is calculated from the unit in the data frame columns and the given target unit. Also updates the unit names encoded in the data frame columns for time aggregated metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame
|
The input data frame with valid units in the column labels. |
required |
to_unit
|
str
|
The target unit. See constants.UNITS for possible units. |
required |
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
The scaled data frame with replaced units in column labels. |
Raises:
| Type | Description |
|---|---|
raises KeyError
|
If the 'to_unit' is not found in UNITS, or if the attrs dictionary has no unit field. |
raises ValueError
|
If input units are inconsistent, i.e. mixed power and energy columns. |
Source code in evals/utils.py
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 | |
split_location_carrier(index, names)
Split location and carrier in the index.
The location must be encoded in the string and match the regex '^[A-Z]{2}\d\s\d'. Subsequent characters become the carrier name. The location defaults to an emtpy string if the regex does not match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
pandas.MultiIndex
|
A pandas Multiindex with the innermost level to split. |
required |
names
|
list
|
The list of output Multiindex names. |
required |
Returns:
| Type | Description |
|---|---|
pandas.MultiIndex
|
The resulting Multiindex with one additional level due to the splitting. |
Source code in evals/utils.py
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 | |
split_urban_central_heat_losses_and_consumption(df, heat_loss)
Split urban heat amounts by a heat loss factor.
Amounts for urban central heat contain distribution losses. However, the evaluation shows final demands in the results. Therefore, heat network distribution losses need to be separated from the total amounts because grid distribution losses do not arrive at the metering endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pandas.DataFrame | pandas.Series
|
The input data frame with values for urban central heat technologies. |
required |
heat_loss
|
int
|
The heat loss factor from the configuration file. |
required |
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
The data frame with split heat amounts for end user demand (urban dentral heat), distribution grid losses (urban dentral heat losses) and anything else from the input data frame (not urban central heat). |
Source code in evals/utils.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 | |
trade_mask(comp, scopes, buses=('bus0', 'bus1'))
Get the mask for a given trade type.
The logic only compares bus0 and bus1 in a given component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comp
|
pandas.DataFrame
|
The component data frame. Should be one a branch_component, i.e. 'Line', 'Link', or 'Transformer'. |
required |
scopes
|
str | tuple
|
The trade scope(s) to match. One or multiple of 'local', 'domestic', 'foreign'. |
required |
buses
|
tuple
|
Two buses to determine the trade type from. The trade type will be 'local', 'domestic', or 'foreign', for same location, same country code, or different country code, respectively. |
('bus0', 'bus1')
|
Returns:
| Type | Description |
|---|---|
pandas.Series
|
A pandas Series with the same index as component index and 1 or 0 as values for match or differ, respectively. |
Raises:
| Type | Description |
|---|---|
ValueError
|
In case the passed trade type is not supported and to prevent unintended string matches. |
Source code in evals/utils.py
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 | |