Skip to content

Commit

Permalink
Merge pull request #287 from choderalab/testsystem_control_seed
Browse files Browse the repository at this point in the history
Add the ability to control the random seed to testsystem
  • Loading branch information
Lnaden committed Feb 23, 2018
2 parents 27bc127 + 21bf1dd commit 9582976
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pymbar/testsystems/exponential_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def analytical_entropies(self):
def analytical_x_squared(self):
return self.analytical_variances() + self.analytical_means() ** 2.

def sample(self, N_k=(10, 20, 30, 40, 50), mode='u_kln'):
def sample(self, N_k=(10, 20, 30, 40, 50), mode='u_kln', seed=None):
"""Draw samples from the distribution.
Parameters
Expand All @@ -101,6 +101,7 @@ def sample(self, N_k=(10, 20, 30, 40, 50), mode='u_kln'):
mode : str, optional, default='u_kln'
If 'u_kln', return K x K x N_max matrix where u_kln[k,l,n] is reduced potential of sample n from state k evaluated at state l.
If 'u_kn', return K x N_tot matrix where u_kn[k,n] is reduced potential of sample n (in concatenated indexing) evaluated at state k.
seed: int, optional, default=None. Provides control over the random seed for replicability.
Returns
-------
Expand All @@ -123,6 +124,9 @@ def sample(self, N_k=(10, 20, 30, 40, 50), mode='u_kln'):
N_k[k] is the number of samples generated from state k
"""

np.random.seed(seed)

N_k = np.array(N_k, np.int32)
if len(N_k) != self.n_states:
raise Exception("N_k has %d states while self.n_states has %d states." % (len(N_k), self.n_states))
Expand Down
9 changes: 7 additions & 2 deletions pymbar/testsystems/harmonic_oscillators.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def analytical_free_energies(self, subtract_component=0):
def analytical_entropies(self, subtract_component = 0):
return self.analytical_observable(observable = 'potential energy') - self.analytical_free_energies(subtract_component)

def sample(self, N_k=[10, 20, 30, 40, 50], mode='u_kn'):
def sample(self, N_k=[10, 20, 30, 40, 50], mode='u_kn', seed = None):
"""Draw samples from the distribution.
Parameters
Expand All @@ -109,6 +109,8 @@ def sample(self, N_k=[10, 20, 30, 40, 50], mode='u_kn'):
If 'u_kln', return K x K x N_max matrix where u_kln[k,l,n] is reduced potential of sample n from state k evaluated at state l.
If 'u_kn', return K x N_tot matrix where u_kn[k,n] is reduced potential of sample n (in concatenated indexing) evaluated at state k.
seed: int, optional, default=None. Provides control over the random seed for replicability.
Returns
-------
if mode == 'u_kn':
Expand All @@ -130,6 +132,9 @@ def sample(self, N_k=[10, 20, 30, 40, 50], mode='u_kn'):
N_k[k] is the number of samples generated from state k
"""

np.random.seed(seed)

N_k = np.array(N_k, np.int32)
if len(N_k) != self.n_states:
raise Exception("N_k has %d states while self.n_states has %d states." % (len(N_k), self.n_states))
Expand Down Expand Up @@ -207,6 +212,6 @@ def evenly_spaced_oscillators(cls, n_states, n_samples_per_state, lower_O_k=1.0,
N_k = (np.ones(n_states) * n_samples_per_state).astype('int')

testsystem = cls(O_k, k_k)
x_n, u_kn, N_k_output, s_n = testsystem.sample(N_k, mode='u_kn')
x_n, u_kn, N_k_output, s_n = testsystem.sample(N_k, mode='u_kn', seed=seed)

return name, testsystem, x_n, u_kn, N_k_output, s_n

0 comments on commit 9582976

Please sign in to comment.