Network initialization crashes when layout dictionary is missing node positions
Describe the bug
Network space initialization incorrectly handles nodes that are missing from the layout mapping. When a node ID is not found in the layout, node_positions.get(node_id) returns None. However, the code immediately wraps this in a NumPy array: pos = np.array(None).
Because a NumPy array (even one wrapping None) is not the Python None singleton, the subsequent guard if pos is not None: evaluates to True. This causes the invalid position to be passed to scipy.spatial.KDTree, which crashes the initialization with a ValueError.
Expected behavior
The Network space should identify nodes without specified positions and skip them when building the spatial KDTree. If the resulting list of positions is empty, it should gracefully set self._kdtree = None, allowing the space to function in a purely topological mode (as intended and documented).
To Reproduce
import networkx as nx
from mesa.discrete_space import Network
# 1. Create a graph with nodes
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
# 2. Attempt to initialize Network with an empty layout dictionary
# This raises: ValueError: data must be of shape (n, m) (from SciPy KDTree)
net = Network(G, layout={})
Additional context Suggested Fix: Check for None before wrapping the position in a NumPy array
Why would someone pass an empty dictionary in layout? The layout is basically a mapping from node_id to position so I can't understand the motive for doing so.
Why would someone pass an empty dictionary in layout? The layout is basically a mapping from node_id to position so I can't understand the motive for doing so.
While empty layout isn’t a common input, I would like you to see this as an error handling problem. Right now Mesa crashes with a SciPy ValueError because None positions get converted to np.array(None) and slip past the if pos is not None check.
So the bug is less “empty layout should be encouraged” and more “invalid/missing layout entries should be handled safely and predictably” (either skipped for topological mode or rejected with a clear SpaceException).
I lean towards @codebreaker32's perspective on this one regarding the API design.
While the current SciPy ValueError is undeniably cryptic and needs to be handled better, I don't think we should implement a fix that silently accepts an empty or incomplete layout.
Here is why:
- If a user explicitly passes the
layoutargument, the framework should assume they want a spatial network. - If a user programmatically generates a layout dictionary but accidentally misses a few nodes, and we silently skip those nodes or fall back to a purely topological network, we might be masking a bug in the user's data generation code. They will expect spatial queries to work for those nodes later, and it will fail silently or behave unpredictably.
I agree with @falloficarus22 that the current error is a bad developer experience. However, instead of making the initialization gracefully fall back to topological mode, we should enforce strict validation.
I think we should check for None before the NumPy conversion, and if any node in the graph is missing from the layout, we immediately raise a clear, Mesa-specific error. For example: ValueError("Node ID '3' is missing from the provided layout dictionary.") or something similar to this.
This addresses the cryptic crash issue while upholding the principle that invalid inputs shouldn't be silently ignored.
How does this approach sound to both of you and to the maintainers?
At face value, without looking closely at the code and the possible problems it might give, I agree with @Nithurshen take.
I agree with the strict validation direction here.
If layout is explicitly provided, failing fast with a clear Mesa-specific error feels safer than silently degrading to topological mode. That avoids masking user-side data issues while still improving the current cryptic SciPy crash.
One small thought — it might also help to validate the layout completeness before attempting KDTree construction, so we fail early and predictably.