Fix use of stochastic decorator
@@ -32,9 +32,19 @@ import bayeslite
from bayeslite import read_pandas
from bayeslite.backends.loom_backend import LoomBackend
+def set_numpy_seed(seed):
+ """
+ The `stochastic` decorator provides 64 byte strings as its seeds. numpy
+ uses 32 bit unsigned integers as its seeds Takes a 64 byte seed, extracts
+ the first 32 bits (4 bytes) from it and uses those as the numpy random
+ seed.
+ """
+ np_seed = struct.unpack("!I", seed[:4])[0] # convert to 32 bit uint
+ np.random.seed(np_seed)
+
def axis_aligned_gaussians(means, size, seed=None):
if seed is not None:
- np.random.seed(seed)
+ set_numpy_seed(seed)
return [np.random.multivariate_normal(mean, [[0,1], [1,0]], size=size)
for mean
in means]
@@ -44,7 +54,7 @@ def mix(gaussians, p, seed=None):
assert reduce(lambda sum, n: sum + n, p) == 1
if seed is not None:
- np.random.seed(seed)
+ set_numpy_seed(seed)
choices = np.random.choice([0,1], size=len(gaussians[0]), p=p)
return [gaussians[choices[i]][i] for i in range(len(choices))]
@@ -108,8 +118,7 @@ def test_mix_ratio(seed):
sample_gaussians = axis_aligned_gaussians(means, sample_size, seed=seed)
samples = mix(sample_gaussians, mix_ratio, seed=seed)
- packed_seed = struct.pack('<QQQQ', seed, seed, seed, seed)
- with bayeslite.bayesdb_open(seed=packed_seed) as bdb:
+ with bayeslite.bayesdb_open(seed=seed) as bdb:
register_loom(bdb)
prepare_bdb(bdb, samples, table, seed)
@@ -137,8 +146,7 @@ def test_simulate_y_from_partially_populated_row(seed):
sample_gaussians = axis_aligned_gaussians(means, sample_size, seed=seed)
samples = mix(sample_gaussians, mix_ratio, seed=seed)
- packed_seed = struct.pack('<QQQQ', seed, seed, seed, seed)
- with bayeslite.bayesdb_open(seed=packed_seed) as bdb:
+ with bayeslite.bayesdb_open(seed=seed) as bdb:
register_loom(bdb)
prepare_bdb(bdb, samples, table, seed)
