-
Notifications
You must be signed in to change notification settings - Fork 39
Modernize NumPy Random Functions and Fix Mypy Errors for #756 #760
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
base: master
Are you sure you want to change the base?
Modernize NumPy Random Functions and Fix Mypy Errors for #756 #760
Conversation
|
Thank you @mohamed-laarej. I am not sure why you introduced a function |
|
Hi @jonbrenas, Thank you so much for taking the time to review my pull request and for your feedback! I really appreciate it. Regarding your questions:
I understand that these errors might not be appearing in the CI environment or on your end. It's possible there are slight environment differences, or perhaps other changes resolved the underlying type issues. My intention was just to ensure the code passed If these specific casts and the Thanks again for your guidance! |
|
Thank you @mohamed-laarej . I think both the casts and the additional function are fine. I feel like they clutter the code a bit but not more than other changes that are required so I don't think they are really problematic. I know they don't raise errors in It would also make sense to unpin the value of |
|
Thanks @jonbrenas, that makes sense! I'll work on fixing the remaining Interestingly, my local environment was already using an unpinned NumPy version during my initial work, but I'll ensure the project configuration reflects this now. I'll push the updates once they're ready. |
generator (rng) instead of legacy np.random or Python's random module and unpins the NumPy version in pyproject.toml
- Replaced all Python random.choice() with rng.choice() for consistency - Replaced random.sample() with rng.choice(..., replace=False) - Added .tolist() to convert NumPy arrays to Python lists where needed - Added str() casting for np.str_ values to ensure Python string compatibility - Fixed 'low >= high' errors in rng.integers() calls by ensuring high > low - Specifically fixed tests/anoph/test_frq.py by changing rng.integers(1, len(cohorts_areas)) to rng.integers(1, len(cohorts_areas)+1) to avoid invalid ranges - Applied int() casting to NumPy integer types where Python int was expected - Fixed site_mask selection to ensure only valid masks are used for each test context Addresses feedback from PR malariagen#760 and resolves test failures.
Description:
This PR resolves issue #756 by updating test files to use NumPy’s modern Generator API and fixing
mypytestsChanges:
np.random.randint,np.random.choice, ...) withrng.integers,rng.choice, etc., across test files.rng = np.random.default_rng(seed=42)in test files, with localrngoverrides where needed for reproducibility (simulate_cnv_discordant_read_calls).mypyerrors with type casting in affected files.Testing:
mypy malariagen_data tests --ignore-missing-imports: No issues.pre-commit run --all-files: Passed.poetry run pytest -v tests/anoph: All tests passed.Notes:
Used
NumPy 2.2.4.Open to adopting local
rnginstances if preferred for test isolation.Thanks for reviewing!
Fixes #756