Fix: Enforce strict layout validation in Network space#3386
Merged
Fix: Enforce strict layout validation in Network space#3386
Conversation
|
Performance benchmarks:
|
quaquel
reviewed
Feb 26, 2026
mesa/discrete_space/network.py
Outdated
| # Create cells and gather KD-Tree data simultaneously | ||
| for node_id in self.G.nodes: | ||
| pos = np.array(node_positions.get(node_id)) | ||
| raw_pos = node_positions.get(node_id) |
Member
There was a problem hiding this comment.
would it not make more sense to use:
Suggested change
| raw_pos = node_positions.get(node_id) | |
| try: | |
| raw_pos = node_positions[node_id] | |
| except KeyError as e: | |
| raise SpaceException() from e |
quaquel
reviewed
Feb 26, 2026
Member
quaquel
left a comment
There was a problem hiding this comment.
Looks fine to me with 1 question/suggestion.
quaquel
approved these changes
Mar 2, 2026
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
This PR fixes a bug where initializing a
Networkspace with an incomplete or emptylayoutdictionary resulted in a cryptic SciPyValueError. It introduces strict validation to explicitly catch missing node positions before spatial processing begins.Bug / Issue
Fixes #3385
When a
layoutdictionary was provided but was missing entries for certain node IDs,node_positions.get(node_id)returnedNone. ThisNonevalue was incorrectly wrapped into a NumPy array (np.array(None)), which bypassed the subsequentpos is not Noneguard. This invalid position was then passed toscipy.spatial.KDTree, causing a crash with the cryptic error.Implementation
__init__method inmesa/discrete_space/network.py. Extracted the position lookup (raw_pos = node_positions.get(node_id)) and added an explicitNonecheck before NumPy conversion. If a node is missing from the layout, the initialization now immediately halts and raises aSpaceExceptionwith a clear, Mesa-specific error message detailing the exact missing Node ID.if pos is not None:guard later in the loop since positions are now strictly validated.Testing
test_network_missing_layout_nodeintests/discrete_space/test_discrete_space.py.layout={}) and a partially missing layout both correctly raise the expectedSpaceException.Additional Notes
As discussed in the issue thread with the maintainers, this strict validation approach was chosen over silently falling back to a purely topological network mode. This prevents masking potential bugs in the user's data generation code where they might mistakenly expect spatial queries to work for dropped nodes.