Skip to content

Commit 5d2e3f8

Browse files
committed
Some CV cleanup; more periodicity to CVs
1 parent 76ea517 commit 5d2e3f8

3 files changed

Lines changed: 89 additions & 68 deletions

File tree

‎paths_cli/wizard/core.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from paths_cli.wizard.joke import name_joke
33
from paths_cli.wizard.tools import a_an
44

5+
from collections import namedtuple
6+
7+
WizardSay = namedtuple("WizardSay", ['msg', 'mode'])
8+
59
def name(wizard, obj, obj_type, store_name, default=None):
610
wizard.say(f"Now let's name your {obj_type}.")
711
name = None
@@ -72,3 +76,5 @@ def inner(*args, **kwargs):
7276
obj = func(*args, **kwargs)
7377
return obj
7478
return inner
79+
80+

‎paths_cli/wizard/cvs.py‎

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from paths_cli.parsing.tools import custom_eval
33
from paths_cli.wizard.load_from_ops import load_from_ops
44
from paths_cli.wizard.load_from_ops import LABEL as _load_label
5+
from paths_cli.wizard.core import get_object
6+
57
from functools import partial
68
import numpy as np
79

@@ -28,65 +30,67 @@ def mdtraj_atom_helper(wizard, user_input, n_atoms): # no-cov
2830
def _get_topology(wizard):
2931
from paths_cli.wizard.engines import engines
3032
topology = None
31-
# TODO: isn't this get_missing_object?
32-
while topology is None:
33-
if len(wizard.engines) == 0:
34-
# SHOULD NEVER GET HERE
35-
wizard.say("Hey, you need to define an MD engine before you "
36-
"create CVs that refer to it. Let's do that now!")
37-
engine = engines(wizard)
38-
wizard.register(engine, 'engine', 'engines')
39-
wizard.say("Now let's get back to defining your CV")
40-
elif len(wizard.engines) == 1:
41-
topology = list(wizard.engines.values())[0].topology
42-
else:
43-
engines = list(wizard.engines.keys)
44-
name = wizard.ask("You have defined multiple engines. Which one "
45-
"should I use to define your CV?",
46-
options=engines)
47-
topology = wizard.engines[name].topology
33+
# TODO: this is very similar to get_missing_object, but has more
34+
# reporting; is there some way to add the reporting to
35+
# get_missing_object?
36+
if len(wizard.engines) == 0:
37+
# SHOULD NEVER GET HERE IF WIZARDS ARE DESIGNED CORRECTLY
38+
wizard.say("Hey, you need to define an MD engine before you "
39+
"create CVs that refer to it. Let's do that now!")
40+
engine = engines(wizard)
41+
wizard.register(engine, 'engine', 'engines')
42+
wizard.say("Now let's get back to defining your CV.")
43+
topology = engine.topology
44+
elif len(wizard.engines) == 1:
45+
topology = list(wizard.engines.values())[0].topology
46+
else:
47+
wizard.say("You have defined multiple engines, and need to pick "
48+
"one to use to get a the topology for your CV.")
49+
engine = wizard.obj_selector('engines', 'engine', engines)
50+
topology = engine.topology
51+
wizard.say("Now let's get back to defining your CV.")
4852

4953
return topology
5054

55+
@get_object
5156
def _get_atom_indices(wizard, topology, n_atoms, cv_user_str):
52-
arr = None
5357
helper = partial(mdtraj_atom_helper, n_atoms=n_atoms)
54-
while arr is None:
55-
# switch to get_custom_eval
56-
atoms_str = wizard.ask(f"Which atoms do you want to {cv_user_str}?",
57-
helper=helper)
58-
try:
59-
indices = custom_eval(atoms_str)
60-
except Exception as e:
61-
wizard.exception(f"Sorry, I did't understand '{atoms_str}'.", e)
62-
mdtraj_atom_helper(wizard, '?', n_atoms)
63-
continue
64-
65-
try:
66-
# move this logic to parsing.tools
67-
arr = np.array(indices)
68-
if arr.dtype != int:
69-
raise TypeError("Input is not integers")
70-
if arr.shape != (1, n_atoms):
71-
# try to clean it up
72-
if len(arr.shape) == 1 and arr.shape[0] == n_atoms:
73-
arr.shape = (1, n_atoms)
74-
else:
75-
raise TypeError(f"Invalid input. Requires {n_atoms} "
76-
"atoms.")
77-
except Exception as e:
78-
wizard.exception(f"Sorry, I did't understand '{atoms_str}'", e)
79-
mdtraj_atom_helper(wizard, '?', n_atoms)
80-
arr = None
81-
continue
58+
# switch to get_custom_eval
59+
atoms_str = wizard.ask(f"Which atoms do you want to {cv_user_str}?",
60+
helper=helper)
61+
try:
62+
indices = custom_eval(atoms_str)
63+
except Exception as e:
64+
wizard.exception(f"Sorry, I did't understand '{atoms_str}'.", e)
65+
mdtraj_atom_helper(wizard, '?', n_atoms)
66+
return
67+
68+
try:
69+
# move this logic to parsing.tools
70+
arr = np.array(indices)
71+
if arr.dtype != int:
72+
raise TypeError("Input is not integers")
73+
if arr.shape != (1, n_atoms):
74+
# try to clean it up
75+
if len(arr.shape) == 1 and arr.shape[0] == n_atoms:
76+
arr.shape = (1, n_atoms)
77+
else:
78+
raise TypeError(f"Invalid input. Requires {n_atoms} "
79+
"atoms.")
80+
except Exception as e:
81+
wizard.exception(f"Sorry, I did't understand '{atoms_str}'", e)
82+
mdtraj_atom_helper(wizard, '?', n_atoms)
83+
return
8284

8385
return arr
8486

87+
8588
def _mdtraj_function_cv(wizard, cv_does_str, cv_user_prompt, func,
86-
kwarg_name, n_atoms):
89+
kwarg_name, n_atoms, period):
8790
from openpathsampling.experimental.storage.collective_variables import \
8891
MDTrajFunctionCV
8992
wizard.say(f"We'll make a CV that measures the {cv_does_str}.")
93+
period_min, period_max = period
9094
topology = _get_topology(wizard)
9195
indices = _get_atom_indices(wizard, topology, n_atoms=n_atoms,
9296
cv_user_str=cv_user_prompt)
@@ -99,7 +103,8 @@ def _mdtraj_function_cv(wizard, cv_does_str, cv_user_prompt, func,
99103
" Topology: " + repr(topology.mdtraj))
100104
wizard.say(summary)
101105

102-
return MDTrajFunctionCV(func, topology, **kwargs)
106+
return MDTrajFunctionCV(func, topology, period_min=period_min,
107+
period_max=period_max, **kwargs)
103108

104109
def distance(wizard):
105110
return _mdtraj_function_cv(
@@ -108,7 +113,8 @@ def distance(wizard):
108113
cv_user_prompt="measure the distance between",
109114
func=md.compute_distances,
110115
kwarg_name='atom_pairs',
111-
n_atoms=2
116+
n_atoms=2,
117+
period=(None, None)
112118
)
113119

114120
def angle(wizard):
@@ -118,7 +124,8 @@ def angle(wizard):
118124
cv_user_prompt="use to define the angle",
119125
func=md.compute_angles,
120126
kwarg_name='angle_indices',
121-
n_atoms=3
127+
n_atoms=3,
128+
period=(-np.pi, np.pi)
122129
)
123130

124131
def dihedral(wizard):
@@ -128,13 +135,15 @@ def dihedral(wizard):
128135
cv_user_prompt="use to define the dihedral angle",
129136
func=md.compute_dihedrals,
130137
kwarg_name='indices',
131-
n_atoms=4
138+
n_atoms=4,
139+
period=(-np.pi, np.pi)
132140
)
133141

134142
def rmsd(wizard):
135143
raise NotImplementedError("RMSD has not yet been implemented")
136144

137145
def coordinate(wizard):
146+
# TODO: atom_index should be from wizard.ask_custom_eval
138147
atom_index = coord = None
139148
while atom_index is None:
140149
idx = wizard.ask("For which atom do you want to get the "
@@ -153,6 +162,27 @@ def coordinate(wizard):
153162
except KeyError as e:
154163
wizard.bad_input("Please select one of 'x', 'y', or 'z'")
155164

165+
def _get_period(wizard):
166+
# to be used in custom CVs ... or maybe just in the file CV itself?
167+
is_periodic = period_min = period_max = None
168+
while is_periodic is None:
169+
is_periodic_char = wizard.ask("Is this CV periodic?",
170+
options=["[Y]es", "[N]o"])
171+
is_periodic = {'y': True, 'n': False}[is_periodic_char]
172+
173+
if is_periodic:
174+
while period_min is None:
175+
period_min = wizard.ask_custom_eval(
176+
"What is the lower bound of the period?"
177+
)
178+
while period_max is None:
179+
period_max = wizard.ask_custom_eval(
180+
"What is the upper bound of the period?"
181+
)
182+
return period_min, period_max
183+
184+
185+
156186
SUPPORTED_CVS = {}
157187

158188
if HAS_MDTRAJ:

‎paths_cli/wizard/volumes.py‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,7 @@ def cv_defined_volume(wizard):
5050
wizard.say("A CV-defined volume allows an interval in a CV.")
5151
cv = wizard.obj_selector('cvs', "CV", cvs)
5252
period_min = period_max = lambda_min = lambda_max = None
53-
is_periodic = None
54-
while is_periodic is None:
55-
is_periodic_char = wizard.ask("Is this CV periodic?",
56-
options=["[Y]es", "[N]o"])
57-
is_periodic = {'y': True, 'n': False}[is_periodic_char]
58-
59-
if is_periodic:
60-
while period_min is None:
61-
period_min = wizard.ask_custom_eval(
62-
"What is the lower bound of the period?"
63-
)
64-
while period_max is None:
65-
period_max = wizard.ask_custom_eval(
66-
"What is the upper bound of the period?"
67-
)
68-
53+
is_periodic = cv.is_periodic
6954
volume_bound_str = ("What is the {bound} allowed value for "
7055
f"'{cv.name}' in this volume?")
7156

@@ -82,7 +67,7 @@ def cv_defined_volume(wizard):
8267
if is_periodic:
8368
vol = paths.PeriodicCVDefinedVolume(
8469
cv, lambda_min=lambda_min, lambda_max=lambda_max,
85-
period_min=period_min, period_max=period_max
70+
period_min=cv.period_min, period_max=cv.period_max
8671
)
8772
else:
8873
vol = paths.CVDefinedVolume(

0 commit comments

Comments
 (0)