Skip to content

Modify Space Drawers to use explicit Cell positions#3323

Merged
quaquel merged 12 commits intomesa:mainfrom
codebreaker32:modify_space_drawer
Feb 20, 2026
Merged

Modify Space Drawers to use explicit Cell positions#3323
quaquel merged 12 commits intomesa:mainfrom
codebreaker32:modify_space_drawer

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

@codebreaker32 codebreaker32 commented Feb 16, 2026

This PR updates the mesa.visualization.space_drawers to leverage the newly introduced cell.position attribute for the DiscreteSpace API.

Previously, Space Drawers had to independently guess or mathematically recalculate the physical geometry of spaces (e.g., computing hex offsets or running network layout algorithms).

Key Changes

  1. HexSpaceDrawer
  • viz_xmin, viz_xmax, viz_ymin, and viz_ymax are now dynamically calculated directly from the absolute minimum and maximum values of the cells' physical positions.
  • _get_hexmesh now anchors hexagons exactly at cell.position instead of relying on a hardcoded row/col math algorithm.
  • Corrected the return type of _get_hexmesh from list[tuple[float, float]] to the factually accurate list[list[tuple[float, float]]] (a list of hexagons).
  1. NetworkSpaceDrawer
  • The drawer now intercepts pre-calculated network node coordinates directly from the cells (cell._position). This prevents the visualizer from redundantly executing heavy NetworkX layout algorithms (like nx.spring_layout) if the model has already calculated them during setup.
  1. VoronoiSpaceDrawer
  • Uses cell.position instead of centroid coordinates

Note: Full backward compatibility has been maintained for mesa.space objects via fallbacks

@quaquel quaquel added the enhancement Release notes label label Feb 16, 2026
@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Do we need fallback if we are removing mesa.space?

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.6% [-1.1%, -0.1%] 🔵 -2.7% [-3.3%, -2.2%]
BoltzmannWealth large 🔵 +1.0% [+0.1%, +1.9%] 🔵 +1.5% [-0.6%, +3.8%]
Schelling small 🔵 +0.6% [+0.1%, +1.0%] 🔵 -0.0% [-0.2%, +0.2%]
Schelling large 🔵 -1.8% [-2.7%, -0.9%] 🔵 -5.2% [-10.8%, -2.0%]
WolfSheep small 🔵 -1.8% [-2.1%, -1.5%] 🔵 -0.2% [-0.4%, -0.1%]
WolfSheep large 🔵 -1.3% [-3.0%, +0.6%] 🔵 -0.6% [-2.7%, +1.6%]
BoidFlockers small 🔵 +1.5% [+1.2%, +1.8%] 🔵 +1.2% [+1.1%, +1.4%]
BoidFlockers large 🔵 +2.1% [+1.7%, +2.5%] 🔵 +1.0% [+0.8%, +1.1%]

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Feb 16, 2026

Do we need fallback if we are removing mesa.space?

No, I'll try to open a pr later this week for removing mesa.space.

@quaquel quaquel mentioned this pull request Feb 17, 2026
@quaquel
Copy link
Copy Markdown
Member

quaquel commented Feb 18, 2026

In #3337, I removed mesa.space. Can you update this PR accordingly. Code wise, I have given it a quick check already and did not see anything strange.

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

All Set! I have updated the PR

Copy link
Copy Markdown
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.

Have you visually checked everything?

Looks fine to me otherwise.

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Have you visually checked everything?

Yes, On virus_on_network

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

I ran virus_on_network and I think something is wrong However its not due to this PR because its persistent on main as well

Screenshot (210)

See "infected count isn't reflected well in viz"

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Remaining examples are working fine.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Feb 18, 2026

There is more weird stuff going on with networks. If you reset the model, the visual style is also changed. Initially, it is a grey circle with a white center. On reset, it becomes a grey center. @Sahil-Chhoker, any thoughts?

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Feb 18, 2026

I have started to dig into this. The problem is in SpaceRenderer._map_coordinates. In the if clause for networks, stuff is mapped from node ids to positions, but of course we don't need to do that anymore if position is already stored in the cell.

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

I have started to dig into this. The problem is in SpaceRenderer._map_coordinates. In the if clause for networks, stuff is mapped from node ids to positions, but of course we don't need to do that anymore if position is already stored in the cell.

This doesn't seem to solve the issue

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Feb 18, 2026

If I do

        elif isinstance(self.space, Network):
            # no need to map network node IDs to positions 
            mapped_arguments["loc"] = arguments["loc"].astype(float)

the issue is gone

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Screenshot (215)

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Ok, My bad. I was running it on main branch. Running fine on this branch

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

codebreaker32 commented Feb 18, 2026

I have opened a PR to fix it. That can be merged after this one.

Copy link
Copy Markdown
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.

a last couple of questions, but after that, this seems good to go.

@quaquel quaquel merged commit b147bce into mesa:main Feb 20, 2026
14 checks passed
souro26 pushed a commit to souro26/mesa that referenced this pull request Feb 20, 2026
@codebreaker32 codebreaker32 deleted the modify_space_drawer branch March 9, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants