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

Visualize PropertyLayers #2336

Merged
merged 13 commits into from
Oct 5, 2024
Merged

Visualize PropertyLayers #2336

merged 13 commits into from
Oct 5, 2024

Conversation

EwoutH
Copy link
Member

@EwoutH EwoutH commented Oct 3, 2024

This PR adds support for visualizing PropertyLayers in the Matplotlib-based space visualization component. It allows users to overlay PropertyLayer data on top of the existing grid and agent visualizations, or on its own.

It introduces a new propertylayer_portrayal parameter to customize the appearance of PropertyLayers and refactors the existing space visualization code for better modularity and flexibility.

Motive

The PropertyLayer is becoming a key feature in Mesa, so it should be visualized easily. This feature enables users to easily visualize multiple layers of data simultaneously, such as environmental factors or agent properties, alongside the agents themselves.

Implementation

  • Added a new propertylayer_portrayal parameter to the make_space_matplotlib function.
  • Implemented a draw_property_layers function to render PropertyLayer data using Matplotlib's imshow.
  • Modified the _draw_grid function to incorporate PropertyLayer visualization before drawing agents.
  • Added support for both color-based and colormap-based PropertyLayer rendering.
  • Implemented alpha blending for PropertyLayer visualization to allow overlaying multiple layers.
  • Added colorbar support for PropertyLayer visualization.

Usage Examples

You can visualize multiple PropertyLayers ("grass" and "temperature" in this case) along with agents in a grid space:

def agent_portrayal(agent):
    return {"color": "red", "shape": "o", "size": 20}

propertylayer_portrayal = {
    "grass": {
        "colormap": "Greens",
        "alpha": 1,
    },
    "temperature": {
        "colormap": "coolwarm",
        "alpha": 0.333,
        "vmin": 0,
        "vmax": 100
    },
}

model = ExampleModel(10, 10)
page = SolaraViz(
    model,
    [make_space_matplotlib(agent_portrayal, propertylayer_portrayal)],
)

image

You can also use a single color for a PropertyLayer. Here we render the "cell_layer" PropertyLayer using shades of black, with the highest value having 0% opacity and the lowest value having 100% opacity. For Game of Life that means alive cells are fully black, and dead cells transparent.

propertylayer_portrayal = {
    "cell_layer": {
        "color": "Black",
        "alpha": 1,
        "colorbar": False,
    },
}

image

Any matplotlib color is valid. The highest value will have that color with 0% opacity, the lowest with 100% opacity (which can again be scaled with modifying vmin and vmax).

Additional Notes

  • Currently supported spaces: Grid and standalone PropertyLayers.
    • ContinuousSpace, NetworkGrid, and VoronoiGrid can be added in future PR(s).
  • Future work could include adding support for additional space types and improving the efficiency of rendering for large grids.
  • The PR includes updates to handle different sizes between the PropertyLayer and the grid space, issuing warnings when there's a mismatch.
  • Colorbar display can be toggled using the colorbar parameter in the portrayal dictionary.
  • The coordinate system has been validated to ensure consistent axis directions between PropertyLayers and the grid space.
  • A demo using the Game of Life model has been implemented in a separate PR (GoL_fast: Visualize the Game of Life cells using the PropertyLayer mesa-examples#214) to showcase these new visualization capabilities.

TODO

Closes #2138.

@EwoutH EwoutH requested a review from Corvince October 3, 2024 08:17
@EwoutH EwoutH requested a review from wang-boyu October 3, 2024 08:20

This comment was marked as off-topic.

@EwoutH
Copy link
Member Author

EwoutH commented Oct 3, 2024

This is ready for a first round of review, API wise its done. Any conceptual, feature or API feedback is welcome. If no major things come up, I will finish the tests and such.

The GoL example is also available here: projectmesa/mesa-examples#214

EwoutH and others added 5 commits October 4, 2024 09:03
When adding "colorbar": False to a propertylayer_portrayal, it turns of the colorbar.
visualisation of PropertyLayers the ContiniousGrid and Vonoroid space could be added later
@EwoutH EwoutH added the feature Release notes label label Oct 4, 2024
@EwoutH
Copy link
Member Author

EwoutH commented Oct 4, 2024

Implemented the last features (turning color bars on and off), validated the PropertyLayer coordinate system was visualized on the same axis as the Agent one, and updated the PR description.

image

I have no idea what for tests to add, suggestions are welcome.

Ready for review.

@EwoutH EwoutH marked this pull request as ready for review October 4, 2024 07:46

This comment was marked as off-topic.

@EwoutH
Copy link
Member Author

EwoutH commented Oct 5, 2024

Going to merge this, it breaks nothing and adds a new feature.

Post-merge review still appreciated!

@EwoutH EwoutH merged commit 47b3bd9 into projectmesa:main Oct 5, 2024
10 of 12 checks passed
@rht
Copy link
Contributor

rht commented Oct 5, 2024

Going to merge this, it breaks nothing and adds a new feature.

This is not fine. You are breaking the convention of a review needed into a new feature that is not experimental. This is definitely not fair to other developers who have waited for a long time even for a review, e.g. @wang-boyu with the recent Mesa-Geo stuff, even though he is the lead maintainer on the repo.

@EwoutH
Copy link
Member Author

EwoutH commented Oct 5, 2024

It’s a valid concern. I wanted to include it in the beta, so users could start testing it and delivering feedback, and updating example models (like projectmesa/mesa-examples#214). I thought I had an approval on the API for this PR and #2341 from @quaquel.

@projectmesa/maintainers I would like your perspective on how to handle cases like this, for if they happen in the future.

space = getattr(model, "space", None)

if isinstance(space, mesa.space._Grid):
_draw_grid(space, space_ax, agent_portrayal, propertylayer_portrayal, model)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either of:

  • Why pass an entire model instead of just the layers?
  • Why not use the space argument and access it from the model instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: s/Why not use the space/Why not remove the space/

Copy link
Member Author

@EwoutH EwoutH Oct 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review and the questions, I will try to look into them soon.

Also feel free to open a PR if you already have concrete solutions in mind.

Edit: Yeah I now remind, there are some inconsistencies between the draw functions here. When I add support for other spaces I will try to address these.

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

Successfully merging this pull request may close these issues.

SolaraViz: Visualize PropertyLayers
3 participants