22from paths_cli .parsing .tools import custom_eval
33from paths_cli .wizard .load_from_ops import load_from_ops
44from paths_cli .wizard .load_from_ops import LABEL as _load_label
5+ from paths_cli .wizard .core import get_object
6+
57from functools import partial
68import numpy as np
79
@@ -28,65 +30,67 @@ def mdtraj_atom_helper(wizard, user_input, n_atoms): # no-cov
2830def _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
5156def _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+
8588def _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
104109def 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
114120def 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
124131def 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
134142def rmsd (wizard ):
135143 raise NotImplementedError ("RMSD has not yet been implemented" )
136144
137145def 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+
156186SUPPORTED_CVS = {}
157187
158188if HAS_MDTRAJ :
0 commit comments