-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: add the
aiida-pseudo family cutoffs set
command
This command allows to set recommended cutoffs for a given family. The cutoffs are specified in a JSON file that is given as an argument and the stringency is specified through the `-s/--stringency` option. If the stringency already exists, it is simply overwritten.
- Loading branch information
Showing
6 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Commands to inspect or modify the contents of pseudo potential families.""" | ||
import json | ||
|
||
import click | ||
|
||
from aiida.cmdline.utils import decorators, echo | ||
|
||
from ..groups.mixins import RecommendedCutoffMixin | ||
from .params import arguments, options | ||
from .root import cmd_root | ||
|
||
|
||
@cmd_root.group('family') | ||
def cmd_family(): | ||
"""Command group to inspect or modify the contents of pseudo potential families.""" | ||
|
||
|
||
@cmd_family.group('cutoffs') | ||
def cmd_family_cutoffs(): | ||
"""Command group to inspect or modify the recommended cutoffs of pseudo potential families.""" | ||
|
||
|
||
@cmd_family_cutoffs.command('set') | ||
@arguments.PSEUDO_POTENTIAL_FAMILY() | ||
@click.argument('cutoffs', type=click.File(mode='rb')) | ||
@options.STRINGENCY(required=True) | ||
@decorators.with_dbenv() | ||
def cmd_family_cutoffs_set(family, cutoffs, stringency): # noqa: D301 | ||
"""Set the recommended cutoffs for a pseudo potential family. | ||
The cutoffs should be provided as a JSON file through the argument `CUTOFFS` which should have the structure: | ||
\b | ||
{ | ||
"Ag": { | ||
"cutoff_wfc": 50.0, | ||
"cutoff_rho": 200.0 | ||
}, | ||
... | ||
} | ||
where the cutoffs are expected to be in electronvolt. | ||
""" | ||
if not isinstance(family, RecommendedCutoffMixin): | ||
raise click.BadParameter(f'family `{family}` does not support recommended cutoffs to be set.') | ||
|
||
try: | ||
data = json.load(cutoffs) | ||
except ValueError as exception: | ||
raise click.BadParameter(f'`{cutoffs.name}` contains invalid JSON: {exception}', param_hint='CUTOFFS') | ||
|
||
try: | ||
family.set_cutoffs({stringency: data}) | ||
except ValueError as exception: | ||
raise click.BadParameter(f'`{cutoffs.name}` contains invalid cutoffs: {exception}', param_hint='CUTOFFS') | ||
|
||
echo.echo_success(f'set cutoffs for `{family}` with the stringency `{stringency}`.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Reusable arguments for CLI commands.""" | ||
from aiida.cmdline.params.arguments import OverridableArgument | ||
from .types import PseudoPotentialFamilyParam | ||
|
||
__all__ = ('PSEUDO_POTENTIAL_FAMILY') | ||
|
||
PSEUDO_POTENTIAL_FAMILY = OverridableArgument( | ||
'family', type=PseudoPotentialFamilyParam(sub_classes=('aiida.groups:pseudo.family',)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding: utf-8 -*- | ||
# pylint: disable=unused-argument | ||
"""Tests for the command `aiida-pseudo show`.""" | ||
import json | ||
import pytest | ||
|
||
from aiida_pseudo.cli.family import cmd_family_cutoffs_set | ||
from aiida_pseudo.groups.family import CutoffsFamily | ||
|
||
|
||
@pytest.mark.usefixtures('clear_db') | ||
def test_family_cutoffs_set(run_cli_command, get_pseudo_family, tmp_path): | ||
"""Test the `aiida-pseudo family cutoffs set` command.""" | ||
family = get_pseudo_family(cls=CutoffsFamily) | ||
cutoffs = { | ||
'normal': {}, | ||
'high': {}, | ||
} | ||
|
||
for element in family.elements: | ||
cutoffs['normal'][element] = {'cutoff_wfc': 1.0, 'cutoff_rho': 2.0} | ||
cutoffs['high'][element] = {'cutoff_wfc': 3.0, 'cutoff_rho': 6.0} | ||
|
||
# Set only the normal cutoffs for the family | ||
family.set_cutoffs({'normal': cutoffs['normal']}, 'normal') | ||
|
||
filepath = tmp_path / 'cutoffs.json' | ||
|
||
# Invalid JSON | ||
filepath.write_text('invalid content') | ||
result = run_cli_command(cmd_family_cutoffs_set, [family.label, str(filepath)], raises=True) | ||
assert "Error: Missing option '-s' / '--stringency'" in result.output | ||
|
||
# Invalid cutoffs structure | ||
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 'Error: Invalid value for CUTOFFS:' in result.output | ||
|
||
# Set correct stringency | ||
stringency = 'high' | ||
filepath.write_text(json.dumps(cutoffs['high'])) | ||
result = run_cli_command(cmd_family_cutoffs_set, [family.label, str(filepath), '-s', stringency]) | ||
assert 'Success: set cutoffs for' in result.output | ||
assert stringency in family.get_cutoff_stringencies() | ||
assert family.get_cutoffs(stringency) == cutoffs[stringency] |