Skip to content

Commit

Permalink
CLI: filter cutoffs from JSON file for family cutoffs set
Browse files Browse the repository at this point in the history
Currently the `family cutoffs set` command requires that the JSON file
that contains the cutoffs _only_ has the cutoff keys specified for each
element. This is because the cutoffs are validated by the
`RecommendedCutoffMixin.validate_cutoffs()` method, which doesn't allow
for extraneous keys.

However, this is causing issues for the workaround we have set
up for installing the SSSP and Pseudo-dojo families manually by first
downloading them, since the `metadata.json` obtained this way contains
other keys as well.

Here we first filter the data loaded from the JSON file for the cutoffs,
before passing the dictionary to the `set_cutoffs` method.
  • Loading branch information
mbercx committed May 8, 2021
1 parent 05f345a commit c4a0d37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion aiida_pseudo/cli/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,15 @@ def cmd_family_cutoffs_set(family, cutoffs, stringency, unit): # noqa: D301
except ValueError as exception:
raise click.BadParameter(f'`{cutoffs.name}` contains invalid JSON: {exception}', param_hint='CUTOFFS')

cutoffs_dict = dict()
for element, values in data.items():
try:
cutoffs_dict[element] = {'cutoff_wfc': values['cutoff_wfc'], 'cutoff_rho': values['cutoff_rho']}
except KeyError as exception:
raise click.BadParameter(f'`{cutoffs.name}` is missing cutoffs for element: {element}') from exception

try:
family.set_cutoffs(data, stringency, unit=unit)
family.set_cutoffs(cutoffs_dict, stringency, unit=unit)
except ValueError as exception:
raise click.BadParameter(f'`{cutoffs.name}` contains invalid cutoffs: {exception}', param_hint='CUTOFFS')

Expand Down
8 changes: 7 additions & 1 deletion tests/cli/test_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ def test_family_cutoffs_set(run_cli_command, get_pseudo_family, generate_cutoffs
assert "Error: Missing option '-s' / '--stringency'" in result.output
assert sorted(family.get_cutoff_stringencies()) == sorted(['low', 'normal'])

# Invalid cutoffs structure
# Missing cutoffs
filepath.write_text(json.dumps({'Ar': {'cutoff_rho': 300}}))
result = run_cli_command(cmd_family_cutoffs_set, [family.label, str(filepath), '-s', 'high'], raises=True)
assert 'is missing cutoffs for element:' in result.output
assert sorted(family.get_cutoff_stringencies()) == sorted(['low', 'normal'])

# Invalid cutoffs structure
filepath.write_text(json.dumps({'Ar': {'cutoff_rho': 300, 'cutoff_wfc': 300, 'GME': 'moon'}}))
result = run_cli_command(cmd_family_cutoffs_set, [family.label, str(filepath), '-s', 'high'], raises=True)
assert 'Error: Invalid value for CUTOFFS:' in result.output
assert sorted(family.get_cutoff_stringencies()) == sorted(['low', 'normal'])

Expand Down

0 comments on commit c4a0d37

Please sign in to comment.