Commit ccbf5012 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

Merge branch 'fix_random' into 'NIFTy_6'

workaround for https://github.com/numpy/numpy/issues/16539

See merge request !532
parents 2e470ca7 40180f55
Pipeline #76389 passed with stages
in 13 minutes and 2 seconds
......@@ -77,10 +77,18 @@ objects, generate new ones via :func:`spawn_sseq()`.
import numpy as np
# fix for numpy issue #16539
def _fix_seed(seed):
if isinstance(seed, int):
return (seed, 0, 0, 0)
raise TypeError("random seed should have integer type")
# Stack of SeedSequence objects. Will always start out with a well-defined
# default. Users can change the "random seed" used by a calculation by pushing
# a different SeedSequence before invoking any other nifty6.random calls
_sseq = [np.random.SeedSequence(42)]
_sseq = [np.random.SeedSequence(_fix_seed(42))]
# Stack of random number generators associated with _sseq.
_rng = [np.random.default_rng(_sseq[-1])]
......@@ -196,7 +204,7 @@ def push_sseq_from_seed(seed):
In all other situations, it is highly recommended to use the
:class:`Context` class for managing the RNG state.
"""
_sseq.append(np.random.SeedSequence(seed))
_sseq.append(np.random.SeedSequence(_fix_seed(seed)))
_rng.append(np.random.default_rng(_sseq[-1]))
......@@ -276,7 +284,7 @@ class Context(object):
def __init__(self, inp):
if not isinstance(inp, np.random.SeedSequence):
inp = np.random.SeedSequence(inp)
inp = np.random.SeedSequence(_fix_seed(inp))
self._sseq = inp
def __enter__(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment