Skip to content

Fix/datacollector missing attribute error#3041

Merged
quaquel merged 4 commits intomesa:mainfrom
codebyNJ:fix/datacollector-missing-attribute-error
Dec 30, 2025
Merged

Fix/datacollector missing attribute error#3041
quaquel merged 4 commits intomesa:mainfrom
codebyNJ:fix/datacollector-missing-attribute-error

Conversation

@codebyNJ
Copy link
Copy Markdown
Contributor

Problem

DataCollector silently skipped reporters for non-existent agent attributes,
causing silent data loss.

Solution
Added validation to raise AttributeError with informative message including:

  • Agent ID
  • Agent type
  • Missing attribute name
  • Reporter name

Changes

  • Modified mesa/datacollection.py
  • Added tests to tests/test_datacollector.py

Testing

  • All 18 tests passing
  • No regressions
  • Backward compatible

To Reproduce
Run the below code

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())

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.0% [+1.0%, +3.0%] 🔵 +1.9% [+1.7%, +2.0%]
BoltzmannWealth large 🔵 -0.9% [-1.5%, -0.4%] 🔵 -2.1% [-4.1%, -0.5%]
Schelling small 🔵 -0.7% [-1.7%, +0.3%] 🔵 +0.8% [+0.2%, +1.4%]
Schelling large 🔵 -0.2% [-0.7%, +0.2%] 🔵 +0.7% [-0.2%, +1.8%]
WolfSheep small 🔵 -0.7% [-1.4%, -0.0%] 🔵 -0.1% [-0.3%, +0.0%]
WolfSheep large 🔵 -1.6% [-5.0%, +0.5%] 🔵 +1.6% [+0.7%, +2.7%]
BoidFlockers small 🔵 -1.1% [-1.4%, -0.9%] 🔵 -0.1% [-0.3%, +0.1%]
BoidFlockers large 🔵 -1.1% [-1.6%, -0.6%] 🔵 +0.2% [-0.0%, +0.5%]

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 30, 2025

Thanks for the PR.

It now changes over a 1000 lines of code, that can’t be right?

@codebyNJ
Copy link
Copy Markdown
Contributor Author

codebyNJ commented Dec 30, 2025

@EwoutH sorry it's a wrong push I will update it. It's just few lines update.

- Add validation in _new_agent_reporter() to check attribute exists
- Add validation in _new_agenttype_reporter() for consistency
- Raise AttributeError with informative message if attribute missing
- Includes agent ID, agent type, attribute name, and reporter name
- Prevents silent data loss in agent reporters
- Add 3 comprehensive test cases:
  * test_agent_missing_attribute_error()
  * test_agent_valid_attributes_still_work()
  * test_lambda_reporters_still_work()
- All 18 tests passing
- No breaking changes
@codebyNJ codebyNJ force-pushed the fix/datacollector-missing-attribute-error branch from b06d9b0 to d066fb1 Compare December 30, 2025 09:01
@codebyNJ
Copy link
Copy Markdown
Contributor Author

@EwoutH I have updated the PR.

@codebyNJ codebyNJ force-pushed the fix/datacollector-missing-attribute-error branch from 454008f to 0bdc98d Compare December 30, 2025 13:32
@quaquel quaquel added the bug Release notes label label Dec 30, 2025
@quaquel quaquel added the trigger-benchmarks Special label that triggers the benchmarking CI label Dec 30, 2025
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +6.2% [+5.3%, +7.2%] 🔵 +1.4% [+1.2%, +1.6%]
BoltzmannWealth large 🔵 -0.1% [-0.6%, +0.4%] 🔵 +3.1% [+1.5%, +4.9%]
Schelling small 🔵 -0.1% [-1.0%, +1.0%] 🔵 +0.3% [-0.3%, +0.9%]
Schelling large 🔵 -0.3% [-0.5%, -0.0%] 🔵 +3.1% [+1.4%, +5.1%]
WolfSheep small 🔵 -1.6% [-2.3%, -1.0%] 🔵 -1.5% [-1.7%, -1.3%]
WolfSheep large 🔵 -2.5% [-6.4%, -0.1%] 🔵 -1.8% [-2.9%, -0.7%]
BoidFlockers small 🔵 -0.4% [-0.8%, +0.0%] 🔵 +0.9% [+0.7%, +1.2%]
BoidFlockers large 🔵 -0.8% [-1.3%, -0.2%] 🔵 -0.3% [-0.7%, +0.0%]

@quaquel quaquel merged commit a9edcbc into mesa:main Dec 30, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label trigger-benchmarks Special label that triggers the benchmarking CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataCollector silently skips reporters for non-existent agent attributes

3 participants