Skip to content

Commit

Permalink
fix other value error, add type
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Nov 6, 2024
1 parent 5c40d03 commit df54164
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
50 changes: 25 additions & 25 deletions src/pymatgen/apps/borg/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pymatgen.io.vasp.outputs import Dynmat, Oszicar, Vasprun

if TYPE_CHECKING:
from collections.abc import Sequence
from typing import Any, Literal

from typing_extensions import Self
Expand Down Expand Up @@ -208,8 +209,8 @@ class SimpleVaspToComputedEntryDrone(VaspToComputedEntryDrone):
def __init__(self, inc_structure: bool = False) -> None:
"""
Args:
inc_structure (bool): Set to True if you want ComputedStructureEntries to be returned instead of
ComputedEntries. Structure will be parsed from the CONTCAR.
inc_structure (bool): Return ComputedStructureEntries (True) instead of
ComputedEntries (False). Structure will be parsed from the CONTCAR.
"""
self._inc_structure = inc_structure
self._parameters = {"is_hubbard", "hubbards", "potcar_spec", "run_type"}
Expand Down Expand Up @@ -285,13 +286,10 @@ def assimilate(self, path: PathLike) -> ComputedStructureEntry | ComputedEntry |
return ComputedStructureEntry(structure, energy, parameters=param, data=data)
return ComputedEntry(structure.composition, energy, parameters=param, data=data)

except ValueError as exc:
if "not all necessary files are present" in str(exc):
except Exception as exc:
if isinstance(exc, ValueError) and "not all necessary files are present" in str(exc):
raise

# TODO: other value error should be suppressed?

except Exception as exc:
logger.debug(f"error in {path}: {exc}")
return None

Expand Down Expand Up @@ -324,7 +322,13 @@ class GaussianToComputedEntryDrone(AbstractDrone):
Like the GaussianOutput class, this is still in early beta.
"""

def __init__(self, inc_structure=False, parameters=None, data=None, file_extensions=(".log",)):
def __init__(
self,
inc_structure: bool = False,
parameters: list[str] | None = None,
data: list[str] | None = None,
file_extensions: Sequence[str] = (".log",),
) -> None:
"""
Args:
inc_structure (bool): Set to True if you want
Expand Down Expand Up @@ -365,7 +369,7 @@ def __init__(self, inc_structure=False, parameters=None, data=None, file_extensi
def __str__(self) -> Literal["GaussianToComputedEntryDrone"]:
return "GaussianToComputedEntryDrone"

def assimilate(self, path):
def assimilate(self, path: PathLike) -> ComputedStructureEntry | ComputedEntry | None:
"""Assimilate data in a directory path into a ComputedEntry object.
Args:
Expand All @@ -379,29 +383,25 @@ def assimilate(self, path):
except Exception as exc:
logger.debug(f"error in {path}: {exc}")
return None
param = {}
for p in self._parameters:
param[p] = getattr(gau_run, p)
data = {}
for d in self._data:
data[d] = getattr(gau_run, d)

param = {p: getattr(gau_run, p) for p in self._parameters}
data = {d: getattr(gau_run, d) for d in self._data}

if self._inc_structure:
entry = ComputedStructureEntry(
return ComputedStructureEntry(
gau_run.final_structure,
gau_run.final_energy,
parameters=param,
data=data,
)
else:
entry = ComputedEntry(
gau_run.final_structure.composition,
gau_run.final_energy,
parameters=param,
data=data,
)
return entry
return ComputedEntry(
gau_run.final_structure.composition,
gau_run.final_energy,
parameters=param,
data=data,
)

def get_valid_paths(self, path):
def get_valid_paths(self, path: tuple[str, str, str]) -> list[str]:
"""Check if path contains files with define extensions.
Args:
Expand Down
8 changes: 5 additions & 3 deletions src/pymatgen/io/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

from typing_extensions import Self

from pymatgen.util.typing import PathLike

__author__ = "Shyue Ping Ong, Germain Salvato-Vallverdu, Xin Chen"
__copyright__ = "Copyright 2013, The Materials Virtual Lab"
__version__ = "0.1"
Expand Down Expand Up @@ -576,13 +578,13 @@ class GaussianOutput:
Save a matplotlib plot of the potential energy surface to a file
"""

def __init__(self, filename):
def __init__(self, filename: PathLike) -> None:
"""
Args:
filename: Filename of Gaussian output file.
"""
self.filename = filename
self._parse(filename)
self.filename = str(filename)
self._parse(self.filename)

@property
def final_energy(self):
Expand Down

0 comments on commit df54164

Please sign in to comment.