Skip to content

Commit

Permalink
add warning for unsupported settings yaml parameters (#1051)
Browse files Browse the repository at this point in the history
* adding warning and test

* be more precise about allowed fields handling

* using more explicit set notation

* add rever file
  • Loading branch information
atravitz authored Dec 13, 2024
1 parent 7eb6025 commit 8374172
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
23 changes: 23 additions & 0 deletions news/settings_yaml_warning.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* CLI setup will raise warnings for unsupported top-level fields.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
20 changes: 11 additions & 9 deletions openfecli/parameters/plan_network_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ def parse_yaml_planner_options(contents: str) -> CliYaml:
"""
raw = yaml.safe_load(contents)

if False:
# todo: warnings about extra fields we don't expect?
expected = {'mapper', 'network'}
for field in raw:
if field in expected:
continue
warnings.warn(f"Ignoring unexpected section: '{field}'")
expected_fields = {'mapper', 'network'}
present_fields = set(raw.keys())
usable_fields = present_fields.intersection(expected_fields)
ignored_fields = present_fields.difference(expected_fields)

return CliYaml(**raw)
for field in ignored_fields:
warnings.warn(f"Ignoring unexpected section: '{field}'")

filtered = {k:raw[k] for k in usable_fields}

return CliYaml(**filtered)


def load_yaml_planner_options(path: Optional[str], context) -> PlanNetworkOptions:
Expand Down Expand Up @@ -185,7 +187,7 @@ def load_yaml_planner_options(path: Optional[str], context) -> PlanNetworkOption
Currently it can contain sections for customising the
atom mapper and network planning algorithm,
these are addressed using a `mapper:` or `network:` key in the yaml file.
The algorithm to be used for these sections is then specified by the `method:` key.
The algorithm to be used for these sections is then specified by the `method:` key.
For choosing mappers, either the LomapAtomMapper or KartografAtomMapper are allowed choices,
while for the network planning algorithm either the generate_minimal_spanning_tree or
generate_minimal_redundant_network options are allowed.
Expand Down
19 changes: 19 additions & 0 deletions openfecli/tests/parameters/test_plan_network_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def partial_network_yaml():
scorer: default_lomap_scorer
"""

@pytest.fixture()
def unsupported_field_yaml():
return """\
protocol:
settings:
forcefield_settings:
small_molecule_forcefield: 'espaloma-0.2.2'
protocol_repeats: 2
"""

def test_loading_full_yaml(full_yaml):
d = plan_network_options.parse_yaml_planner_options(full_yaml)
Expand Down Expand Up @@ -64,3 +73,13 @@ def test_loading_network_yaml(partial_network_yaml):
assert d.network
assert d.network.method == 'generate_radial_network'
assert d.network.settings['scorer'] == 'default_lomap_scorer'

def test_raise_unsupported_fields_warning(full_yaml, unsupported_field_yaml):
with pytest.warns(UserWarning, match='Ignoring unexpected section:'):
d = plan_network_options.parse_yaml_planner_options(full_yaml + unsupported_field_yaml)

assert d.mapper
assert d.mapper.method == 'LomapAtomMapper'.lower()
assert d.mapper.settings['timeout'] == 120
assert d.network
assert d.network.method == 'generate_radial_network'

0 comments on commit 8374172

Please sign in to comment.