Skip to content
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

Use correct random number gen, and make seeds reproducible. #2392

Open
kosack opened this issue Sep 4, 2023 · 4 comments
Open

Use correct random number gen, and make seeds reproducible. #2392

kosack opened this issue Sep 4, 2023 · 4 comments

Comments

@kosack
Copy link
Contributor

kosack commented Sep 4, 2023

Describe the bug

Anywhere where random numbers are used should both use Numpy's new Generator API and also we should probably allow a configurable seed for reproducibility (and write the used seed to the provenance output as well).

We could define some core "services" of Tools/Components that have a shared config, with a common random number generator being one of them.

Supporting information

> ruff --select NPY check . --exclude=tests | grep random

ctapipe/fitting.py:79:14: NPY002 Replace legacy `np.random.choice` call with `np.random.Generator`
ctapipe/fitting.py:149:5: NPY002 Replace legacy `np.random.seed` call with `np.random.Generator`
ctapipe/image/modifications.py:73:9: NPY002 Replace legacy `np.random.seed` call with `np.random.Generator`
ctapipe/image/modifications.py:80:20: NPY002 Replace legacy `np.random.poisson` call with `np.random.Generator`
ctapipe/image/modifications.py:94:28: NPY002 Replace legacy `np.random.multinomial` call with `np.random.Generator`

Additional context
Add any other context about the problem here.

@kosack kosack changed the title Use correct random number gen, and make seeds reprodicible. Use correct random number gen, and make seeds reproducible. Sep 4, 2023
@kosack
Copy link
Contributor Author

kosack commented Sep 4, 2023

I.e. there should be one random number generator with a configurable seed that can be used by all components.

@maxnoe
Copy link
Member

maxnoe commented Sep 4, 2023

Unfortunately, it's not that easy.

The occurences you found are in numba jitted code, which just supports one global rng via those "legacy" functions.

@kosack
Copy link
Contributor Author

kosack commented Sep 4, 2023

It does say this in the manual:

Numba supports numpy.random.Generator() objects. As of version 0.56, users can pass individual NumPy Generator objects into Numba functions and use their methods inside the functions. The same algorithms are used as NumPy for random number generation hence maintaining parity between the random number generated using NumPy and Numba under identical arguments (also the same documentation notes as NumPy Generator methods apply). The current Numba support for Generator is not thread-safe, hence we do not recommend using Generator methods in methods with parallel execution logic.

So as long as we don't use parallel=True, it should be able to use a centrally configured Generator. Haven't tested it though. I don't see a random.choice method in Generator though.

@maxnoe
Copy link
Member

maxnoe commented Sep 4, 2023

Ah, great, that's pretty new.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants