-
Notifications
You must be signed in to change notification settings - Fork 928
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
Enhance DataCollector to validate model_reporters functions #2605
Open
peter-kinger
wants to merge
1
commit into
projectmesa:main
Choose a base branch
from
peter-kinger:enhance-datacollector-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,13 +134,88 @@ | |
for name, columns in tables.items(): | ||
self._new_table(name, columns) | ||
|
||
def _validate_model_reporter(self, name, reporter, model): | ||
"""Validate model reporter and issue warnings if necessary. | ||
|
||
Args: | ||
name: Name of the reporter | ||
reporter: Reporter definition | ||
model: Model instance | ||
""" | ||
# Type 1: Lambda function | ||
if isinstance(reporter, types.LambdaType): | ||
try: | ||
# Try to call the lambda with a model instance | ||
reporter(model) | ||
except Exception as e: | ||
warnings.warn( | ||
f"Warning: Lambda reporter '{name}' failed: {e!s}\n" | ||
f"Example of valid lambda: lambda m: len(m.agents)", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
return | ||
|
||
# Type 2: Method of class/instance | ||
if callable(reporter) and not isinstance(reporter, types.LambdaType): | ||
try: | ||
# Try to call the method | ||
reporter(model) | ||
except Exception as e: | ||
warnings.warn( | ||
f"Warning: Method reporter '{name}' failed: {e!s}\n" | ||
f"Example of valid method: self.get_agent_count or Model.get_agent_count", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
return | ||
|
||
# Type 3: Class attributes (string) | ||
if isinstance(reporter, str): | ||
if not hasattr(model, reporter): | ||
warnings.warn( | ||
f"Warning: Model reporter '{name}' references attribute '{reporter}' " | ||
f"which is not defined in the model.\n" | ||
f"Example of valid attribute: 'model_attribute'", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
return | ||
|
||
# Type 4: Function with parameters in list | ||
if isinstance(reporter, list): | ||
if not reporter or not callable(reporter[0]): | ||
warnings.warn( | ||
f"Warning: Invalid function list format for reporter '{name}'.\n" | ||
f"First element must be a callable function.\n" | ||
f"Example: [function, [param1, param2]]", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
return | ||
|
||
# If none of the above types match | ||
warnings.warn( | ||
f"Warning: Model reporter '{name}' has invalid type: {type(reporter)}.\n" | ||
f"Must be one of:\n" | ||
f"1. Lambda function: lambda m: len(m.agents)\n" | ||
f"2. Method: self.get_count or Model.get_count\n" | ||
f"3. Attribute name (str): 'model_attribute'\n" | ||
f"4. Function list: [function, [param1, param2]]", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
|
||
def _new_model_reporter(self, name, reporter): | ||
"""Add a new model-level reporter to collect. | ||
|
||
Args: | ||
name: Name of the model-level variable to collect. | ||
reporter: Attribute string, or function object that returns the | ||
variable when given a model instance. | ||
reporter: Can be one of four types: | ||
1. Attribute name (str): "attribute_name" | ||
2. Lambda function: lambda m: len(m.agents) | ||
3. Method: model.get_count or Model.get_count | ||
4. List of [function, [parameters]] | ||
""" | ||
self.model_reporters[name] = reporter | ||
self.model_vars[name] = [] | ||
|
@@ -263,6 +338,9 @@ | |
"""Collect all the data for the given model object.""" | ||
if self.model_reporters: | ||
for var, reporter in self.model_reporters.items(): | ||
# Add validation | ||
self._validate_model_reporter(var, reporter, model) | ||
|
||
Comment on lines
+341
to
+343
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this imply that the validation is done every single time you try to collect the data? That seems inefficient and overkill. |
||
# Check if lambda or partial function | ||
if isinstance(reporter, types.LambdaType | partial): | ||
# Use deepcopy to store a copy of the data, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why issue a warning instead of an exception?