Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phas new numpy rng generator without setting/getting states manually #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion plancklens/sims/phas.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,25 @@ def __init__(self, lib_dir, get_state_func=np.random.get_state, nsims_max=None):
self._rng_db = rng_db(os.path.join(lib_dir, 'rngdb.db'), idtype='INTEGER')
self._get_rng_state = get_state_func

@staticmethod
def get_state(idx):
"""Returns a random number generator state from a seed. """
#sg = np.random.SeedSequence(idx)
#mt19937 = np.random.MT19937(sg)
#rs = np.random.RandomState(mt19937)
rs = np.random.Generator(np.random.MT19937())
dictionary = rs.__getstate__()
l = [dictionary[k] for k in dictionary.keys()]
return [l[0], l[1]['key'], l[1]['pos'], 0, 0.0]
#return rs.get_state()

def get_sim(self, idx, **kwargs):
"""Returns sim number idx and caches random number generator state. """
if self.has_nmax(): assert idx < self.nmax

if not self.is_stored(idx):
self._rng_db.add(idx, self._get_rng_state())
#self._rng_db.add(idx, self._get_rng_state())
self._rng_db.add(idx, self.get_state(idx))
return self._build_sim_from_rng(self._rng_db.get(idx), **kwargs)

def has_nmax(self):
Expand Down Expand Up @@ -193,3 +207,4 @@ def get_sim(self, idx, idf=None, phas_only=False):

def hashdict(self):
return {'nfields': self.nfields, 'lmax':self.lmax}