Skip to content

Commit 2cdd6aa

Browse files
authored
Merge pull request #89 from dwhswenson/cv-mode
2 parents 02b3374 + 63034a1 commit 2cdd6aa

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

paths_cli/commands/pathsampling.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from paths_cli import OPSCommandPlugin
55
from paths_cli.parameters import (
6-
INPUT_FILE, OUTPUT_FILE, INIT_CONDS, SCHEME, N_STEPS_MC
6+
INPUT_FILE, OUTPUT_FILE, INIT_CONDS, SCHEME, N_STEPS_MC,
7+
SIMULATION_CV_MODE,
78
)
89

910

@@ -16,9 +17,12 @@
1617
@SCHEME.clicked(required=False)
1718
@INIT_CONDS.clicked(required=False)
1819
@N_STEPS_MC
19-
def pathsampling(input_file, output_file, scheme, init_conds, nsteps):
20+
@SIMULATION_CV_MODE.clicked()
21+
def pathsampling(input_file, output_file, scheme, init_conds, nsteps,
22+
cv_mode):
2023
"""General path sampling, using setup in INPUT_FILE"""
2124
storage = INPUT_FILE.get(input_file)
25+
SIMULATION_CV_MODE(storage, cv_mode)
2226
pathsampling_main(output_storage=OUTPUT_FILE.get(output_file),
2327
scheme=SCHEME.get(storage, scheme),
2428
init_conds=INIT_CONDS.get(storage, init_conds),

paths_cli/parameters.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import click
2+
import warnings
23
from paths_cli.param_core import (
34
Option, Argument, OPSStorageLoadSingle, OPSStorageLoadMultiple,
45
OPSStorageLoadNames, StorageLoader, GetByName, GetByNumber, GetOnly,
@@ -133,3 +134,41 @@ def get(self, storage, names):
133134
help="number of Monte Carlo trials to run")
134135

135136
MULTI_CV = CVS
137+
138+
139+
class CVMode:
140+
"""Class for generating CVMode parameters.
141+
"""
142+
def __init__(self, options, default):
143+
allowed = {"production", "analysis", "no-caching"}
144+
if extras := set(options) - allowed:
145+
raise ValueError(f"Invalid options: {extras}")
146+
if default not in options:
147+
raise ValueError(f"Default '{default}' not in options {options}")
148+
149+
self.default = default
150+
self.param = Option(
151+
"--cv-mode",
152+
type=click.Choice(options),
153+
help=(
154+
"Mode for CVs (only used for SimStore DB files). Default "
155+
f"'{default}'."
156+
),
157+
default=default,
158+
)
159+
160+
def clicked(self):
161+
return self.param.clicked()
162+
163+
def __call__(self, storage, cv_mode):
164+
from openpathsampling.experimental.storage import Storage
165+
if cv_mode != self.default and not isinstance(storage, Storage):
166+
warnings.warn("Not a SimStore file: cv-mode argument unused")
167+
return
168+
169+
for cv in storage.cvs:
170+
# TODO: add logger
171+
# _logger.info(f"Setting '{cv.name}' to mode '{cv_mode}'.")
172+
cv.mode = cv_mode
173+
174+
SIMULATION_CV_MODE = CVMode(["production", "no-caching"], "production")

paths_cli/tests/test_parameters.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,49 @@ def test_APPEND_FILE(ext):
511511
os.remove(filename)
512512
os.rmdir(tempdir)
513513
undo_monkey_patch(stored_functions)
514+
515+
516+
class TestCVMode:
517+
def test_bad_option(self):
518+
with pytest.raises(ValueError, match="Invalid options"):
519+
CVMode(["production", "foo"], "production")
520+
521+
def test_bad_default(self):
522+
with pytest.raises(ValueError, match="not in options"):
523+
CVMode(["production", "no-caching"], "analysis")
524+
525+
def test_call(self, tmp_path):
526+
cv_mode = CVMode(["production", "no-caching"], "no-caching")
527+
from openpathsampling.experimental.storage.collective_variables \
528+
import CollectiveVariable
529+
from openpathsampling.experimental.storage import (
530+
Storage, monkey_patch_all
531+
)
532+
cv = CollectiveVariable(lambda s: s.xyz[0][0]).named('x')
533+
filename = str(tmp_path / "foo.db")
534+
stored_functions = pre_monkey_patch()
535+
monkey_patch_all(paths)
536+
st = Storage(filename, mode='w')
537+
assert cv.mode == "analysis"
538+
st.save(cv)
539+
st.close()
540+
del cv
541+
542+
storage = Storage(filename, mode='r')
543+
cv = storage.cvs['x']
544+
assert cv.mode == "analysis"
545+
cv_mode(storage, "no-caching")
546+
assert cv.mode == "no-caching"
547+
undo_monkey_patch(stored_functions)
548+
549+
def test_call_non_simstore(self, tmp_path):
550+
cv_mode = CVMode(["production", "no-caching"], "no-caching")
551+
filename = str(tmp_path / "foo.nc")
552+
cv = paths.FunctionCV("x", lambda x: x.xyz[0][0])
553+
st = paths.Storage(filename, mode='w')
554+
st.save(cv)
555+
st.close()
556+
557+
storage = paths.Storage(filename, mode='r')
558+
with pytest.warns(UserWarning, match="Not a SimStore"):
559+
cv_mode(storage, "production")

0 commit comments

Comments
 (0)