Commit 50def814 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

more new syntax

parent fcb0662c
Pipeline #71927 passed with stages
in 14 minutes and 44 seconds
......@@ -178,10 +178,9 @@ class MetricGaussianKL(Energy):
_local_samples = []
sseq = random.spawn_sseq(self._n_samples)
for i in range(self._lo, self._hi):
random.push_sseq(sseq[i])
_local_samples.append(met.draw_sample(from_inverse=True,
dtype=lh_sampling_dtype))
random.pop_sseq()
with random.Context(sseq[i]):
_local_samples.append(met.draw_sample(
from_inverse=True, dtype=lh_sampling_dtype))
_local_samples = tuple(_local_samples)
else:
if len(_local_samples) != self._hi-self._lo:
......@@ -272,9 +271,8 @@ class MetricGaussianKL(Energy):
samp = full(self._hamiltonian.domain, 0.)
sseq = random.spawn_sseq(self._n_samples)
for i, v in enumerate(self._local_samples):
random.push_sseq(sseq[self._lo+i])
with random.Context(sseq[self._lo+i]):
samp = samp + self._hamiltonian(lin+v).metric.draw_sample(from_inverse=False, dtype=dtype)
if self._mirror_samples:
samp = samp + self._hamiltonian(lin-v).metric.draw_sample(from_inverse=False, dtype=dtype)
random.pop_sseq()
return _allreduce_sum_field(self._comm, samp)/self._n_eff_samples
......@@ -38,11 +38,9 @@ pmp = pytest.mark.parametrize
@pytest.fixture(params=PARAMS)
def field(request):
ift.random.push_sseq_from_seed(request.param[0])
with ift.random.Context(request.param[0]):
S = ift.ScalingOperator(request.param[1], 1.)
res = S.draw_sample()
ift.random.pop_sseq()
return res
return S.draw_sample()
def test_gaussian(field):
......
......@@ -38,7 +38,7 @@ pmp = pytest.mark.parametrize
@pmp('noise', [1, 1e-2, 1e2])
@pmp('seed', [4, 78, 23])
def test_gaussian_energy(space, nonlinearity, noise, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
dim = len(space.shape)
hspace = space.get_default_codomain()
ht = ift.HarmonicTransformOperator(hspace, target=space)
......@@ -72,4 +72,3 @@ def test_gaussian_energy(space, nonlinearity, noise, seed):
energy = ift.GaussianEnergy(d, N) @ d_model()
ift.extra.check_jacobian_consistency(
energy, xi0, ntries=10, tol=1e-6)
ift.random.pop_sseq()
......@@ -248,7 +248,7 @@ def testOuter(fdomain, domain):
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
@pmp('seed', [12, 3])
def testValueInserter(sp, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
ind = []
for ss in sp.shape:
if ss == 1:
......@@ -257,7 +257,6 @@ def testValueInserter(sp, seed):
ind.append(int(ift.random.current_rng().integers(0, ss-1)))
op = ift.ValueInserter(sp, ind)
ift.extra.consistency_check(op)
ift.random.pop_sseq()
@pmp('sp', _pow_spaces)
......@@ -278,14 +277,13 @@ def testSpecialSum(sp):
ift.extra.consistency_check(op)
def metatestMatrixProductOperator(sp, mat_shape, seed, **kwargs):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
mat = ift.random.current_rng().standard_normal(mat_shape)
op = ift.MatrixProductOperator(sp, mat, **kwargs)
ift.extra.consistency_check(op)
mat = mat + 1j*ift.random.current_rng().standard_normal(mat_shape)
op = ift.MatrixProductOperator(sp, mat, **kwargs)
ift.extra.consistency_check(op)
ift.random.pop_sseq()
@pmp('sp', [ift.RGSpace(10)])
@pmp('spaces', [None, (0,)])
......@@ -314,7 +312,7 @@ def testMatrixProductOperator_2d_flatten(sp, seed):
@pmp('seed', [12, 3])
def testPartialExtractor(seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
tgt = {'a': ift.RGSpace(1), 'b': ift.RGSpace(2)}
dom = tgt.copy()
dom['c'] = ift.RGSpace(3)
......@@ -322,7 +320,6 @@ def testPartialExtractor(seed):
tgt = ift.MultiDomain.make(tgt)
op = ift.PartialExtractor(dom, tgt)
ift.extra.consistency_check(op)
ift.random.pop_sseq()
@pmp('seed', [12, 3])
def testSlowFieldAdapter(seed):
......
......@@ -39,7 +39,7 @@ def testAmplitudesConsistency(rseed, sspace, Astds, offset_std, N):
sc.add(op(s.extract(op.domain)))
return sc.mean.val, sc.var.sqrt().val
ift.random.push_sseq_from_seed(rseed)
with ift.random.Context(rseed):
nsam = 100
fsspace = ift.RGSpace((12,), (0.4,))
......@@ -91,7 +91,6 @@ def testAmplitudesConsistency(rseed, sspace, Astds, offset_std, N):
em, estd = stats(fa.slice_fluctuation(0), samples)
assert_allclose(m, em, rtol=0.5)
ift.random.pop_sseq()
assert op.target[-2] == sspace
assert op.target[-1] == fsspace
......@@ -38,19 +38,18 @@ seed = list2fixture([4, 78, 23])
def testBasics(space, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
S = ift.ScalingOperator(space, 1.)
s = S.draw_sample()
var = ift.Linearization.make_var(s)
model = ift.ScalingOperator(var.target, 6.)
ift.extra.check_jacobian_consistency(model, var.val)
ift.random.pop_sseq()
@pmp('type1', ['Variable', 'Constant'])
@pmp('type2', ['Variable'])
def testBinary(type1, type2, space, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
dom1 = ift.MultiDomain.make({'s1': space})
dom2 = ift.MultiDomain.make({'s2': space})
dom = ift.MultiDomain.union((dom1, dom2))
......@@ -88,11 +87,10 @@ def testBinary(type1, type2, space, seed):
model = ift.FFTOperator(space)(select_s1*select_s2)
pos = ift.from_random("normal", dom)
ift.extra.check_jacobian_consistency(model, pos, ntries=20)
ift.random.pop_sseq()
def testSpecialDistributionOps(space, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
S = ift.ScalingOperator(space, 1.)
pos = S.draw_sample()
alpha = 1.5
......@@ -101,12 +99,11 @@ def testSpecialDistributionOps(space, seed):
ift.extra.check_jacobian_consistency(model, pos, ntries=20)
model = ift.UniformOperator(space, alpha, q)
ift.extra.check_jacobian_consistency(model, pos, ntries=20)
ift.random.pop_sseq()
@pmp('neg', [True, False])
def testAdder(space, seed, neg):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
S = ift.ScalingOperator(space, 1.)
f = S.draw_sample()
f1 = S.draw_sample()
......@@ -114,7 +111,6 @@ def testAdder(space, seed, neg):
ift.extra.check_jacobian_consistency(op, f)
op = ift.Adder(f1.val.ravel()[0], neg=neg, domain=space)
ift.extra.check_jacobian_consistency(op, f)
ift.random.pop_sseq()
@pmp('target', [ift.RGSpace(64, distances=.789, harmonic=True),
......@@ -123,7 +119,7 @@ def testAdder(space, seed, neg):
@pmp('causal', [True, False])
@pmp('minimum_phase', [True, False])
def testDynamicModel(target, causal, minimum_phase, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
dct = {
'target': target,
'harmonic_padding': None,
......@@ -160,7 +156,6 @@ def testDynamicModel(target, causal, minimum_phase, seed):
# FIXME I dont know why smaller tol fails for 3D example
ift.extra.check_jacobian_consistency(
model, pos, tol=1e-5, ntries=20)
ift.random.pop_sseq()
@pmp('h_space', _h_spaces)
......
......@@ -206,7 +206,7 @@ def testOuter(fdomain, domain):
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
@pmp('seed', [12, 3])
def testValueInserter(sp, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
ind = []
for ss in sp.shape:
if ss == 1:
......@@ -214,4 +214,3 @@ def testValueInserter(sp, seed):
else:
ind.append(int(ift.random.current_rng().integers(0, ss - 1)))
_check_repr(ift.ValueInserter(sp, ind))
ift.random.pop_sseq()
......@@ -32,11 +32,10 @@ from ..common import setup_function, teardown_function
])
@pytest.mark.parametrize('seed', [13, 2])
def test_value_inserter(sp, seed):
ift.random.push_sseq_from_seed(seed)
with ift.random.Context(seed):
ind = tuple([int(ift.random.current_rng().integers(0, ss - 1)) for ss in sp.shape])
op = ift.ValueInserter(sp, ind)
f = ift.from_random('normal', op.domain)
ift.random.pop_sseq()
inp = f.val
ret = op(f).val
assert_(ret[ind] == inp)
......
......@@ -79,4 +79,5 @@ def test_rand6():
ift.random.setState(state)
b = ift.random.current_rng().integers(0,1000000000)
np.testing.assert_equal(a,b)
ift.random.pop_sseq()
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