projectmesa / mesa
Showing 1 of 2 files from the diff.

@@ -29,6 +29,7 @@
Loading
29 29
    model_cls: Type[Model],
30 30
    parameters: Dict[ParameterName, Union[ParameterValue, Iterable[ParameterValue]]],
31 31
    model_reporters: Any = None,
32 +
    agent_reporters: Any = None,
32 33
    nr_processes: Optional[int] = None,
33 34
    iterations: int = 1,
34 35
    max_steps: int = 1000,
@@ -38,7 +39,11 @@
Loading
38 39
39 40
    kwargs_list = _make_model_kwargs(parameters)
40 41
    process_func = partial(
41 -
        _model_run_func, model_cls, max_steps=max_steps, model_reporters=model_reporters
42 +
        _model_run_func,
43 +
        model_cls,
44 +
        max_steps=max_steps,
45 +
        model_reporters=model_reporters,
46 +
        agent_reporters=agent_reporters,
42 47
    )
43 48
44 49
    total_iterations = len(kwargs_list) * iterations
@@ -49,20 +54,16 @@
Loading
49 54
        if nr_processes == 1:
50 55
            for kwargs in kwargs_list:
51 56
                data = process_func(kwargs)
52 -
                results.append(data)
57 +
                results.extend(data)
53 58
                pbar.update()
54 59
55 60
        else:
56 61
            with mp.Pool(nr_processes) as p:
57 62
                for data in p.imap_unordered(process_func, kwargs_list):
58 -
                    results.append(data)
63 +
                    results.extend(data)
59 64
                    pbar.update()
60 65
61 -
    print(results)
62 -
    df_results = pd.DataFrame(results)
63 -
    print(df_results)
64 -
65 -
    return df_results
66 +
    return results
66 67
67 68
68 69
def _make_model_kwargs(
@@ -86,23 +87,38 @@
Loading
86 87
    kwargs: ModelParameters,
87 88
    max_steps: int,
88 89
    model_reporters: Any,
89 -
) -> Dict[str, Any]:
90 +
    agent_reporters: Any,
91 +
) -> List[Dict[str, Any]]:
90 92
    """Run a single model run."""
91 93
    model = model_cls(**kwargs)
92 94
    while model.running and model.schedule.steps < max_steps:
93 95
        model.step()
94 96
95 -
    data = _collect_data(model, model_reporters)
97 +
    model_data, agent_data = _collect_data(model, model_reporters, agent_reporters)
98 +
99 +
    data = [{**kwargs, **model_data, **agent_datum} for agent_datum in agent_data]
96 100
97 -
    return {**kwargs, **data}
101 +
    return data
98 102
99 103
100 -
def _collect_data(model: Model, model_reporters: Any) -> Any:
101 -
    "stub that just returns the model hash, but should handle the reporters"
102 -
    dc = DataCollector(model_reporters=model_reporters)
104 +
def _collect_data(
105 +
    model: Model,
106 +
    model_reporters: Dict[str, Any] = None,
107 +
    agent_reporters: Dict[str, Any] = None,
108 +
) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]:
109 +
    """Collect model and agent data from a model using mesas datacollector."""
110 +
    dc = DataCollector(model_reporters=model_reporters, agent_reporters=agent_reporters)
103 111
    dc.collect(model)
112 +
104 113
    model_data = {key: value[0] for key, value in dc.model_vars.items()}
105 -
    return model_data
114 +
115 +
    agent_data = []
116 +
    raw_agent_data = dc._agent_records.get(model.schedule.steps, [])
117 +
    for data in raw_agent_data:
118 +
        agent_dict = dict(zip(dc.agent_reporters, data[2:]))
119 +
        agent_dict["AgentID"] = data[1]
120 +
        agent_data.append(agent_dict)
121 +
    return model_data, agent_data
106 122
107 123
108 124
try:
Files Coverage
mesa 83.07%
Project Totals (19 files) 83.07%
1874.1
TRAVIS_PYTHON_VERSION=3.6
TRAVIS_OS_NAME=linux
1874.2
TRAVIS_PYTHON_VERSION=3.7
TRAVIS_OS_NAME=linux
1875.1
TRAVIS_PYTHON_VERSION=3.6
TRAVIS_OS_NAME=linux
1874.3
TRAVIS_PYTHON_VERSION=3.8
TRAVIS_OS_NAME=linux
1875.3
TRAVIS_PYTHON_VERSION=3.8
TRAVIS_OS_NAME=linux
1875.2
TRAVIS_PYTHON_VERSION=3.7
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading