-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatacollector.py
61 lines (50 loc) · 2.06 KB
/
datacollector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
""" This file contains the datacollector, extending the datacollector as
defined in the MESA framework.
"""
from typing import Dict, Tuple
import numpy as np
import pandas as pd
from mesa.datacollection import DataCollector
class PDTDataCollector(DataCollector):
""" Defines the datacollector.
"""
def __init__(self, model_reporters: dict = None, agent_reporters: dict = None,
tables: dict = None, proportion_reporters: Dict[str, Tuple[str, str]] = None):
""" Initializes the datacollector. In addition to the initialization of the
MESA datacollector, proportional reporters are added. In the current implementation
this consists of the trust in stranger proportion.
"""
super().__init__(model_reporters, agent_reporters, tables)
self.proportion_reporters = proportion_reporters
def _get_agents_vars_sum(self):
""" TODO
"""
agent_vars_sum = {}
records = self._agent_records.values()
records = np.array(list(records))
rep_names = self.agent_reporters.keys()
for i, rep_name in zip(range(2, 2 + len(rep_names)), rep_names):
summed_var = np.sum(records[:, :, i], axis=0)
agent_vars_sum[rep_name] = summed_var
return agent_vars_sum
def get_agent_vars_sum_dataframe(self):
""" TODO
"""
agent_vars_sum = self._get_agents_vars_sum()
return pd.DataFrame(agent_vars_sum)
def _get_proportions(self):
""" TODO
"""
agent_proportions_vars = {}
agent_vars_sum = self._get_agents_vars_sum()
for rep_name, prop_names in self.proportion_reporters.items():
a = agent_vars_sum[prop_names[0]]
b = agent_vars_sum[prop_names[1]]
prop = a/b
agent_proportions_vars[rep_name] = prop
return agent_proportions_vars
def get_agent_props_dataframe(self):
""" TODO
"""
agent_proportions = self._get_proportions()
return pd.DataFrame(agent_proportions)