-
Notifications
You must be signed in to change notification settings - Fork 9
Add color parameter to components and bulk color assignment via topology accessor #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…s.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]
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.
📝 WalkthroughWalkthroughThis PR extends component constructors with an optional color parameter and introduces topology-based color management APIs (set_component_color, set_component_colors, set_carrier_color) to replace the prior colors accessor. It refactors color resolution logic in the plotting pipeline and updates documentation accordingly. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…nctionality:
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
|
coderabbitai review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
flixopt/components.py (1)
1703-1718: Missingcolorparameter inSourceAndSinkfor API consistency.All other component classes (
LinearConverter,Storage,Transmission,Source,Sink) now accept acolorparameter, butSourceAndSinkdoes not. For a consistent API surface, consider adding thecolorparameter here as well.Suggested fix
def __init__( self, label: str, inputs: list[Flow] | None = None, outputs: list[Flow] | None = None, prevent_simultaneous_flow_rates: bool = True, meta_data: dict | None = None, + color: str | None = None, ): super().__init__( label, inputs=inputs, outputs=outputs, prevent_simultaneous_flows=(inputs or []) + (outputs or []) if prevent_simultaneous_flow_rates else None, meta_data=meta_data, + color=color, ) self.prevent_simultaneous_flow_rates = prevent_simultaneous_flow_rates
🧹 Nitpick comments (1)
flixopt/linear_converters.py (1)
615-633: LGTM! Color parameter correctly added to HeatPumpWithSource.Completes the consistent addition of the
colorparameter across all LinearConverter subclasses.Optional: The class docstrings for
Boiler,Power2Heat,HeatPump,CoolingTower,CHP, andHeatPumpWithSourcecould be updated to document the newcolorparameter in their Args sections. However, since the parameter is inherited from the base class and the behavior is documented in the user guide, this is a nice-to-have improvement.
Summary
Add
colorparameter to all component classes and provide bulk color assignment methods viaflow_system.topology.Changes
Added
colorparameter to component classes:LinearConverter,Storage,Transmission,Source,SinkBoiler,Power2Heat,HeatPump,CoolingTower,CHP,HeatPumpWithSourceAdded bulk color assignment methods to
TopologyAccessor:set_component_color(label, color)- single componentset_component_colors(colors, overwrite=True)- multiple componentsset_carrier_color(carrier, color)- carrier colors (affects bus colors)Usage
Type of Change
Testing
Checklist
Summary by CodeRabbit
Release Notes
New Features
colorparameter to component constructors for visualization customization at instantiation.set_component_color(),set_component_colors(), andset_carrier_color()for flexible color management.Documentation
✏️ Tip: You can customize this high-level summary in your review settings.