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

Implemented post_process in Altair based components #2641

Merged
merged 10 commits into from
Jan 31, 2025

Conversation

sanika-n
Copy link
Contributor

Feature

I implemented post_process for Altair based components. And I just tested it out on the Boltzmann Wealth Model, with the following code:

def post_process(chart: alt.Chart) -> alt.Chart:
    return (
        chart
        .configure_legend(
            titleFontSize=14,  
            labelFontSize=12
        )
        .properties(title="Agent Wealth Distribution")
        .configure_view(
            stroke="black",
            strokeWidth=1
        )
    )

Before

image

After

Screenshot 2025-01-25 010414

Copy link

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.0% [+0.1%, +4.0%] 🔵 -1.2% [-1.5%, -0.9%]
BoltzmannWealth large 🔵 -0.9% [-1.6%, -0.3%] 🔵 -1.5% [-3.3%, +0.1%]
Schelling small 🔵 -1.9% [-2.3%, -1.6%] 🔵 -1.6% [-1.7%, -1.5%]
Schelling large 🔵 -1.4% [-1.9%, -1.1%] 🔵 -1.7% [-2.1%, -1.2%]
WolfSheep small 🔵 +0.7% [+0.3%, +1.2%] 🔵 -1.0% [-1.1%, -0.8%]
WolfSheep large 🔵 -1.4% [-2.1%, -0.7%] 🔵 -3.3% [-4.4%, -2.4%]
BoidFlockers small 🔵 -0.2% [-0.8%, +0.4%] 🔵 +1.9% [+1.7%, +2.1%]
BoidFlockers large 🔵 +0.4% [-0.2%, +1.0%] 🔵 +1.6% [+1.4%, +1.8%]

Copy link
Member

@tpike3 tpike3 left a comment

Choose a reason for hiding this comment

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

Thanks @sanika-n!

Could you update the docstrings to ensure the API accurately reflects the change (~ Line 33) https://mesa.readthedocs.io/stable/apis/visualization.html#module-mesa.visualization.components.altair_components

And although this passes the tests check, it would be good to add a test to post process in the solara_viz test

mock_space_altair = mocker.spy(

Copy link
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@sanika-n
Copy link
Contributor Author

I tried implementing the tests, it is working locally but it not working here... I noticed that @nissu99 is facing the same problem
image

image

@sanika-n sanika-n mentioned this pull request Jan 27, 2025
@tpike3
Copy link
Member

tpike3 commented Jan 27, 2025

@sanika-n I would look at the imports and __inits__; It seems to be failing here

ERROR    reacton:core.py:1721 Component react.component(mesa.visualization.components.altair_components.SpaceAltair) raised exception NameError("name 'alt' is not defined")
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/reacton/core.py", line 1702, in _render
    root_element = el.component.f(*el.args, **el.kwargs)
  File "/Users/runner/work/mesa/mesa/mesa/visualization/components/altair_components.py", line 69, in SpaceAltair
    chart = _draw_grid(space, agent_portrayal)
  File "/Users/runner/work/mesa/mesa/mesa/visualization/components/altair_components.py", line 163, in _draw_grid
    "x": alt.X("x", axis=None, type=x_y_type),
         ^^^

NameError: name 'alt' is not defined. Did you mean: 'all'?

@sanika-n
Copy link
Contributor Author

@tpike3, Thank you so much, that fixed it

@tpike3 tpike3 merged commit c58af09 into projectmesa:main Jan 31, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants