fix(hex_ant): fix relative imports and PropertyLayer API to make example fully runnable#470
Open
dietcoke-17 wants to merge 2 commits into
Open
fix(hex_ant): fix relative imports and PropertyLayer API to make example fully runnable#470dietcoke-17 wants to merge 2 commits into
dietcoke-17 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fa7a20f19e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
fa7a20f to
4ace501
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #357 — hex_ant was completely non-functional. Running
solara run app.pycrashed immediately and the model never reached the browser. This PR works through
every crash in sequence until the example fully runs.
Bug / Issue
Bug 1 — Relative imports in app.py and model.py
app.py — before
from .agent import AntState
from .model import AntForaging
removed the . before the module
why this change
Relative imports (
.module) only work when Python treats the directory as a package with a known parent. When Solara executesapp.pydirectly,__package__isNone, so there is no parent to be relative toResult: Model loads past the import stage.
Bug 2 — Direct PropertyLayer item assignment in
_init_environmentmodel.py — before
self.grid.pheromone_home[center] = 1.0
self.grid.home[center] = 1
model.py — after
self.grid.pheromone_home.data[center] = 1.0
self.grid.home.data[center] = 1
Why
In Mesa 3.x,
PropertyLayeris a wrapper object, not a numpy array. Direct item assignment (layer[x, y] = val) is not supported. The underlying numpy array is accessed through the.dataattribute, which does support index assignment.Result: Model initialises, nest and food sources are placed,
visualization opens in browser.
Bug 3 — Wrong dynamic layer lookup in
_update_pheromone_layermodel.py — before
np_layer = self.grid.property_layers[layer_name]
model.py — after
np_layer = getattr(self.grid, layer_name).data
Why:
self.grid.property_layers does not exist as an attribute.
HexGridoverrides__getattr__and routes ALL attribute access through the internal property layer dictionary — so accessing.property_layersis itself treated as a layer name lookup and fails. The correct pattern for dynamic layer access by name isgetattr(self.grid, layer_name). The.datais also needed here for the same reason as Bug 2 — numpy operations like*=and boolean masking must be performed on the rawarray, not the wrapper object.
Testing
Additional Notes
Relation to existing PRs
Verified
Tested on Windows, Mesa 3.5.0, Python 3.14:
solara run app.pystarts without errorAfter Codex Suggestion
In
model.pyandapp.py: used try/except import pattern to support bothsolara run app.py(direct execution) and package import via test runner and it workedAlso CI Failing Issue
Also fixed a pre-existing lint error in rl/wolf_sheep/agents.py — two variables x, y were unpacked but never actually used anywhere. Renamed them to _x, _y to tell the linter they are intentionally unused. This was flagged by pre-commit CI and is unrelated to the hex_ant fix.