|
| 1 | +import pytest |
| 2 | +import mock |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from functools import partial |
| 6 | + |
| 7 | +from paths_cli.tests.wizard.mock_wizard import mock_wizard |
| 8 | + |
| 9 | +from paths_cli.wizard.cvs import ( |
| 10 | + _get_topology, _get_atom_indices, distance, angle, dihedral, rmsd, |
| 11 | + coordinate, cvs, SUPPORTED_CVS |
| 12 | +) |
| 13 | + |
| 14 | +import openpathsampling as paths |
| 15 | +from openpathsampling.experimental.storage.collective_variables import \ |
| 16 | + MDTrajFunctionCV |
| 17 | +from openpathsampling.tests.test_helpers import make_1d_traj |
| 18 | + |
| 19 | +@pytest.mark.parametrize('n_engines', [0, 1, 2]) |
| 20 | +def test_get_topology(ad_engine, n_engines): |
| 21 | + inputs = {0: [], 1: [], 2: ['1']}[n_engines] |
| 22 | + wizard = mock_wizard(inputs) |
| 23 | + engines = [ad_engine, paths.engines.NoEngine(None).named('foo')] |
| 24 | + wizard.engines = {eng.name: eng for eng in engines[:n_engines]} |
| 25 | + def mock_register(obj, obj_type, store_name): |
| 26 | + wizard.engines[obj.name] = obj |
| 27 | + wizard.register = mock_register |
| 28 | + |
| 29 | + mock_engines = mock.Mock(return_value=ad_engine) |
| 30 | + patch_loc = 'paths_cli.wizard.engines.engines' |
| 31 | + with mock.patch(patch_loc, new=mock_engines): |
| 32 | + topology = _get_topology(wizard) |
| 33 | + assert isinstance(topology, |
| 34 | + paths.engines.openmm.topology.MDTrajTopology) |
| 35 | + |
| 36 | +@pytest.mark.parametrize('inputs', [ |
| 37 | + (['[[1, 2]]']), (['1, 2']), |
| 38 | + (['[[1, 2, 3]]', '[[1, 2]]']), |
| 39 | +]) |
| 40 | +def test_get_atom_indices(ad_engine, inputs): |
| 41 | + wizard = mock_wizard(inputs) |
| 42 | + arr = _get_atom_indices(wizard, ad_engine.topology, 2, "use") |
| 43 | + np.testing.assert_array_equal(arr, np.array([[1, 2]])) |
| 44 | + assert wizard.console.input_call_count == len(inputs) |
| 45 | + if len(inputs) > 1: |
| 46 | + assert "I didn't understand" in wizard.console.log_text |
| 47 | + |
| 48 | +def _mdtraj_function_test(wizard, func, md_func, ad_openmm, ad_engine): |
| 49 | + md = pytest.importorskip('mdtraj') |
| 50 | + wizard.engines[ad_engine.name] = ad_engine |
| 51 | + with ad_openmm.as_cwd(): |
| 52 | + cv = func(wizard) |
| 53 | + pdb = md.load('ad.pdb') |
| 54 | + assert isinstance(cv, MDTrajFunctionCV) |
| 55 | + traj = paths.engines.openmm.trajectory_from_mdtraj(pdb) |
| 56 | + np.testing.assert_array_almost_equal(md_func(pdb)[0], cv(traj)) |
| 57 | + |
| 58 | +def test_distance(ad_openmm, ad_engine): |
| 59 | + md = pytest.importorskip('mdtraj') |
| 60 | + wizard = mock_wizard(['0, 1']) |
| 61 | + md_func = partial(md.compute_distances, atom_pairs=[[0, 1]]) |
| 62 | + _mdtraj_function_test(wizard, distance, md_func, ad_openmm, ad_engine) |
| 63 | + |
| 64 | +def test_angle(ad_openmm, ad_engine): |
| 65 | + md = pytest.importorskip('mdtraj') |
| 66 | + wizard = mock_wizard(['0, 1, 2']) |
| 67 | + md_func = partial(md.compute_angles, angle_indices=[[0, 1, 2]]) |
| 68 | + _mdtraj_function_test(wizard, angle, md_func, ad_openmm, ad_engine) |
| 69 | + |
| 70 | +def test_dihedral(ad_openmm, ad_engine): |
| 71 | + md = pytest.importorskip('mdtraj') |
| 72 | + wizard = mock_wizard(['0, 1, 2, 3']) |
| 73 | + md_func = partial(md.compute_dihedrals, indices=[[0, 1, 2, 3]]) |
| 74 | + _mdtraj_function_test(wizard, dihedral, md_func, ad_openmm, ad_engine) |
| 75 | + |
| 76 | +@pytest.mark.parametrize('inputs', [ |
| 77 | + (['0', 'x']), ('foo', '0', 'x'), (['0', 'q', 'x']) |
| 78 | +]) |
| 79 | +def test_coordinate(inputs): |
| 80 | + wizard = mock_wizard(inputs) |
| 81 | + cv = coordinate(wizard) |
| 82 | + traj = make_1d_traj([5.0]) |
| 83 | + assert cv(traj[0]) == 5.0 |
| 84 | + if 'foo' in inputs: |
| 85 | + assert "I can't make an atom index" in wizard.console.log_text |
| 86 | + if 'q' in inputs: |
| 87 | + assert "Please select one of" in wizard.console.log_text |
| 88 | + |
| 89 | +@pytest.mark.parametrize('inputs', [(['foo', 'Distance']), (['Distance'])]) |
| 90 | +def test_cvs(inputs): |
| 91 | + wizard = mock_wizard(inputs) |
| 92 | + say_hello = mock.Mock(return_value="hello") |
| 93 | + with mock.patch.dict(SUPPORTED_CVS, {'Distance': say_hello}): |
| 94 | + assert cvs(wizard) == "hello" |
| 95 | + assert wizard.console.input_call_count == len(inputs) |
0 commit comments