Skip to content

Commit c682745

Browse files
Ben-geopre-commit-ci[bot]adamamer20
authored
Documentation/datacollector (#173)
* api reference * classes data collector * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added dc in class and introductory tutorial * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * doc error * added ipynb doc * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * edit usability * better doc * import datacollector from mesa_frames --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Adam Amer <[email protected]>
1 parent f238736 commit c682745

File tree

9 files changed

+445
-19
lines changed

9 files changed

+445
-19
lines changed

docs/api/index.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@ This page provides a high-level overview of all public mesa-frames objects, func
2424
.. toctree::
2525
:maxdepth: 2
2626

27-
reference/space/index
27+
reference/space/index
28+
29+
.. grid-item-card::
30+
31+
.. toctree::
32+
:maxdepth: 2
33+
34+
reference/datacollector
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Data Collection
2+
=====
3+
4+
.. currentmodule:: mesa_frames
5+
6+
.. autoclass:: DataCollector
7+
:members:
8+
:inherited-members:
9+
:autosummary:
10+
:autosummary-nosignatures:

docs/general/user-guide/1_classes.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,30 @@ class GridWorld(ModelDF):
6464
```
6565

6666
A continuous GeoSpace, NetworkSpace, and a collection to have multiple spaces in the models are in the works! 🚧
67+
68+
## DataCollector 🗂️
69+
70+
`DataCollector` records model- and agent-level data during simulation.
71+
You configure what to collect, how to store it, and when to trigger collection.
72+
73+
Example:
74+
75+
```python
76+
class ExampleModel(ModelDF):
77+
def __init__(self):
78+
super().__init__()
79+
self.agents = MoneyAgent(self)
80+
self.datacollector = DataCollector(
81+
model=self,
82+
model_reporters={"total_wealth": lambda m: m.agents["wealth"].sum()},
83+
agent_reporters={"wealth": "wealth"},
84+
storage="csv",
85+
storage_uri="./data",
86+
trigger=lambda m: m.schedule.steps % 2 == 0
87+
)
88+
89+
def step(self):
90+
self.agents.step()
91+
self.datacollector.conditional_collect()
92+
self.datacollector.flush()
93+
```

docs/general/user-guide/2_introductory-tutorial.ipynb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,32 @@
4949
"metadata": {},
5050
"outputs": [],
5151
"source": [
52-
"from mesa_frames import ModelDF, AgentSetPolars\n",
52+
"from mesa_frames import ModelDF, AgentSetPolars, DataCollector\n",
5353
"\n",
5454
"\n",
5555
"class MoneyModelDF(ModelDF):\n",
5656
" def __init__(self, N: int, agents_cls):\n",
5757
" super().__init__()\n",
5858
" self.n_agents = N\n",
5959
" self.agents += agents_cls(N, self)\n",
60+
" self.datacollector = DataCollector(\n",
61+
" model=self,\n",
62+
" model_reporters={\"total_wealth\": lambda m: m.agents[\"wealth\"].sum()},\n",
63+
" agent_reporters={\"wealth\": \"wealth\"},\n",
64+
" storage=\"csv\",\n",
65+
" storage_uri=\"./data\",\n",
66+
" trigger=lambda m: m.schedule.steps % 2 == 0,\n",
67+
" )\n",
6068
"\n",
6169
" def step(self):\n",
6270
" # Executes the step method for every agentset in self.agents\n",
6371
" self.agents.do(\"step\")\n",
6472
"\n",
6573
" def run_model(self, n):\n",
6674
" for _ in range(n):\n",
67-
" self.step()"
75+
" self.step()\n",
76+
" self.datacollector.conditional_collect\n",
77+
" self.datacollector.flush()"
6878
]
6979
},
7080
{

0 commit comments

Comments
 (0)