-
Notifications
You must be signed in to change notification settings - Fork 877
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
model: Move random seed and random to __init__ #1940
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1940 +/- ##
==========================================
- Coverage 79.87% 79.82% -0.05%
==========================================
Files 15 15
Lines 1267 1264 -3
Branches 277 277
==========================================
- Hits 1012 1009 -3
Misses 216 216
Partials 39 39 ☔ View full report in Codecov by Sentry. |
Why would this have to wait for 3.0? I am not sure I understand how this is a breaking change? |
Because this PR requires mandatory model I forgot that #2026 hasn't been released as a patch release. cc: @EwoutH we need #2026 in a patch release so that 2.2.x is backward compatible with 2.1.x. |
2.3 is released, so we can start with 3.0 development work, including moving this PR forward! |
@jackiekazil This PR looks good; the ruff failure appears to be outside the code change and I assume it will go away once main is merged into it. |
@rht would you like to rebase this PR and see if we can move it forward, or would you like me to do it? |
Rebased. |
Fixing the tests. |
Unfortunately, the seed param is non-optional in the visualization. The caveat of merging this PR is that users will have to explicitly pass |
mesa/model.py
Outdated
obj._steps = 0 | ||
obj._time = 0 | ||
return obj | ||
|
||
def __init__(self, *args: Any, **kwargs: Any) -> None: |
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 wrap everything into kwargs, instead of having seed=None
explicit?
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.
That makes sense, I'm changing it to that regardless of whether it fixes the visualization situation or not.
I am not intimately familiar with the visualization side of things. Can you explain the problem a bit more (and see my code comment, which might fix this also?). |
All the context you need is in mesa/mesa/visualization/solara_viz.py Lines 128 to 135 in a48dd80
Actually, if the |
Given that `super().__init__()` is now necessary for user's model class `__init__()`, this simplifies the `Model` construct. And `model.random` can be initialized with other RNG objects (`np.random.default_rng(...)`, or any other RNG). Prior discussion projectmesa#1938 Please enter the commit message for your changes. Lines starting
This is because the seed param is no longer always passed by the user to mesa.Model.__init__.
I implemented exactly this way in the 3rd commit. So we're all good now regarding the move to remove It seems to be the build cache that the CI fails. The test_time.py passed locally on my machine. |
I have resolved the conflicts with main. I can confirm that the tests pass fine locally, but still fail on CI. Not sure why or how to move this forward, but it would be great if we can merge this soon. |
You found a nice rabbit hole? Seems some of the schedulers depend in some way on the new method? |
yes this is a real rabbit hole. I have established that the problem occurs only if you instantiate MockModel with keyword arguments inside unittest.TestCase instances. My current idea is just to move all tests over to pytest and see if that fixes the issue. Removing e.g., caches does not solve it. I will be flying tomorrow so then I'll have time to fix this. |
Very particular.
Would be good anyways. |
This is turning into an insane rabbit hole. I have been running many tests in #2295, but still no luck. A short summary
My last resort is eliminating MockModel inside test_time.py and testing all schedulers directly. This is a better test practice anyway. However, I would still love to figure out what the .... is going on here. |
Another update: If I move Anyone any idea? |
To test whether it is the cache problem, the cache can be explicitly deleted at https://github.com/projectmesa/mesa/actions/caches. Another pattern I noticed is that in test_datacollector.py and test_batch_run.py, This is a problem that the user might encounter, so I think it is worth checking the bottom of it instead of sweeping it under the rug. |
I forgot to mention this, but yes, that is the first thing I tested, and no luck.
Yes, but without arguments, it works. The moment keyword arguments are passed to MockModel, the type error occurs. And only if you do this inside a
I would like to get to the bottom of it, but I am quite confident it is not something a user will encounter. Locally, all tests pass, no problem. All examples also pass, no problem. So it really seems a problem related to testing and the CI runners. |
Could be an OS thing, or an version thing. You’re on MacOS right? Which Python version? And Pytest and NumPy? |
Yes, I am on macos which of course we don't test. I have an ubuntu and windows VM, so I can test tomorrow. Python version does not matter, all versions in CI error in the same way. Numpy is not in the picture for the error. |
We do have a macOS run in the CI right? Or do you mean something different? |
You are right, I overlooked it. MacOs with python 3.12. I think I am on 3.11 but I highly doubt that is the problem. |
I was finally able to reproduce this locally. Instead of just |
I have run all tests from within my IDE without a problem. Are you using the command line, because it would be great if I can also reproduce it locally. |
I was using the command line, and also made sure that |
That is in line with my latest test. I copied |
my guess it is this line in |
This seems to be the explanation. This PR removes the need of the SolaraViz code to use |
The CI passed, and running |
Given that
super().__init__()
is now necessary for user's model class__init__()
, the main motivation: this simplifies theModel
construct.And
model.random
can be initialized with other RNG objects (np.random.default_rng(...)
, or any other RNG).Prior discussion #1938.
This is a breaking change and should wait until Mesa 3.0.