Skip to content

Commit 95cf996

Browse files
committed
Refactor DataCollector model reporters for improved efficiency and readability
1 parent 6f62e99 commit 95cf996

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

docs/general/user-guide/4_datacollector.ipynb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
},
176176
{
177177
"cell_type": "code",
178-
"execution_count": 20,
178+
"execution_count": null,
179179
"id": "5f14f38c",
180180
"metadata": {},
181181
"outputs": [
@@ -198,8 +198,10 @@
198198
"model_csv.dc = DataCollector(\n",
199199
" model=model_csv,\n",
200200
" model_reporters={\n",
201-
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
202-
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
201+
" \"total_wealth\": lambda m: sum(\n",
202+
" s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n",
203+
" ),\n",
204+
" \"n_agents\": lambda m: len(m.sets.ids),\n",
203205
" },\n",
204206
" agent_reporters={\n",
205207
" \"wealth\": \"wealth\",\n",
@@ -226,7 +228,7 @@
226228
},
227229
{
228230
"cell_type": "code",
229-
"execution_count": 21,
231+
"execution_count": null,
230232
"id": "8763a12b2bbd4a93a75aff182afb95dc",
231233
"metadata": {
232234
"editable": true
@@ -249,8 +251,10 @@
249251
"model_parq.dc = DataCollector(\n",
250252
" model=model_parq,\n",
251253
" model_reporters={\n",
252-
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
253-
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
254+
" \"total_wealth\": lambda m: sum(\n",
255+
" s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n",
256+
" ),\n",
257+
" \"n_agents\": lambda m: len(m.sets.ids),\n",
254258
" },\n",
255259
" agent_reporters={\n",
256260
" \"wealth\": \"wealth\",\n",
@@ -279,7 +283,7 @@
279283
},
280284
{
281285
"cell_type": "code",
282-
"execution_count": 22,
286+
"execution_count": null,
283287
"id": "7cdc8c89c7104fffa095e18ddfef8986",
284288
"metadata": {
285289
"editable": true
@@ -289,7 +293,14 @@
289293
"model_s3 = MoneyModel(1000)\n",
290294
"model_s3.dc = DataCollector(\n",
291295
" model=model_s3,\n",
292-
" model_reporters={\n",
296+
" model_reporters = {\n",
297+
"\"total_wealth\": lambda m: sum(\n",
298+
"s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n",
299+
"),\n",
300+
"\"n_agents\": lambda m: len(m.sets.ids),\n",
301+
"}\n",
302+
"\n",
303+
"\n",
293304
" \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n",
294305
" \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n",
295306
" },\n",

0 commit comments

Comments
 (0)