-
Notifications
You must be signed in to change notification settings - Fork 923
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
issue a user warning if random is None #2479
Conversation
for more information, see https://pre-commit.ci
The release/deploy action is failing with |
Performance benchmarks:
|
Looks unrelated to this PR, it happens also on the main branch. |
Thanks! The example models are being tested with werror, so there can't be warnings in them. From the CI it seems a few need to be updated. |
Which highlights the usefulness of these warnings! |
Was just investigating the release build issue, as hatchling tagged a new build which fixed their broken build form this morning. Reran the release job and it passes like it should. |
Any other comments? I suggest fixing examples once this is merged. |
Sorry, bit of a miscommunication. I would like the examples updated in this PR (was waiting for that). That's the nice thing now we have the examples in the main repo, we can directly show how it affects examples and the CI keeps working. |
I think you are mistaken. Below, I show the errors. These are examples in the examples repo, not those in the main repo. The examples in the main repo are part of the unit tests of the main repo.
|
You're right, my bad. We made a slight regression in example testing, because the unittest weren't running with
I will look into if we can better configure this in the CI - and if other warnings have creeped in. |
I am not 100% convinced we need a warning here. In my experience people receive warnings very differently. I, for example, am always a bit scared by them and have the feeling something is wrong. |
I am not sure what you are suggesting. If this were log messages, then yes it would make sense to issue an INFO level log message. However, I am using the warnings library here, so it will always be some kind of warning, unless I miss something obvious? |
for more information, see https://pre-commit.ci
Not everything listed there is examples related. I have now fixed the examples so they all correctly instantiate a new-style space with an explicit random number generator. I also fixed all cell_space related unittests to longer issue the warnings added in this PR. Do you want me to fix the as an aside: it might be worth reviewing the property layer user warning. I have no idea how to get rid of that one even in my own code. |
What if we let users pass a certain value to def __init__(self, random: Union[Random, bool, None] = None):
if random is None:
warnings.warn(
"Random number generator not specified, this can make models non-reproducible. "
"Please pass a random number generator explicitly to make it reproducible, or pass random=True to create a new random number generator."
UserWarning,
stacklevel=2,
)
self.random = Random()
elif random is True:
self.random = Random() # Create new RNG without warning
else:
self.random = random |
I don't think thats a common use case. I was thinking more about just ignoring the argument. If you already make a choice about what to pass to "random", you can just pass a random generator. But @quaquel is right, there is no such functionality as I imagined. I am sure I saw some "info" messages before, but then it was probably just some regular "print" calls formatted nicely. So I would say just leave it as it is now in this PR. |
A lot of users are going to encounter this warning, so I really want is documented well. It should be clear:
I think that's too much for one warning message, so maybe we can include a nice write up somewhere, and then link to it in the error message |
It can originate in three places: The simple solution is to just add simple note to the docstring of these three classes. Something like """
note: A `UserWarning` is issued if `random=None`. You can resolve this warning by explicitly passing a random number generator. In most cases, this will be the seeded random number generator in the model. So, you would do `random=self.random` in a `Model` or `Agent` instance.
""" |
Okay, that helps a lot. So it's mainly for the discrete spaces. In that case I'm fine with just some added docstring. |
Aside from the docstring, would this warning message make it even clearer? warnings.warn(
f"Random number generator not specified while initializing {self.__class__.__name__}. "
"This can make models non-reproducible. To fix this, pass the model's random "
"number generator (e.g., random=self.random from within Model or Agent classes)",
UserWarning,
stacklevel=2,
) |
The latter part could be added, the first part about which class will be clear from the warning context |
Adds a user warning to
AgentSet
,CellCollection
, andDiscreteSpace
if they are initialized without an explicit random number generator.closes #2477