Skip to content

Commit 666f9a9

Browse files
committed
major steps toward TPS wizard
1 parent 250f4e2 commit 666f9a9

6 files changed

Lines changed: 337 additions & 59 deletions

File tree

‎paths_cli/wizard/core.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,16 @@ def interpret_req(req):
5050

5151
return string
5252

53+
54+
def get_missing_object(wizard, obj_dict, display_name, fallback_func):
55+
if len(obj_dict) == 0:
56+
obj = fallback_func(wizard)
57+
elif len(obj_dict) == 1:
58+
obj = list(obj_dict.values())[0]
59+
else:
60+
objs = list(obj_dict.keys())
61+
sel = wizard.ask_enumerate(f"Which {display_name} would you like "
62+
"to use?", options=objs)
63+
obj = objs[sel]
64+
return obj
65+

‎paths_cli/wizard/joke.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def _joke4(name, obj_type):
3737

3838
def name_joke(name, obj_type):
3939
rnd = random.random()
40-
if 0 <= rnd < 0.30:
40+
if rnd < 0.25:
4141
joke = _joke1
42-
elif 0.30 <= rnd < 0.60:
42+
elif rnd < 0.50:
4343
joke = _joke2
44-
elif 0.60 <= rnd < 0.75:
44+
elif rnd < 0.65:
4545
joke = _joke3
4646
else:
4747
joke = _joke4

‎paths_cli/wizard/shooting.py‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from paths_cli.wizard.core import get_missing_object
2+
from paths_cli.wizard.engines import engines
3+
from paths_cli.wizard.cvs import cvs
4+
from paths_cli.parsing.core import custom_eval
5+
6+
import numpy as np
7+
8+
9+
def uniform_selector(wizard):
10+
import openpathsampling as paths
11+
return paths.UniformSelector()
12+
13+
def gaussian_selector(wizard):
14+
import openpathsampling as paths
15+
cv = wizard.ask_enumerate("Which CV do you want the Gaussian to be "
16+
"based on?", options=wizard.cvs.keys())
17+
l_0 = wizard.ask_custom_eval(f"At what value of {cv.name} should the "
18+
"Gaussian be centered?")
19+
std = wizard.ask_custom_eval("What should be the standard deviation of "
20+
"the Gaussian?")
21+
alpha = 0.5 / (std**2)
22+
selector = paths.GaussianBiasSelector(cv, alpha=alpha, l_0=l_0)
23+
return selector
24+
25+
def random_velocities(wizard):
26+
pass
27+
28+
def gaussian_momentum_shift(wizard):
29+
pass
30+
31+
def get_allowed_modifiers(engine):
32+
allowed = []
33+
randomize_attribs = ['randomize_velocities', 'apply_constraints']
34+
if any(hasattr(engine, attr) for attr in randomize_attribs):
35+
allowed.append('random_velocities')
36+
37+
if not engine.has_constraints():
38+
allowed.append('velocity_changers')
39+
40+
return allowed
41+
42+
SHOOTING_SELECTORS = {
43+
'Uniform random': uniform_selector,
44+
'Gaussian bias': gaussian_selector,
45+
}
46+
47+
48+
def _get_selector(wizard, selectors):
49+
if selectors is None:
50+
selectors = SHOOTING_SELECTORS
51+
selector = None
52+
while selector is None:
53+
sel = wizard.ask_enumerate("How do you want to select shooting "
54+
"points?", options=list(selectors.keys()))
55+
selector = selectors[sel](wizard)
56+
57+
return selector
58+
59+
def one_way_shooting(wizard, selectors=None, engine=None):
60+
from openpathsampling import strategies
61+
if engine is None:
62+
engine = get_missing_object(wizard, wizard.engines, 'engine',
63+
engines)
64+
65+
selector = _get_selector(wizard, selectors)
66+
strat = strategies.OneWayShootingStrategy(selector=selector)
67+
return strat
68+
69+
def two_way_shooting(wizard, selectors=None, modifiers=None):
70+
pass
71+
72+
def spring_shooting(wizard, engine=None):
73+
from openpathsampling import strategies
74+
if engine is None:
75+
engine = get_missing_object(wizard, wizard.engines, 'engine',
76+
engines)
77+
78+
delta_max = wizard.ask_custom_eval(
79+
"What is the maximum shift (delta_max) in frames?", type_=int
80+
)
81+
k_spring = wizard.ask_custom_eval("What is the spring constant k?",
82+
type_=float)
83+
strat = strategies.SpringShootingStrategy(delta_max=delta_max,
84+
k_spring=k_spring)
85+
return strat
86+
87+
88+
SHOOTING_TYPES = {
89+
'One-way (stochastic) shooting': one_way_shooting,
90+
# 'Two-way shooting': two_way_shooting,
91+
'Spring shooting': spring_shooting,
92+
}
93+
94+
95+
def shooting(wizard, shooting_types=None, engine=None):
96+
if shooting_types is None:
97+
shooting_types = SHOOTING_TYPES
98+
99+
if engine is None:
100+
engine = get_missing_object(wizard, wizard.engines, 'engine',
101+
engines)
102+
103+
# allowed_modifiers = get_allowed_modifiers(engine)
104+
# TWO_WAY = 'Two-way shooting'
105+
# if len(allowed_modifiers) == 0 and TWO_WAY in shooting_types:
106+
# del shooting_types[TWO_WAY]
107+
# else:
108+
# shooting_types[TWO_WAY] = partial(two_way_shooting,
109+
# allowed_modifiers=allowed_modifiers)
110+
111+
shooting_type = None
112+
if len(shooting_types) == 1:
113+
shooting_type = list(shooting_types.values())[0]
114+
else:
115+
while shooting_type is None:
116+
type_name = wizard.ask_enumerate(
117+
"Select the type of shooting move.",
118+
options=list(shooting_types.keys())
119+
)
120+
shooting_type = shooting_types[type_name]
121+
122+
shooting_strategy = shooting_type(wizard)
123+
return shooting_strategy
124+

‎paths_cli/wizard/tps.py‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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)})

‎paths_cli/wizard/volumes.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def _vol_intro(wizard, as_state):
66
if as_state:
7-
req = wizard.requirements['states']
7+
req = wizard.requirements['state']
88
if len(wizard.states) == 0:
99
intro = ("Now let's define stable states for your system. "
1010
f"You'll need to define {interpret_req(req)} of them.")

0 commit comments

Comments
 (0)