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

first try

parent ad4c6cde
Pipeline #71099 failed with stages
in 10 minutes and 58 seconds
......@@ -80,11 +80,10 @@
%% Cell type:code id: tags:
``` python
import numpy as np
np.random.seed(40)
import nifty6 as ift
import matplotlib.pyplot as plt
%matplotlib inline
```
......
......@@ -5,8 +5,6 @@ import numpy as np
import nifty6 as ift
np.random.seed(40)
N0s, a0s, b0s, c0s = [], [], [], []
for ii in range(10, 26):
......@@ -15,15 +13,15 @@ for ii in range(10, 26):
N = int(2**ii)
print('N = {}'.format(N))
uv = np.random.rand(N, 2) - 0.5
vis = np.random.randn(N) + 1j*np.random.randn(N)
rng = ift.random.current_rng()
uv = rng.uniform(-.5, .5, (N,2))
vis = rng.normal(0., 1., N) + 1j*rng.normal(0., 1., N)
uvspace = ift.RGSpace((nu, nv))
visspace = ift.UnstructuredDomain(N)
img = np.random.randn(nu*nv)
img = img.reshape((nu, nv))
img = rng.standard_normal((nu, nv))
img = ift.makeField(uvspace, img)
t0 = time()
......
......@@ -27,8 +27,6 @@ import numpy as np
import nifty6 as ift
if __name__ == '__main__':
np.random.seed(41)
# Set up the position space of the signal
mode = 2
if mode == 0:
......@@ -62,7 +60,7 @@ if __name__ == '__main__':
p = R(sky)
mock_position = ift.from_random('normal', harmonic_space)
tmp = p(mock_position).val.astype(np.float64)
data = np.random.binomial(1, tmp)
data = ift.random.current_rng().binomial(1, tmp)
data = ift.Field.from_raw(R.target, data)
# Compute likelihood and Hamiltonian
......
......@@ -46,8 +46,6 @@ def make_random_mask():
if __name__ == '__main__':
np.random.seed(42)
# Choose space on which the signal field is defined
if len(sys.argv) == 2:
mode = int(sys.argv[1])
......
......@@ -44,8 +44,6 @@ def exposure_2d():
if __name__ == '__main__':
np.random.seed(42)
# Choose space on which the signal field is defined
if len(sys.argv) == 2:
mode = int(sys.argv[1])
......@@ -94,7 +92,7 @@ if __name__ == '__main__':
lamb = R(sky)
mock_position = ift.from_random('normal', domain)
data = lamb(mock_position)
data = np.random.poisson(data.val.astype(np.float64))
data = ift.random.current_rng().poisson(data.val.astype(np.float64))
data = ift.Field.from_raw(d_space, data)
likelihood = ift.PoissonianEnergy(data) @ lamb
......
......@@ -33,20 +33,18 @@ import nifty6 as ift
def random_los(n_los):
starts = list(np.random.uniform(0, 1, (n_los, 2)).T)
ends = list(np.random.uniform(0, 1, (n_los, 2)).T)
starts = list(ift.random.current_rng().random((n_los, 2)).T)
ends = list(ift.random.current_rng().random((n_los, 2)).T)
return starts, ends
def radial_los(n_los):
starts = list(np.random.uniform(0, 1, (n_los, 2)).T)
ends = list(0.5 + 0*np.random.uniform(0, 1, (n_los, 2)).T)
starts = list(ift.random.current_rng().random((n_los, 2)).T)
ends = list(0.5 + 0*ift.random.current_rng().random((n_los, 2)).T)
return starts, ends
if __name__ == '__main__':
np.random.seed(420)
# Choose between random line-of-sight response (mode=0) and radial lines
# of sight (mode=1)
if len(sys.argv) == 2:
......
......@@ -44,20 +44,18 @@ class SingleDomain(ift.LinearOperator):
def random_los(n_los):
starts = list(np.random.uniform(0, 1, (n_los, 2)).T)
ends = list(np.random.uniform(0, 1, (n_los, 2)).T)
starts = list(ift.random.current_rng().random((n_los, 2)).T)
ends = list(ift.random.current_rng().random((n_los, 2)).T)
return starts, ends
def radial_los(n_los):
starts = list(np.random.uniform(0, 1, (n_los, 2)).T)
ends = list(0.5 + 0*np.random.uniform(0, 1, (n_los, 2)).T)
starts = list(ift.random.current_rng().random((n_los, 2)).T)
ends = list(0.5 + 0*ift.random.current_rng().random((n_los, 2)).T)
return starts, ends
if __name__ == '__main__':
np.random.seed(43)
# Choose between random line-of-sight response (mode=0) and radial lines
# of sight (mode=1)
if len(sys.argv) == 2:
......
......@@ -19,7 +19,6 @@ import matplotlib.pyplot as plt
import numpy as np
import nifty6 as ift
np.random.seed(12)
def polynomial(coefficients, sampling_points):
......@@ -86,7 +85,7 @@ if __name__ == '__main__':
N_params = 10
N_samples = 100
size = (12,)
x = np.random.random(size) * 10
x = ift.random.current_rng().random(size) * 10
y = np.sin(x**2) * x**3
var = np.full_like(y, y.var() / 10)
var[-2] *= 4
......
from .version import __version__
from . import random
from .domains.domain import Domain
from .domains.structured_domain import StructuredDomain
from .domains.unstructured_domain import UnstructuredDomain
......
......@@ -25,6 +25,7 @@ from ..field import Field
from ..linearization import Linearization
from ..operators.operator import Operator
from ..sugar import makeOp
from .. import random
def _f_on_np(f, arr):
......@@ -67,7 +68,8 @@ class _InterpolationOperator(Operator):
if table_func is not None:
if inv_table_func is None:
raise ValueError
a = func(np.random.randn(10))
# MR FIXME: not sure whether we should have this in production code
a = func(random.current_rng().random(10))
a1 = _f_on_np(lambda x: inv_table_func(table_func(x)), a)
np.testing.assert_allclose(a, a1)
self._table = _f_on_np(table_func, self._table)
......
......@@ -11,7 +11,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2019 Max-Planck-Society
# Copyright(C) 2013-2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
......@@ -113,10 +113,6 @@ class MetricGaussianKL_MPI(Energy):
preconditioning is done by default.
_samples : None
Only a parameter for internal uses. Typically not to be set by users.
seed_offset : int
A parameter with which one can controll from which seed the samples
are drawn. Per default, the seed is different for MPI tasks, but the
same every time this class is initialized.
Note
----
......@@ -132,7 +128,7 @@ class MetricGaussianKL_MPI(Energy):
def __init__(self, mean, hamiltonian, n_samples, constants=[],
point_estimates=[], mirror_samples=False,
napprox=0, _samples=None, seed_offset=0,
napprox=0, _samples=None,
lh_sampling_dtype=np.float64):
super(MetricGaussianKL_MPI, self).__init__(mean)
......@@ -159,10 +155,10 @@ class MetricGaussianKL_MPI(Energy):
if napprox > 1:
met._approximation = makeOp(approximation2endo(met, napprox))
_samples = []
rand_state = np.random.get_state()
sseq = random.spawn_sseq(n_samples)
for i in range(lo, hi):
if mirror_samples:
np.random.seed(i//2+seed_offset)
random.push_sseq(sseq[i//2])
if (i % 2) and (i-1 >= lo):
_samples.append(-_samples[-1])
......@@ -170,16 +166,16 @@ class MetricGaussianKL_MPI(Energy):
_samples.append(((i % 2)*2-1) *
met.draw_sample(from_inverse=True,
dtype=lh_sampling_dtype))
random.pop_sseq()
else:
np.random.seed(i+seed_offset)
random.push_sseq(sseq[i])
_samples.append(met.draw_sample(from_inverse=True,
dtype=lh_sampling_dtype))
np.random.set_state(rand_state)
random.pop_sseq()
_samples = tuple(_samples)
if mirror_samples:
n_samples *= 2
self._samples = _samples
self._seed_offset = seed_offset
self._n_samples = n_samples
self._lin = Linearization.make_partial_var(mean, constants)
v, g = None, None
......@@ -205,7 +201,7 @@ class MetricGaussianKL_MPI(Energy):
return MetricGaussianKL_MPI(
position, self._hamiltonian, self._n_samples, self._constants,
self._point_estimates, _samples=self._samples,
seed_offset=self._seed_offset, lh_sampling_dtype=self._sampdt)
lh_sampling_dtype=self._sampdt)
@property
def value(self):
......@@ -245,11 +241,13 @@ class MetricGaussianKL_MPI(Energy):
raise NotImplementedError()
lin = self._lin.with_want_metric()
samp = full(self._hamiltonian.domain, 0.)
rand_state = np.random.get_state()
np.random.seed(rank+np.random.randint(99999))
for v in self._samples:
sseq = random.spawn_sseq(n_samples)
for i, v in enumerate(self._samples):
# FIXME: this is not yet correct. We need to use the _global_ sample
# index, not the local one!
random.push_sseq(sseq[i])
samp = samp + self._hamiltonian(lin+v).metric.draw_sample(from_inverse=False, dtype=dtype)
np.random.set_state(rand_state)
random.pop_sseq()
return allreduce_sum_field(samp)
def metric_sample(self, from_inverse=False, dtype=np.float64):
......
......@@ -11,21 +11,50 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2019 Max-Planck-Society
# Copyright(C) 2013-2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
import numpy as np
_sseq = [np.random.SeedSequence(42)]
_rng = [np.random.default_rng(_sseq[-1])]
def spawn_sseq(n, parent=None):
if parent is None:
global _sseq
parent = _sseq[-1]
return parent.spawn(n)
def current_rng():
return _rng[-1]
def push_sseq(sseq):
_sseq.append(sseq)
_rng.append(np.random.default_rng(_sseq[-1]))
def push_sseq_from_seed(seed):
_sseq.append(np.random.SeedSequence(seed))
_rng.append(np.random.default_rng(_sseq[-1]))
def pop_sseq():
_sseq.pop()
_rng.pop()
class Random(object):
@staticmethod
def pm1(dtype, shape):
if np.issubdtype(dtype, np.complexfloating):
x = np.array([1+0j, 0+1j, -1+0j, 0-1j], dtype=dtype)
x = x[np.random.randint(4, size=shape)]
x = x[_rng[-1].integers(0, 4, size=shape)]
else:
x = 2*np.random.randint(2, size=shape) - 1
x = 2*_rng[-1].integers(0, 2, size=shape)-1
return x.astype(dtype, copy=False)
@staticmethod
......@@ -42,10 +71,10 @@ class Random(object):
raise TypeError("mean must not be complex for a real result field")
if np.issubdtype(dtype, np.complexfloating):
x = np.empty(shape, dtype=dtype)
x.real = np.random.normal(mean.real, std*np.sqrt(0.5), shape)
x.imag = np.random.normal(mean.imag, std*np.sqrt(0.5), shape)
x.real = _rng[-1].normal(mean.real, std*np.sqrt(0.5), shape)
x.imag = _rng[-1].normal(mean.imag, std*np.sqrt(0.5), shape)
else:
x = np.random.normal(mean, std, shape).astype(dtype, copy=False)
x = _rng[-1].normal(mean, std, shape).astype(dtype, copy=False)
return x
@staticmethod
......@@ -57,13 +86,13 @@ class Random(object):
raise TypeError("low and high must not be complex")
if np.issubdtype(dtype, np.complexfloating):
x = np.empty(shape, dtype=dtype)
x.real = np.random.uniform(low, high, shape)
x.imag = np.random.uniform(low, high, shape)
x.real = _rng[-1].uniform(low, high, shape)
x.imag = _rng[-1].uniform(low, high, shape)
elif np.issubdtype(dtype, np.integer):
if not (np.issubdtype(type(low), np.integer) and
np.issubdtype(type(high), np.integer)):
raise TypeError("low and high must be integer")
x = np.random.randint(low, high+1, shape)
x = _rng[-1].integers(low, high+1, shape)
else:
x = np.random.uniform(low, high, shape)
x = _rng[-1].uniform(low, high, shape)
return x.astype(dtype, copy=False)
......@@ -39,8 +39,8 @@ setup(name="nifty6",
packages=find_packages(include=["nifty6", "nifty6.*"]),
zip_safe=True,
license="GPLv3",
setup_requires=['scipy>=1.4.1'],
install_requires=['scipy>=1.4.1'],
setup_requires=['scipy>=1.4.1', 'numpy>=1.17'],
install_requires=['scipy>=1.4.1', 'numpy>=1.17'],
python_requires='>=3.5',
classifiers=[
"Development Status :: 4 - Beta",
......
......@@ -37,9 +37,11 @@ pmp = pytest.mark.parametrize
@pytest.fixture(params=PARAMS)
def field(request):
np.random.seed(request.param[0])
ift.random.push_sseq_from_seed(request.param[0])
S = ift.ScalingOperator(request.param[1], 1.)
return S.draw_sample()
res = S.draw_sample()
ift.random.pop_sseq()
return res
def test_gaussian(field):
......@@ -102,7 +104,7 @@ def test_inverse_gamma(field):
return
field = field.exp()
space = field.domain
d = np.random.normal(10, size=space.shape)**2
d = ift.random.current_rng().normal(10, size=space.shape)**2
d = ift.Field(space, d)
energy = ift.InverseGammaLikelihood(d)
ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)
......@@ -113,7 +115,7 @@ def testPoissonian(field):
return
field = field.exp()
space = field.domain
d = np.random.poisson(120, size=space.shape)
d = ift.random.current_rng().poisson(120, size=space.shape)
d = ift.Field(space, d)
energy = ift.PoissonianEnergy(d)
ift.extra.check_jacobian_consistency(energy, field, tol=1e-7)
......@@ -124,7 +126,7 @@ def test_bernoulli(field):
return
field = field.sigmoid()
space = field.domain
d = np.random.binomial(1, 0.1, size=space.shape)
d = ift.random.current_rng().binomial(1, 0.1, size=space.shape)
d = ift.Field(space, d)
energy = ift.BernoulliEnergy(d)
ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)
......@@ -51,8 +51,6 @@ def _spec2(k):
])
@pmp('space2', [ift.RGSpace((8,), harmonic=True), ift.LMSpace(12)])
def test_power_synthesize_analyze(space1, space2):
np.random.seed(11)
p1 = ift.PowerSpace(space1)
fp1 = ift.PS_field(p1, _spec1)
p2 = ift.PowerSpace(space2)
......@@ -82,8 +80,6 @@ def test_power_synthesize_analyze(space1, space2):
])
@pmp('space2', [ift.RGSpace((8,), harmonic=True), ift.LMSpace(12)])
def test_DiagonalOperator_power_analyze2(space1, space2):
np.random.seed(11)
fp1 = ift.PS_field(ift.PowerSpace(space1), _spec1)
fp2 = ift.PS_field(ift.PowerSpace(space2), _spec2)
......
......@@ -37,7 +37,7 @@ pmp = pytest.mark.parametrize
@pmp('noise', [1, 1e-2, 1e2])
@pmp('seed', [4, 78, 23])
def test_gaussian_energy(space, nonlinearity, noise, seed):
np.random.seed(seed)
ift.random.push_sseq_from_seed(seed)
dim = len(space.shape)
hspace = space.get_default_codomain()
ht = ift.HarmonicTransformOperator(hspace, target=space)
......@@ -45,6 +45,7 @@ def test_gaussian_energy(space, nonlinearity, noise, seed):
pspace = ift.PowerSpace(hspace, binbounds=binbounds)
Dist = ift.PowerDistributor(target=hspace, power_space=pspace)
xi0 = ift.Field.from_random(domain=hspace, random_type='normal')
ift.random.pop_sseq()
def pspec(k):
return 1/(1 + k**2)**dim
......
......@@ -28,7 +28,6 @@ pmp = pytest.mark.parametrize
@pmp('point_estimates', ([], ['a'], ['b'], ['a', 'b']))
@pmp('mirror_samples', (True, False))
def test_kl(constants, point_estimates, mirror_samples):
np.random.seed(42)
dom = ift.RGSpace((12,), (2.12))
op0 = ift.HarmonicSmoothingOperator(dom, 3)
op = ift.ducktape(dom, None, 'a')*(op0.ducktape('b'))
......
......@@ -51,7 +51,6 @@ slow_minimizers = ['ift.SteepestDescent(IC)']
slow_minimizers)
@pmp('space', spaces)
def test_quadratic_minimization(minimizer, space):
np.random.seed(42)
starting_point = ift.Field.from_random('normal', domain=space)*10
covariance_diagonal = ift.Field.from_random('uniform', domain=space) + 0.5
covariance = ift.DiagonalOperator(covariance_diagonal)
......@@ -76,7 +75,6 @@ def test_quadratic_minimization(minimizer, space):
@pmp('space', spaces)
def test_WF_curvature(space):
np.random.seed(42)
required_result = ift.full(space, 1.)
s = ift.Field.from_random('uniform', domain=space) + 0.5
......@@ -121,7 +119,6 @@ def test_rosenbrock(minimizer):
from scipy.optimize import rosen, rosen_der, rosen_hess_prod
except ImportError:
raise SkipTest
np.random.seed(42)
space = ift.DomainTuple.make(ift.UnstructuredDomain((2,)))
starting_point = ift.Field.from_random('normal', domain=space)*10
......
......@@ -41,15 +41,13 @@ _pow_spaces = [
pmp = pytest.mark.parametrize
dtype = list2fixture([np.float64, np.complex128])
np.random.seed(42)
@pmp('sp', _p_RG_spaces)
def testLOSResponse(sp, dtype):
starts = np.random.randn(len(sp.shape), 10)
ends = np.random.randn(len(sp.shape), 10)
sigma_low = 1e-4*np.random.randn(10)
sigma_ups = 1e-5*np.random.randn(10)
starts = ift.random.current_rng().standard_normal((len(sp.shape), 10))
ends = ift.random.current_rng().standard_normal((len(sp.shape), 10))
sigma_low = 1e-4*ift.random.current_rng().standard_normal(10)
sigma_ups = 1e-5*ift.random.current_rng().standard_normal(10)
op = ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups)
ift.extra.consistency_check(op, dtype, dtype)
......@@ -70,7 +68,7 @@ def testOperatorCombinations(sp, dtype):
def testLinearInterpolator():
sp = ift.RGSpace((10, 8), distances=(0.1, 3.5))
pos = np.random.rand(2, 23)
pos = ift.random.current_rng().random((2, 23))
pos[0, :] *= 0.9
pos[1, :] *= 7*3.5
op = ift.LinearInterpolator(sp, pos)
......@@ -250,15 +248,16 @@ def testOuter(fdomain, domain):
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
@pmp('seed', [12, 3])
def testValueInserter(sp, seed):
np.random.seed(seed)
ift.random.push_sseq_from_seed(seed)
ind = []
for ss in sp.shape:
if ss == 1:
ind.append(0)
else:
ind.append(np.random.randint(0, ss-1))
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)
......@@ -282,18 +281,19 @@ def testSpecialSum(sp):
@pmp('sp', [ift.RGSpace(10)])
@pmp('seed', [12, 3])
def testMatrixProductOperator(sp, seed):
np.random.seed(seed)
mat = np.random.randn(*sp.shape, *sp.shape)
ift.random.push_sseq_from_seed(seed)
mat = ift.random.current_rng().standard_normal((*sp.shape, *sp.shape))
op = ift.MatrixProductOperator(sp, mat)
ift.extra.consistency_check(op)
mat = mat + 1j*np.random.randn(*sp.shape, *sp.shape)
mat = mat + 1j*ift.random.current_rng().standard_normal((*sp.shape, *sp.shape))
op = ift.MatrixProductOperator(sp, mat)
ift.extra.consistency_check(op)
ift.random.pop_sseq()
@pmp('seed', [12, 3])
def testPartialExtractor(seed):
np.random.seed(seed)
ift.random.push_sseq_from_seed(seed)
tgt = {'a': ift.RGSpace(1), 'b': ift.RGSpace(2)}
dom = tgt.copy()
dom['c'] = ift.RGSpace(3)
......@@ -301,6 +301,7 @@ 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])
......
......@@ -42,7 +42,6 @@ def test_fft1D(d, dtype, op):
tol = _get_rtol(dtype)
a = ift.RGSpace(dim1, distances=d)
b = ift.RGSpace(dim1, distances=1./(dim1*d), harmonic=True)
np.random.seed(16)
fft = op(domain=a, target=b)
inp = ift.Field.from_random(
......
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