Skip to content
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

viz: Automatically visualize Cell color attributes in cell space #2558

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

EwoutH
Copy link
Member

@EwoutH EwoutH commented Dec 20, 2024

Summary

Adds automatic visualization of a Cell's color attribute in Mesa's matplotlib-based visualization. When a Cell has a color attribute set to any valid matplotlib color, it will now be automatically displayed in the visualization without requiring manual configuration.

Motive

Currently, visualizing properties of Cells in cell spaces requires manual configuration of the visualization code. This can be cumbersome, especially for the common case of wanting to display cells in different colors. By automatically detecting and visualizing the color attribute, we make it much easier for users to create visually informative cell-based models.

The PR aligns with Mesa's philosophy of providing sensible defaults while maintaining flexibility - users can still override the visualization behavior if needed, but get reasonable visualization out of the box.

Implementation

The implementation modifies the draw_orthogonal_grid() and draw_hex_grid() functions to:

  1. Check if the space is a cell space by testing for all_cells attribute
  2. For each cell, check for a color attribute
  3. For orthogonal grids:
    • Create a color matrix initialized as transparent
    • Convert cell colors to RGBA format
    • Display using imshow before drawing agents/grid
  4. For hex grids:
    • Create hexagon patches for each cell
    • Set face colors based on cell colors
    • Use PatchCollection for efficient rendering
  5. Handle invalid colors gracefully with warnings
  6. Preserve proper layering (cells behind agents and grid)

Changes are backward compatible - spaces without cells or cells without colors are rendered exactly as before.

Usage Examples

Simple usage:

class ForestCell(Cell):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.is_burning = False
        self.color = "green"  # Initial color
        
    def step(self):
        if self.is_burning:
            self.color = "red"  # Cell turns red when burning
        elif any(n.is_burning for n in self.neighbors):
            self.is_burning = True

The visualization automatically shows green cells turning red as fire spreads, no additional visualization code needed.

Screenshots comparing visualization:

Before (manually configuring colors):

def draw_cells(model):
    colors = []
    for cell in model.grid.all_cells:
        if cell.is_burning:
            colors.append("red")
        else:
            colors.append("green") 
    # Manual color plotting code...

After (automatic color visualization):

# No manual visualization code needed!
# Just set cell.color and it appears automatically

TODO: [Screenshots]

Additional Notes

  • Colors can be specified in any format matplotlib accepts (names, hex codes, RGB tuples, etc.)
  • Alpha/transparency is supported through RGBA colors or alpha specification
  • Invalid colors produce warnings but don't break visualization
  • Feature adds minimal overhead - color checks only happen for cell spaces
  • Future work could extend this to other cell properties beyond color

Closes #2557

This change adds automatic visualization of Cell color attributes in the Mesa cell space visualization. When a Cell has a color attribute, it will now be automatically rendered in the space visualization without requiring manual configuration.

Key changes:
- Update orthogonal grid drawing to show cell colors using imshow 
- Update hex grid drawing to show cell colors using PatchCollection
- Handle invalid colors gracefully with warnings
- Keep existing agent and grid line visualization behavior
- Add documentation with examples

Colors can be specified using any valid matplotlib color format (names, hex codes, RGB/RGBA tuples). The visualization maintains proper layering with cell colors appearing behind agents and grid lines.

Cell colors are now fully optional - cells without color attributes will be transparent/unfilled just like before.
@EwoutH EwoutH added feature Release notes label visualisation labels Dec 20, 2024
@EwoutH EwoutH marked this pull request as draft December 20, 2024 06:59
Copy link

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -4.5% [-5.5%, -3.3%] 🔵 -1.1% [-1.4%, -0.9%]
BoltzmannWealth large 🔵 -1.4% [-1.8%, -1.1%] 🔵 -0.9% [-1.6%, +0.0%]
Schelling small 🔵 -1.5% [-1.9%, -1.1%] 🔵 +0.1% [+0.0%, +0.2%]
Schelling large 🔵 -8.0% [-17.9%, -1.1%] 🔵 -0.6% [-1.7%, +0.6%]
WolfSheep small 🔵 -0.4% [-0.8%, -0.0%] 🔵 +0.7% [-0.9%, +2.3%]
WolfSheep large 🔵 -0.2% [-0.8%, +0.4%] 🔴 +5.4% [+3.4%, +7.5%]
BoidFlockers small 🔵 +3.5% [+2.9%, +4.1%] 🔵 +1.1% [+0.4%, +1.8%]
BoidFlockers large 🔵 +3.2% [+2.9%, +3.6%] 🔵 +0.8% [-0.0%, +1.6%]

@quaquel
Copy link
Member

quaquel commented Dec 20, 2024

No, see my response in #2557.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Release notes label visualisation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cell space: Automatically visualise color attribute of Cells
2 participants