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

Update random number generator #2037

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Update random number generator #2037

wants to merge 13 commits into from

Conversation

hrobarts
Copy link
Contributor

@hrobarts hrobarts commented Jan 14, 2025

Changes

Update AcquistionGeometry, ImageGeometry and VectorGeometry allocate method to use numpy PCG64DXSM random number generator. Avoids creating an array of float64 then casting to the array dtype, also works with seed.

This update will no longer allow users to set the random seed globally, which may have an impact where algorithms use allocate internally with no option to pass the seed. This is currently a problem with the PowerMethod - see #1585 for PR allowing seed to be passed.

Testing you performed

Tests in test_DataContainer and test_AcquisitionGeometry

Please add any demo scripts to https://github.com/TomographicImaging/CIL-Demos/tree/main/misc

Related issues/links

Closes #2033
Might close #804

Checklist

  • I have performed a self-review of my code
  • I have added docstrings in line with the guidance in the developer guide
  • I have updated the relevant documentation
  • I have implemented unit tests that cover any new or modified functionality
  • CHANGELOG.md has been updated with any functionality change
  • Request review from all relevant developers
  • Change pull request label to 'Waiting for review'

Contribution Notes

Please read and adhere to the developer guide and local patterns and conventions.

  • The content of this Pull Request (the Contribution) is intentionally submitted for inclusion in CIL (the Work) under the terms and conditions of the Apache-2.0 License
  • I confirm that the contribution does not violate any intellectual property rights of third parties

@hrobarts hrobarts self-assigned this Jan 14, 2025
@hrobarts
Copy link
Contributor Author

I looked at some different methods for random number generation

  1. Previous method
    Using numpy.random.seed() and numpy.random.random() directly.
    Cons: doesn't let us choose the data type
  2. Calling numpy.random.Generator() in each file
    Cons: no global seed
  3. Use numpy.random.Generator() as a global rng in a utils file, with a set_seed method
stream = np.random.PCG64DXSM
global_rng = np.random.Generator(stream(None))

def set_seed(seed):
    global global_rng
    global_rng = np.random.Generator(stream(seed))

Pros: global seed
Cons: using a fixed bit generator

  1. Use numpy.random.Generator() in a RandomGenerator class in a utils file
class RandomGenerator:
    def __init__(self, seed=None):
        self._seed = seed
        self._rng = np.random.Generator(np.random.PCG64DXSM(seed))

    def set_seed(self, seed):
        self._seed = seed
        self._rng = np.random.Generator(np.random.PCG64DXSM(seed))

    def __getattr__(self, name):
        return getattr(self._rng, name)

global_rng = RandomGenerator(seed=None)

Use __getattr__ method so we can directly call the random numbers like global_rng.random() rather than something like global_rng.get_rng().random()
Pros: global seed
Cons: complicated

Method Pros Cons
numpy.random.seed() in each file Global seed Doesn't let us choose the data type
numpy.random.Generator(seed) in each file No global seed
Global Generator with set_seed method Global seed Less flexible for future changes
RandomGenerator Class with __getattr__ Global seed Complicated

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

Successfully merging this pull request may close these issues.

Update random allocation in DataContainer allocate random seed does not work
1 participant