Skip to content

Fix: Property layer data mapping for both Altair and Matplotlib backends#2869

Merged
EwoutH merged 7 commits intomesa:mainfrom
Sahil-Chhoker:altair-propertylayer-datamapping
Nov 5, 2025
Merged

Fix: Property layer data mapping for both Altair and Matplotlib backends#2869
EwoutH merged 7 commits intomesa:mainfrom
Sahil-Chhoker:altair-propertylayer-datamapping

Conversation

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

@Sahil-Chhoker Sahil-Chhoker commented Oct 29, 2025

Summary

The mapping of every property layer was flipped vertically for Altair backend, this PR aims to fix that.

Bug / Issue

Related to #2864.

Implementation

Flipped the data for Altair dataframe.

Testing

Before (Wrong according to data):
image

After:
image

Matches the data and matplolib backend now.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR attempts to fix the coordinate mapping for property layer visualization in the Altair backend by swapping the x/y coordinate generation and reversing the y-axis to match the expected visualization orientation (origin at bottom-left).

  • Swapped np.repeat and np.tile operations for x and y coordinates
  • Added y-axis reversal to flip the coordinate system

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.4% [-3.2%, -1.7%] 🔵 -1.0% [-1.1%, -0.9%]
BoltzmannWealth large 🔵 -2.4% [-3.5%, -1.4%] 🟢 -8.1% [-11.3%, -4.6%]
Schelling small 🔵 -0.8% [-1.2%, -0.5%] 🔵 -1.3% [-2.2%, -0.3%]
Schelling large 🔵 -1.2% [-2.0%, -0.4%] 🔵 -4.3% [-8.4%, +0.3%]
WolfSheep small 🔵 -1.7% [-1.9%, -1.4%] 🔵 -1.5% [-1.6%, -1.3%]
WolfSheep large 🔵 -3.8% [-5.5%, -2.1%] 🟢 -8.0% [-9.9%, -6.2%]
BoidFlockers small 🔵 +0.4% [-0.2%, +1.0%] 🔵 +1.9% [+1.6%, +2.3%]
BoidFlockers large 🔵 +1.5% [+1.1%, +1.9%] 🔵 +1.3% [+1.0%, +1.6%]

@Sahil-Chhoker Sahil-Chhoker changed the title Fix: Property layer data mapping for Altair Fix: Property layer data mapping for both Altair and Matplotlib backends Oct 29, 2025
@flucco
Copy link
Copy Markdown
Contributor

flucco commented Oct 29, 2025

I believe the transpose operation in the MPL code is correct and shouldn't be removed. It changes the data from W x H, which is what Mesa uses, to H x W, what is what imshow uses. I don't think the matplotlib backend is flipped in its current form. Rather, since you said "according to data", I think you may be reading the sugarmap.txt file (and asymmetrical edited versions of it) incorrectly.

It's confusing because Mesa arranges PropertyLayer values in W x H arrays. This means the row is x and the column is y. I think it's good that Mesa does it this way, I wouldn't change it, but it can lead to confusion if you try to look at the sugarmap.txt file in order to understand how the data should look on screen. You don't usually expect to read x from top to bottom and y from left to right, but this is how the data is laid out in the PropertyLayers. (I may be totally wrong in my assumptions about how you're reading the data -- I'm just basing it off my own experience when I was trying to understand the same thing. It would be helpful to see the original .txt file you're using for your matplotlib examples.)

This might seem like just a matter of perspective, but it's not, because the width vs. height distinction is extremely important. If you edit your examples to something with differing width and height (it's easy to edit it to 40x50), honestly I'm not totally sure but I think it will either look wrong once you run it or just break the visualization entirely.

With the Altair code, I'm not sure but I think the y-coordinates are reversed and the x coordinates are fine. You have an earlier version of this PR that looks like it might work.

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator Author

Sorry for not replying, was busy for a few days and thanks for pointing it out, I'll try cross checking with the older version of mesa.

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator Author

I think this should address the problem, in the end the data was flipped vertically, I've also compared it with the mesa 3.2 version, and yeah the data is originally mapped from W x H to H x W which is indeed confusing.

@Sahil-Chhoker Sahil-Chhoker added the bug Release notes label label Oct 31, 2025
@flucco
Copy link
Copy Markdown
Contributor

flucco commented Oct 31, 2025

Sorry for not replying, was busy for a few days and thanks for pointing it out, I'll try cross checking with the older version of mesa.

No worries! Glad I could help. The coordinate layout stuff is super hard to think about IMO.

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 1, 2025

@tpike3 since you’re most familiar with the Sugerscape example, can you give this a review?

@tpike3
Copy link
Copy Markdown
Member

tpike3 commented Nov 5, 2025

For me the most important thing is Mesa is internally consistent. Regardless of Altair or Matplotlib users should expect to find the origin in the same spot. The lat/long switches on GIS libraries have caused me no end of issues.

So I wholeheartedly approve of this fix.

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 5, 2025

With this approval, once you're ready, I can believe you can now merge yourself :)
(highly recommend squashing while merging)

@EwoutH EwoutH merged commit e48c1cd into mesa:main Nov 5, 2025
12 checks passed
dhiraj-143r pushed a commit to dhiraj-143r/mesa that referenced this pull request Nov 6, 2025
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 7, 2025

We released Mesa 3.3.1 with this bugfix, so you should now be able to update Mesa (pip install -U mesa) and have this bug resolved!

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.

5 participants