-
Notifications
You must be signed in to change notification settings - Fork 936
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
Fix: Property layer visualization for HexGrid #2646
Fix: Property layer visualization for HexGrid #2646
Conversation
for more information, see https://pre-commit.ci
…Sahil-Chhoker/mesa into property-layer-fix-for-hex-grid
Performance benchmarks:
|
Thanks for this. The example looks great. I am taking a look at the code now. |
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.
Just a few questions and ideas. Looks very good already.
Are you sure it's fine as it is? I had a few questions about it:
|
I gave it a first quick pass and focussed on the high-level stuff. The zorder one is a good question (and actually interacts with #2642). Ideally (but beyond this pr), zorder should become controllable for property layers to explicitly control layering. |
Thank you for the review! I have added the suggested changes. Please let me know if you have any additional recommendations to further improve the code quality. |
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.
looks good, just a few remaining clarifications
I think this is ready to be merged. @Sahil-Chhoker do you have any outstanding doubts? If not, I'll merge this later today. |
Thanks @quaquel, I think I'm fine. |
@quaquel, I ran into a bug while testing this feature again with the
I am very sorry for pushing code without properly testing it. |
can you share a minimum example with the bug? I might be able to help diagnose it. |
Sorry for the late response, here is my example I was testing the code with: from mesa.experimental.cell_space.grid import HexGrid
from mesa import Model
import math
from mesa.experimental.cell_space import CellAgent
from mesa.visualization import SolaraViz, make_space_component
from mesa.experimental.cell_space.property_layer import PropertyLayer
import numpy as np
class myAgent(CellAgent):
def __init__(self, model, cell=None):
super().__init__(model)
self.cell = cell
class myModel(Model):
def __init__(self, seed=None, width=10, height=15 , n_agents=4):
super().__init__(seed=seed)
self.width = width
self.height = height
self.num_agents = n_agents
self.grid = HexGrid(
(self.width, self.height), capacity=math.inf, torus=False, random=self.random
)
self.test_layer = PropertyLayer("test", (self.width, self.height), default_value=1, dtype=int)
self.test_layer.data = np.random.randint(2, 10, (self.width, self.height))
self.grid.add_property_layer(self.test_layer)
myAgent(self, self.grid.all_cells.cells[17])
def portrayal(agent):
if agent is None:
return None
return {
"color": "tab:orange",
"size": 70,
}
def post_process(ax):
ax.set_aspect("equal")
ax.set_xticks([])
ax.set_yticks([])
ax.get_figure().set_size_inches(8, 8)
property_layer_portrayal = {
"test": {
"colormap": "Blues",
"alpha": 0.7,
"vmin": 0,
"vmax": 10,
}
}
model_params = {
"seed": {
"type": "InputText",
"value": 42,
"label": "Random Seed",
},
"n_agents": {
"type": "SliderInt",
"value": 4,
"min": 1,
"max": 10,
"step": 1,
"label": "Number of Agents",
},
"width": 6,
"height": 5,
}
model = myModel()
space_component = make_space_component(
agent_portrayal=portrayal, draw_grid=True, post_process=post_process, propertylayer_portrayal=property_layer_portrayal
)
page = SolaraViz(
model,
components=[space_component],
model_params=model_params,
name="Test Model",
)
page |
@quaquel, While testing the code mentioned above, I discovered another bug: whenever the model's step count is increased, the hex grid simply disappears. After diagnosing the issue, I found that |
What happens if you use a normal cache instant of |
|
I think I know what's going on. |
I thought it was cleaner than storing results in a list and returning them. Function does work as intended. Should I change it into a function instead? |
It depends on what we prefer. In case of a normal function. we can memoize the return allowing us to effectively generate the hexmesh vertices only once. |
Now that I have this knowledge that generators don't go well with caching (thanks to you), I think we should change this to a function. |
Can you put in a new PR which does this? Otherwise, I'll pick up tomorrow. |
Summary
Property layers were not properly visualized for HexGrids, and this PR aims to resolve that issue. It utilizes the same coordinate system as the
draw_hex_grid
function and generates aPolyCollection
that overlays color values on the grid.Bug / Issue
fixes #2433
Before
After
Additional Notes:
I am not entirely sure if this was the best approach to address the issue. If there is a more effective or elegant solution, I would greatly appreciate your recommendations.