-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI: add the aiida-pseudo family cutoffs set
command
#55
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for making the cutoffs an argument and the stringency a required option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, in the current conception. I think originally I intended to allow to specify multiple stringencies at once and then let one define the default. But indeed, now that only on stringency is defined at a time, maybe it makes more sense to specify it as an argument. On the other hand, leaving it like this does make it possible in the future to make the command more flexible and also allow a file with multiple stringencies defined, where the keys are the stringency name. Then it makes more sense to make the default stringency parameter an option and use the first one defined in the file as the default such that the flag becomes optional. Think that is the best option actually thinking about it now