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

Allow global setting of random number seed + better RepartitionedHybridTopologyFactory test #808

Draft
wants to merge 3 commits into
base: 0.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions perses/tests/test_coordinate_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CARBON_MASS = 12.01 # float (implicitly in units of AMU)
REFERENCE_PLATFORM = openmm.Platform.getPlatformByName("Reference")
running_on_github_actions = os.environ.get('GITHUB_ACTIONS', None) == 'true'
rng = np.random.RandomState(42)
#########################################
# Tests
#########################################
Expand All @@ -27,7 +28,7 @@ def test_coordinate_conversion():
geometry_engine = geometry.FFAllAngleGeometryEngine({'test': 'true'})
#try to transform random coordinates to and from cartesian
for i in range(200):
indices = np.random.randint(100, size=4)
indices = rng.randint(100, size=4)
atom_position = unit.Quantity(np.array([ 0.80557722 ,-1.10424644 ,-1.08578826]), unit=unit.nanometers)
bond_position = unit.Quantity(np.array([ 0.0765, 0.1 , -0.4005]), unit=unit.nanometers)
angle_position = unit.Quantity(np.array([ 0.0829 , 0.0952 ,-0.2479]) ,unit=unit.nanometers)
Expand Down Expand Up @@ -134,7 +135,7 @@ def test_try_random_itoc():
angle_position = unit.Quantity(np.array([ 0.0829 , 0.0952 ,-0.2479]) ,unit=unit.nanometers)
torsion_position = unit.Quantity(np.array([-0.057 , 0.0951 ,-0.1863] ) ,unit=unit.nanometers)
for i in range(1000):
atom_position += unit.Quantity(np.random.normal(size=3), unit=unit.nanometers)
atom_position += unit.Quantity(rng.normal(size=3), unit=unit.nanometers)
r, theta, phi = _get_internal_from_omm(atom_position, bond_position, angle_position, torsion_position)
recomputed_xyz, _ = geometry_engine._internal_to_cartesian(bond_position, angle_position, torsion_position, r, theta, phi)
new_r, new_theta, new_phi = _get_internal_from_omm(recomputed_xyz,bond_position, angle_position, torsion_position)
Expand Down
4 changes: 2 additions & 2 deletions perses/tests/test_relative.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from simtk import unit, openmm
import numpy as np
import os
import random
from nose.tools import nottest
from pkg_resources import resource_filename

Expand Down Expand Up @@ -36,6 +35,7 @@
ENERGY_THRESHOLD = 1e-1
REFERENCE_PLATFORM = openmm.Platform.getPlatformByName("CPU")
aminos = ['ALA','ARG','ASN','ASP','CYS','GLN','GLU','GLY','HIS','ILE','LEU','LYS','MET','PHE','PRO','SER','THR','TRP','TYR','VAL']
rng = np.random.RandomState(42)

def run_hybrid_endpoint_overlap(topology_proposal, current_positions, new_positions):
"""
Expand Down Expand Up @@ -809,7 +809,7 @@ def flattenedHybridTopologyFactory_energies(topology, chain, system, positions,

# Create point mutation engine to mutate residue at id 2 to a random amino acid
aminos_updated = [amino for amino in aminos if amino not in ['ALA', 'PRO']]
mutant = random.choice(aminos_updated)
mutant = rng.choice(aminos_updated)
print(f'Making mutation ALA->{mutant}')
point_mutation_engine = PointMutationEngine(wildtype_topology=topology,
system_generator=system_generator,
Expand Down
11 changes: 6 additions & 5 deletions perses/tests/test_smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
external_parallelism = None
internal_parallelism = {'library': ('dask', 'LSF'), 'num_processes': 2}
os.system(f"mkdir {trajectory_directory}")
rng = np.random.RandomState(42)
#######################


Expand Down Expand Up @@ -180,7 +181,7 @@ def dummy_ancestry_generator_function():
"""
ancestries = [np.arange(10)]
for i in range(10):
new_ancestries = np.random.choice(ancestries[-1], 10)
new_ancestries = rng.choice(ancestries[-1], 10)
ancestries.append(new_ancestries)
return ancestries

Expand All @@ -195,7 +196,7 @@ def test_multinomial_resample():
"""
test the multinomial resampler
"""
total_works = np.random.rand(10)
total_works = rng.rand(10)
num_resamples = 10
resampled_works, resampled_indices = multinomial_resample(total_works, num_resamples)
assert all(_val == np.average(total_works) for _val in resampled_works), f"the returned resampled works are not a uniform average"
Expand All @@ -207,22 +208,22 @@ def test_ESS():
test the effective sample size computation
"""
#the ESS already passes with a normalization assertion
dummy_prev_works, dummy_works_incremental = np.random.rand(10), np.random.rand(10)
dummy_prev_works, dummy_works_incremental = rng.rand(10), rng.rand(10)
normalized_ESS = ESS(dummy_prev_works, dummy_works_incremental)

def test_CESS():
"""
test the conditional effective sample size computation
"""
#the CESS must be guaranteed to be between 0 and 1
dummy_prev_works, dummy_works_incremental = np.random.rand(10), np.random.rand(10)
dummy_prev_works, dummy_works_incremental = rng.rand(10), rng.rand(10)
_CESS = CESS(dummy_prev_works, dummy_works_incremental)

def test_compute_timeseries():
"""
test the compute_timeseries function
"""
reduced_potentials = np.random.rand(100)
reduced_potentials = rng.rand(100)
data = compute_timeseries(reduced_potentials)
assert len(data[3]) <= len(reduced_potentials), f"the length of uncorrelated data is at most the length of the raw data"

Expand Down
8 changes: 5 additions & 3 deletions perses/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ def test_write_array():
view1 = NetCDFStorageView(storage, 'envname1', 'modname')
view2 = NetCDFStorageView(storage, 'envname2', 'modname')

from numpy.random import random
import numpy as np
rng = np.random.RandomState(42)

shape = (10,3)
array = random(shape)
array = rng.random(shape)
view1.write_array('singleton', array)

for iteration in range(10):
array = random(shape)
array = rng.random(shape)
view1.write_array('varname', array, iteration=iteration)
view2.write_array('varname', array, iteration=iteration)

Expand Down
5 changes: 3 additions & 2 deletions perses/tests/test_topology_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
beta = 1.0 / kT
ENERGY_THRESHOLD = 1e-6
PROHIBITED_RESIDUES = ['CYS']
rng = np.random.RandomState(42)

running_on_github_actions = os.environ.get('GITHUB_ACTIONS', None) == 'true'

Expand Down Expand Up @@ -515,7 +516,7 @@ def test_specify_allowed_mutants():
for chain in modeller.topology.chains():
if chain.id == chain_id:
residues = chain._residues
mutant_res = np.random.choice(residues[1:-1])
mutant_res = rng.choice(residues[1:-1])

pm_top_engine = topology_proposal.PointMutationEngine(modeller.topology, system_generator, chain_id, allowed_mutations=allowed_mutations)

Expand Down Expand Up @@ -558,7 +559,7 @@ def test_propose_self():
for chain in modeller.topology.chains():
if chain.id == chain_id:
residues = [res for res in chain._residues if res.name not in PROHIBITED_RESIDUES]
mutant_res = np.random.choice(residues[1:-1])
mutant_res = rng.choice(residues[1:-1])
allowed_mutations = [(mutant_res.id,mutant_res.name)]

pm_top_engine = topology_proposal.PointMutationEngine(modeller.topology, system_generator, chain_id, allowed_mutations=allowed_mutations)
Expand Down
Loading