Skip to content

Commit

Permalink
fix(noise): RandomGenerator rename and other GaussianNoiseDataset bugs (
Browse files Browse the repository at this point in the history
#106)

- randomgen 1.18 renamed RandomGenerator to Generator.
This commit renames Generator back to RandomGenerator on import
for synthesis.noise, in the same way as was done in analysis.delay.
- Fixes bug in how autocorrelations are being identified in GaussianNoiseDataset
- Fixes inconsistent variable name in mpi_random_seed
(both 'gen' and 'randomgen' are used to refer to the same variable)
  • Loading branch information
ssiegelx authored Oct 2, 2020
1 parent ae2f1b6 commit ba7648d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions draco/synthesis/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@

import contextlib

import randomgen
import numpy as np

try:
# For backwards compatibility
from randomgen import RandomGenerator
except ImportError:
from randomgen import Generator as RandomGenerator

from caput import config
from caput.time import STELLAR_S
from cora.util import nputil
Expand Down Expand Up @@ -103,18 +108,17 @@ def process(self, data):
vis = data.vis[:]

# create a random generator, and create a local seed state
rg = randomgen.generator.RandomGenerator()
rg = RandomGenerator()

with mpi_random_seed(self.seed, randomgen=rg) as rg:
with mpi_random_seed(self.seed, gen=rg) as rg:
noise = rg.normal(
scale=np.sqrt(tools.invert_no_zero(data.weight[:]) / 2),
size=(2,) + data.weight[:].shape,
)
vis[:] = noise[0] + 1j * noise[1]

for si, prod in enumerate(data.prodstack):
prod_inputs = data.prod[prod]
if prod_inputs[0] == prod_inputs[1]:
if prod[0] == prod[1]:
# This is an auto-correlation
vis[:, si].real *= 2 ** 0.5
vis[:, si].imag = 0.0
Expand Down Expand Up @@ -390,7 +394,7 @@ def draw_complex_wishart(C, n):


@contextlib.contextmanager
def mpi_random_seed(seed, extra=0, randomgen=None):
def mpi_random_seed(seed, extra=0, gen=None):
"""Use a specific random seed for the numpy.random context or for the RandomGen context, and return to the original state on exit.
This is designed to work for MPI computations, incrementing the actual seed
Expand Down Expand Up @@ -428,7 +432,7 @@ def mpi_random_seed(seed, extra=0, randomgen=None):
np.random.seed(new_seed)

# we will be setting the numpy.random context
if randomgen is None:
if gen is None:
# Copy the old state for restoration later.
old_state = np.random.get_state()

Expand Down

0 comments on commit ba7648d

Please sign in to comment.