Skip to content

Network visualization silently drops agents on non-contiguous nodes due to IndexError suppression #3023

@codebyNJ

Description

@codebyNJ

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

  1. Create a NetworkGrid with non-contiguous node IDs
  2. Try to visualize the network
  3. Observe that agents on nodes 5, 10, and 15 are missing from visualization
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelease notes label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions