Skip to content

Commit

Permalink
Explicit datacollector import
Browse files Browse the repository at this point in the history
Use from mesa.datacollection import DataCollector
  • Loading branch information
EwoutH committed Sep 20, 2024
1 parent de9e87f commit 37f89d3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion benchmarks/BoltzmannWealth/boltzmann_wealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import mesa
from mesa.datacollection import DataCollector


def compute_gini(model):
Expand Down Expand Up @@ -44,7 +45,7 @@ def __init__(self, seed=None, n=100, width=10, height=10):
self.num_agents = n
self.grid = mesa.space.MultiGrid(width, height, True)
self.schedule = mesa.time.RandomActivation(self)
self.datacollector = mesa.DataCollector(
self.datacollector = DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)
# Create agents
Expand Down
3 changes: 2 additions & 1 deletion docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ You'd add a data collector to the model like this:

```python
import mesa
from mesa.datacollection import DataCollector

# ...

class MyModel(mesa.Model):
def __init__(self, n_agents):
# ...
self.dc = mesa.DataCollector(model_reporters={"agent_count":
self.dc = DataCollector(model_reporters={"agent_count":
lambda m: m.schedule.get_agent_count()},
agent_reporters={"name": lambda a: a.name})

Expand Down
3 changes: 2 additions & 1 deletion docs/tutorials/MoneyModel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""a simple version of the boltman wealth model"""

import mesa
from mesa.datacollection import DataCollector


def compute_gini(model):
Expand Down Expand Up @@ -71,7 +72,7 @@ def __init__(self, N, width, height):
y = self.random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))

self.datacollector = mesa.DataCollector(
self.datacollector = DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)

Expand Down
7 changes: 4 additions & 3 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
"outputs": [],
"source": [
"import mesa\n",
"from mesa.datacollection import DataCollector\n",
"\n",
"# Data visualization tools.\n",
"import seaborn as sns\n",
Expand Down Expand Up @@ -751,7 +752,7 @@
"\n",
"The data collector stores three categories of data: model-level variables, agent-level variables, and tables (which are a catch-all for everything else). Model- and agent-level variables are added to the data collector along with a function for collecting them. Model-level collection functions take a model object as an input, while agent-level collection functions take an agent object as an input. Both then return a value computed from the model or each agent at their current state. When the data collector’s `collect` method is called, with a model object as its argument, it applies each model-level collection function to the model, and stores the results in a dictionary, associating the current value with the current step of the model. Similarly, the method applies each agent-level collection function to each agent currently in the schedule, associating the resulting value with the step of the model, and the agent’s `unique_id`.\n",
"\n",
"Let's add a DataCollector to the model with [`mesa.DataCollector`](https://github.com/projectmesa/mesa/blob/main/mesa/datacollection.py), and collect two variables. At the agent level, we want to collect every agent's wealth at every step. At the model level, let's measure the model's [Gini Coefficient](https://en.wikipedia.org/wiki/Gini_coefficient), a measure of wealth inequality."
"Let's add a [`DataCollector`](https://github.com/projectmesa/mesa/blob/main/mesa/datacollection.py) to the model and collect two variables. At the agent level, we want to collect every agent's wealth at every step. At the model level, let's measure the model's [Gini Coefficient](https://en.wikipedia.org/wiki/Gini_coefficient), a measure of wealth inequality."
]
},
{
Expand Down Expand Up @@ -818,7 +819,7 @@
" y = self.random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))\n",
"\n",
" self.datacollector = mesa.DataCollector(\n",
" self.datacollector = DataCollector(\n",
" model_reporters={\"Gini\": compute_gini}, agent_reporters={\"Wealth\": \"wealth\"}\n",
" )\n",
"\n",
Expand Down Expand Up @@ -1060,7 +1061,7 @@
" y = self.random.randrange(self.grid.height)\n",
" self.grid.place_agent(a, (x, y))\n",
"\n",
" self.datacollector = mesa.DataCollector(\n",
" self.datacollector = DataCollector(\n",
" model_reporters={\"Gini\": compute_gini},\n",
" agent_reporters={\"Wealth\": \"wealth\", \"Steps_not_given\": \"steps_not_given\"},\n",
" )\n",
Expand Down

0 comments on commit 37f89d3

Please sign in to comment.