Skip to content

DataCollector silently skips reporters for non-existent agent attributes #3042

@codebyNJ

Description

@codebyNJ

When a DataCollector agent reporter uses a string attribute name that doesn't exist on an agent, the data collection silently fails without any error or warning. This causes silent data loss and makes it very difficult to debug reporter configuration mistakes.

Expected Behavior
DataCollector should raise an informative AttributeError when trying to collect data from a non-existent attribute:
AttributeError: Agent 1 of type Agent has no attribute 'health' (reporter: 'health')

Solution
Add validation in DataCollector._new_agent_reporter() and DataCollector._new_agenttype_reporter() to check if the attribute exists before creating the reporter function. Raise AttributeError with an informative message if the attribute doesn't exist.

To Reproduce the issue

from mesa import Agent, Model
from mesa.datacollection import DataCollector

class TestAgent(Agent):
    def __init__(self, model):
        super().__init__(model)
        self.wealth = 100
    
    def step(self):
        pass

class TestModel(Model):
    def __init__(self):
        super().__init__()
        agent = TestAgent(self)
        self.add_agent(agent)
    
    def step(self):
        for agent in self.agents:
            agent.step()

model = TestModel()

dc = DataCollector(
    agent_reporters={
        "wealth": "wealth",
        "health": "health",
        "status": "status"
    }
)

dc.collect(model)
print(dc.get_agent_vars_dataframe())

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