def test_init_threads_restart(self): """Tests successful initialization of restarted threads""" settings = configure('../../data/atesa.config') # First, make restart.pkl settings.degeneracy = 1 settings.initial_coordinates = [ '../test_data/test.rst7', '../test_data/test_two_init.rst7' ] settings.rc_definition = '1.00 + 2.34*CV0 - 5.67*CV1' settings.rc_reduced_cvs = False settings.cvs = [ 'pytraj.distance(traj, \'@1 @2\')[0]', 'pytraj.angle(traj, \'@2 @3 @4\')[0]' ] settings.include_qdot = False settings.topology = '../test_data/test.prmtop' allthreads = main.init_threads(settings) allthreads[0].history.init_coords = [ [ '../test_data/test_velocities_init.rst7', '../test_data/test_velocities_init_bwd.rst7' ], [ '../test_data/test_two_init.rst7', '../test_data/test_two_init_bwd.rst7' ] ] allthreads[0].history.prod_results = [['fwd', 'bwd'], ['bwd', 'bwd']] allthreads[1].history.init_coords = [ [ '../test_data/test_velocities_init.rst7', '../test_data/test_velocities_init_bwd.rst7' ], [ '../test_data/test_two_init.rst7', '../test_data/test_two_init_bwd.rst7' ] ] allthreads[1].history.prod_results = [['bwd', 'bwd'], ['fwd', 'bwd']] allthreads[0].history.timestamps = [1, 3] allthreads[1].history.timestamps = [2, 4] pickle.dump(allthreads, open('restart.pkl', 'wb'), protocol=2) # Then, reset settings, set restart = True, and try again settings = configure('../../data/atesa.config') settings.restart = True settings.degeneracy = 1 allthreads = main.init_threads(settings) assert len(allthreads) == 2 assert allthreads[0].history.prod_results == [['fwd', 'bwd'], ['bwd', 'bwd']] assert allthreads[1].history.prod_results == [['bwd', 'bwd'], ['fwd', 'bwd']] assert allthreads[0].topology == 'test.prmtop' assert allthreads[1].topology == 'test.prmtop'
def test_main(self): """Tests information_error.main using sham shooting points in test_data""" settings = configure('../../data/atesa.config') settings.topology = '../test_data/test.prmtop' settings.cvs = ['pytraj.distance(traj, \'@1 @2\')[0]', 'pytraj.angle(traj, \'@2 @3 @4\')[0]'] settings.include_qdot = False settings.DEBUG = True # to skip resampling and substitute our own as_decorr.out file shutil.copy('../test_data/test_velocities_init.rst7', '../test_temp') shutil.copy('../test_data/test_two_init.rst7', '../test_temp') shutil.copy('../test_data/as.out', '../test_temp') shutil.copy('../test_data/as_big.out', 'as_decorr_6054.out') # First test without extant settings.pkl file with pytest.raises(FileNotFoundError): information_error.main() settings.__dict__.pop('env') # env attribute is not picklable pickle.dump(settings, open('settings.pkl', 'wb'), protocol=2) # main will look for this file to load in settings shutil.copy('../test_data/as_big.out', 'as_decorr_6054.out') information_error.main() assert os.path.exists('info_err.out') assert float(open('info_err.out', 'r').readlines()[0].split(' ')[0]) == 6054 assert float('%.2f' % float(open('info_err.out', 'r').readlines()[0].split(' ')[1])) == pytest.approx(0.16, 1E-2)
def test_algorithm_aimless_shooting_prod_always_new_not_accepted(self): """Tests algorithm with job_type = 'aimless_shooting', always_new = True and thread.current_type = ['prod', 'prod'] for a move that isn't accepted""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.topology = '../test_data/test.prmtop' settings.always_new = True settings.initial_coordinates = ['../test_data/test_velocities.rst7'] settings.min_dt = -1 settings.max_dt = -1 # set these to the same value to guarantee which frame is chosen allthreads = main.init_threads(settings) allthreads[0].current_type = ['prod', 'prod'] allthreads[0].history.prod_results = [['bwd', 'fwd'], ['fwd', 'fwd'] ] # accepted then not accepted allthreads[0].history.prod_trajs = [[ '../test_data/test.nc', '../test_data/test.nc' ], ['../test_data/test.nc', '../test_data/test.nc']] allthreads[0].suffix = 1 allthreads[0].history.last_accepted = 0 jobtype = factory.jobtype_factory(settings.job_type) jobtype.algorithm(allthreads[0], allthreads, allthreads, settings) assert filecmp.cmp( allthreads[0].history.init_inpcrd[1], '../test_data/test.rst7') # test.rst7 is last frame of test.nc os.remove( '../test_data/test.nc_frame_-1.rst7' ) # have to do this manually because aimless shooting's mdengine getframe method keeps the '../test_data/' in front of the file name
def config_equilibrium_path_sampling(): """Sets up configuration settings for equilibrium path sampling tests, to be overwritten as needed""" shutil.copy('../../data/atesa.config', 'eps.config') with open('eps.config', 'a') as f: # need to set these things before calling configure() f.write('\njob_type = \'equilibrium_path_sampling\'') f.write('\neps_rc_min = -50' ) # crazy wide range so everything gets included f.write('\neps_rc_max = 50') f.write('\neps_rc_step = 1') f.write('\neps_rc_overlap = 0.1') f.write('\neps_n_steps = 6') f.write('\neps_out_freq = 1') f.write('\neps_dynamic_seed = 3') f.write('\nsamples_per_window = -1') f.close() settings = configure('eps.config') settings.DEBUG = True settings.job_type = 'equilibrium_path_sampling' settings.topology = '../test_data/test.prmtop' settings.rc_reduced_cvs = False settings.include_qdot = False settings.cvs = [ 'pytraj.distance(traj, \'@1 @2\')[0]', 'pytraj.angle(traj, \'@2 @3 @4\')[0]' ] settings.rc_definition = '1.00 + 2.34*CV1 - 0.67*CV2' settings.initial_coordinates = ['../test_data/test.rst7'] return settings
def test_update_results_aimless_shooting_prod(self): """Tests update_results with job_type = 'aimless_shooting' and thread.current_type = ['prod', 'prod']""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.initial_coordinates = [ '../test_data/test_velocities.rst7', '../test_data/test_velocities.rst7' ] settings.topology = '../test_data/test.prmtop' settings.commit_fwd = [[1, 2], [3, 4], [1.5, 2.0], ['lt', 'gt']] settings.commit_bwd = [[1, 2], [3, 4], [2.0, 1.5], ['gt', 'lt']] allthreads = main.init_threads(settings) allthreads[0].current_type = ['prod', 'prod'] allthreads[0].history.prod_trajs = [[ '../test_data/test.nc', '../test_data/test.nc' ]] allthreads[0].history.init_coords = [[ 'test_velocities.rst7_0_init.rst7' ]] shutil.copy( '../test_data/test_velocities.rst7', 'test_velocities.rst7_0_init.rst7') # create the needed file jobtype = factory.jobtype_factory(settings.job_type) jobtype.update_results(allthreads[0], allthreads, settings) assert os.path.exists('restart.pkl') assert os.path.exists('status.txt') assert allthreads[0].history.prod_results == [['', '']]
def test_interpret_cv(self): """Test interpret_cv for a distance, angle, dihedral, and difference of distances for both pytraj and mdtraj""" settings = configure('../../data/atesa.config') settings.cvs = [ 'pytraj.distance(traj, \'@1 @2\')[0]', 'pytraj.angle(traj, \'@1 @2 @3\')[0]', 'pytraj.dihedral(traj, \'@1 @2 @3 @4\')[0]', 'pytraj.distance(traj, \'@1 @2\')[0] - pytraj.distance(traj, \'@3 @4\')[0]', 'mdtraj.compute_distances(mtraj, numpy.array([[83, 84]]))[0][0] * 10', 'mdtraj.compute_angles(mtraj, numpy.array([[83, 85, 86]]))[0][0] * 180 / numpy.pi', 'mdtraj.compute_dihedrals(mtraj, numpy.array([[84, 83, 85, 86]]))[0][0] * 180 / numpy.pi', '(mdtraj.compute_distances(mtraj, numpy.array([[101, 103]]))[0][0] * 10) - (mdtraj.compute_distances(mtraj, numpy.array([[101, 104]]))[0][0] * 10)' ] assert utilities.interpret_cv(1, settings) == (['1', '2'], 'distance', 2) assert utilities.interpret_cv(2, settings) == (['1', '2', '3'], 'angle', 3) assert utilities.interpret_cv(3, settings) == (['1', '2', '3', '4'], 'dihedral', 4) assert utilities.interpret_cv(4, settings) == (['1', '2', '3', '4'], 'diffdistance', 4) assert utilities.interpret_cv(5, settings) == (['84', '85'], 'distance', 2) assert utilities.interpret_cv(6, settings) == (['84', '86', '87'], 'angle', 3) assert utilities.interpret_cv(7, settings) == (['85', '84', '86', '87'], 'dihedral', 4) assert utilities.interpret_cv( 8, settings) == (['102', '104', '102', '105'], 'diffdistance', 4)
def test_algorithm_aimless_shooting_init(self): """Tests algorithm with job_type = 'aimless_shooting' and thread.current_type = ['init']""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.degeneracy = 1 settings.initial_coordinates = ['../test_data/test_velocities.rst7'] allthreads = main.init_threads(settings) allthreads[0].current_type = ['init'] allthreads[0].history.init_coords = [[ 'test_velocities.rst7_0_init.rst7' ]] jobtype = factory.jobtype_factory(settings.job_type) jobtype.algorithm(allthreads[0], allthreads, allthreads, settings) assert allthreads[0].current_type == [ ] # result for missing .rst7 file (haven't copied it yet) allthreads[0].current_type = ['init'] # reset last result shutil.copy( '../test_data/test_velocities.rst7', 'test_velocities.rst7_0_init.rst7') # create the needed file jobtype.algorithm(allthreads[0], allthreads, allthreads, settings) assert allthreads[0].current_type == ['init' ] # results for .rst7 was found assert allthreads[0].history.init_coords == [[ 'test_velocities.rst7_0_init.rst7', 'test_velocities.rst7_0_init_bwd.rst7' ]]
def test_update_history_equilibrium_path_sampling_out_of_bounds(self): """Tests update_history with job_type = 'equilibrium_path_sampling' with an initially out-of-bounds RC value""" shutil.copy('../../data/atesa.config', 'eps.config') with open( 'eps.config', 'a' ) as f: # need to set these things before calling configure() f.write('\njob_type = \'equilibrium_path_sampling\'') f.write('\neps_rc_min = -0.1' ) # crazy wide range so everything gets included f.write('\neps_rc_max = 0.1') f.write('\neps_rc_step = 0.1') f.write('\neps_overlap = 0.01') f.write('\neps_dynamic_seed = 3') f.close() settings = configure('eps.config') settings.DEBUG = True settings.job_type = 'equilibrium_path_sampling' settings.topology = '../test_data/test.prmtop' settings.rc_reduced_cvs = False settings.include_qdot = False settings.cvs = [ 'pytraj.distance(traj, \'@1 @2\')[0]', 'pytraj.angle(traj, \'@2 @3 @4\')[0]' ] settings.rc_definition = '1.00 + 2.34*CV1 - 0.67*CV2' settings.initial_coordinates = ['../test_data/test.rst7'] with pytest.raises(RuntimeError): allthreads = main.init_threads(settings)
def test_check_commit_none(self): """Tests check_commit using a dummy coordinate file and commitments defined to get result ''""" settings = configure('../../data/atesa.config') settings.topology = '../test_data/test.prmtop' settings.commit_fwd = [[1, 2], [3, 4], [1.0, 1.5], ['gt', 'lt']] settings.commit_bwd = [[1, 2], [3, 4], [0.5, 2.0], ['lt', 'gt']] assert utilities.check_commit('../test_data/test.rst7', settings) == ''
def test_check_commit_traj(self): """Tests check_commit using a dummy trajectory file and commitments defined to get result 'fwd'""" settings = configure('../../data/atesa.config') settings.topology = '../test_data/test.prmtop' settings.commit_fwd = [[1, 2], [3, 4], [1.0, 1.7], ['gt', 'lt']] settings.commit_bwd = [[1, 2], [3, 4], [0.5, 2.0], ['lt', 'gt']] assert utilities.check_commit('../test_data/test.nc', settings) == 'fwd'
def test_get_batch_file_aimless_shooting_broken(self): """Tests thread.get_batch_template with job_type = 'aimless_shooting' and invalid type = 'fwdd'""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.degeneracy = 1 allthreads = main.init_threads(settings) with pytest.raises(ValueError): allthreads[0].get_batch_template('fwdd', settings)
def test_get_next_step_aimless_shooting_prod(self): """Tests thread.get_next_step with job_type = 'aimless_shooting' and type = ['fwd','bwd']""" settings = configure('../../data/atesa.config') settings.degeneracy = 1 settings.job_type = 'aimless_shooting' allthreads = main.init_threads(settings) allthreads[0].current_type = ['prod', 'prod'] assert allthreads[0].get_next_step(settings) == (['init'], ['init'])
def test_check_for_successful_step_committor_analysis(self): """Tests check_for_successful_step with job_type = 'committor_analysis'""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.committor_analysis_use_rc_out = False allthreads = main.init_threads(settings) jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.check_for_successful_step(allthreads[0], settings) == True
def test_process_to_be_terminated(self): """Tests process.py for a thread with terminated = True""" settings = configure('../../data/atesa.config') settings.degeneracy = 1 allthreads = main.init_threads(settings) allthreads[0].terminated = True running = [allthreads[0]] process(allthreads[0], running, settings) assert running == []
def test_get_batch_file_aimless_shooting_amber(self): """Tests thread.get_batch_template with job_type = 'aimless_shooting' and md_engine = 'amber'""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.degeneracy = 1 settings.md_engine = 'amber' allthreads = main.init_threads(settings) assert allthreads[0].get_batch_template( 'init', settings) == 'amber_' + settings.batch_system + '.tpl'
def test_gatekeep_committor_analysis(self): """Tests gatekeep with job_type = 'committor_analysis'""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.DEBUG = True settings.committor_analysis_use_rc_out = False allthreads = main.init_threads(settings) allthreads[0].jobids = ['123456'] jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.gatekeeper(allthreads[0], settings) == True
def test_update_results_aimless_shooting_init(self): """Tests update_results with job_type = 'aimless_shooting' and thread.current_type = ['init']""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.degeneracy = 1 allthreads = main.init_threads(settings) allthreads[0].current_type = ['init'] jobtype = factory.jobtype_factory(settings.job_type) jobtype.update_results(allthreads[0], allthreads, settings) assert os.path.exists('restart.pkl')
def test_main(self): """Tests main.main with DEBUG = True to skip actual job submission/monitoring""" settings = configure('../../data/atesa.config') settings.initial_coordinates = ['../test_data/test.rst7'] settings.topology = '../test_data/test.prmtop' settings.degeneracy = 2 settings.overwrite = True settings.resample = False settings.DEBUG = True assert main.main(settings) == 'ATESA run exiting normally'
def test_gatekeep_aimless_shooting(self): """Tests gatekeep with job_type = 'aimless_shooting'""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.degeneracy = 1 settings.DEBUG = True allthreads = main.init_threads(settings) allthreads[0].jobids = ['123456'] jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.gatekeeper(allthreads[0], settings) == True
def test_check_termination_aimless_shooting_init(self): """Tests check_termination with job_type = 'aimless_shooting' and thread.current_type = ['init']""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.degeneracy = 1 allthreads = main.init_threads(settings) allthreads[0].current_type = ['init'] jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.check_termination( allthreads[0], allthreads, settings) == False # never terminate after an 'init' step
def test_get_cvs_with_qdot_broken(self): """Tests get_cvs with a dummy coordinate file, include_qdot = True, and a coordinate file without velocities""" settings = configure('../../data/atesa.config') settings.include_qdot = True settings.topology = '../test_data/test.prmtop' settings.cvs = [ 'pytraj.distance(traj, \'@1 @3\')[0]', 'pytraj.distance(traj, \'@2 @4\')[0]' ] with pytest.raises(IndexError): result = utilities.get_cvs('../test_data/test.rst7', settings)
def test_check_termination_committor_analysis(self): """Tests check_termination with job_type = 'committor_analysis'""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.committor_analysis_use_rc_out = False allthreads = main.init_threads(settings) allthreads[0].current_type = ['prod', 'prod', 'prod'] jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.check_termination(allthreads[0], allthreads, settings) == False assert allthreads[0].terminated == True
def test_get_initial_coordinates_committor_analysis_rc_out_does_not_exist( self): """Tests get_initial_coordinates with job_type = 'committor_analysis' using an RC out file that does not exist""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.committor_analysis_use_rc_out = True settings.path_to_rc_out = '../test_data/rc_eval_FAKE.out' settings.rc_threshold = 0.002 jobtype = factory.jobtype_factory(settings.job_type) with pytest.raises(FileNotFoundError): allthreads = main.init_threads(settings)
def test_get_next_step_committor_analysis(self): """Tests get_next_step with job_type = 'committor_analysis'""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.committor_analysis_use_rc_out = False settings.committor_analysis_n = 3 allthreads = main.init_threads(settings) jobtype = factory.jobtype_factory(settings.job_type) this_types, this_names = jobtype.get_next_step(allthreads[0], settings) assert this_types == ['prod', 'prod', 'prod'] assert this_names == ['0', '1', '2']
def test_configure_directory(self): """Tests configure.py behavior in correcting an improperly formatted directory""" shutil.copy( '../../data/atesa.config', 'atesa.config' ) # todo: make a dedicated 'test' copy of atesa.config for tests to use. config_lines = open('atesa.config', 'r').readlines() for line in config_lines: if 'working_directory' in line: line = 'working_directory = \'/foo/bar/\'\n' open('atesa_temp.config', 'a').write(line) settings = configure('atesa_temp.config') assert settings.working_directory == '/foo/bar'
def test_check_commit_value_errors(self): """Tests check_commit using a dummy coordinate file and commitments defined to get ValueErrors""" settings = configure('../../data/atesa.config') settings.topology = '../test_data/test.prmtop' settings.commit_fwd = [[1, 2], [3, 4], [1.0, 1.5], ['t', 'lt']] settings.commit_bwd = [[1, 2], [3, 4], [0.5, 2.0], ['lt', 'gt']] with pytest.raises(ValueError): utilities.check_commit('../test_data/test.rst7', settings) settings.commit_fwd = [[1, 2], [3, 4], [1.0, 1.5], ['gt', 'lt']] settings.commit_bwd = [[1, 2], [3, 4], [1.0, 1.5], ['t', 'lt']] with pytest.raises(ValueError): utilities.check_commit('../test_data/test.rst7', settings)
def test_get_initial_coordinates_committor_analysis_rc_out(self): """Tests get_initial_coordinates with job_type = 'committor_analysis' using an RC out file""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.committor_analysis_use_rc_out = True settings.path_to_rc_out = '../test_data/rc_eval.out' settings.rc_threshold = 0.002 jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.get_initial_coordinates(settings) == [ '1.1_1.2_2.2_2.6.rst7_2_10_init_fwd.rst_2_1_2.rst7_5_init_fwd.rst', '1.1_1.2_2.2_2.6.rst7_2_10_init_fwd.rst_4_58_3.rst7_4_init_fwd.rst' ]
def test_get_initial_coordinates_committor_analysis_rc_out_no_shooting_points( self): """Tests get_initial_coordinates with job_type = 'committor_analysis' using an RC out file containing no shooting points within the threshold""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.committor_analysis_use_rc_out = True settings.path_to_rc_out = '../test_data/rc_eval.out' settings.rc_threshold = 0.0002 jobtype = factory.jobtype_factory(settings.job_type) with pytest.raises(RuntimeError): allthreads = main.init_threads(settings)
def test_get_batch_template_committor_analysis(self): """Tests get_batch_template with job_type = 'committor_analysis'""" settings = configure('../../data/atesa.config') settings.job_type = 'committor_analysis' settings.md_engine = 'amber' settings.batch_system = 'slurm' settings.committor_analysis_use_rc_out = False settings.path_to_templates = sys.path[0] + '/atesa/data/templates' allthreads = main.init_threads(settings) jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.get_batch_template(allthreads[0], 'prod', settings) == 'amber_slurm.tpl'
def test_get_inpcrd_aimless_shooting_init(self): """Tests get_inpcrd with job_type = 'aimless_shooting' and thread.current_type = ['init']""" settings = configure('../../data/atesa.config') settings.job_type = 'aimless_shooting' settings.degeneracy = 1 settings.initial_coordinates = ['../test_data/test_velocities.rst7'] allthreads = main.init_threads(settings) allthreads[0].current_type = ['init'] jobtype = factory.jobtype_factory(settings.job_type) assert jobtype.get_inpcrd( allthreads[0]) == allthreads[0].history.init_inpcrd assert allthreads[0].history.init_inpcrd == ['test_velocities.rst7']