Folium KPICollection Visualization System¶
KPICollectionMapVisualizer
¶
Draws a KPI collection on a folium map, one feature group per KPI group.
Groups the KPIs with a KPIGroupingManager, creates one feature group per
group and applies all generators to each KPI, for example areas with text
overlays, or lines with arrows. The tooltip can list related KPIs of the
same object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generators
|
FoliumObjectGenerator | List[FoliumObjectGenerator]
|
Single generator or list of generators for visualization |
required |
study_manager
|
StudyManager | None
|
StudyManager for enhanced KPI relationships and tooltips |
None
|
include_related_kpis_in_tooltip
|
bool
|
Add related KPIs to tooltip display |
False
|
kpi_grouping_manager
|
KPIGroupingManager
|
Custom grouping manager (optional) |
None
|
value_formatting
|
VALUE_FORMATTING_OPTIONS
|
Number format of the KPI values in the text overlays
and in the default tooltip and popup ( |
None
|
**kwargs
|
Any
|
Additional arguments passed to data items |
{}
|
Examples:
Areas colored by price, with the price as text, one feature group per scenario:
>>> import folium
>>> from mesqual.kpis import Aggregations, FlagAggKPIBuilder
>>> study.scen.add_kpis_from_definitions_to_all_child_datasets(
... FlagAggKPIBuilder().for_flag('Country.Results.price').with_aggregation(Aggregations.Mean).build()
... )
>>> prices = study.scen.get_merged_kpi_collection()
>>> from mesqual.visualizations.folium_viz_system import (
... AreaFeatureResolver,
... AreaGenerator,
... PropertyMapper,
... TextOverlayGenerator,
... )
>>> from mesqual.visualizations.value_mapping_system import SegmentedContinuousColorscale
>>> colors = SegmentedContinuousColorscale(segments={(30, 60): 'YlOrRd'})
>>> visualizer = KPICollectionMapVisualizer(
... generators=[
... AreaGenerator(AreaFeatureResolver(fill_color=PropertyMapper.from_kpi_value(colors))),
... TextOverlayGenerator(),
... ],
... study_manager=study,
... )
>>> m = folium.Map(location=[48, 9], zoom_start=6)
>>> feature_groups = visualizer.generate_and_add_feature_groups_to_map(prices, m, show='first')
>>> len(feature_groups)
4
>>> m.save('prices.html')
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
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 | |
generate_and_add_feature_groups_to_map
¶
generate_and_add_feature_groups_to_map(kpi_collection: KPICollection, folium_map: Map, show: SHOW_OPTIONS = 'none', overlay: bool = False) -> list[FeatureGroup]
Generate feature groups and add them to map.
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
446 447 448 449 450 451 452 453 454 455 456 457 | |
get_feature_groups
¶
get_feature_groups(kpi_collection: KPICollection, show: SHOW_OPTIONS = 'none', overlay: bool = False) -> list[FeatureGroup]
Create one feature group per KPI group, drawn with all generators.
The KPI groups come from the grouping manager. A generator that raises an error for a KPI is skipped for that KPI, and a warning is logged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpi_collection
|
KPICollection
|
Collection of KPIs to visualize |
required |
show
|
SHOW_OPTIONS
|
Which feature groups to show initially ('first', 'last', 'none') |
'none'
|
overlay
|
bool
|
Whether feature groups should be overlay controls |
False
|
Returns:
| Type | Description |
|---|---|
list[FeatureGroup]
|
List of folium FeatureGroup objects ready to add to map |
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
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 | |
KPIGroupingManager
¶
Groups KPIs into the feature groups of a map.
KPIs with the same attribute values go into one group, apart from the
attributes in kpi_attribute_keys_to_exclude_from_grouping, such as the
object name. The groups are sorted by the attributes in
kpi_attribute_sort_order and by the value order in
kpi_attribute_category_orders. The manager also names the groups and finds
the related KPIs of an object for the tooltips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpi_attribute_category_orders
|
dict[str, list[str]]
|
Custom ordering for specific attribute values |
None
|
kpi_attribute_keys_to_exclude_from_grouping
|
list[str]
|
Attributes to ignore during grouping |
None
|
kpi_attribute_sort_order
|
list[str]
|
Order of attributes for group sorting |
None
|
Examples:
>>> from mesqual.kpis import Aggregations, FlagAggKPIBuilder
>>> study.scen.add_kpis_from_definitions_to_all_child_datasets(
... FlagAggKPIBuilder().for_flag('Country.Results.price').with_aggregation(Aggregations.Mean).build()
... )
>>> prices = study.scen.get_merged_kpi_collection()
>>> manager = KPIGroupingManager(
... kpi_attribute_category_orders={'dataset_name': ['short', 'base', 'high_demand', 'without_N4']},
... )
>>> groups = manager.get_kpi_groups(prices)
>>> [len(group) for group in groups]
[2, 2, 2, 2]
>>> manager.get_feature_group_name(groups[0])
'Mean Country.Results.price short EUR_per_MWh 2024-03-28 2024-03-31 4 1.0 1'
The name joins the values of all attributes that the KPIs of the group have in common, including the dataset attributes (here the dates, days, demand factor and periods of the dataset) and empty values.
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
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 | |
get_kpi_groups
¶
get_kpi_groups(kpi_collection: KPICollection) -> list[KPICollection]
Group KPIs by their attributes, in the configured sort order.
Creates logical groups of KPIs based on their attributes, excluding specified attributes from grouping and applying custom sort orders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpi_collection
|
KPICollection
|
Collection of KPIs to group |
required |
Returns:
| Type | Description |
|---|---|
list[KPICollection]
|
List of KPICollection objects, each representing a logical group |
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
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_feature_group_name
¶
get_feature_group_name(kpi_group: KPICollection) -> str
Return the feature group name of a KPI group.
The name joins, with spaces, the values of the attributes that all KPIs
of the group have in common: first arithmetic_operation,
aggregation, flag, dataset_name and unit, then the other
attributes in their order. The attributes of DEFAULT_EXCLUDE_ATTRIBUTES
and the values None are left out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpi_group
|
KPICollection
|
KPI group to generate name for |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable feature group name |
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
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 | |
get_related_kpi_groups
¶
get_related_kpi_groups(kpi: KPI, study_manager: StudyManager | None) -> dict[str, KPICollection]
Get related KPIs grouped by relationship type.
The candidates are the KPIs of the merged KPI collection of
study_manager.scen_comp with the same object, flag and model flag as
kpi. Each candidate goes into the first group it matches:
- Different Aggregations: same dataset, value comparison and arithmetic operation; another aggregation.
- Different Datasets: same aggregation, value comparison and arithmetic operation; another dataset.
- Different Comparisons / Operations: same dataset and aggregation; another value comparison or arithmetic operation.
Without study_manager, or if kpi has no flag or aggregation, all
groups are empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kpi
|
KPI
|
Source KPI to find relatives for |
required |
study_manager
|
StudyManager | None
|
StudyManager for accessing merged KPI collection |
required |
Returns:
| Type | Description |
|---|---|
dict[str, KPICollection]
|
Dict mapping relationship type to KPICollection of related KPIs |
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
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 | |
KPIQuantityToTextConverterResolver
¶
Resolves the value converter of the KPIs of a feature group.
The strategies:
'per_feature_group': one converter per feature group, for all KPIs of the group.'per_collection': one converter per base unit, for the whole collection.- A
QuantityToTextConverter: this converter for all KPIs. None: aQuantityToTextConverterwith default settings.
The resolver caches the converters per feature group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
VALUE_FORMATTING_OPTIONS
|
Formatting strategy. |
required |
kpi_collection
|
KPICollection
|
The whole KPI collection, for |
required |
Examples:
>>> from mesqual.kpis import Aggregations, FlagAggKPIBuilder
>>> study.scen.add_kpis_from_definitions_to_all_child_datasets(
... FlagAggKPIBuilder().for_flag('Country.Results.price').with_aggregation(Aggregations.Mean).build()
... )
>>> prices = study.scen.get_merged_kpi_collection()
>>> resolver = KPIQuantityToTextConverterResolver('per_feature_group', prices)
>>> converter = resolver.get_converter_for_kpi('Prices', prices)
>>> [converter.convert(kpi.quantity) for kpi in prices][:4]
['42 €/MWh', '46 €/MWh', '53 €/MWh', '59 €/MWh']
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.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 | |
get_converter_for_kpi
¶
get_converter_for_kpi(group_id: str, kpi_group: KPICollection) -> QuantityToTextConverter
Get converter for a specific KPI, using group-level caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_id
|
str
|
Unique identifier for the feature group (used for caching) |
required |
kpi_group
|
KPICollection
|
Full KPI group for building converters |
required |
Returns:
| Type | Description |
|---|---|
QuantityToTextConverter
|
QuantityToTextConverter configured for this KPI |
Source code in src/mesqual/visualizations/folium_viz_system/kpi_collection_map_visualizer.py
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 | |