Skip to content

Commit 6531c49

Browse files
authored
Merge pull request #34 from dwhswenson/multiple-traj-init-conds
Allow multiple uses of --init-conds
2 parents 7149565 + 3229fba commit 6531c49

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

paths_cli/commands/equilibrate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def equilibrate_main(output_storage, scheme, init_conds, multiplier,
4343
extra_steps):
4444
import openpathsampling as paths
4545
init_conds = scheme.initial_conditions_from_trajectories(init_conds)
46+
scheme.assert_initial_conditions(init_conds)
4647
simulation = paths.PathSampling(
4748
storage=output_storage,
4849
move_scheme=scheme,

paths_cli/param_core.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,51 @@ def get(self, storage, name):
273273
raise RuntimeError("Couldn't find %s", name)
274274

275275
return result
276+
277+
278+
class OPSStorageLoadMultiple(OPSStorageLoadSingle):
279+
"""Objects that can guess a single object or have multiple specified.
280+
281+
Parameters
282+
----------
283+
param : :class:`.AbstractParameter`
284+
the Option or Argument wrapping a click decorator
285+
store : Str
286+
the name of the store to search
287+
value_strategies : List[Callable[(:class:`.Storage`, Str), Any]]
288+
The strategies to be used when the CLI provides a value for this
289+
parameter. Each should be a callable taking a storage and the string
290+
input from the CLI, and should return the desired object or None if
291+
it cannot be found.
292+
none_strategies : List[Callable[:class:`openpathsampling.Storage, Any]]
293+
The strategies to be used when the CLI does not provide a value for
294+
this parameter. Each should be a callable taking a storage, and
295+
returning the desired object or None if it cannot be found.
296+
"""
297+
def get(self, storage, names):
298+
"""Load desired objects from storage.
299+
300+
Parameters
301+
----------
302+
storage : openpathsampling.Storage
303+
the input storage to search
304+
names : List[Str] or None
305+
strings from CLI providing the identifier (name or index) for
306+
this object; None if not provided
307+
"""
308+
if names == tuple():
309+
names = None
310+
311+
if names is None or isinstance(names, (str, int)):
312+
listified = True
313+
names = [names]
314+
else:
315+
listified = False
316+
317+
results = [super(OPSStorageLoadMultiple, self).get(storage, name)
318+
for name in names]
319+
320+
if listified:
321+
results = results[0]
322+
323+
return results

paths_cli/parameters.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import click
22
from paths_cli.param_core import (
3-
Option, Argument, OPSStorageLoadSingle, OPSStorageLoadNames,
4-
StorageLoader, GetByName, GetByNumber, GetOnly, GetOnlySnapshot,
5-
GetPredefinedName
3+
Option, Argument, OPSStorageLoadSingle, OPSStorageLoadMultiple,
4+
OPSStorageLoadNames, StorageLoader, GetByName, GetByNumber, GetOnly,
5+
GetOnlySnapshot, GetPredefinedName
66
)
77

88

@@ -18,10 +18,10 @@
1818
store='schemes',
1919
)
2020

21-
INIT_CONDS = OPSStorageLoadSingle(
22-
param=Option('-t', '--init-conds',
21+
INIT_CONDS = OPSStorageLoadMultiple(
22+
param=Option('-t', '--init-conds', multiple=True,
2323
help=("identifier for initial conditions "
24-
+ "(sample set or trajectory)")),
24+
+ "(sample set or trajectory)" + HELP_MULTIPLE)),
2525
store='samplesets',
2626
value_strategies=[GetByName('tags'), GetByNumber('samplesets'),
2727
GetByNumber('trajectories')],
@@ -57,7 +57,6 @@
5757
store='engines'
5858
)
5959

60-
6160
STATES = OPSStorageLoadNames(
6261
param=Option('-s', '--state', type=str, multiple=True,
6362
help='name of state' + HELP_MULTIPLE),

paths_cli/tests/commands/test_pathsampling.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ def test_pathsampling(tps_fixture):
2626

2727
results = runner.invoke(pathsampling, ['setup.nc', '-o', 'foo.nc',
2828
'-n', '1000'])
29-
assert results.exit_code == 0
3029
expected_output = (f"True\n{scheme.__uuid__}\n{init_conds.__uuid__}"
3130
"\n1000\n")
31+
3232
assert results.output == expected_output
33+
assert results.exit_code == 0
3334

3435

3536
def test_pathsampling_main(tps_fixture):

paths_cli/tests/test_parameters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ def test_get_none(self, num_in_file):
234234
obj = INIT_CONDS.get(st, None)
235235
assert obj == stored_things[num_in_file - 1]
236236

237+
def test_get_multiple(self):
238+
filename = self.create_file('number-traj')
239+
storage = paths.Storage(filename, mode='r')
240+
traj0, traj1 = self.PARAMETER.get(storage, (0, 1))
241+
assert traj0 == self.traj
242+
assert traj1 == self.other_traj
243+
244+
237245
class TestINIT_SNAP(ParamInstanceTest):
238246
PARAMETER = INIT_SNAP
239247
def setup(self):

0 commit comments

Comments
 (0)