-
Notifications
You must be signed in to change notification settings - Fork 1k
Updating Examples and Docs inline with the new API. #2819
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
Performance benchmarks:
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
WalkthroughThis update refactors several Mesa example applications and tutorials to adopt the new Changes
Sequence Diagram(s)sequenceDiagram
participant Model
participant SpaceRenderer
participant AgentPortrayalStyle
participant SolaraViz
Model->>SpaceRenderer: Initialize with model and backend
SpaceRenderer->>AgentPortrayalStyle: Use for agent portrayal
SpaceRenderer->>SpaceRenderer: Render grid and agents
SpaceRenderer->>SolaraViz: Pass renderer as component
SolaraViz->>SpaceRenderer: Display visualization
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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: 1
🔭 Outside diff range comments (1)
mesa/examples/basic/schelling/app.py (1)
18-44
: Fix invalid marker usage causing pipeline failure.The matplotlib backend doesn't support image file paths as markers. The pipeline failure confirms this: "ValueError caused by unrecognized marker style './resources/orange_happy.png'".
Replace image file paths with valid matplotlib markers:
def agent_portrayal(agent): style = AgentPortrayalStyle( x=agent.cell.coordinate[0], y=agent.cell.coordinate[1], - marker="./resources/orange_happy.png", + marker="o", # Use circle marker size=75, ) if agent.type == 0: if agent.happy: style.update( - ("marker", "./resources/blue_happy.png"), + ("marker", "s"), # Use square marker ) else: style.update( - ("marker", "./resources/blue_unhappy.png"), + ("marker", "^"), # Use triangle marker ("size", 50), ("zorder", 2), ) else: if not agent.happy: style.update( - ("marker", "./resources/orange_unhappy.png"), + ("marker", "v"), # Use inverted triangle marker ("size", 50), ("zorder", 2), ) return style
🧹 Nitpick comments (2)
mesa/examples/basic/schelling/app.py (1)
60-67
: Update comments to reflect actual implementation.The comments mention "images as markers" but the code uses matplotlib marker symbols, not actual image files.
-# Note: Models with images as markers are very performance intensive. +# Note: The renderer provides a unified interface for drawing agents and grid structure. # Here we use renderer.render() to render the agents and grid in one go. # This function always renders the grid and then renders the agents or # property layers on top of it if specified. It also supports passing the # post_process function to fine-tune the plot after rendering in itself.mesa/examples/basic/boid_flockers/app.py (1)
20-34
: Optimize marker application in style update.The marker caching approach is excellent for performance. However, line 33 redundantly reapplies the same marker that was already set.
if neighbors >= 2: - boid_style.update(("color", "green"), ("marker", MARKER_CACHE[rounded_deg])) + boid_style.update(("color", "green"))The marker is already set during initial style creation, so it doesn't need to be reapplied when only the color changes.
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
mesa/examples/basic/boid_flockers/app.py
(3 hunks)mesa/examples/basic/boltzmann_wealth_model/app.py
(3 hunks)mesa/examples/basic/conways_game_of_life/app.py
(3 hunks)mesa/examples/basic/schelling/app.py
(2 hunks)mesa/examples/basic/virus_on_network/app.py
(2 hunks)
🧠 Learnings (5)
📓 Common learnings
Learnt from: Sahil-Chhoker
PR: projectmesa/mesa#2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/schelling/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/boltzmann_wealth_model/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/boid_flockers/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/virus_on_network/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
🧬 Code Graph Analysis (2)
mesa/examples/basic/conways_game_of_life/app.py (2)
mesa/visualization/space_renderer.py (1)
SpaceRenderer
(44-394)mesa/visualization/components/portrayal_components.py (1)
AgentPortrayalStyle
(18-73)
mesa/examples/basic/boltzmann_wealth_model/app.py (2)
mesa/visualization/space_renderer.py (1)
SpaceRenderer
(44-394)mesa/visualization/components/portrayal_components.py (1)
AgentPortrayalStyle
(18-73)
🪛 GitHub Actions: build
mesa/examples/basic/schelling/app.py
[error] 67-67: Test failure in test_schelling_model: ValueError caused by unrecognized marker style './resources/orange_happy.png' in matplotlib scatter plot marker setting.
mesa/examples/basic/boltzmann_wealth_model/app.py
[error] 54-54: Test failure in test_boltzmann_model: ValueError due to invalid RGBA argument np.int64(1) in matplotlib color handling during agent rendering.
🧰 Additional context used
🧠 Learnings (5)
📓 Common learnings
Learnt from: Sahil-Chhoker
PR: projectmesa/mesa#2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/schelling/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/boltzmann_wealth_model/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/boid_flockers/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
mesa/examples/basic/virus_on_network/app.py (1)
Learnt from: Sahil-Chhoker
PR: #2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
🧬 Code Graph Analysis (2)
mesa/examples/basic/conways_game_of_life/app.py (2)
mesa/visualization/space_renderer.py (1)
SpaceRenderer
(44-394)mesa/visualization/components/portrayal_components.py (1)
AgentPortrayalStyle
(18-73)
mesa/examples/basic/boltzmann_wealth_model/app.py (2)
mesa/visualization/space_renderer.py (1)
SpaceRenderer
(44-394)mesa/visualization/components/portrayal_components.py (1)
AgentPortrayalStyle
(18-73)
🪛 GitHub Actions: build
mesa/examples/basic/schelling/app.py
[error] 67-67: Test failure in test_schelling_model: ValueError caused by unrecognized marker style './resources/orange_happy.png' in matplotlib scatter plot marker setting.
mesa/examples/basic/boltzmann_wealth_model/app.py
[error] 54-54: Test failure in test_boltzmann_model: ValueError due to invalid RGBA argument np.int64(1) in matplotlib color handling during agent rendering.
🔇 Additional comments (15)
mesa/examples/basic/schelling/app.py (2)
7-7
: Import change looks good.The addition of
SpaceRenderer
import aligns with the new visualization API.
71-79
: SolaraViz configuration updated correctly.The direct usage of the renderer object instead of the old component approach is consistent with the new API.
mesa/examples/basic/conways_game_of_life/app.py (4)
4-6
: Import changes align with new API.The replacement of old components with
SpaceRenderer
andAgentPortrayalStyle
is consistent with the modernization effort.
9-14
: Agent portrayal correctly implemented.The function properly returns
AgentPortrayalStyle
with valid matplotlib markers and appropriate styling based on agent state.
58-66
: Renderer setup is well-implemented.The explicit use of
draw_agents()
followed bypost_process
assignment provides clear control over the rendering pipeline. The detailed comments effectively explain the renderer's capabilities.
73-78
: SolaraViz configuration follows new API correctly.The direct renderer usage aligns with the modernized visualization approach.
mesa/examples/basic/boltzmann_wealth_model/app.py (3)
5-5
: Import changes support new visualization API.The addition of
SpaceRenderer
andAgentPortrayalStyle
aligns with the modernization effort across examples.Also applies to: 8-8
44-55
: Renderer setup is well-documented and structured.The explicit rendering steps with colormap configuration provide clear control over the visualization pipeline. The comprehensive comments effectively explain the process.
70-76
: SolaraViz configuration updated correctly.The combination of the renderer with additional plot components demonstrates proper usage of the new API.
mesa/examples/basic/virus_on_network/app.py (4)
13-13
: Import changes support Altair backend usage.The imports align with the new visualization API and demonstrate cross-backend compatibility.
Also applies to: 16-16
19-25
: Agent portrayal correctly updated for new API.The color scheme simplification and size increase improve readability while maintaining proper state-based visualization.
96-108
: Post-processing function properly adapted for Altair.The chart styling and legend configuration demonstrate proper Altair usage for visualization enhancement.
111-131
: Altair backend implementation is well-integrated.The consistent use of Altair across the renderer and plot components with matching color schemes demonstrates proper cross-component coordination.
mesa/examples/basic/boid_flockers/app.py (2)
9-10
: Import changes align with new visualization API.The consolidated import and addition of
AgentPortrayalStyle
support the modernized rendering approach.
75-83
: Renderer setup demonstrates concise API usage.The method chaining approach effectively combines renderer creation and rendering in a single line while maintaining readability.
@tpike3, can you please also take a look at this PR. |
@Sahil-Chhoker I would not worry about the However, the goal is to provide users with examples that show the wide range of possibilities with your new front end. As an example, could you make half of them altair backends. This will also help, in some small way, the LLM issue overtime as it will help generate via users more and diverse examples of current Mesa for its probabilistic generation. In addition for the maintainers, the examples are the most effective way to show them what you did and how you upgraded the front end. Remember, for repeated users of Mesa big changes can be really annoying, because they are learning the new syntax while trying to build their models (which is what they are most concerned about). The examples and tutorial are critical to reduce that pain and sell them on why we decided to pursue this upgrade. Short version, really leverage the examples to show off the new front end capabilities and provide easy references for users, as this is the best way to communicate to the Mesa community the visualization module's new functionality. |
@tpike3, I think I'm done with the basic examples, they show the fastest way to setup visualization and every possible customization except the property layers which can be achieved through advanced examples. Please go through them and tell me if you find anything concerning. |
@Sahil-Chhoker Just went through the tutorials--- Those are great! I would absolutely add a property layer tutorial here I think that would be hugely helpful. Appreciating the the Model may not lend itself to property layer attributes, I would say there is two paths. 1 - In 6 add a overview of property layer and reference an example or 2 - just make up some aspect of property layer for Boltzmann wealth just to demo the capability that is only used for that tutorial. If you go with path 2 - I would probably put it as 7 then bump everything else up 1. Just make sure you change the binder and colab hyperlinks as required. By Monday, I will really try and put the examples through their paces, so we can identify if there are any problems like we have in existing examples as noted in #2822 |
I think I'll make a separate tutorial for the property layer, so your points are noted!
Thanks, please do! |
@tpike3, I am not sure how binder works, do we have a binder link for every tutorial or a common one for every tutorial? |
@coderabbitai full review |
✅ Actions performedFull review triggered. |
Performance benchmarks:
|
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
♻️ Duplicate comments (4)
mesa/examples/basic/conways_game_of_life/app.py (1)
1-6
: Add CLI compatibility imports as previously requested.The past review comment requested adding CLI compatibility imports that are still missing.
Apply this diff to add the missing imports:
+import os +import sys + +sys.path.insert(0, os.path.abspath("../../../..")) + from mesa.examples.basic.conways_game_of_life.model import ConwaysGameOfLifemesa/examples/basic/schelling/app.py (1)
1-2
: Add CLI compatibility imports as previously requested.The past review comment requested CLI compatibility imports that are still missing.
Apply this diff to add the missing imports:
import os +import sys + +sys.path.insert(0, os.path.abspath("../../../.."))docs/tutorials/6_visualization_rendering_with_space_renderer.ipynb (2)
16-17
: LGTM! Binder link correctly configured.The Binder badge and URL are properly set up for this tutorial, matching the format requested in past review comments.
227-247
: LGTM! Comprehensive explanation of interactive controls.The documentation thoroughly explains the Play Interval Slider, Render Interval Slider, and Use Threads Checkbox, addressing the feedback from past review comments about explaining what each control does.
🧹 Nitpick comments (2)
docs/tutorials/7_visualization_propertylayer_visualization.ipynb (1)
170-179
: Remove useless expression from the code cell.The semicolon at the end of line 179 creates a useless expression as flagged by static analysis.
-g.set(title="Wealth distribution", xlabel="Wealth", ylabel="number of agents"); +g.set(title="Wealth distribution", xlabel="Wealth", ylabel="number of agents")docs/tutorials/6_visualization_rendering_with_space_renderer.ipynb (1)
161-171
: Remove useless expression from the code cell.Similar to the other notebook, the semicolon creates a useless expression as flagged by static analysis.
-g.set(title="Wealth distribution", xlabel="Wealth", ylabel="number of agents"); +g.set(title="Wealth distribution", xlabel="Wealth", ylabel="number of agents")
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (18)
docs/tutorials/4_visualization_basic.ipynb
(8 hunks)docs/tutorials/5_visualization_dynamic_agents.ipynb
(9 hunks)docs/tutorials/6_visualization_rendering_with_space_renderer.ipynb
(1 hunks)docs/tutorials/7_visualization_propertylayer_visualization.ipynb
(1 hunks)docs/tutorials/8_visualization_custom.ipynb
(1 hunks)docs/tutorials/9_batch_run.ipynb
(1 hunks)mesa/examples/advanced/epstein_civil_violence/app.py
(3 hunks)mesa/examples/advanced/pd_grid/app.py
(2 hunks)mesa/examples/advanced/sugarscape_g1mt/app.py
(3 hunks)mesa/examples/advanced/wolf_sheep/app.py
(2 hunks)mesa/examples/basic/boid_flockers/app.py
(3 hunks)mesa/examples/basic/boltzmann_wealth_model/app.py
(2 hunks)mesa/examples/basic/conways_game_of_life/app.py
(3 hunks)mesa/examples/basic/schelling/app.py
(3 hunks)mesa/examples/basic/virus_on_network/app.py
(2 hunks)mesa/visualization/backends/altair_backend.py
(0 hunks)mesa/visualization/backends/matplotlib_backend.py
(1 hunks)mesa/visualization/space_renderer.py
(2 hunks)
💤 Files with no reviewable changes (1)
- mesa/visualization/backends/altair_backend.py
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: Sahil-Chhoker
PR: projectmesa/mesa#2819
File: mesa/examples/basic/boltzmann_wealth_model/app.py:13-15
Timestamp: 2025-07-23T04:03:11.386Z
Learning: PR #2818 "Allowing color mapping to integers in AgentPortrayalStyle" fixes integer color mapping issues in AgentPortrayalStyle by modifying the matplotlib backend's collect_agent_data function to properly handle integer color values with colormaps, enabling patterns like AgentPortrayalStyle(color=agent.wealth) to work correctly.
📚 Learning: pr #2818 "allowing color mapping to integers in agentportrayalstyle" fixes integer color mapping iss...
Learnt from: Sahil-Chhoker
PR: projectmesa/mesa#2819
File: mesa/examples/basic/boltzmann_wealth_model/app.py:13-15
Timestamp: 2025-07-23T04:03:11.386Z
Learning: PR #2818 "Allowing color mapping to integers in AgentPortrayalStyle" fixes integer color mapping issues in AgentPortrayalStyle by modifying the matplotlib backend's collect_agent_data function to properly handle integer color values with colormaps, enabling patterns like AgentPortrayalStyle(color=agent.wealth) to work correctly.
Applied to files:
mesa/examples/advanced/pd_grid/app.py
mesa/examples/basic/boid_flockers/app.py
mesa/examples/basic/conways_game_of_life/app.py
mesa/visualization/backends/matplotlib_backend.py
mesa/examples/advanced/sugarscape_g1mt/app.py
mesa/examples/basic/schelling/app.py
mesa/examples/basic/virus_on_network/app.py
docs/tutorials/7_visualization_propertylayer_visualization.ipynb
mesa/examples/advanced/wolf_sheep/app.py
mesa/examples/advanced/epstein_civil_violence/app.py
mesa/examples/basic/boltzmann_wealth_model/app.py
docs/tutorials/5_visualization_dynamic_agents.ipynb
docs/tutorials/4_visualization_basic.ipynb
📚 Learning: in mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer ...
Learnt from: Sahil-Chhoker
PR: projectmesa/mesa#2803
File: mesa/visualization/backends/altair_backend.py:312-553
Timestamp: 2025-06-20T16:44:20.677Z
Learning: In mesa/visualization/backends/altair_backend.py, the author prefers to keep the draw_propertylayer method intact despite its complexity, as they believe breaking it into smaller functions would divert attention from the main logic flow. The method is well-documented and follows a clear sequence of operations for property layer rendering.
Applied to files:
mesa/examples/advanced/pd_grid/app.py
mesa/examples/basic/boid_flockers/app.py
mesa/visualization/backends/matplotlib_backend.py
mesa/examples/advanced/sugarscape_g1mt/app.py
mesa/examples/basic/virus_on_network/app.py
docs/tutorials/7_visualization_propertylayer_visualization.ipynb
mesa/examples/advanced/epstein_civil_violence/app.py
mesa/examples/basic/boltzmann_wealth_model/app.py
docs/tutorials/5_visualization_dynamic_agents.ipynb
docs/tutorials/4_visualization_basic.ipynb
docs/tutorials/6_visualization_rendering_with_space_renderer.ipynb
🪛 Ruff (0.12.2)
docs/tutorials/7_visualization_propertylayer_visualization.ipynb
205-205: Found useless expression. Either assign it to a variable or remove it.
(B018)
docs/tutorials/6_visualization_rendering_with_space_renderer.ipynb
215-215: Found useless expression. Either assign it to a variable or remove it.
(B018)
⏰ Context from checks skipped due to timeout of 180000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: run-benchmarks
🔇 Additional comments (57)
docs/tutorials/8_visualization_custom.ipynb (1)
350-350
: LGTM! Tutorial link correctly updated.The link update to tutorial 9 is appropriate and maintains navigation consistency.
docs/tutorials/9_batch_run.ipynb (1)
383-383
: LGTM! Tutorial link correctly updated.The link update to tutorial 10 maintains proper navigation flow in the tutorial sequence.
mesa/visualization/backends/matplotlib_backend.py (1)
216-216
: LGTM! Alpha value retention improves argument handling.The change from
pop("alpha")
to direct access preserves the alpha value in the arguments dictionary for downstream use in scatter plot creation, which improves consistency with the visualization API updates.mesa/visualization/space_renderer.py (2)
7-7
: LGTM! Import added for error suppression.The
contextlib
import is appropriately added to support the IndexError handling in network coordinate mapping.
156-160
: LGTM! Race condition handling for network coordinates.The use of
contextlib.suppress(IndexError)
appropriately handles the race condition where coordinate indices update before position data is ready. The FIXME comment correctly indicates this is a temporary solution that should be improved in the future.mesa/examples/advanced/epstein_civil_violence/app.py (3)
10-13
: LGTM! Proper imports for new visualization API.The imports correctly bring in
SpaceRenderer
andAgentPortrayalStyle
for the modernized visualization approach.
67-74
: LGTM! Excellent migration to SpaceRenderer API.The refactoring properly:
- Creates a
SpaceRenderer
instance with matplotlib backend- Configures agent drawing with the updated portrayal function
- Assigns the post-processing function to the renderer
- Updates
SolaraViz
to use the renderer directly instead of component listsThis aligns perfectly with the new visualization API approach.
28-34
: No change needed forAgentPortrayalStyle.update
syntax
Theupdate
method is explicitly implemented to accept one or more(key, value)
tuple arguments for batch property updates and is used consistently throughout the library (see tutorials 5, 6, and 7). The tuple-based API is intentional and correct. You may still assign single properties viaportrayal.color = value
if you prefer, but the existingupdate(("color", value))
calls are valid and require no modification.Likely an incorrect or invalid review comment.
mesa/examples/basic/conways_game_of_life/app.py (2)
10-14
: LGTM! Correct implementation of AgentPortrayalStyle.The agent portrayal function correctly uses the new
AgentPortrayalStyle
class with appropriate properties for Conway's Game of Life visualization.
58-62
: LGTM! Proper SpaceRenderer setup.The
SpaceRenderer
is correctly instantiated and configured for agent-only rendering, which is appropriate for Conway's Game of Life where only cell states matter.mesa/examples/advanced/sugarscape_g1mt/app.py (2)
25-28
: LGTM! Appropriate post-processing for Altair backend.The
post_process
function correctly sets fixed chart dimensions for the Altair visualization, which is essential for consistent display sizing.
57-61
: LGTM! Correct SpaceRenderer implementation with Altair backend.The
SpaceRenderer
is properly configured with the Altair backend and includes both agent and property layer portrayals, maintaining the full visualization capabilities of the original implementation.mesa/examples/basic/schelling/app.py (2)
20-21
: LGTM! Improved path handling for cross-platform compatibility.Using
os.path.dirname
andos.path.abspath
ensures proper resource path resolution regardless of the current working directory.
76-81
: LGTM! Appropriate SpaceRenderer usage with helpful comments.The
SpaceRenderer
setup with matplotlib backend is correct, and the comments about performance and therender()
method functionality are valuable for users.mesa/examples/advanced/pd_grid/app.py (3)
19-21
: LGTM! Clean AgentPortrayalStyle implementation.The agent portrayal function correctly uses
AgentPortrayalStyle
with conditional coloring based on agent cooperation strategy.
43-43
: LGTM! Consistent backend specification.Explicitly specifying
backend="altair"
andgrid=True
for the plot component maintains consistency with the SpaceRenderer backend choice.
47-48
: LGTM! Proper SpaceRenderer setup with Altair backend.The
SpaceRenderer
instantiation with Altair backend and immediaterender()
call correctly implements the new visualization pattern.mesa/examples/advanced/wolf_sheep/app.py (3)
18-22
: LGTM! Good default initialization pattern.Creating the
AgentPortrayalStyle
with sensible defaults (size, marker, zorder) and then updating specific properties based on agent type is a clean approach.
33-33
: LGTM! Efficient multiple property updates.Using
portrayal.update()
with multiple tuple arguments to set marker, size, and zorder simultaneously is an efficient way to customize GrassPatch visualization.
84-89
: LGTM! Correct SpaceRenderer configuration.The
SpaceRenderer
setup with matplotlib backend,draw_agents()
call, andpost_process
assignment correctly implements the new visualization pattern.mesa/examples/basic/virus_on_network/app.py (6)
13-16
: LGTM! Clean import updates for new API.The import changes correctly replace
make_space_component
withSpaceRenderer
and addAgentPortrayalStyle
for the new visualization approach.
19-25
: Good improvement in agent portrayal implementation.The function now returns an
AgentPortrayalStyle
object instead of a dictionary, which is the correct approach for the new API. The color changes (removing "tab:" prefixes) align well with Altair backend requirements, and increasing size from 10 to 20 improves agent visibility.
96-108
: Excellent post-processing function for Altair.The rewritten
post_process_lineplot
function correctly configures Altair chart properties and legend styling instead of manipulating matplotlib axes. The legend styling with stroke, fill, and positioning creates a professional appearance.
111-117
: Great demonstration of explicit rendering workflow.This code excellently demonstrates the new
SpaceRenderer
approach with:
- Clear model instantiation
- Explicit structure drawing with customized node/edge styling
- Separate agent drawing call
- Good use of comments explaining the workflow
This provides a clear example for users migrating to the new API.
120-124
: Consistent backend usage and color scheme.The
StatePlot
correctly uses the Altair backend with colors matching the agent portrayal, and properly integrates the post-processing function.
126-135
: Clean SolaraViz integration.The
SolaraViz
setup correctly uses the renderer instance directly instead of the deprecated component list approach.mesa/examples/basic/boltzmann_wealth_model/app.py (6)
1-2
: Good addition of Altair import.Adding the Altair import is necessary for the post-processing function to configure chart properties.
7-10
: Correct imports for new visualization API.The import changes properly bring in
SpaceRenderer
andAgentPortrayalStyle
for the modernized visualization approach.
15-18
: Excellent use of integer color mapping.The agent portrayal correctly uses
agent.wealth
directly as the color value. This leverages the integer color mapping functionality that was fixed in PR #2818, allowing the colormap to handle the wealth-to-color translation seamlessly.
40-54
: Well-implemented post-processing function.The
post_process
function correctly configures an Altair chart with:
- Proper color encoding using viridis colormap
- Gradient legend with appropriate styling
- Clear domain specification (0-10) for wealth values
The docstring clearly explains the purpose, making it educational for users.
59-70
: Excellent educational comments and implementation.The comments provide clear explanations of:
- SpaceRenderer's role and layer-based approach
- Backend specification
- Grid customization options
- Agent rendering with colormap parameters
This serves as a great example for users learning the new API. The implementation correctly demonstrates the explicit rendering workflow.
75-83
: Good integration and documentation.The
SolaraViz
setup is clean and well-documented. The comment about running withsolara run app.py
is helpful for users. The direct renderer usage follows the new pattern correctly.mesa/examples/basic/boid_flockers/app.py (4)
4-5
: Clean import updates for new API.The imports correctly replace the deprecated components with
SpaceRenderer
andAgentPortrayalStyle
for the modernized visualization approach.
24-29
: Excellent conditional styling implementation.The
boid_draw
function showcases a clean pattern:
- Creates base
AgentPortrayalStyle
with default properties- Uses the
update()
method for conditional modifications based on agent state- Preserves the cached marker optimization for performance
This demonstrates effective use of the new styling system.
72-76
: Great streamlined rendering approach.The chained
SpaceRenderer(...).render(agent_portrayal=boid_draw)
call demonstrates the most concise way to set up visualization with the new API. The comment clearly explains this as the quickest visualization method.
78-83
: Clean SolaraViz integration.The
SolaraViz
setup correctly uses the renderer instance directly, following the new pattern established across the examples.docs/tutorials/4_visualization_basic.ipynb (5)
63-64
: Correct import updates for new visualization API.The imports properly replace
make_space_component
withSpaceRenderer
and addAgentPortrayalStyle
to support the modernized visualization approach.
191-195
: Excellent updated explanation of agent portrayal.The text now correctly describes that portrayal functions return
AgentPortrayalStyle
objects rather than dictionaries. This provides accurate guidance for users learning the new API.
204-205
: Good agent portrayal implementation.The function correctly returns an
AgentPortrayalStyle
object with appropriate color and size parameters, demonstrating the new approach clearly.
252-270
: Comprehensive SpaceRenderer documentation.This new section excellently explains:
- SpaceRenderer's role in visualization
- Backend options (matplotlib/altair)
- Post-processing capabilities
- The render() method usage
- Full-screen viewing tip
This provides valuable educational content for users transitioning to the new API.
281-293
: Clean tutorial implementation.The visualization setup correctly demonstrates:
- SpaceRenderer instantiation with backend specification
- Chained render() call with agent portrayal
- Direct renderer usage in SolaraViz
- Proper component integration
This serves as a clear example for tutorial users.
docs/tutorials/5_visualization_dynamic_agents.ipynb (6)
17-17
: Correct Binder link update.The link now correctly points to the current tutorial (5_visualization_dynamic_agents.ipynb) rather than the previous one.
63-64
: Consistent import updates.The imports correctly use
SpaceRenderer
andAgentPortrayalStyle
to maintain consistency with the new visualization API across all tutorials.
193-203
: Valuable educational content on backend compatibility.This expanded explanation excellently covers:
- Backend-specific shape support (matplotlib vs altair)
- Cross-references to documentation
- Important compatibility warnings
- Limited conversion support between backends
This helps users avoid common pitfalls when working with different rendering backends.
215-219
: Good demonstration of conditional agent styling.The
agent_portrayal
function effectively shows:
- Base
AgentPortrayalStyle
creation with default properties- Conditional styling using the
update()
method- Wealth-based visual differentiation
This builds well on the concepts from the previous tutorial.
274-282
: Consistent tutorial implementation.The visualization setup correctly follows the established pattern with
SpaceRenderer
and maintains consistency with the previous tutorial's approach.
306-306
: Good tutorial progression link.The updated link correctly points to the next tutorial on SpaceRenderer, maintaining proper tutorial flow.
docs/tutorials/7_visualization_propertylayer_visualization.ipynb (6)
16-17
: LGTM! Binder link is correctly formatted.The Binder badge and link are properly configured to launch this specific tutorial.
61-65
: LGTM! Imports align with the new Mesa visualization API.The imports correctly demonstrate the transition to
SpaceRenderer
,AgentPortrayalStyle
, andPropertyLayerStyle
components, replacing the oldermake_space_component
approach.
83-161
: LGTM! Well-structured Boltzmann Wealth Model implementation.The model implementation is clean and properly demonstrates:
- Agent movement and wealth exchange mechanics
- Property layer creation with random test data
- Data collection for Gini coefficient tracking
The addition of the property layer with
np.random.randint(0, 10, size=(width, height))
provides a good foundation for demonstrating property layer visualization.
314-327
: LGTM! Property layer portrayal function demonstrates best practices.The
propertylayer_portrayal
function correctly:
- Uses conditional logic based on
layer.name
for multi-layer support- Returns
PropertyLayerStyle
objects with appropriate styling- Demonstrates key properties like color, alpha, and colorbar
344-378
: LGTM! Comprehensive demonstration of SpaceRenderer capabilities.The code properly demonstrates:
- Individual drawing methods (
draw_structure
,draw_agents
,draw_propertylayer
)- The consolidated
render()
method withspace_kwargs
- Both approaches provide equivalent functionality with different API styles
431-441
: LGTM! SolaraViz integration follows the new visualization patterns.The dashboard setup correctly:
- Uses
SpaceRenderer
instance directly instead of component lists- Includes custom plot components with post-processing
- Provides interactive model parameters
- Demonstrates the modern Mesa visualization approach
docs/tutorials/6_visualization_rendering_with_space_renderer.ipynb (4)
81-152
: LGTM! Standard Boltzmann Wealth Model implementation.The model implementation is consistent with Mesa best practices and provides a solid foundation for demonstrating visualization features.
306-346
: LGTM! Excellent demonstration of backend-specific rendering.The code effectively shows:
- Matplotlib backend with line styling parameters (
lw
,ls
,color
,alpha
)- Altair backend with different parameter names (
grid_width
,grid_dash
, etc.)- Clear distinction between backend-specific approaches
This helps users understand the API differences between backends.
372-414
: LGTM! Clear examples of post-processing for both backends.The post-processing functions demonstrate:
- Matplotlib: Direct axis manipulation with
ax.set_title()
,ax.grid()
, etc.- Altair: Chart property configuration with method chaining
- Backend-specific customization patterns
The warning about backend incompatibility is particularly valuable for users.
464-474
: LGTM! Modern SolaraViz integration pattern.The dashboard setup follows the updated Mesa visualization approach by:
- Passing
SpaceRenderer
instance directly toSolaraViz
- Including custom components with post-processing
- Providing interactive model parameters
This demonstrates the transition from the older component-based approach.
There is a binder link for each tutorial. Essentially, it just goes to the url of your file in your github repo and gives you a server based jupyter environment. https://mybinder.org/ |
Then does just updating the final destination keep the links consistent? |
Yes, updating the hyperlink in the markdown is all you need to do. |
@Sahil-Chhoker -- A couple things going through the examples
You are doing excellent work, and I think this will be the most thoroughly we have have gone through examples with respect to the visualizations. |
Thanks @Sahil-Chhoker I think we have culminated with this PR--- Two relatively quick things:
Then I will merge, On related topics: For the sugarscape issue, I could not consistently recreate the issues, so will need to explore it more later, but my sense it is is about the model not the visual. As you look to the next PR and potentially adding multiple pages, could you take a look at the Updating the below function I can prevent the overlap in sugarscape, but it is also not behaving the way I would hope or necessarily responsive to parameter changes and may break other things.
|
I'm not sure what test is missing, codecov's report kind of confuses me. |
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.
LGTM!
Excellent work thanks @Sahil-Chhoker
The way I understand it is Codecov looks at two things Overall, you increased the number percentage of tests for the repo (to include examples) and you covered some basic functionality of visualization with tests of post-process so it would seem pretty draconian to delay this PR any further. |
Summary
Updating the examples and docs showcasing the usage of the new API.
Motive
Part of my GSoC commitment.
Implementation
Updated the examples to include the basic usage of renderer with comments explaining what the code is actually doing.
Additional Notes
@tpike3, I've updated basic examples showcasing basic functionalities of the renderer, please review them and tell me how can they be improved.
One functionality I've left out is passing the kwargs to
draw_structure
to customize how the grid is drawn, I couldn't find a good place to fit it in, in the basic examples, maybe you can suggest something.Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Refactor