Repository navigation
Fix registering an AgentSet that has no agents yet - #206
Open
Jay-Naik2526 wants to merge 3 commits into
Open
Jay-Naik2526 wants to merge 3 commits into
Jay-Naik2526 wants to merge 3 commits into
Conversation
A new AgentSet is created with an empty DataFrame that has no columns, so it has no unique_id column until agents are added. AgentSetRegistry.add and _recompute_ids read agentset["unique_id"] directly and raised polars ColumnNotFoundError for such a set. AgentSet.index now returns an empty UInt64 Series named unique_id when the column does not exist yet, and the registry uses it instead of indexing the column. This also fixes AgentSet.pos for an empty set, since it is based on index. Adds tests for registering an empty AgentSet (including removing another set while it remains registered) and for the index of an empty AgentSet. Fixes mesa#204
for more information, see https://pre-commit.ci
Author
|
A note on the second commit: it comes from the pre-commit.ci autofix bot, not from this change. The The actual change is only the first commit (4 files: |
The ExampleAgentSetA fixture sets a column on an empty AgentSet, which fails with polars 2.x (ShapeError: can't broadcast Series of length 4 to length 0). Build the populated set with AgentSet.add instead, so the new test passes with both polars 1.36.1 and 2.1.0.
Author
|
Context on the red checks, in case it helps with review:
|
This branch has not been deployed
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.
Fixes #204.
Problem
A new
AgentSetstarts with an empty DataFrame that has no columns (seeAgentSet.__init__), so it has nounique_idcolumn until agents are added.AgentSetRegistry.addandAgentSetRegistry._recompute_idsreadagentset["unique_id"]directly, so registering a set that has no agents yet raisedpolars.exceptions.ColumnNotFoundError: unable to find column "unique_id":Change
AgentSet.indexreturns an emptyUInt64series namedunique_idwhen the column does not exist yet.AgentSetRegistry.addand_recompute_idsuseagentset.indexinstead of reading the column directly.AgentSet.posis built onindex, so it works for an empty set as well.I kept the existing design (an empty AgentSet has no schema) because of the comment in
__init__about adding agents later. If you would rather give an empty AgentSet aunique_idschema instead, I am happy to rework it that way.Tests
tests/test_agentsetregistry.py::test_add_empty_agentset: registers an empty set, adds a populated set next to it, then removes the populated set while the empty one stays (this exercises_recompute_ids).tests/test_agentset.py::test_index_empty:indexof a new AgentSet is an emptyUInt64series namedunique_id.Both new tests fail on
mainwith theColumnNotFoundErrorabove and pass with this change. WithMESA_FRAMES_RUNTIME_TYPECHECKING=1, the full suite gives 146 passed and 1 failed; the failure istests/test_datacollector.py::TestDataCollector::test_postgress, which needs a running PostgreSQL database and fails the same way onmain.ruff checkandruff format --checkpass.