-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
DataCollector silently skips reporters for non-existent agent attributes #3042
Description
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())