|
| 1 | +from paths_cli.wizard.tools import a_an |
| 2 | +from paths_cli.wizard.core import get_missing_object |
| 3 | +from paths_cli.wizard.shooting import ( |
| 4 | + shooting, |
| 5 | + # ALGORITHMS |
| 6 | + one_way_shooting, |
| 7 | + two_way_shooting, |
| 8 | + spring_shooting, |
| 9 | + # SELECTORS |
| 10 | + uniform_selector, |
| 11 | + gaussian_selector, |
| 12 | + # MODIFIERS |
| 13 | + random_velocities, |
| 14 | + # all_atom_delta_v, |
| 15 | + # single_atom_delta_v, |
| 16 | +) |
| 17 | +from functools import partial |
| 18 | + |
| 19 | +def _select_states(wizard, state_type): |
| 20 | + states = [] |
| 21 | + do_another = True |
| 22 | + while do_another: |
| 23 | + options = [name for name in wizard.states |
| 24 | + if name not in states] |
| 25 | + done = f"No more {state_type} states to select" |
| 26 | + if len(states) >= 1: |
| 27 | + options.append(done) |
| 28 | + |
| 29 | + state = wizard.ask_enumerate( |
| 30 | + f"Pick a state to use for as {a_an(state_type)} {state_type} " |
| 31 | + "state", options=options |
| 32 | + ) |
| 33 | + if state == done: |
| 34 | + do_another = False |
| 35 | + else: |
| 36 | + states.append(state) |
| 37 | + |
| 38 | + state_objs = [wizard.states[state] for state in states] |
| 39 | + return state_objs |
| 40 | + |
| 41 | +def _get_pathlength(wizard): |
| 42 | + pathlength = None |
| 43 | + while pathlength is None: |
| 44 | + len_str = wizard.ask("How long (in frames) do you want yout " |
| 45 | + "trajectories to be?") |
| 46 | + try: |
| 47 | + pathlength = int(len_str) |
| 48 | + except Exceptions as e: |
| 49 | + wizard.exception(f"Sorry, I couldn't make '{len_str}' into " |
| 50 | + "an integer.", e) |
| 51 | + return pathlength |
| 52 | + |
| 53 | +def flex_length_tps_network(wizard): |
| 54 | + import openpathsampling as paths |
| 55 | + initial_states = _select_states(wizard, 'initial') |
| 56 | + final_states = _select_states(wizard, 'final') |
| 57 | + network = paths.TPSNetwork(initial_states, final_states) |
| 58 | + return network |
| 59 | + |
| 60 | +def fixed_length_tps_network(wizard): |
| 61 | + pathlength = _get_pathlength(wizard) |
| 62 | + initial_states = _select_states(wizard, 'initial') |
| 63 | + final_states = _select_states(wizard, 'final') |
| 64 | + network = paths.FixedLengthTPSNetwork(initial_states, final_states, |
| 65 | + length=pathlength) |
| 66 | + return network |
| 67 | + |
| 68 | +def tps_network(wizard): |
| 69 | + import openpathsampling as paths |
| 70 | + FIXED = "Fixed length TPS" |
| 71 | + FLEX = "Flexible length TPS" |
| 72 | + tps_types = { |
| 73 | + FLEX: paths.TPSNetwork, |
| 74 | + FIXED: paths.FixedLengthTPSNetwork, |
| 75 | + } |
| 76 | + network_type = None |
| 77 | + while network_type is None: |
| 78 | + network_type = wizard.ask_enumerate( |
| 79 | + "Do you want to do flexible path length TPS (recommended) " |
| 80 | + "or fixed path length TPS?", options=list(tps_types.keys()) |
| 81 | + ) |
| 82 | + network_class = tps_types[network_type] |
| 83 | + |
| 84 | + if network_type == FIXED: |
| 85 | + pathlength = _get_pathlength(wizard) |
| 86 | + network_class = partial(network_class, length=pathlength) |
| 87 | + |
| 88 | + initial_states = _select_states(wizard, 'initial') |
| 89 | + final_states = _select_states(wizard, 'final') |
| 90 | + |
| 91 | + # TODO: summary |
| 92 | + |
| 93 | + obj = network_class(initial_states, final_states) |
| 94 | + return obj |
| 95 | + |
| 96 | +def _get_network(wizard): |
| 97 | + if len(wizard.networks) == 0: |
| 98 | + network = tps_network(wizard) |
| 99 | + elif len(wizard.networks) == 1: |
| 100 | + network = list(wizard.networks.values())[0] |
| 101 | + else: |
| 102 | + networks = list(wizard.networks.keys()) |
| 103 | + sel = wizard.ask_enumerate("Which network would you like to use?", |
| 104 | + options=networks) |
| 105 | + network = wizard.networks[network] |
| 106 | + return network |
| 107 | + |
| 108 | +def tps_scheme(wizard, network=None): |
| 109 | + import openpathsampling as paths |
| 110 | + from openpathsampling import strategies |
| 111 | + if network is None: |
| 112 | + network = get_missing_object(wizard, wizard.networks, 'network', |
| 113 | + tps_network) |
| 114 | + |
| 115 | + shooting_strategy = shooting(wizard) |
| 116 | + # TODO: add an option for shifting maybe? |
| 117 | + global_strategy = strategies.OrganizeByMoveGroupStrategy() |
| 118 | + scheme = paths.MoveScheme(network) |
| 119 | + scheme.append(shooting_strategy) |
| 120 | + scheme.append(global_strategy) |
| 121 | + return scheme |
| 122 | + |
| 123 | +def tps_finalize(wizard): |
| 124 | + pass |
| 125 | + |
| 126 | +def tps_setup(wizard): |
| 127 | + network = tps_network(wizard) |
| 128 | + scheme = tps_scheme(wizard, network) |
| 129 | + # name it |
| 130 | + # provide final info on how to use it |
| 131 | + return scheme |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + from paths_cli.wizard.wizard import Wizard |
| 136 | + wiz = Wizard({'tps_network': ('networks', 1, '='), |
| 137 | + 'tps_scheme': ('schemes', 1, '='), |
| 138 | + 'tps_finalize': (None, None, None)}) |
0 commit comments