-
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
add group_by
method to AgentSet
for attribute-based grouping
#2092
Conversation
Signed-off-by: Naymul Islam <[email protected]>
for more information, see https://pre-commit.ci
Performance benchmarks:
|
Thanks for this PR! It looks like it could fill a nice use case, especially for attributes with discrete values. On thing I'm thinking about is if we can do something smart to select ranges or thresholds for attributes with continuous values. For example divide agents in to bins based on some attribute. Curious what the performance of this is compared to (multiple) |
I also had a quick look. I like the direction. Some quick points:
|
Hi, Integrating range or threshold-based grouping for continuous attributes is a great idea. We could potentially extend |
Addressing non-discrete attributes through binning or type checks is a valid concern. I propose adding an optional |
Signed-off-by: Naymul Islam <[email protected]>
…mul/mesa into feature-agentset-group-by
for more information, see https://pre-commit.ci
I tried pandas'
i.e. they seem to just let you do it even if the group has 1 element each. |
I like to get other people's perspective on this as well. Personally, I am in favor of mimicking pandas as much as possible, so just group on the float as normal. This keeps the code nice and simple. If you want to do binning, you can already easily code this up through my_agents.do(some_binning_function).group_by("my_newly assigned attribute") |
Returns: | ||
dict: A dictionary where keys are attribute values and values are lists of agents with those attribute values. | ||
""" | ||
grouped_agents = defaultdict(list) |
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.
Should this not be a defaultdict(AgentSet)
? Similar to the binning discussion, this would mimic pandas in that groupby returns a group identifier and an AgentSet with the agents in that group.
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.
You raise a valid point regarding the return type of the group_by method. Changing the return type to defaultdict(AgentSet) would indeed align more closely with pandas' groupby functionality.
grouped_agents = defaultdict(AgentSet)
when adding agents to the groups:
for agent in self: attr_value = getattr(agent, attr_name, None) if attr_value not in grouped_agents: grouped_agents[attr_value] = AgentSet(model=self.model) grouped_agents[attr_value].add(agent)
Let me know whats your thought on these changes. Looking forward :)
Thanks for this! Just one thought on the binning conversation and a question regarding the appropriate return type. The code itself, and the tests look fine. |
Hi sorry for the late reply. I think we don't need to change the It Here is example usage for binning and grouping :
|
@ai-naymul is this PR ready for (final) review? If so, can you mark it as such (by pressing the "Ready to review") button? Please also update the PR message with a bit of context (including which problem this solves and how it's implemented on a high level). |
Hi sorry for the inconvenience, I was having some health issues and wasn't able to work on this properly as well. I think need some improvement like need the feedback regarding the return type of the group_by method from @quaquel . I think it's final for review although. I will add some description about this pr as well. |
@ai-naymul are you still interested on working on this? If so I would like to have an explanation of the feature and an example in the PR message. A bit like: #1890 or #1916 (but it can be shorter since it's a smaller feature). |
This has been superseded by #2220. @ai-naymul thanks for the hard work on this. I started from your code in #2220. |
Discussion(#2074)
In this pr I have added a group_by method in the AgentSet class, allowing agents to grouped based on some specific attributes.