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 modern RNG #3041

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions scanpy/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import h5py
import numpy as np
from anndata import __version__ as anndata_version
from numpy.random import MT19937
from numpy.random import Generator as NPGenerator
from numpy.typing import NDArray
from packaging.version import Version
from scipy import sparse
from sklearn.utils import check_random_state
Expand Down Expand Up @@ -70,11 +73,14 @@ class RNGIgraph:
See :func:`igraph.set_random_number_generator` for the requirements.
"""

def __init__(self, random_state: int = 0) -> None:
self._rng = check_random_state(random_state)
def __init__(self, random_state: AnyRandom = 0) -> None:
mt = MT19937()
mt.state = check_random_state(random_state).get_state()
self._rng = NPGenerator(mt)

def __getattr__(self, attr: str):
return getattr(self._rng, "normal" if attr == "gauss" else attr)
self.gauss = self._rng.normal
self.random = self._rng.uniform
self.randint = partial(self._rng.integers, dtype=int)


def ensure_igraph() -> None:
Expand Down
Loading