diff --git a/perses/tests/test_coordinate_numba.py b/perses/tests/test_coordinate_numba.py index fdbddb4ab..9b46fbd2c 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) @@ -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) diff --git a/perses/tests/test_relative.py b/perses/tests/test_relative.py index 37223df8a..b2a8eabac 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 @@ -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): """ @@ -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, diff --git a/perses/tests/test_smc.py b/perses/tests/test_smc.py index 6f0fe47b2..4f953ed5b 100644 --- a/perses/tests/test_smc.py +++ b/perses/tests/test_smc.py @@ -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) ####################### @@ -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 @@ -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" @@ -207,7 +208,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(): @@ -215,14 +216,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 3036c25f1..ecf276fed 100644 --- a/perses/tests/test_storage.py +++ b/perses/tests/test_storage.py @@ -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) diff --git a/perses/tests/test_topology_proposal.py b/perses/tests/test_topology_proposal.py index 217ce3012..194a129a8 100644 --- a/perses/tests/test_topology_proposal.py +++ b/perses/tests/test_topology_proposal.py @@ -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' @@ -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) @@ -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)