Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement experimental DataCollector API v3 #2199

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mesa/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from mesa.experimental import cell_space
from mesa.experimental.datacollector import DataCollector

__all__ = ["cell_space"]
__all__ = ["cell_space", "DataCollector"]
42 changes: 42 additions & 0 deletions mesa/experimental/datacollector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from collections import defaultdict

import pandas as pd


class DataCollector:
def __init__(self, model, group_reporters, groups=None):
self.model = model
self.group_reporters = group_reporters
self.groups = groups

Check warning on line 10 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L8-L10

Added lines #L8 - L10 were not covered by tests
self.data = defaultdict(lambda: defaultdict(list))

def get_group(self, group_name):
if group_name == "model":
return self.model

Check warning on line 15 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L15

Added line #L15 was not covered by tests
elif group_name == "agents":
return self.model.agents

Check warning on line 17 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L17

Added line #L17 was not covered by tests
else:
try:
return getattr(self.model, group_name)
except AttributeError as e:
raise Exception(f"Unknown group: {group_name}") from e

Check warning on line 22 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L19-L22

Added lines #L19 - L22 were not covered by tests

def report(self, reporter, group):
if group is self.model:
return reporter(group)

Check warning on line 26 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L26

Added line #L26 was not covered by tests
if isinstance(reporter, str):
if hasattr(group, "get"):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be replaced with isinstance(group, AgentSet) or isinstance of Group.

return group.get(reporter)

Check warning on line 29 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L29

Added line #L29 was not covered by tests
else:
raise Exception()
return reporter(group)

Check warning on line 32 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L31-L32

Added lines #L31 - L32 were not covered by tests

def collect(self):
for group_name, reporters in self.group_reporters.items():
group = self.get_group(group_name)

Check warning on line 36 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L36

Added line #L36 was not covered by tests
for name, reporter in reporters.items():
value = self.report(reporter, group)
self.data[group_name][name].append(value)

Check warning on line 39 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L38-L39

Added lines #L38 - L39 were not covered by tests

def to_df(self, group_name):
return pd.DataFrame(self.data[group_name])

Check warning on line 42 in mesa/experimental/datacollector.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/datacollector.py#L42

Added line #L42 was not covered by tests
2 changes: 1 addition & 1 deletion mesa/visualization/components/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
def PlotMatplotlib(model, measure, dependencies: list[any] | None = None):
fig = Figure()
ax = fig.subplots()
df = model.datacollector.get_model_vars_dataframe()
df = model.datacollector.to_df("model")

Check warning on line 120 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L120

Added line #L120 was not covered by tests
if isinstance(measure, str):
ax.plot(df.loc[:, measure])
ax.set_ylabel(measure)
Expand Down