Skip to content

fix(examples): fix inverted grid dimensions in Conway Streamlit app - #4037

Merged
quaquel merged 1 commit into
mesa:mainfrom
pojeesahil:fix-conway-streamlit-grid-dimensions
Nov 12, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
pojeesahil:fix-conway-streamlit-grid-dimensions

Conversation

@pojeesahil

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Fixes an argument inversion bug in Conway's Game of Life Streamlit app (mesa/examples/basic/conways_game_of_life/st_app.py) where height and width were swapped during model instantiation, causing cells outside square bounds to be silently dropped and never rendered on the heatmap.

Bug / Issue

In st_app.py:14:

height = st.slider("Select Grid Height", min_value=10, max_value=100, step=10, value=15)
width = st.slider("Select Grid Width", min_value=10, max_value=100, step=10, value=20)
model = ConwaysGameOfLife(height, width)

Because the signature of ConwaysGameOfLife.__init__ is (width=50, height=50), passing (height, width) inverted the dimensions:

  • The model simulated a grid of width=15, height=20.
  • Meanwhile, df_grid was constructed with range(width) (0..19) and range(height) (0..14).
  • On each step, updating df_grid.loc[...] for cell coordinates with y >= 15 matched no rows in df_grid, which Pandas silently ignored.
  • Result: With the default 20x15 sliders, 75 cells (25% of the grid) were permanently frozen at state 0 (red) on the display, while living agents with $x \ge 15$ were never rendered.
    Additionally:
  • Line 10 shadowed model = st.title(...) before model creation.
  • Line 27 allocated an unused agent_counts array and unused numpy import.

Implementation

  • Changed model instantiation to explicit keyword arguments: ConwaysGameOfLife(width=width, height=height).
  • Replaced model = st.title(...) with st.title(...).
  • Removed dead agent_counts variable and unused numpy import.

Testing

  • Ran pytest on tests/examples/test_examples.py -k "conway" (passed).
  • Verified with non-square slider values (e.g., $W=20, H=15$) that all 300 cells correctly update and render dynamically.
  • Cleaned and verified with ruff check and ruff format.

before:
even through width=10,height=20 , only bottom 10 layer are changing
image

After:
all layers are changing properly
image

@github-actions github-actions Bot added the example Changes the examples or adds to them. label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.9% [-0.6%, +2.2%] 🔵 +2.4% [+1.9%, +2.7%]
BoltzmannWealth large 🔵 -1.4% [-2.3%, -0.4%] 🟢 -6.7% [-9.7%, -4.1%]
Schelling small 🔵 +1.8% [+1.2%, +2.4%] 🔵 +0.3% [-0.4%, +1.0%]
Schelling large 🔵 +1.4% [+0.6%, +2.3%] 🔵 -1.1% [-3.7%, +1.2%]
WolfSheep small 🟢 -4.0% [-4.5%, -3.5%] 🔵 +1.5% [-3.1%, +5.8%]
WolfSheep large 🔵 -0.9% [-1.8%, +0.0%] 🔵 +1.9% [-1.0%, +4.8%]
SugarscapeG1mt small 🔵 +1.0% [+0.5%, +1.6%] 🔵 -0.2% [-1.6%, +1.3%]
SugarscapeG1mt large 🔵 +1.2% [+0.5%, +1.7%] 🔵 -1.8% [-3.1%, -0.5%]
BoidFlockers small 🔵 -0.2% [-0.7%, +0.2%] 🔵 +0.6% [+0.2%, +1.1%]
BoidFlockers large 🔵 -1.0% [-1.5%, -0.4%] 🔵 -0.3% [-0.6%, -0.0%]
MultiLevelAllianceModel small 🔵 -0.8% [-3.4%, +2.0%] 🔵 -0.7% [-1.2%, -0.2%]
MultiLevelAllianceModel large 🟢 -7.6% [-9.1%, -6.1%] 🔵 -3.4% [-5.5%, -1.7%]

@pojeesahil

Copy link
Copy Markdown
Contributor Author

Note:The codecov/patch failure is because st_app.py is an interactive Streamlit script not invoked in the headless test suite (similar to #3781)

step = 0
# init grid
df_grid = pd.DataFrame()
agent_counts = np.zeros((model.grid.width, model.grid.height))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this line removed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

agent_counts is never used anywhere in the script. The app only uses df_grid to update and display the simulation, so I removed it along with the unused numpy import.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Confirmed locally: the old call swaps width/height, so 75 of 300 cells never update on the default sliders. The fix is correct, and agent_counts also has no use in the script

@quaquel
quaquel merged commit eb40246 into mesa:main Nov 12, 2026
16 of 17 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

example Changes the examples or adds to them.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants