@@ -8,46 +8,53 @@ class DataCollector:
8
8
Example: a model consisting of a hybrid of Boltzmann wealth model and
9
9
Epstein civil violence.
10
10
```
11
- def get_citizen():
12
- return model.get_agents_of_type(Citizen)
11
+ groups = {
12
+ "quiescents": lambda: model.agents.select(
13
+ agent_type=Citizen, filter_func=lambda a: a.condition == "Quiescent"
14
+ ),
15
+ "citizens": lambda: model.get_agents_of_type(Citizen),
16
+ }
13
17
14
18
collectors = {
15
- model: {
16
- "n_quiescent": lambda model: len(
17
- model.agents.select(
18
- agent_type=Citizen,
19
- filter_func=lambda a: a.condition == "Quiescent"
20
- )
21
- ),
22
- "gini": lambda model: calculate_gini(model.agents.get("wealth"))
23
- },
24
- get_citizen: {"condition": condition},
25
- # This is a string, because model.agents may refer to a different
26
- # object, over time.
27
- "agents": {"wealth": "wealth"}
19
+ ("n_quiescent", "quiescents"): len,
20
+ ("gini", "model"): lambda model: calculate_gini(model.agents.get("wealth")),
21
+ # A better way to do the former:
22
+ ("gini", "agents"): lambda agents: calculate_gini(agents.get("wealth")),
23
+ ("gini_quiescent", "quiescents"): lambda agents: calculate_gini(
24
+ agents.get("wealth")
25
+ ),
26
+ ("condition", "citizens"): "condition",
27
+ # "agents" is a string, because model.agents may refer to a different
28
+ # object, over time, when accessed from scratch each time.
29
+ ("wealth", "agents"): "wealth",
28
30
}
31
+
29
32
# Then finally
30
33
model.datacollector = DataCollector(model, collectors=collectors).collect()
31
34
```
32
35
"""
33
36
34
- def __init__ (self , model , collectors = None ) -> "DataCollector" :
37
+ def __init__ (self , model , groups = None , collectors = None ) -> "DataCollector" :
35
38
self .model = model
39
+ self .groups = groups
36
40
self .collectors = collectors
37
41
self .data_collection = defaultdict (list )
38
42
return self
39
43
40
44
def collect (self ) -> "DataCollector" :
41
- for group , group_collector in self .collectors .items ():
45
+ group_data = defaultdict (dict )
46
+ for (name , group ), collector in self .collectors .items ():
42
47
group_object = group
43
48
if group == "agents" :
44
49
group_object = self .model .agents
45
50
elif callable (group ):
46
51
group_object = group ()
47
- data = {
48
- name : self ._collect_group (group_object , collector )
49
- for name , collector in group_collector .items ()
50
- }
52
+ elif isinstance (group , str ):
53
+ group_object = self .groups [group ]
54
+
55
+ group_data [group ][name ] = self ._collect_group (group_object , collector )
56
+
57
+ for group , data in group_data .items ():
51
58
self .data_collection [group ].append (data )
52
59
return self
53
60
0 commit comments