Skip to content

Commit

Permalink
Tidy code (stfc#62)
Browse files Browse the repository at this point in the history
Co-authored-by: ElliottKasoar <[email protected]>
  • Loading branch information
ElliottKasoar and ElliottKasoar authored Mar 11, 2024
1 parent b5f83b5 commit d8d23e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
29 changes: 14 additions & 15 deletions janus_core/single_point.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Prepare and perform single point calculations."""

from collections.abc import Collection
from pathlib import Path
from typing import Any, Optional

Expand Down Expand Up @@ -184,7 +185,7 @@ def _get_stress(self) -> MaybeList[ndarray]:
@staticmethod
def _remove_invalid_props(
struct: Atoms,
properties: Optional[list[str]] = None,
properties: Collection[str] = (),
) -> None:
"""
Remove any invalid properties from calculator results.
Expand All @@ -193,35 +194,33 @@ def _remove_invalid_props(
----------
struct : Atoms
Structure with attached results from calculator.
properties : Optional[list[str]]
Physical properties requested to be calculated. Default is [].
properties : Collection[str]
Physical properties requested to be calculated. Default is ().
"""
properties = properties if properties else []
rm_keys = []

# Find any properties with non-finite values
for prop in struct.calc.results:
if not isfinite(struct.calc.results[prop]).all():
rm_keys.append(prop)
rm_keys = [
prop
for prop in struct.calc.results
if not isfinite(struct.calc.results[prop]).all()
]

# Raise error if property was explicitly requested, otherwise remove
for prop in rm_keys:
if prop in properties:
raise ValueError(
f"'{prop}' contains non-finite values for this structure."
)
struct.calc.results.pop(prop)
del struct.calc.results[prop]

def _clean_results(self, properties: Optional[list[str]] = None) -> None:
def _clean_results(self, properties: Collection[str] = ()) -> None:
"""
Remove results with NaN or inf values from calc.results dictionary.
Parameters
----------
properties : Optional[list[str]]
Physical properties requested to be calculated. Default is [].
properties : Collection[str]
Physical properties requested to be calculated. Default is ().
"""
properties = properties if properties else []

if isinstance(self.struct, list):
for image in self.struct:
self._remove_invalid_props(image, properties)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_singlepoint_calc_kwargs(tmp_path):
def test_singlepoint_default_write():
"""Test default write path."""
results_path = Path(".").absolute() / "NaCl-results.xyz"
assert not Path(results_path).exists()
assert not results_path.exists()

result = runner.invoke(
app,
Expand Down

0 comments on commit d8d23e1

Please sign in to comment.