Skip to content

Commit 7fec891

Browse files
committed
TPS wizard works through state defs
Only the TPS network and scheme remaining!
1 parent 7af7eae commit 7fec891

4 files changed

Lines changed: 78 additions & 32 deletions

File tree

‎paths_cli/wizard/core.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ def abort_retry_quit(wizard, obj_type):
3535

3636

3737
def interpret_req(req):
38-
_, num, direction = req
39-
dir_str = {'+': 'at least ', '=': '', '-': 'at most '}[direction]
40-
return dir_str + str(num)
38+
_, min_, max_ = req
39+
string = ""
40+
if min_ == max_:
41+
return str(min_)
42+
43+
if min_ >= 1:
44+
string += "at least " + str(min_)
45+
46+
if max_ < float("inf"):
47+
if string != "":
48+
string += " and "
49+
string += "at most " + str(max_)
50+
51+
return string
4152

‎paths_cli/wizard/cvs.py‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,17 @@ def _get_topology(wizard):
4747

4848
return topology
4949

50-
def _get_atom_indices(wizard, topology, cv_type):
50+
def _get_atom_indices(wizard, topology, n_atoms, cv_user_str):
5151
# TODO: move the logic here to parsing.tools
52-
n_atoms = {'distance': 2,
53-
'angle': 3,
54-
'dihedral': 4}[cv_type]
5552
arr = None
5653
helper = partial(mdtraj_atom_helper, n_atoms=n_atoms)
5754
while arr is None:
58-
atoms_str = wizard.ask("Which atoms do you want to measure the "
59-
"{cv_type} between?", helper=helper)
55+
atoms_str = wizard.ask(f"Which atoms do you want to {cv_user_str}?",
56+
helper=helper)
6057
try:
6158
indices = custom_eval(atoms_str)
6259
except Exception as e:
63-
wizard.exception(f"Sorry, I did't understand '{atoms_str}'", e)
60+
wizard.exception(f"Sorry, I did't understand '{atoms_str}'.", e)
6461
mdtraj_atom_helper(wizard, '?', n_atoms)
6562
continue
6663

@@ -81,21 +78,25 @@ def _get_atom_indices(wizard, topology, cv_type):
8178
arr = None
8279
continue
8380

84-
if arr is not None:
85-
atom_names = [str(topology.mdtraj.atom(i)) for i in arr[0]]
86-
wizard.say("Using atoms: " + " ".join(atom_names))
8781
return arr
8882

8983
def _mdtraj_function_cv(wizard, cv_does_str, cv_user_prompt, func,
9084
kwarg_name, n_atoms):
9185
from openpathsampling.experimental.storage.collective_variables import \
9286
MDTrajFunctionCV
93-
wizard.say("We'll make a CV that measures the distance between two "
94-
"atoms.")
87+
wizard.say(f"We'll make a CV that measures the {cv_does_str}.")
9588
topology = _get_topology(wizard)
96-
indices = _get_atom_indices(wizard, topology, n_atoms=n_atoms)
89+
indices = _get_atom_indices(wizard, topology, n_atoms=n_atoms,
90+
cv_user_str=cv_user_prompt)
9791
kwargs = {kwarg_name: indices}
9892

93+
summary = ("Here's what we'll create:\n"
94+
" Function: " + str(func.__name__) + "\n"
95+
" Atoms: " + " ".join([str(topology.mdtraj.atom(i))
96+
for i in indices[0]]) + "\n"
97+
" Topology: " + repr(topology.mdtraj))
98+
wizard.say(summary)
99+
99100
return MDTrajFunctionCV(func, topology, **kwargs)
100101

101102
def distance(wizard):

‎paths_cli/wizard/volumes.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def union_volume(wizard):
3838
return _binary_func_volume(wizard, operator.__or__)
3939

4040
def negated_volume(wizard):
41-
wizard.say("This volume will be everything not in the subvolume. ")
41+
wizard.say("This volume will be everything not in the subvolume.")
4242
wizard.say("Let's make the subvolume.")
4343
subvol = volumes(wizard)
4444
vol = ~subvol
@@ -54,7 +54,7 @@ def cv_defined_volume(wizard):
5454
while is_periodic is None:
5555
is_periodic_char = wizard.ask("Is this CV periodic?",
5656
options=["[Y]es", "[N]o"])
57-
is_periodic = {'y': True, 'n': False}
57+
is_periodic = {'y': True, 'n': False}[is_periodic_char]
5858

5959
if is_periodic:
6060
while period_min is None:

‎paths_cli/wizard/wizard.py‎

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
from paths_cli.parsing.core import custom_eval
1010
from paths_cli.wizard.joke import name_joke
1111

12+
DISPLAY_NAME = {
13+
'engines': "engine",
14+
'cvs': "CV",
15+
'states': "state",
16+
'tps_network': "network",
17+
}
18+
1219
WIZARD_PAGES = {
1320
"engines": (engines, "engine"),
1421
"cvs": (cvs, "cv"),
@@ -44,6 +51,9 @@ def __init__(self, requirements):
4451
self.console = Console()
4552
self.default = {}
4653

54+
def debug(content):
55+
self.console.print(content)
56+
4757
def _speak(self, content, preface):
4858
# we do custom wrapping here
4959
width = self.console.width - len(preface)
@@ -109,7 +119,7 @@ def ask_custom_eval(self, question, options=None, default=None,
109119
return result
110120

111121
def obj_selector(self, store_name, text_name, create_func):
112-
opts = {name: lambda wiz: obj
122+
opts = {name: lambda wiz, o=obj: o
113123
for name, obj in getattr(self, store_name).items()}
114124
create_new = f"Create a new {text_name}"
115125
opts[create_new] = create_func
@@ -123,16 +133,15 @@ def obj_selector(self, store_name, text_name, create_func):
123133
def exception(self, msg, exception):
124134
self.bad_input(msg + "\nHere's the error I got:\n" + str(exception))
125135

126-
def _req_allows_another(self, req):
127-
store, num, direction = req
136+
def _req_do_another(self, req):
137+
store, min_, max_ = req
128138
if store is None:
129-
return True
139+
return (True, False)
130140

131141
count = len(getattr(self, store))
132-
result = {'=': count < num,
133-
'+': True,
134-
'-': count < num}[direction]
135-
return result
142+
allows = count < max_
143+
requires = count < min_
144+
return requires, allows
136145

137146
def name(self, obj, obj_type, store_name, default=None):
138147
self.say(f"Now let's name your {obj_type}.")
@@ -187,22 +196,44 @@ def save_to_file(self):
187196
def _do_storage(self, storage):
188197
pass
189198

199+
def _ask_do_another(self, obj_type):
200+
do_another = None
201+
while do_another is None:
202+
do_another_char = self.ask(
203+
f"Would you like to make another {obj_type}?",
204+
options=["[Y]es", "[N]o"]
205+
)
206+
try:
207+
do_another = yes_no(do_another_char)
208+
except KeyError:
209+
self.bad_input("Sorry, I didn't understand that.")
210+
return do_another
211+
190212
def run_wizard(self):
191213
for page, req in self.requirements.items():
192214
store_name, _, _ = req
193-
while self._req_allows_another(req):
215+
do_another = True
216+
while do_another:
194217
func, obj_type = WIZARD_PAGES[page]
195218
obj = func(self)
196219
self.register(obj, obj_type, store_name)
220+
requires_another, allows_another = self._req_do_another(req)
221+
if requires_another:
222+
do_another = True
223+
elif not requires_another and allows_another:
224+
do_another = self._ask_do_another(obj_type)
225+
else:
226+
do_another = False
227+
197228

198229

199230
TPS_WIZARD = Wizard({
200231
# WIZARD_PAGE_NAME: (store_name, num, exact/at least/at most)
201-
'engines': ('engines', 1, '='),
202-
'cvs': ('cvs', 1, '+'),
203-
'states': ('states', 2, '+'),
204-
'tps_network': ('metworks', 1, '='),
205-
'tps_scheme': ('schemes', 1, '='),
232+
'engines': ('engines', 1, 1),
233+
'cvs': ('cvs', 1, float('inf')),
234+
'states': ('states', 2, float('inf')),
235+
'tps_network': ('metworks', 1, 1),
236+
'tps_scheme': ('schemes', 1, 1),
206237
'tps_finalize': (None, None, None),
207238
})
208239

@@ -222,3 +253,6 @@ def run_wizard(self):
222253
'mstis_network': (1, '='),
223254
'mstis_scheme': (1, '='),
224255
})
256+
257+
if __name__ == "__main__":
258+
TPS_WIZARD.run_wizard()

0 commit comments

Comments
 (0)