|
| 1 | +import pytest |
| 2 | +from click.testing import CliRunner |
| 3 | +from unittest.mock import patch |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from paths_cli.commands.bootstrap_init import * |
| 7 | +import openpathsampling as paths |
| 8 | +from openpathsampling.engines import toy |
| 9 | +from openpathsampling.tests.test_helpers import make_1d_traj |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def toy_2_state_engine(): |
| 13 | + pes = ( |
| 14 | + toy.OuterWalls([1.0, 1.0], [0.0, 0.0]) + |
| 15 | + toy.Gaussian(-1.0, [12.0, 12.0], [-0.5, 0.0]) + |
| 16 | + toy.Gaussian(-1.0, [12.0, 12.0], [0.5, 0.0]) |
| 17 | + ) |
| 18 | + topology=toy.Topology( |
| 19 | + n_spatial = 2, |
| 20 | + masses =[1.0, 1.0], |
| 21 | + pes = pes |
| 22 | + ) |
| 23 | + integ = toy.LangevinBAOABIntegrator(dt=0.02, temperature=0.1, gamma=2.5) |
| 24 | + options = { |
| 25 | + 'integ': integ, |
| 26 | + 'n_frames_max': 5000, |
| 27 | + 'n_steps_per_frame': 1 |
| 28 | + } |
| 29 | + |
| 30 | + engine = toy.Engine( |
| 31 | + options=options, |
| 32 | + topology=topology |
| 33 | + ) |
| 34 | + return engine |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def toy_2_state_cv(): |
| 38 | + return paths.FunctionCV("x", lambda s: s.xyz[0][0]) |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def toy_2_state_volumes(toy_2_state_cv): |
| 42 | + state_A = paths.CVDefinedVolume( |
| 43 | + toy_2_state_cv, |
| 44 | + float("-inf"), |
| 45 | + -0.3, |
| 46 | + ).named("A") |
| 47 | + state_B = paths.CVDefinedVolume( |
| 48 | + toy_2_state_cv, |
| 49 | + 0.3, |
| 50 | + float("inf"), |
| 51 | + ).named("B") |
| 52 | + return state_A, state_B |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def toy_2_state_tis(toy_2_state_cv, toy_2_state_volumes): |
| 56 | + state_A, state_B = toy_2_state_volumes |
| 57 | + interfaces = paths.VolumeInterfaceSet( |
| 58 | + toy_2_state_cv, |
| 59 | + float("-inf"), |
| 60 | + [-0.3, -0.2, -0.1], |
| 61 | + ) |
| 62 | + tis = paths.MISTISNetwork( |
| 63 | + [(state_A, interfaces, state_B)], |
| 64 | + ) |
| 65 | + return tis |
| 66 | + |
| 67 | + |
| 68 | +def print_test(init_frame, network, engine, transition, output_storage): |
| 69 | + print(init_frame.__uuid__) |
| 70 | + print(network.__uuid__) |
| 71 | + print(engine.__uuid__) |
| 72 | + # apparently transition UUID isn't preserved, but these are? |
| 73 | + print(transition.stateA.__uuid__) |
| 74 | + print(transition.stateB.__uuid__) |
| 75 | + print([e.__uuid__ for e in transition.ensembles]) |
| 76 | + print(isinstance(output_storage, paths.Storage)) |
| 77 | + |
| 78 | +@patch('paths_cli.commands.bootstrap_init.bootstrap_init_main', print_test) |
| 79 | +def test_bootstrap_init(tis_fixture): |
| 80 | + scheme, network, engine, init_conds = tis_fixture |
| 81 | + runner = CliRunner() |
| 82 | + with runner.isolated_filesystem(): |
| 83 | + storage = paths.Storage("setup.nc", 'w') |
| 84 | + storage.save(init_conds) |
| 85 | + for obj in tis_fixture: |
| 86 | + storage.save(obj) |
| 87 | + |
| 88 | + storage.tags["init_snap"] = init_conds[0][0] |
| 89 | + storage.close() |
| 90 | + |
| 91 | + results = runner.invoke(bootstrap_init, [ |
| 92 | + 'setup.nc', |
| 93 | + '-o', 'foo.nc', |
| 94 | + '--initial-state', "A", |
| 95 | + '--final-state', "B", |
| 96 | + '--init-frame', 'init_snap', |
| 97 | + ]) |
| 98 | + |
| 99 | + transitions = list(network.transitions.values()) |
| 100 | + assert len(transitions) == 1 |
| 101 | + transition = transitions[0] |
| 102 | + stateA = transition.stateA |
| 103 | + stateB = transition.stateB |
| 104 | + ensembles = transition.ensembles |
| 105 | + |
| 106 | + expected_output = ( |
| 107 | + f"{init_conds[0][0].__uuid__}\n{network.__uuid__}\n" |
| 108 | + f"{engine.__uuid__}\n" |
| 109 | + f"{stateA.__uuid__}\n{stateB.__uuid__}\n" |
| 110 | + f"{[e.__uuid__ for e in ensembles]}\n" |
| 111 | + "True\n" |
| 112 | + ) |
| 113 | + assert results.exit_code == 0 |
| 114 | + assert results.output == expected_output |
| 115 | + |
| 116 | + |
| 117 | +def test_bootstrap_init_main(toy_2_state_tis, toy_2_state_engine, tmp_path): |
| 118 | + network = toy_2_state_tis |
| 119 | + engine = toy_2_state_engine |
| 120 | + scheme = paths.DefaultScheme(network, engine) |
| 121 | + init_frame = toy.Snapshot( |
| 122 | + coordinates=np.array([[-0.5, -0.5]]), |
| 123 | + velocities=np.array([[0.0,0.0]]), |
| 124 | + engine=engine |
| 125 | + ) |
| 126 | + assert len(network.transitions) == 1 |
| 127 | + transition = list(network.transitions.values())[0] |
| 128 | + output_storage = paths.Storage(tmp_path / "output.nc", mode='w') |
| 129 | + init_conds, bootstrapper = bootstrap_init_main(init_frame, network, |
| 130 | + engine, transition, |
| 131 | + output_storage) |
| 132 | + init_conds.sanity_check() |
0 commit comments