Skip to content

Commit d4b3f29

Browse files
committed
remove old parsing from engines, cvs, volumes
1 parent f31b1c9 commit d4b3f29

13 files changed

Lines changed: 87 additions & 71 deletions

File tree

‎paths_cli/parsing/cvs.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,3 @@ def cv_kwargs_remapper(dct):
5858
}
5959

6060
CV_PARSER = ParserPlugin(CVParserPlugin, aliases=['cvs'])
61-
cv_parser = Parser(TYPE_MAPPING, label="CVs")

‎paths_cli/parsing/engines.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,3 @@ def openmm_options(dct):
5858
}
5959

6060
ENGINE_PARSER = ParserPlugin(EngineParserPlugin, aliases=['engines'])
61-
engine_parser = Parser(TYPE_MAPPING, label="engines")

‎paths_cli/parsing/networks.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
InstanceBuilder, Parser, Builder, Parameter
33
)
44
from paths_cli.parsing.tools import custom_eval
5-
from paths_cli.parsing.volumes import volume_parser
6-
from paths_cli.parsing.cvs import cv_parser
75
from paths_cli.parsing.plugins import NetworkParserPlugin, ParserPlugin
6+
from paths_cli.parsing.root_parser import parser_for
87

98
build_interface_set = InstanceBuilder(
109
builder=Builder('openpathsampling.VolumeInterfaceSet'),
1110
parameters=[
12-
Parameter('cv', cv_parser, description="the collective variable "
13-
"for this interface set"),
11+
Parameter('cv', parser_for('cv'), description="the collective "
12+
"variable for this interface set"),
1413
Parameter('minvals', custom_eval), # TODO fill in JSON types
1514
Parameter('maxvals', custom_eval), # TODO fill in JSON types
1615
],
@@ -20,6 +19,7 @@
2019
def mistis_trans_info(dct):
2120
dct = dct.copy()
2221
transitions = dct.pop('transitions')
22+
volume_parser = parser_for('volume')
2323
trans_info = [
2424
(
2525
volume_parser(trans['initial_state']),
@@ -45,9 +45,9 @@ def tis_trans_info(dct):
4545
build_tps_network = NetworkParserPlugin(
4646
builder=Builder('openpathsampling.TPSNetwork'),
4747
parameters=[
48-
Parameter('initial_states', volume_parser,
48+
Parameter('initial_states', parser_for('volume'),
4949
description="initial states for this transition"),
50-
Parameter('final_states', volume_parser,
50+
Parameter('final_states', parser_for('volume'),
5151
description="final states for this transition")
5252
],
5353
name='tps'

‎paths_cli/parsing/root_parser.py‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
from paths_cli.parsing.core import Parser, InstanceBuilder
2-
from paths_cli.parsing.engines import engine_parser
3-
from paths_cli.parsing.cvs import cv_parser
4-
from paths_cli.parsing.volumes import volume_parser
5-
from paths_cli.parsing.networks import network_parser
6-
from paths_cli.parsing.schemes import scheme_parser
72
from paths_cli.parsing.plugins import ParserPlugin
83

94

10-
TYPE_MAPPING = {
11-
'engines': engine_parser,
12-
'cvs': cv_parser,
13-
'volumes': volume_parser,
14-
'states': volume_parser,
15-
'networks': network_parser,
16-
'moveschemes': scheme_parser,
17-
}
18-
195
_DEFAULT_PARSE_ORDER = [
206
'engine',
217
'cv',
@@ -30,6 +16,21 @@
3016
PARSERS = {}
3117
ALIASES = {}
3218

19+
class ParserProxy:
20+
def __init__(self, parser_name):
21+
self.parser_name = parser_name
22+
23+
@property
24+
def _proxy(self):
25+
return PARSERS[self.parser_name]
26+
27+
@property
28+
def named_objs(self):
29+
return self._proxy.named_objs
30+
31+
def __call__(self, *args, **kwargs):
32+
return self._proxy(*args, **kwargs)
33+
3334
def parser_for(parser_name):
3435
"""Delayed parser calling.
3536
@@ -40,9 +41,7 @@ def parser_for(parser_name):
4041
parser_name : str
4142
the name of the parser to use
4243
"""
43-
def load_and_call_parser(*args, **kwargs):
44-
return PARSERS[parser_name](*args, **kwargs)
45-
return load_and_call_parser
44+
return ParserProxy(parser_name)
4645

4746
def _get_parser(parser_name):
4847
"""

‎paths_cli/parsing/schemes.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
)
44
from paths_cli.parsing.tools import custom_eval
55
from paths_cli.parsing.shooting import shooting_selector_parser
6-
from paths_cli.parsing.engines import engine_parser
76
from paths_cli.parsing.networks import network_parser
87
from paths_cli.parsing.strategies import (
98
strategy_parser, SP_SELECTOR_PARAMETER
109
)
1110
from paths_cli.parsing.plugins import SchemeParserPlugin, ParserPlugin
11+
from paths_cli.parsing.root_parser import parser_for
1212

1313

1414
NETWORK_PARAMETER = Parameter('network', network_parser)
1515

16-
ENGINE_PARAMETER = Parameter('engine', engine_parser) # reuse elsewhere?
16+
ENGINE_PARAMETER = Parameter('engine', parser_for('engine')) # reuse elsewhere?
1717

1818
STRATEGIES_PARAMETER = Parameter('strategies', strategy_parser, default=None)
1919

‎paths_cli/parsing/shooting.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from paths_cli.parsing.core import (
22
InstanceBuilder, Parser, Builder, Parameter
33
)
4-
from paths_cli.parsing.cvs import cv_parser
4+
from paths_cli.parsing.root_parser import parser_for
55
from paths_cli.parsing.tools import custom_eval
66
import numpy as np
77

@@ -21,7 +21,7 @@ def remapping_gaussian_stddev(dct):
2121
builder=Builder('openpathsampling.GaussianBiasSelector',
2222
remapper=remapping_gaussian_stddev),
2323
parameters=[
24-
Parameter('cv', cv_parser),
24+
Parameter('cv', parser_for('cv')),
2525
Parameter('mean', custom_eval),
2626
Parameter('stddev', custom_eval),
2727
],

‎paths_cli/parsing/strategies.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Parser, Builder, Parameter
33
)
44
from paths_cli.parsing.shooting import shooting_selector_parser
5-
from paths_cli.parsing.engines import engine_parser
65
from paths_cli.parsing.plugins import StrategyParserPlugin, ParserPlugin
6+
from paths_cli.parsing.root_parser import parser_for
77

88
def _strategy_name(class_name):
99
return f"openpathsampling.strategies.{class_name}"
@@ -15,7 +15,7 @@ def _group_parameter(group_name):
1515
# TODO: maybe this moves into shooting once we have the metadata?
1616
SP_SELECTOR_PARAMETER = Parameter('selector', shooting_selector_parser)
1717

18-
ENGINE_PARAMETER = Parameter('engine', engine_parser,
18+
ENGINE_PARAMETER = Parameter('engine', parser_for('engine'),
1919
description="the engine for moves of this "
2020
"type")
2121

‎paths_cli/parsing/topology.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
from .errors import InputError
3+
from paths_cli.parsing.root_parser import parser_for
34

45
def get_topology_from_engine(dct):
56
"""If given the name of an engine, use that engine's topology"""
6-
from paths_cli.parsing.engines import engine_parser
7+
# from paths_cli.parsing.engines import engine_parser
8+
engine_parser = parser_for('engine')
79
if dct in engine_parser.named_objs:
810
engine = engine_parser.named_objs[dct]
911
try:

‎paths_cli/parsing/volumes.py‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import functools
33

44
from .core import Parser, InstanceBuilder, custom_eval, Parameter
5-
from .cvs import cv_parser
65
from paths_cli.parsing.plugins import VolumeParserPlugin
6+
from paths_cli.parsing.root_parser import parser_for
77

88
# TODO: extra function for volumes should not be necessary as of OPS 2.0
99
def cv_volume_build_func(**dct):
@@ -21,7 +21,7 @@ def cv_volume_build_func(**dct):
2121
build_cv_volume = VolumeParserPlugin(
2222
builder=cv_volume_build_func,
2323
parameters=[
24-
Parameter('cv', cv_parser,
24+
Parameter('cv', parser_for('cv'),
2525
description="CV that defines this volume"),
2626
Parameter('lambda_min', custom_eval,
2727
description="Lower bound for this volume"),
@@ -31,10 +31,6 @@ def cv_volume_build_func(**dct):
3131
name='cv-volume',
3232
)
3333

34-
def _use_parser(dct):
35-
# this is a hack to get around circular definitions
36-
return volume_parser(dct)
37-
3834
# jsonschema type for combination volumes
3935
VOL_ARRAY_TYPE = {
4036
'type': 'array',
@@ -45,7 +41,7 @@ def _use_parser(dct):
4541
builder=lambda subvolumes: functools.reduce(operator.__and__,
4642
subvolumes),
4743
parameters=[
48-
Parameter('subvolumes', _use_parser,
44+
Parameter('subvolumes', parser_for('volume'),
4945
json_type=VOL_ARRAY_TYPE,
5046
description="List of the volumes to intersect")
5147
],
@@ -56,7 +52,7 @@ def _use_parser(dct):
5652
builder=lambda subvolumes: functools.reduce(operator.__or__,
5753
subvolumes),
5854
parameters=[
59-
Parameter('subvolumes', _use_parser,
55+
Parameter('subvolumes', parser_for('volume'),
6056
json_type=VOL_ARRAY_TYPE,
6157
description="List of the volumes to join into a union")
6258
],
@@ -67,6 +63,3 @@ def _use_parser(dct):
6763
'cv-volume': build_cv_volume,
6864
'intersection': build_intersection_volume,
6965
}
70-
71-
volume_parser = Parser(TYPE_MAPPING, label="volumes")
72-

‎paths_cli/tests/parsing/test_networks.py‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from unittest import mock
3+
from paths_cli.tests.parsing.utils import mock_parser
34
import numpy as np
45

56
import yaml
@@ -39,11 +40,13 @@ def test_mistis_trans_info(cv_and_states):
3940
}]
4041
}
4142
patch_base = 'paths_cli.parsing.networks'
42-
43-
with mock.patch.dict(f"{patch_base}.volume_parser.named_objs",
44-
{"A": state_A, "B": state_B}), \
45-
mock.patch.dict(f"{patch_base}.cv_parser.named_objs",
46-
{'cv': cv}):
43+
parser = {
44+
'cv': mock_parser('cv', named_objs={'cv': cv}),
45+
'volume': mock_parser('volume', named_objs={
46+
"A": state_A, "B": state_B
47+
}),
48+
}
49+
with mock.patch.dict('paths_cli.parsing.root_parser.PARSERS', parser):
4750
results = mistis_trans_info(dct)
4851

4952
check_unidirectional_tis(results, state_A, state_B, cv)
@@ -62,11 +65,13 @@ def test_tis_trans_info(cv_and_states):
6265
}
6366
}
6467

65-
patch_base = 'paths_cli.parsing.networks'
66-
with mock.patch.dict(f"{patch_base}.volume_parser.named_objs",
67-
{"A": state_A, "B": state_B}), \
68-
mock.patch.dict(f"{patch_base}.cv_parser.named_objs",
69-
{'cv': cv}):
68+
parser = {
69+
'cv': mock_parser('cv', named_objs={'cv': cv}),
70+
'volume': mock_parser('volume', named_objs={
71+
"A": state_A, "B": state_B
72+
}),
73+
}
74+
with mock.patch.dict('paths_cli.parsing.root_parser.PARSERS', parser):
7075
results = tis_trans_info(dct)
7176

7277
check_unidirectional_tis(results, state_A, state_B, cv)
@@ -77,8 +82,11 @@ def test_build_tps_network(cv_and_states):
7782
_, state_A, state_B = cv_and_states
7883
yml = "\n".join(["initial_states:", " - A", "final_states:", " - B"])
7984
dct = yaml.load(yml, yaml.FullLoader)
80-
patch_loc = 'paths_cli.parsing.networks.volume_parser.named_objs'
81-
with mock.patch.dict(patch_loc, {"A": state_A, "B": state_B}):
85+
parser = {
86+
'volume': mock_parser('volume', named_objs={"A": state_A,
87+
"B": state_B}),
88+
}
89+
with mock.patch.dict('paths_cli.parsing.root_parser.PARSERS', parser):
8290
network = build_tps_network(dct)
8391
assert isinstance(network, paths.TPSNetwork)
8492
assert len(network.initial_states) == len(network.final_states) == 1

0 commit comments

Comments
 (0)