You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Currently, the DataCollector's model_reporters validation has limitations when handling string attributes. The issue arises because:
Model reporters can accept four types of inputs:
model_reporters= {
"Count": lambdam: len(m.agents), # Lambda function"Status": "market_status", # Attribute name (string)"Metric": model.calculate_metric, # Method reference"Custom": [calculate_wealth, [param1, param2]] # Function with parameters
}
When using string attributes (e.g., "market_status"), there's no validation to check if these attributes actually exist in the model. This leads to:
Silent failures when collecting undefined attributes
Confusion when attributes are misspelled or not initialized
Delayed error detection until runtime
Adding a validation mechanism to ensure model_reporters keys mapped to callable functions or valid strings would greatly improve error detection and provide better debugging experiences for users.
Expected behavior
When using DataCollector, if an invalid value is provided in model_reporters (e.g., a non-callable object or undefined function):
Raise a ValueError with a clear error message, such as:
"Invalid reporter for 'Gini': must be a callable function/method or a valid attribute name as a string."
This prompt will help users:
Understand their mistake.
Quickly identify whether the provided function (e.g., compute_gini) is missing, incorrectly implemented, or improperly referenced.
`frommesa.datacollectionimportDataCollectorcollector=DataCollector(
model_reporters={"Gini": "compute_gini"}, # Error: compute_gini is not callable agent_reporters={"Wealth": "wealth"}
)`
Expected Output:
`ValueError: Invalid reporter for 'Gini': must be a callable function/method or a valid attribut`
Proposed Solution
Modify the DataCollector class to include validation for model_reporters, similar to the following:
def_validate_model_reporter(self, name, reporter):
# Type 1: Lambda functionifcallable(reporter):
return# Type 2: Class attributes (string)elifisinstance(reporter, str):
return# Type xxx: other types mechanism to warnelse:
raiseValueError(
f"Invalid reporter for '{name}': must be a callable function/method or a valid attribute name as a string."
)
I like the idea of this issue and the PR. I hope to find time to look at it more closely soon. Its a part of the code base I am not particularly familiar with, however.
I like the idea of this issue and the PR. I hope to find time to look at it more closely soon. Its a part of the code base I am not particularly familiar with, however.
Thanks for the attention to issues. During the progress of the PR, there were some problems. Let me try to fix them.
Describe the bug
Currently, the DataCollector's model_reporters validation has limitations when handling string attributes. The issue arises because:
Model reporters can accept four types of inputs:
When using string attributes (e.g., "market_status"), there's no validation to check if these attributes actually exist in the model. This leads to:
Adding a validation mechanism to ensure model_reporters keys mapped to callable functions or valid strings would greatly improve error detection and provide better debugging experiences for users.
Expected behavior
When using DataCollector, if an invalid value is provided in model_reporters (e.g., a non-callable object or undefined function):
Raise a ValueError with a clear error message, such as:
"Invalid reporter for 'Gini': must be a callable function/method or a valid attribute name as a string."
This prompt will help users:
To Reproduce
Valid Example:
Invalid Example (Missing Function):
Expected Output:
Proposed Solution
Modify the DataCollector class to include validation for model_reporters, similar to the following:
Additional context
The text was updated successfully, but these errors were encountered: