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

introduce new numpy random_state, especially in store, for reproducibility #46

Open
bkmi opened this issue Jul 13, 2021 · 2 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@bkmi
Copy link
Collaborator

bkmi commented Jul 13, 2021

For reproducibility there is the option to split the seed, we should use it.

https://numpy.org/doc/stable/reference/random/index.html?

swyft/swyft/store/store.py

Lines 94 to 131 in 07a5bff

def add(self, N, prior, bound=None):
"""Adds points to the store.
Args:
N (int): Number of samples
prior (swyft.Prior): Prior
bound (swyft.Bound): Bound object for prior truncation
.. warning::
Calling this method will alter the content of the store by adding
additional points. Currently this cannot be reverted, so use with
care when applying it to the DirectoryStore.
"""
pdf = swyft.TruncatedPrior(prior, bound)
# Lock store while adding new points
self.lock()
self._update()
# Generate new points
z_prop = pdf.sample(N=np.random.poisson(N))
log_lambda_target = pdf.log_prob(z_prop) + np.log(N)
log_lambda_store = self.log_lambda(z_prop)
log_w = np.log(np.random.rand(len(z_prop))) + log_lambda_target
accept_new = log_w > log_lambda_store
z_new = z_prop[accept_new]
log_w_new = log_w[accept_new]
# Anything new?
if sum(accept_new) > 0:
# Add new entries to store
self._append_new_points(z_new, log_w_new)
print("Store: Adding %i new samples to simulator store." % sum(accept_new))
# Update intensity function
self.log_lambdas.resize(len(self.log_lambdas) + 1)
self.log_lambdas[-1] = dict(pdf=pdf.state_dict(), N=N)
log.debug(f" total size of simulator store {len(self)}.")

@bkmi
Copy link
Collaborator Author

bkmi commented Nov 5, 2021

This would be nice for the next release, for reproducibility when dealing with multiprocessing.

@fnattino fnattino linked a pull request Nov 11, 2021 that will close this issue
3 tasks
@cweniger cweniger added the enhancement New feature or request label Nov 18, 2022
@cweniger
Copy link
Member

This would be still useful, keep it for v0.4 as optional enhancement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants