Skip to content

Bugfix for pickling dynamically modified grids#3217

Merged
quaquel merged 4 commits intomesa:mainfrom
quaquel:grid_pickle
Jan 28, 2026
Merged

Bugfix for pickling dynamically modified grids#3217
quaquel merged 4 commits intomesa:mainfrom
quaquel:grid_pickle

Conversation

@quaquel
Copy link
Copy Markdown
Member

@quaquel quaquel commented Jan 27, 2026

This bugfix updates Cell.__getstate__ and Grid.__setstate__ to ensure that the actual structure of the grid is properly preserved when pickling a grid.

closes #3215

@quaquel quaquel added the bug Release notes label label Jan 27, 2026
Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

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

Thanks.

Since pickling has been more often an issue (#2378, #3086, and most recently #3205), should we create a follow-up issue to increase test coverage on pickling follow-up? And maybe pickle and unpickle our example models in a test?

for cell in self._cells.values():
connections = {
coordinate: self._cells[coordinate]
for coordinate in cell.connections.keys()
Copy link
Copy Markdown
Member

@EwoutH EwoutH Jan 27, 2026

Choose a reason for hiding this comment

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

To fix ruff / pre-commit

Suggested change
for coordinate in cell.connections.keys()
for coordinate in cell.connections

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Grid pickling was covered by tests, but not for dynamically modified grids. Theoretically, the grid data structure allows for dynamic modifications. However, because of the caching, which is essential for performance, we did not develop that further into a real API. #3086 was just taking advantage of a version bump on the minimum Python side, so not a bug.

@ShreyasN707
Copy link
Copy Markdown
Contributor

Thanks.

Since pickling has been more often an issue (#2378, #3086, and most recently #3205), should we create a follow-up issue to increase test coverage on pickling follow-up? And maybe pickle and unpickle our example models in a test?

@quaquel Thanks for jumping on this so quickly — it’s great to see this getting fixed. I’d be happy to help add some tests around pickling/unpickling dynamic grids or example models if that would be useful.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -3.8% [-4.5%, -2.9%] 🔵 -2.1% [-2.3%, -1.8%]
BoltzmannWealth large 🔵 -0.3% [-1.1%, +0.6%] 🔵 -0.7% [-3.5%, +2.0%]
Schelling small 🔵 -1.8% [-2.2%, -1.4%] 🔵 -0.9% [-1.1%, -0.6%]
Schelling large 🔵 -0.9% [-1.6%, -0.1%] 🔵 -1.5% [-3.3%, +0.7%]
WolfSheep small 🔵 -2.5% [-2.9%, -2.1%] 🔵 -0.9% [-1.1%, -0.7%]
WolfSheep large 🔵 -1.7% [-3.3%, -0.2%] 🔵 -2.4% [-4.8%, -0.2%]
BoidFlockers small 🔵 -2.7% [-3.1%, -2.4%] 🔵 +1.7% [+1.5%, +2.0%]
BoidFlockers large 🔵 -3.0% [-3.4%, -2.4%] 🔵 +1.1% [+1.0%, +1.3%]

@quaquel quaquel merged commit 105c0d0 into mesa:main Jan 28, 2026
14 checks passed
@quaquel quaquel deleted the grid_pickle branch February 4, 2026 19:37
@EwoutH EwoutH changed the title Bugfix for pickling dynamicly modified grids Bugfix for pickling dynamically modified grids Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grid connectivity changes are lost after pickle / unpickle

3 participants