Commit f766121
authored
Add color parameter to components and bulk color assignment via topology accessor (#585)
* Add color attribute to remaining classes
* 1. Added color parameter to all component classes (flixopt/components.py, flixopt/linear_converters.py):
- LinearConverter, Storage, Transmission, Source, Sink
- Boiler, Power2Heat, HeatPump, CoolingTower, CHP, HeatPumpWithSource
2. Added bulk color assignment methods to TopologyAccessor (flixopt/topology_accessor.py):
# Single component
flow_system.topology.set_component_color('Boiler', '#D35400')
# Multiple components - direct assignment
flow_system.topology.set_component_colors({
'Boiler': '#D35400',
'CHP': 'darkred',
})
# Multiple components - colorscale for groups
flow_system.topology.set_component_colors({
'Oranges': ['Solar1', 'Solar2'],
'Blues': ['Wind1', 'Wind2'],
})
# All components with colorscale
flow_system.topology.set_component_colors('turbo')
# Carrier color (affects bus colors)
flow_system.topology.set_carrier_color('electricity', '#FECB52')
3. Cache invalidation - _invalidate_color_caches() resets cached color dicts when colors change.
4. Updated documentation (docs/user-guide/results-plotting.md) - Fixed the incorrect flow_system.colors references to use flow_system.topology.
Read-only accessors still available:
flow_system.topology.component_colors # dict[str, str]
flow_system.topology.carrier_colors # dict[str, str]
flow_system.topology.bus_colors # dict[str, str]
* The overwrite=False parameter works correctly.
Summary:
# Set colors only for components that don't have one yet
flow_system.topology.set_component_colors('turbo', overwrite=False)
# With dict - skips components that already have colors
flow_system.topology.set_component_colors({
'Boiler': 'red', # Skipped if Boiler already has a color
'CHP': 'blue', # Skipped if CHP already has a color
}, overwrite=False)
Returns only the colors that were actually assigned, so you can see what changed.
* ⏺ Done. Reduced from ~50 lines to ~30 lines while keeping the same functionality:
def set_component_colors(
self,
colors: dict[str, str | list[str]] | str,
overwrite: bool = True,
) -> dict[str, str]:
components = self._fs.components
# Normalize to {label: color} mapping
if isinstance(colors, str):
color_map = process_colors(colors, list(components.keys()))
else:
color_map = {}
for key, value in colors.items():
if isinstance(value, list):
missing = [c for c in value if c not in components]
if missing:
raise KeyError(f"Components not found: {missing}")
color_map.update(process_colors(key, value))
else:
if key not in components:
raise KeyError(f"Component '{key}' not found")
color_map[key] = value
# Apply colors (respecting overwrite flag)
result = {}
for label, color in color_map.items():
if overwrite or components[label].color is None:
components[label].color = color
result[label] = color
self._invalidate_color_caches()
return result
Key improvements:
- Normalize all inputs to a {label: color} dict first
- Single pass to apply colors
- Cleaner error messages
- Shorter docstring with inline examples
* Update CHANGELOG.md
* Add colors to snakey/flows plots
* Add missing color parameter1 parent ca5f54a commit f766121
6 files changed
Lines changed: 172 additions & 21 deletions
File tree
- docs/user-guide
- flixopt
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
246 | 261 | | |
247 | 262 | | |
248 | 263 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
394 | 394 | | |
395 | 395 | | |
396 | 396 | | |
397 | | - | |
| 397 | + | |
398 | 398 | | |
399 | | - | |
| 399 | + | |
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
403 | | - | |
| 403 | + | |
404 | 404 | | |
405 | 405 | | |
406 | 406 | | |
407 | 407 | | |
408 | 408 | | |
409 | 409 | | |
410 | | - | |
411 | | - | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
412 | 423 | | |
413 | | - | |
414 | | - | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
415 | 429 | | |
416 | 430 | | |
417 | 431 | | |
| |||
435 | 449 | | |
436 | 450 | | |
437 | 451 | | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
442 | 455 | | |
443 | 456 | | |
444 | 457 | | |
445 | 458 | | |
446 | 459 | | |
447 | 460 | | |
448 | | - | |
| 461 | + | |
449 | 462 | | |
450 | 463 | | |
451 | 464 | | |
452 | 465 | | |
453 | | - | |
| 466 | + | |
454 | 467 | | |
455 | 468 | | |
456 | 469 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| 175 | + | |
175 | 176 | | |
176 | | - | |
| 177 | + | |
177 | 178 | | |
178 | 179 | | |
179 | 180 | | |
| |||
417 | 418 | | |
418 | 419 | | |
419 | 420 | | |
| 421 | + | |
420 | 422 | | |
421 | 423 | | |
422 | 424 | | |
| |||
425 | 427 | | |
426 | 428 | | |
427 | 429 | | |
| 430 | + | |
428 | 431 | | |
429 | 432 | | |
430 | 433 | | |
| |||
744 | 747 | | |
745 | 748 | | |
746 | 749 | | |
| 750 | + | |
747 | 751 | | |
748 | 752 | | |
749 | 753 | | |
| |||
754 | 758 | | |
755 | 759 | | |
756 | 760 | | |
| 761 | + | |
757 | 762 | | |
758 | 763 | | |
759 | 764 | | |
| |||
1702 | 1707 | | |
1703 | 1708 | | |
1704 | 1709 | | |
| 1710 | + | |
1705 | 1711 | | |
1706 | 1712 | | |
1707 | 1713 | | |
1708 | 1714 | | |
1709 | 1715 | | |
1710 | 1716 | | |
1711 | 1717 | | |
| 1718 | + | |
1712 | 1719 | | |
1713 | 1720 | | |
1714 | 1721 | | |
| |||
1795 | 1802 | | |
1796 | 1803 | | |
1797 | 1804 | | |
| 1805 | + | |
1798 | 1806 | | |
1799 | 1807 | | |
1800 | 1808 | | |
1801 | 1809 | | |
1802 | 1810 | | |
1803 | 1811 | | |
1804 | 1812 | | |
| 1813 | + | |
1805 | 1814 | | |
1806 | 1815 | | |
1807 | 1816 | | |
| |||
1888 | 1897 | | |
1889 | 1898 | | |
1890 | 1899 | | |
| 1900 | + | |
1891 | 1901 | | |
1892 | 1902 | | |
1893 | 1903 | | |
| |||
1897 | 1907 | | |
1898 | 1908 | | |
1899 | 1909 | | |
| 1910 | + | |
1900 | 1911 | | |
1901 | 1912 | | |
1902 | 1913 | | |
| |||
1905 | 1916 | | |
1906 | 1917 | | |
1907 | 1918 | | |
| 1919 | + | |
1908 | 1920 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| 100 | + | |
99 | 101 | | |
100 | 102 | | |
101 | 103 | | |
| |||
176 | 178 | | |
177 | 179 | | |
178 | 180 | | |
| 181 | + | |
179 | 182 | | |
180 | 183 | | |
181 | 184 | | |
| |||
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
| 197 | + | |
194 | 198 | | |
195 | 199 | | |
196 | 200 | | |
| |||
271 | 275 | | |
272 | 276 | | |
273 | 277 | | |
| 278 | + | |
274 | 279 | | |
275 | 280 | | |
276 | 281 | | |
| |||
287 | 292 | | |
288 | 293 | | |
289 | 294 | | |
| 295 | + | |
290 | 296 | | |
291 | 297 | | |
292 | 298 | | |
| |||
368 | 374 | | |
369 | 375 | | |
370 | 376 | | |
| 377 | + | |
371 | 378 | | |
372 | 379 | | |
373 | 380 | | |
| |||
381 | 388 | | |
382 | 389 | | |
383 | 390 | | |
| 391 | + | |
384 | 392 | | |
385 | 393 | | |
386 | 394 | | |
| |||
472 | 480 | | |
473 | 481 | | |
474 | 482 | | |
| 483 | + | |
475 | 484 | | |
476 | 485 | | |
477 | 486 | | |
| |||
492 | 501 | | |
493 | 502 | | |
494 | 503 | | |
| 504 | + | |
495 | 505 | | |
496 | 506 | | |
497 | 507 | | |
| |||
602 | 612 | | |
603 | 613 | | |
604 | 614 | | |
| 615 | + | |
605 | 616 | | |
606 | 617 | | |
607 | 618 | | |
| |||
619 | 630 | | |
620 | 631 | | |
621 | 632 | | |
| 633 | + | |
622 | 634 | | |
623 | 635 | | |
624 | 636 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1051 | 1051 | | |
1052 | 1052 | | |
1053 | 1053 | | |
1054 | | - | |
1055 | | - | |
1056 | | - | |
1057 | | - | |
1058 | | - | |
| 1054 | + | |
| 1055 | + | |
1059 | 1056 | | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
1060 | 1062 | | |
1061 | 1063 | | |
1062 | 1064 | | |
1063 | | - | |
1064 | 1065 | | |
1065 | 1066 | | |
| 1067 | + | |
| 1068 | + | |
1066 | 1069 | | |
1067 | | - | |
1068 | 1070 | | |
1069 | 1071 | | |
1070 | 1072 | | |
| |||
0 commit comments