-
Notifications
You must be signed in to change notification settings - Fork 880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datacollector: Allow collecting data from Agent (sub)classes #2300
datacollector: Allow collecting data from Agent (sub)classes #2300
Conversation
Performance benchmarks:
|
What about the case for e.g. in Epstein civil violence, you want to collect based on citizens that are jailed, which is not based on type? |
This PR doesn't fix everything, we clearly need a new DataCollector. But it's a minimally invasive, fully backwards compatible extension to the current DataCollector that everybody knows. It's even backportable to Mesa 2.x. In Epstein civil violence, you can log if agents are jailed or not and filter afterwards. Not ideal, but workable. Of course I'm open for other suggestions for backwards compatible alternatives. |
The code path has been well tested with model reporter and agent reporter. Also, better than nothing. I'm fine with this PR. |
There are a few lines currently not covered by the tests, but otherwise, this seems fine. It solves a long-standing open issue in a backward-compatible way. It makes sense to add it while we continue work on building the foundations for a better datacollector. |
Added some additional tests. I consciously named it This functionality might even be able to be implemented more elegantly using more existing code. But I think API is solid, and there are distinct use cases for it. One huge advantage of collecting by type, it that you can ensure all agents have those attributes and methods you’re collecting. It also elegantly using the pre-existing |
Added `agenttype_reporters` to Mesa's DataCollector, enabling collection of data specific to agent types.
Added three new test methods to cover the missing codepaths: 1. `test_agenttype_reporter_string_attribute`: This test covers the case where the reporter is a string (attribute name). 2. `test_agenttype_reporter_function_with_params`: This test covers the case where the reporter is a list (function with parameters). 3. `test_agenttype_reporter_multiple_types`: This test explicitly checks that adding reporters for multiple agent types works correctly, which covers the case where `agent_type` is not initially in `self.agenttype_reporters`.
f0c7f0d
to
3dbb5b6
Compare
In 86b184d and 3dbb5b6 I updated this feature to allow collecting from any Agent (sub)class, so also a class like self.datacollector = DataCollector(
agenttype_reporters={
Sheep: {"wool": "wool_amount"},
Animal: {"energy": "energy"} # Collects from all animals
}
) I would love some final reviews on this (also from @Corvince). |
mesa/datacollection.py
Outdated
|
||
Finally, DataCollector can create a pandas DataFrame from each collection. | ||
|
||
The default DataCollector here makes several assumptions: | ||
* The model has an agent list called agents | ||
* The model has a dictionary of AgentSets called agents_by_type | ||
* For collecting agent-level variables, agents must have a unique_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this part about assumptions. All this is is in Model
. Why not say: "the DataCollector is designed to work with Model
or any subclass thereof?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 minor remaining comments but otherwise seems good to go.
All three assumptions are now guarded by that we require the Agent and Model super classes to always be initialized. So they are not relevant anymore for the user.
Thanks for reviewing! I'm merging to keep moving this forward, but @Corvince still I would really love to hear your thoughts on the API. |
…mesa#2300) Enhanced Mesa's DataCollector to allow collecting data from Agent (sub)classes, providing more flexible and granular data collection capabilities. To enable more comprehensive data collection in multi-agent simulations, allowing researchers to track attributes and behaviors specific to different agent types, including custom Agent subclasses. - Modified `DataCollector` class to accept `agenttype_reporters` parameter - Added `_new_agenttype_reporter` method for handling agent-type-specific reporters - Updated `collect` method to handle agent-type-specific data collection - Added `get_agenttype_vars_dataframe` method for retrieving agent-type-specific data - Updated `Model` class to support `agenttype_reporters` in `initialize_data_collector` - Added support for collecting data from all Agent subclasses, not just predefined agent types - Updated docstrings and module-level documentation - Added comprehensive unit tests for the new functionality ```python class MyModel(Model): def __init__(self): super().__init__() self.datacollector = DataCollector( agent_reporters={"life_span": "life_span"}, agenttype_reporters={ Wolf: {"sheep_eaten": "sheep_eaten"}, Sheep: {"wool": "wool_amount"}, Animal: {"energy": "energy"} # Collects from all animals } ) wolf_data = model.datacollector.get_agenttype_vars_dataframe(Wolf) animal_data = model.datacollector.get_agenttype_vars_dataframe(Animal) ``` - Backward compatible with existing DataCollector usage - Supports collecting data from custom Agent subclasses and superclasses
Enhanced Mesa's DataCollector to allow collecting data from Agent (sub)classes, providing more flexible and granular data collection capabilities.
Motive
To enable more comprehensive data collection in multi-agent simulations, allowing researchers to track attributes and behaviors specific to different agent types, including custom Agent subclasses.
Implementation
DataCollector
class to acceptagenttype_reporters
parameter_new_agenttype_reporter
method for handling agent-type-specific reporterscollect
method to handle agent-type-specific data collectionget_agenttype_vars_dataframe
method for retrieving agent-type-specific dataModel
class to supportagenttype_reporters
ininitialize_data_collector
Usage Examples
Additional Notes
Part of #348.