From d7f766060ebe9e5752fe1691b2c87143b6fc67b1 Mon Sep 17 00:00:00 2001 From: Mike Henry <11765982+mikemhenry@users.noreply.github.com> Date: Thu, 17 Jun 2021 16:12:23 -0700 Subject: [PATCH] use random number seed --- perses/tests/test_coordinate_numba.py | 5 +++-- perses/tests/test_relative.py | 6 +++--- perses/tests/test_smc.py | 11 ++++++----- perses/tests/test_storage.py | 8 +++++--- perses/tests/test_topology_proposal.py | 5 +++-- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/perses/tests/test_coordinate_numba.py b/perses/tests/test_coordinate_numba.py index a001a26ba..200b38663 100644 --- a/perses/tests/test_coordinate_numba.py +++ b/perses/tests/test_coordinate_numba.py @@ -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 ######################################### @@ -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) @@ -131,7 +132,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) diff --git a/perses/tests/test_relative.py b/perses/tests/test_relative.py index aefb595ff..3eab0f7ca 100644 --- a/perses/tests/test_relative.py +++ b/perses/tests/test_relative.py @@ -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 @@ -40,6 +39,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): """ @@ -668,7 +668,7 @@ def RepartitionedHybridTopologyFactory_energies(topology, chain, system, positio if res.id == '2': wt_res = res.name aminos_updated = [amino for amino in aminos if amino not in [wt_res, 'PRO', 'HIS', 'TRP', 'PHE', 'TYR']] - mutant = random.choice(aminos_updated) + mutant = rng.choice(aminos_updated) print(f'Making mutation {wt_res}->{mutant}') # Create point mutation engine to mutate residue at id 2 to random amino acid @@ -806,7 +806,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', 'HIS', 'TRP', 'PHE', 'TYR']] - 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, diff --git a/perses/tests/test_smc.py b/perses/tests/test_smc.py index 6dfc28a2a..9d3273c75 100644 --- a/perses/tests/test_smc.py +++ b/perses/tests/test_smc.py @@ -36,6 +36,7 @@ external_parallelism = None internal_parallelism = {'library': ('dask', 'LSF'), 'num_processes': 2} os.system(f"mkdir {trajectory_directory}") +rng = np.random.RandomState(42) ####################### @nottest @@ -179,7 +180,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 @@ -194,7 +195,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" @@ -206,7 +207,7 @@ 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(): @@ -214,14 +215,14 @@ 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" diff --git a/perses/tests/test_storage.py b/perses/tests/test_storage.py index df529c4a3..211b277e9 100644 --- a/perses/tests/test_storage.py +++ b/perses/tests/test_storage.py @@ -80,13 +80,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) diff --git a/perses/tests/test_topology_proposal.py b/perses/tests/test_topology_proposal.py index 7643fe389..a308c7921 100644 --- a/perses/tests/test_topology_proposal.py +++ b/perses/tests/test_topology_proposal.py @@ -35,6 +35,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' @@ -391,7 +392,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) @@ -434,7 +435,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)