-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Network visualization silently drops agents on non-contiguous nodes due to IndexError suppression #3023
Copy link
Copy link
Closed
Labels
bugRelease notes labelRelease notes label
Description
Describe the bug
Agents placed on network nodes with non-contiguous IDs (e.g., nodes [0, 1, 5, 10, 15]) are silently dropped from visualization without any error message. The bug occurs in mesa/visualization/space_renderer.py:168-172 where IndexError is suppressed using contextlib.suppress()
Expected behavior
All agents should be visible in visualization at their correct positions regardless of node ID values. If position mapping fails, users should receive a clear warning message.
To Reproduce
- Create a NetworkGrid with non-contiguous node IDs
- Try to visualize the network
- Observe that agents on nodes 5, 10, and 15 are missing from visualization
- No error message or warning is shown
import networkx as nx
from mesa import Agent, Model
from mesa.space import NetworkGrid
from mesa.visualization import SpaceRenderer
G = nx.Graph()
G.add_nodes_from([0, 1, 5, 10, 15]) # Non-contiguous IDs
G.add_edges_from([(0, 1), (1, 5), (5, 10), (10, 15)])
model = Model()
space = NetworkGrid(G)
model.space = space
# Place agents on each node
for node_id in [0, 1, 5, 10, 15]:
agent = Agent(model)
space.place_agent(agent, node_id)
# Create visualization
renderer = SpaceRenderer(model, space, agent_portrayal=lambda a: {"color": "red"})Additional context
- Affects any NetworkGrid or Network space visualization
- Common with real-world networks that have arbitrary node IDs
- Also affects dynamic networks where nodes are added/removed during simulation
- Root cause: Using node IDs as array indices instead of dictionary keys
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugRelease notes labelRelease notes label