-
Notifications
You must be signed in to change notification settings - Fork 892
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: Refactor Matplotlib plotting #2430
Conversation
for more information, see https://pre-commit.ci
this has become outdated with adding space.agents
for more information, see https://pre-commit.ci
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.
Lot's of interesting stuff in here!
This makes what is there better, cleaner, and complete for all spaces without changing the user-facing API.
Nice!
A debatable choice made here is that agent_portrayal must return keys that are valid for ax.scatter. That is 'c', 's' and 'marker'. I am not sure about this.
I would for now support the "old" values with a simple translation dict {"color": "c, "Layer", "zorder", ...}
. Ideally we shouldn't have to modify our examples.
I also changed the behavior of orthogonal grids a bit. The current code adds an offset to the x and y coordinates, while the grid lines are drawn on integer values. Instead, I change the view limits and draw the grid lines on 0.5, 1.5, etc. I believe this better reflects the desired behavior (i.e., the agent on the coordinate (1,1) is shown centered (1, 1) while the grid lines nicely go around it.
This is a difficult one. It will help maintaining smaller differences between continuous and discrete spaces (since 1, 1 is now identical to 1.0, 1.0), but it might be a bit inconvenient with grids, cell agents, PropertyLayers, etc. Have you checked those now correctly align?
Hexgrids were not supported before. There is a draft implementation
Really awesome!
Thanks for the feedback!
So "Layer" and "Zorder" are already not supported in the current code, and so also not supported here. This is in fact one of the reasons why I am included to raise a value error for any element in the agent portrayal dict that is not supported. Errors should not be passed over in silence. Not sure about the dict. With 3.0, we can break backward compatability. I prefer a clear choice over maintaining 2 different labels for each valid element in the dict. I personally like the clear one-to-one relation between arguments for
I have tested this but not for property layers. I want to take a closer look at those anyway also because of hexgrids. |
I added some first tests. These only make sure that the plotting code (so the Some quick updates:
|
for more information, see https://pre-commit.ci
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.
This PR is remarkably clean for it's size and scope, excellent work. Few minor comments, but they are really nitpicks.
I especially like how the arguments in the dict are now handled and how everything is documented. Thanks!
OrthogonalGrid = SingleGrid | MultiGrid | OrthogonalMooreGrid | OrthogonalVonNeumannGrid | ||
HexGrid = HexSingleGrid | HexMultiGrid | mesa.experimental.cell_space.HexGrid | ||
Network = NetworkGrid | mesa.experimental.cell_space.Network |
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.
This seems something we want to maintain throughout Mesa (like a space hierarchy). Can we put this on a more central place?
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.
This is just typing information, not sure how that could be centralized.
|
||
def draw_property_layers(ax, space, propertylayer_portrayal, model): | ||
|
||
def draw_property_layers( |
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.
Note to self: Make singular draw_property_layer
function, which returns a single axes. Multiple axes with one (semi-transparent) propertylayer each, and optionally a space, can than be combined into a single figure.
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.
Not sure I understand the idea
Co-authored-by: Ewout ter Hoeven <[email protected]>
With #2430 merged, the visual of wolf sheep changed a bit. This updates the png on the landing page to be consistent again with the example.
This refactors the existing matplotlib plotting code in line with #2401. Note that this refactor is useful regardless of how #2389 is resolved. This makes what is there better, cleaner, and complete for all spaces without changing the user-facing API. The return of
agent_potrayal
is now the same for all spaces because they all plot agents with matplotlib's scatter.In short, it is built around dedicated plotting functions for different types of spaces while abstracting away the difference between old-style and new-style discrete spaces. These plotting functions return an axes instance, making it easier to test and use each plotting function separately from Solara.
Any field returned by agent_portrayal that is not used results in an a user warning.
I also changed the behavior of orthogonal grids a bit. The current code adds an offset to the x and y coordinates, while the grid lines are drawn on integer values. Instead, I change the view limits and draw the grid lines on 0.5, 1.5, etc. I believe this better reflects the desired behavior (i.e., the agent on the coordinate (1,1) is shown centered (1, 1) while the grid lines nicely go around it.
Hexgrids were not supported before. This is now added although property layers are not yet shown correctly. All other stuff works.
Network plotting is refactored substantially. Users can specify which layout algorithm to use. The network edges are optional and drawn analogously to the grid in other discrete spaces.
Tests for the individual plotting functions have been added. These only check if the code runs, not if the plots are correct. This has been checked visually.