-
Notifications
You must be signed in to change notification settings - Fork 951
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
Adds Altair plot component and some other fixes #2644
base: main
Are you sure you want to change the base?
Conversation
Performance benchmarks:
|
Thanks for the PR! A few of the automated checks broke, could you look into what caused that? I will leave the review to others. |
@nissu99 also please look at #2643 and #2641 As you are @sanika-n are working in the same space As it looks like you two are looking over the entire altair implementation I would also recommend looking at #2642 discussion It is always good to collaborate and think together you may address some larger visualization challenges. |
"alt defined"
for more information, see https://pre-commit.ci
@tpike3 When I run the tests locally, they don't fail, but they are failing here. |
I am not sure why your tests are passing locally, however for tests to pass they need to updated. It is expecting
|
"removed a line"
"updated toml"
"removed suppress"
for more information, see https://pre-commit.ci
"doc string "
for more information, see https://pre-commit.ci
"operator bug"
Thank You, the issue got resolved by adding altair to the toml file. |
Removing stuff that has become redundant is fine. |
@quaquel i have added all the stuff,can you review it? |
I'll try to review it over the weekend, but I am rather bussy at the moment. This is a part of the code base I am not intimately familiar with and his been a while since I used Altair, so reviewing will take more time than parts of the code base I know inside out. |
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.
I have done the review of basic rectangular and hex grids, first I would love to get them finished up. Then I also noticed that the implementation of the new Network is also missing. I will do a review again once you make the changes, please take a look at the review and don't hesitate to ask for implementations as well if unable to get them.
def _draw_discrete_grid(space, all_agent_data, agent_portrayal): | ||
"""Create Altair visualization for Discrete Grid.""" | ||
invalid_tooltips = ["color", "size", "x", "y"] | ||
x_y_type = "ordinal" | ||
|
||
x_y_type = "ordinal" if not isinstance(space, ContinuousSpace) else "nominal" | ||
encoding_dict = { | ||
"x": alt.X("x", axis=None, type=x_y_type), | ||
"y": alt.Y("y", axis=None, type=x_y_type), | ||
"tooltip": [ | ||
alt.Tooltip(key, type=alt.utils.infer_vegalite_type_for_pandas([value])) | ||
for key, value in all_agent_data[0].items() | ||
if key not in invalid_tooltips | ||
], | ||
} | ||
|
||
has_color = "color" in all_agent_data[0] | ||
if has_color: | ||
encoding_dict["color"] = alt.Color("color", type="nominal") | ||
has_size = "size" in all_agent_data[0] | ||
if has_size: | ||
encoding_dict["size"] = alt.Size("size", type="quantitative") | ||
|
||
chart = ( | ||
alt.Chart( | ||
alt.Data(values=all_agent_data), encoding=alt.Encoding(**encoding_dict) | ||
) | ||
.mark_point(filled=True) | ||
.properties(width=280, height=280) | ||
) | ||
|
||
if not has_size: | ||
length = min(space.width, space.height) | ||
chart = chart.mark_point(size=30000 / length**2, filled=True) | ||
|
||
chart = chart.encode( | ||
x=alt.X( | ||
"x", axis=None, type=x_y_type, scale=alt.Scale(domain=(0, space.width - 1)) | ||
), | ||
y=alt.Y( | ||
"y", axis=None, type=x_y_type, scale=alt.Scale(domain=(0, space.height - 1)) | ||
), | ||
) | ||
|
||
return chart |
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.
case Grid(): | ||
all_agent_data = _get_agent_data_new_discrete_space(space, agent_portrayal) | ||
return _draw_discrete_grid(space, all_agent_data) | ||
case _Grid(): | ||
all_agent_data = _get_agent_data_old__discrete_space(space, agent_portrayal) | ||
case ContinuousSpace(): | ||
all_agent_data = _get_agent_data_continuous_space(space, agent_portrayal) | ||
return _draw_legacy_grid(space, all_agent_data) | ||
case HexGrid(): | ||
return _draw_hex_grid(space, all_agent_data) |
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 order will never draw the HexGrid
, because the hex grid inherits form the Grid. I think reversing their order will fix it.
case Grid(): | ||
# New DiscreteSpace or experimental cell space | ||
for cell in space.all_cells: | ||
for agent in cell.agents: | ||
data = agent_portrayal(agent) | ||
data.update({"x": cell.coordinate[0], "y": cell.coordinate[1]}) | ||
all_agent_data.append(data) | ||
|
||
Returns: | ||
list of dicts | ||
case _Grid(): | ||
# Legacy Grid | ||
for content, (x, y) in space.coord_iter(): | ||
if not content: | ||
continue | ||
agents = [content] if not hasattr(content, "__iter__") else content | ||
for agent in agents: | ||
data = agent_portrayal(agent) | ||
data.update({"x": x, "y": y}) | ||
all_agent_data.append(data) | ||
|
||
case HexGrid(): |
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.
Again this order won't work, because the HexGrid
inherits from Grid.
|
||
case HexGrid(): | ||
# Hex-based grid | ||
for content, (q, r) in space.coord_iter(): |
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.
coord_iter
doesn't exist for HexGrid
def _draw_hex_grid(space, all_agent_data, agent_portrayal): | ||
"""Create Altair visualization for Hex Grid.""" | ||
invalid_tooltips = ["color", "size", "x", "y", "q", "r"] | ||
x_y_type = "quantitative" | ||
|
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.
@Sahil-Chhoker Sorry for the inconvenience. I will test more carefully this time. |
for more information, see https://pre-commit.ci
@Sahil-Chhoker i have made the changes . |
You've done a great job @nissu99 on the grids. It will take some time to test them all but I will do a review soon enough. |
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.
I have done a basic review, the grid drawing can wait, let's go step by step.
Our first step here should be to finalize the agent data collecting logic, therefore why is the agent data collecting logic not made into a function like is done in matplotlib
part?
Also does this implementation cover all the grids (again compare with this)
with contextlib.suppress(ImportError): | ||
import altair as alt |
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.
Don't suppress the import error here.
import mesa.experimental | ||
from mesa.experimental.cell_space import HexGrid |
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.
change the experimental imports to mesa.discrete_space
visualized. The dictionary can contain the following keys: | ||
- "color": A string representing the agent's color (e.g., "red", "#FF0000"). | ||
- "size": A number representing the agent's size. | ||
- "tooltip": A string to display as a tooltip when hovering over the agent. |
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.
Is this tooltip a new feature for altair
? I don't think this is necessary for the moment.
update_counter.get() | ||
space = getattr(model, "grid", None) | ||
if space is None: | ||
# Sometimes the space is defined as model.space instead of model.grid |
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.
we should keep this comment.
space = model.space | ||
|
||
chart = _draw_grid(space, agent_portrayal) | ||
# Apply post-processing if provided |
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.
we should keep this comment as well
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.
sure, will be more cautious while deleting comments.
stuff like vernoi grid has not been added ,will have to look into it to properly add it. |
@nissu99 I can see why you got confused, I think he meant the generic function should be called in the respective draw functions and hence |
I thought ,later he wants to make some universal function which can be reused. |
You don't have to worry about the new API, its still in discussion. |
i tried using the collect_agent_data function it does not give the desired results. |
How did you test it, can you please elaborate how is it not working? |
will ha
i used the internal examples ,somehow it shows no agent showing ,it is not able to extract the data ,will have to properly look into it to understand the issue well. |
That's because you are just getting the agent data, no plotting them on the grid. Maybe check if the data extraction is working or not don't not rely on this visualization. |
@Sahil-Chhoker sorry for not getting back ,just got my college holidays started , i think the extraction is the fault here |
Its working fine for me, can you test again. I don't think its a difficult task, you just replace the agent data collection logic with the |
i get that , it is not working for OrthogonalMooreGrid somehow,for others it works fine. |
It's working for |
It worked ,i made a dumb mistake ,should i make a commit for changing the extraction logic? |
Yeah, refactor the agent placement logic inline with the extraction logic. |
I encountered several issues while reviewing this PR. First, the previous review comments have not been addressed. Additionally, you're not using the new grids in Due to the excessive length of this PR, it's difficult for me to review it entirely. I strongly recommend breaking it down into smaller, atomic PRs that address specific issues. A better approach could be:
This will make the review process smoother and ensure that changes are properly implemented step by step. |
#2435
@quaquel @EwoutH I’ve added a new component, make_altair_plot_component. Could you kindly check if it aligns well with the project?
P.S working on space support.