|
| 1 | +import pytest |
| 2 | +import mock |
| 3 | + |
| 4 | +from paths_cli.tests.wizard.mock_wizard import mock_wizard |
| 5 | + |
| 6 | +from paths_cli.wizard.volumes import ( |
| 7 | + _vol_intro, intersection_volume, union_volume, negated_volume, |
| 8 | + cv_defined_volume, volumes, SUPPORTED_VOLUMES |
| 9 | +) |
| 10 | + |
| 11 | +import openpathsampling as paths |
| 12 | +from openpathsampling.experimental.storage.collective_variables import \ |
| 13 | + CoordinateFunctionCV |
| 14 | + |
| 15 | +from openpathsampling.tests.test_helpers import make_1d_traj |
| 16 | + |
| 17 | +def _wrap(x, period_min, period_max): |
| 18 | + # used in testing periodic CVs |
| 19 | + while x >= period_max: |
| 20 | + x -= period_max - period_min |
| 21 | + while x < period_min: |
| 22 | + x += period_max - period-min |
| 23 | + return x |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def volume_setup(): |
| 28 | + cv = CoordinateFunctionCV(lambda snap: snap.xyz[0][0]).named('x') |
| 29 | + vol1 = paths.CVDefinedVolume(cv, 0.0, 1.0) |
| 30 | + vol2 = paths.CVDefinedVolume(cv, 0.5, 1.5) |
| 31 | + return vol1, vol2 |
| 32 | + |
| 33 | +@pytest.mark.parametrize('as_state,has_state', [ |
| 34 | + (True, False), (True, True), (False, False) |
| 35 | +]) |
| 36 | +def test_vol_intro(as_state, has_state): |
| 37 | + wizard = mock_wizard([]) |
| 38 | + wizard.requirements['state'] = ('states', 2, float('inf')) |
| 39 | + if has_state: |
| 40 | + wizard.states['foo'] = 'placeholder' |
| 41 | + |
| 42 | + intro = _vol_intro(wizard, as_state) |
| 43 | + |
| 44 | + if as_state and has_state: |
| 45 | + assert "another stable state" in intro |
| 46 | + elif as_state and not has_state: |
| 47 | + assert "You'll need to define" in intro |
| 48 | + elif not as_state: |
| 49 | + assert intro is None |
| 50 | + else: |
| 51 | + raise RuntimeError("WTF?") |
| 52 | + |
| 53 | +def _binary_volume_test(volume_setup, func): |
| 54 | + vol1, vol2 = volume_setup |
| 55 | + wizard = mock_wizard([]) |
| 56 | + mock_volumes = mock.Mock(side_effect=[vol1, vol2]) |
| 57 | + with mock.patch('paths_cli.wizard.volumes.volumes', new=mock_volumes): |
| 58 | + vol = func(wizard) |
| 59 | + |
| 60 | + assert "first volume" in wizard.console.log_text |
| 61 | + assert "second volume" in wizard.console.log_text |
| 62 | + assert "Created" in wizard.console.log_text |
| 63 | + return wizard, vol |
| 64 | + |
| 65 | +def test_intersection_volume(volume_setup): |
| 66 | + wizard, vol = _binary_volume_test(volume_setup, intersection_volume) |
| 67 | + assert "intersection" in wizard.console.log_text |
| 68 | + traj = make_1d_traj([0.25, 0.75]) |
| 69 | + assert not vol(traj[0]) |
| 70 | + assert vol(traj[1]) |
| 71 | + |
| 72 | +def test_union_volume(volume_setup): |
| 73 | + wizard, vol = _binary_volume_test(volume_setup, union_volume) |
| 74 | + assert "union" in wizard.console.log_text |
| 75 | + traj = make_1d_traj([0.25, 0.75, 1.75]) |
| 76 | + assert vol(traj[0]) |
| 77 | + assert vol(traj[1]) |
| 78 | + assert not vol(traj[2]) |
| 79 | + |
| 80 | +def test_negated_volume(volume_setup): |
| 81 | + init_vol, _ = volume_setup |
| 82 | + traj = make_1d_traj([0.5, 1.5]) |
| 83 | + assert init_vol(traj[0]) |
| 84 | + assert not init_vol(traj[1]) |
| 85 | + wizard = mock_wizard([]) |
| 86 | + mock_vol = mock.Mock(return_value=init_vol) |
| 87 | + with mock.patch('paths_cli.wizard.volumes.volumes', new=mock_vol): |
| 88 | + vol = negated_volume(wizard) |
| 89 | + |
| 90 | + assert "not in" in wizard.console.log_text |
| 91 | + assert not vol(traj[0]) |
| 92 | + assert vol(traj[1]) |
| 93 | + |
| 94 | +@pytest.mark.parametrize('periodic', [True, False]) |
| 95 | +def test_cv_defined_volume(periodic): |
| 96 | + if periodic: |
| 97 | + min_ = 0.0 |
| 98 | + max_ = 1.0 |
| 99 | + cv = CoordinateFunctionCV( |
| 100 | + lambda snap: _wrap(snap.xyz[0][0], period_min=min_, |
| 101 | + period_max=max_), |
| 102 | + period_min=min_, period_max=max_ |
| 103 | + ).named('x') |
| 104 | + inputs = ['x', '0.75', '1.25'] |
| 105 | + in_state = make_1d_traj([0.2, 0.8]) |
| 106 | + out_state = make_1d_traj([0.5]) |
| 107 | + else: |
| 108 | + cv = CoordinateFunctionCV(lambda snap: snap.xyz[0][0]).named('x') |
| 109 | + inputs = ['x', '0.0', '1.0'] |
| 110 | + in_state = make_1d_traj([0.5]) |
| 111 | + out_state = make_1d_traj([-0.1, 1.1]) |
| 112 | + wizard = mock_wizard(inputs) |
| 113 | + wizard.cvs[cv.name] = cv |
| 114 | + vol = cv_defined_volume(wizard) |
| 115 | + assert "interval" in wizard.console.log_text |
| 116 | + for snap in in_state: |
| 117 | + assert vol(snap) |
| 118 | + for snap in out_state: |
| 119 | + assert not vol(snap) |
| 120 | + |
| 121 | +@pytest.mark.parametrize('intro', [None, "", "foo"]) |
| 122 | +def test_volumes(intro): |
| 123 | + say_hello = mock.Mock(return_value="hello!") |
| 124 | + wizard = mock_wizard(['Hello world']) |
| 125 | + with mock.patch.dict(SUPPORTED_VOLUMES, {'Hello world': say_hello}): |
| 126 | + assert volumes(wizard, intro=intro) == "hello!" |
| 127 | + assert wizard.console.input_call_count == 1 |
| 128 | + |
| 129 | + |
| 130 | + n_statements = 2 * (1 + 3) |
| 131 | + # 2: line and blank; (1 + 3): 1 in volumes + 3 in ask_enumerate |
| 132 | + |
| 133 | + if intro == 'foo': |
| 134 | + assert 'foo' in wizard.console.log_text |
| 135 | + assert len(wizard.console.log) == n_statements + 2 # from intro |
| 136 | + else: |
| 137 | + assert 'foo' not in wizard.console.log_text |
| 138 | + assert len(wizard.console.log) == n_statements |
0 commit comments