Skip to content

Commit

Permalink
Fix test_relax_chgnet (#3417)
Browse files Browse the repository at this point in the history
* fix doc string indentations

* fix variable names shadowing python builtins

* del parens around doc string return type

* fix numpy import broken due to secrets.py shadowing randints.secrets

* add TestPeriodicSite.test_str()

* fix test_relax_chgnet

* fix test_relax_chgnet

* test_relax_chgnet loosen tolerance

fix assert np.linalg.norm(preds["forces"]) == approx(1.119554e-5, rel=1e-3)
assert 1.3530994e-05 == 1.119554e-05 ± 1.1e-08

* remove chgnet relaxed volume comparison from test_structure.py
  • Loading branch information
janosh authored Oct 24, 2023
1 parent 9d1dfa9 commit 0ce5091
Show file tree
Hide file tree
Showing 35 changed files with 212 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ venv/
ENV/
env.bak/
venv.bak/
**/secrets*
docs/tokens.py
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
rev: v0.1.0
hooks:
- id: ruff
args: [--fix]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
Expand All @@ -26,7 +26,7 @@ repos:
- id: black

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
rev: v1.6.0
hooks:
- id: mypy

Expand Down
2 changes: 1 addition & 1 deletion docs/fetch_pmg_contributors.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 92 additions & 109 deletions pymatgen/analysis/gb/grain.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pymatgen/analysis/local_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,8 @@ def _extract_nn_info(self, structure: Structure, nns):
nns ([dicts]): Nearest neighbor information for a structure
Returns:
(list of tuples (Site, array, float)): See nn_info
list[tuple[PeriodicSite, np.ndarray, float]]: tuples of the form
(site, image, weight). See nn_info.
"""
# Get the target information
targets = structure.elements if self.targets is None else self.targets
Expand Down Expand Up @@ -1454,7 +1455,7 @@ def get_nn_info(self, structure: Structure, n: int):
n: index of site for which to determine near neighbors.
Returns:
(dict): representing a neighboring site and the type of
dict: representing a neighboring site and the type of
bond present between site n and the neighboring site.
"""
from pymatgen.io.babel import BabelMolAdaptor
Expand Down
10 changes: 5 additions & 5 deletions pymatgen/analysis/surface_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def wulff_from_chempot(
no_clean (bool): Consider stability of doped slabs only.
Returns:
(WulffShape): The WulffShape at u_ref and u_ads.
WulffShape: The WulffShape at u_ref and u_ads.
"""
latt = SpacegroupAnalyzer(self.ucell_entry.structure).get_conventional_standard_structure().lattice

Expand Down Expand Up @@ -628,7 +628,7 @@ def get_surface_equilibrium(self, slab_entries, delu_dict=None):
format: Symbol("delu_el") where el is the name of the element.
Returns:
(array): Array containing a solution to x equations with x
array: Array containing a solution to x equations with x
variables (x-1 chemical potential and 1 surface energy)
"""
# Generate all possible coefficients
Expand Down Expand Up @@ -930,7 +930,7 @@ def chempot_vs_gamma(
no_label (bool): Option to turn off labels.
Returns:
(Plot): Plot of surface energy vs chempot for all entries.
Plot: Plot of surface energy vs chempot for all entries.
"""
if delu_dict is None:
delu_dict = {}
Expand Down Expand Up @@ -1011,7 +1011,7 @@ def monolayer_vs_BE(self, plot_eads=False):
energy multiplied by number of adsorbates) instead.
Returns:
(Plot): Plot of binding energy vs monolayer for all facets.
Plot: Plot of binding energy vs monolayer for all facets.
"""
ax = pretty_plot(width=8, height=7)
for hkl in self.all_slab_entries:
Expand Down Expand Up @@ -1098,7 +1098,7 @@ def BE_vs_clean_SE(
eV/A^2 (False)
Returns:
(Plot): Plot of clean surface energy vs binding energy for
Plot: Plot of clean surface energy vs binding energy for
all facets.
"""
ax = pretty_plot(width=8, height=7)
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/analysis/wulff.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def weighted_surface_energy(self):
def area_fraction_dict(self):
"""
Returns:
(dict): {hkl: area_hkl/total area on wulff}.
dict: {hkl: area_hkl/total area on wulff}.
"""
return {hkl: area / self.surface_area for hkl, area in self.miller_area_dict.items()}

Expand Down
34 changes: 18 additions & 16 deletions pymatgen/command_line/chargemol_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,27 @@ def __init__(
atomic_densities_path = os.getcwd()
self._atomic_densities_path = atomic_densities_path

self._chgcarpath = self._get_filepath(path, "CHGCAR")
self._potcarpath = self._get_filepath(path, "POTCAR")
self._aeccar0path = self._get_filepath(path, "AECCAR0")
self._aeccar2path = self._get_filepath(path, "AECCAR2")
if run_chargemol and not (self._chgcarpath and self._potcarpath and self._aeccar0path and self._aeccar2path):
self._chgcar_path = self._get_filepath(path, "CHGCAR")
self._potcar_path = self._get_filepath(path, "POTCAR")
self._aeccar0_path = self._get_filepath(path, "AECCAR0")
self._aeccar2_path = self._get_filepath(path, "AECCAR2")
if run_chargemol and not (
self._chgcar_path and self._potcar_path and self._aeccar0_path and self._aeccar2_path
):
raise FileNotFoundError("CHGCAR, AECCAR0, AECCAR2, and POTCAR are all needed for Chargemol.")
if self._chgcarpath:
self.chgcar = Chgcar.from_file(self._chgcarpath)
if self._chgcar_path:
self.chgcar = Chgcar.from_file(self._chgcar_path)
self.structure = self.chgcar.structure
self.natoms = self.chgcar.poscar.natoms
else:
self.chgcar = self.structure = self.natoms = None
warnings.warn("No CHGCAR found. Some properties may be unavailable.", UserWarning)
if self._potcarpath:
self.potcar = Potcar.from_file(self._potcarpath)
if self._potcar_path:
self.potcar = Potcar.from_file(self._potcar_path)
else:
warnings.warn("No POTCAR found. Some properties may be unavailable.", UserWarning)
self.aeccar0 = Chgcar.from_file(self._aeccar0path) if self._aeccar0path else None
self.aeccar2 = Chgcar.from_file(self._aeccar2path) if self._aeccar2path else None
self.aeccar0 = Chgcar.from_file(self._aeccar0_path) if self._aeccar0_path else None
self.aeccar2 = Chgcar.from_file(self._aeccar2_path) if self._aeccar2_path else None

if run_chargemol:
self._execute_chargemol()
Expand All @@ -141,7 +143,7 @@ def _get_filepath(path, filename, suffix=""):
suffix (str): Optional suffix at the end of the filename.
Returns:
(str): Absolute path to the file.
str: Absolute path to the file.
"""
name_pattern = f"{filename}{suffix}*" if filename != "POTCAR" else f"{filename}*"
paths = glob(os.path.join(path, name_pattern))
Expand Down Expand Up @@ -169,10 +171,10 @@ def _execute_chargemol(self, **job_control_kwargs):
"""
with ScratchDir("."):
try:
os.symlink(self._chgcarpath, "./CHGCAR")
os.symlink(self._potcarpath, "./POTCAR")
os.symlink(self._aeccar0path, "./AECCAR0")
os.symlink(self._aeccar2path, "./AECCAR2")
os.symlink(self._chgcar_path, "./CHGCAR")
os.symlink(self._potcar_path, "./POTCAR")
os.symlink(self._aeccar0_path, "./AECCAR0")
os.symlink(self._aeccar2_path, "./AECCAR2")
except OSError as exc:
print(f"Error creating symbolic link: {exc}")

Expand Down
7 changes: 6 additions & 1 deletion pymatgen/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import os
import warnings
from importlib.metadata import PackageNotFoundError, version
from typing import Any

from ruamel.yaml import YAML
Expand All @@ -31,7 +32,11 @@
__email__ = "[email protected]"
__maintainer__ = "Shyue Ping Ong, Matthew Horton, Janosh Riebesell"
__maintainer_email__ = "[email protected]"
__version__ = "2023.10.4"
try:
__version__ = version("pymatgen")
except PackageNotFoundError: # pragma: no cover
# package is not installed
pass


SETTINGS_FILE = os.path.join(os.path.expanduser("~"), ".config", ".pmgrc.yaml")
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/core/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ def get_miller_index_from_coords(
verbose (bool, optional): Whether to print warnings.
Returns:
(tuple): The Miller index.
tuple: The Miller index.
"""
if coords_are_cartesian:
coords = [self.get_fractional_coords(c) for c in coords] # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/core/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ def from_str(species_string: str) -> DummySpecies:
m = re.search(r"([A-ZAa-z]*)([0-9.]*)([+\-]*)(.*)", species_string)
if m:
sym = m.group(1)
if m.group(2) == "" and m.group(3) == "":
if m.group(2) == m.group(3) == "":
oxi = 0.0
else:
oxi = 1.0 if m.group(2) == "" else float(m.group(2))
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/core/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def is_ordered(self) -> bool:
occupancy 1.
"""
total_occu = self.species.num_atoms
return total_occu == 1 and len(self.species) == 1
return total_occu == len(self.species) == 1

def __getitem__(self, el):
"""Get the occupancy for element."""
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,7 @@ def get_miller_index_from_site_indexes(self, site_ids, round_dp=4, verbose=True)
verbose (bool, optional): Whether to print warnings.
Returns:
(tuple): The Miller index.
tuple: The Miller index.
"""
return self.lattice.get_miller_index_from_coords(
self.frac_coords[site_ids],
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/core/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def symmetrically_add_atom(self, specie, point, coords_are_cartesian=False):
coords_are_cartesian (bool): Is the point in Cartesian coordinates
Returns:
(Slab): The modified slab
Slab: The modified slab
"""
# For now just use the species of the
# surface atom as the element to add
Expand Down Expand Up @@ -1440,7 +1440,7 @@ def build_slabs(self):
(4) Add any specified sites to both surfaces.
Returns:
(Slab): The reconstructed slab.
Slab: The reconstructed slab.
"""
slabs = self.get_unreconstructed_slabs()
recon_slabs = []
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/core/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def _combine_site_props(
"""
# special cases

if prop1 is None and prop2 is None:
if prop1 is prop2 is None:
return None

if isinstance(prop1, dict) and prop1 == prop2:
Expand Down Expand Up @@ -608,7 +608,7 @@ def _combine_site_props(
@staticmethod
def _combine_frame_props(prop1: list[dict] | None, prop2: list[dict] | None, len1: int, len2: int) -> list | None:
"""Combine frame properties."""
if prop1 is None and prop2 is None:
if prop1 is prop2 is None:
return None
if prop1 is None:
return [None] * len1 + list(prop2) # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"cross_section": {"barn": {"m": 2, 1e-28: 1}, "mbarn": {"m": 2, 1e-31: 1}},
}

ALL_UNITS: dict[str, dict] = {**BASE_UNITS, **DERIVED_UNITS}
ALL_UNITS: dict[str, dict] = BASE_UNITS | DERIVED_UNITS
SUPPORTED_UNIT_NAMES = tuple(i for d in ALL_UNITS.values() for i in d)

# Mapping unit name --> unit type (unit names must be unique).
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/electronic_structure/bandstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def get_kpoint_degeneracy(self, kpoint, cartesian=False, tol: float = 1e-2):
tol (float): tolerance below which coordinates are considered equal.
Returns:
(int or None): degeneracy or None if structure is not available
int | None: degeneracy or None if structure is not available
"""
all_kpts = self.get_sym_eq_kpoints(kpoint, cartesian, tol=tol)
if all_kpts is not None:
Expand Down
7 changes: 3 additions & 4 deletions pymatgen/electronic_structure/boltztrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,10 @@ def check_acc_bzt_bands(sbs_bz, sbs_ref, warn_thr=(0.03, 0.03)):
- "avg_corr": average of correlation coefficient over the 8 bands
- "avg_dist": average of energy distance over the 8 bands
- "nb_list": list of indexes of the 8 compared bands
- "acc_thr": list of two float corresponding to the two warning
thresholds in input
- "acc_thr": list of two float corresponding to the two warning thresholds in input
- "acc_err": list of two bools:
True if the avg_corr > warn_thr[0], and
True if the avg_dist > warn_thr[1]
True if the avg_corr > warn_thr[0], and
True if the avg_dist > warn_thr[1]
See also compare_sym_bands function doc.
"""
if not sbs_ref.is_metal() and not sbs_bz.is_metal():
Expand Down
3 changes: 1 addition & 2 deletions pymatgen/electronic_structure/dos.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ def get_interpolated_gap(self, tol: float = 0.001, abs_tol: bool = False, spin:
Down - finds the gap in the down spin channel.
Returns:
(gap, cbm, vbm):
Tuple of floats in eV corresponding to the gap, cbm and vbm.
(gap, cbm, vbm): Tuple of floats in eV corresponding to the gap, cbm and vbm.
"""
tdos = self.get_densities(spin)
if not abs_tol:
Expand Down
1 change: 0 additions & 1 deletion pymatgen/io/cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,6 @@ def get_structures(
Returns:
list[Structure]: All structures in CIF file.
"""

if not check_occu: # added in https://github.com/materialsproject/pymatgen/pull/2836
warnings.warn("Structures with unphysical site occupancies are not compatible with many pymatgen features.")
if primitive and symmetrized:
Expand Down
12 changes: 6 additions & 6 deletions pymatgen/io/lammps/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,8 @@ def set_charge_atom(self, charges: dict[int, float]) -> None:
Args:
charges: A dictionary with atom indexes as keys and
charges as values, e.g., to set the charge
of the atom with index 3 to -2, use `{3: -2}`.
charges as values, e.g., to set the charge
of the atom with index 3 to -2, use `{3: -2}`.
"""
for iat, q in charges.items():
self.atoms.loc[iat, "q"] = q
Expand All @@ -886,10 +886,10 @@ def set_charge_atom_type(self, charges: dict[str | int, float]) -> None:
Args:
charges: Dict containing the charges for the atom types to set.
The dict should contain atom types as integers or labels and charges.
Example: change the charge of Li atoms to +3:
charges={"Li": 3}
charges={1: 3} if Li atoms are of type 1
The dict should contain atom types as integers or labels and charges.
Example: change the charge of Li atoms to +3:
charges={"Li": 3}
charges={1: 3} if Li atoms are of type 1
"""
for iat, q in charges.items():
if isinstance(iat, str):
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/io/lammps/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class BaseLammpsGenerator(InputGenerator):
calc_type: Human-readable string used to briefly describe the type of computations performed by LAMMPS.
settings: Dictionary containing the values of the parameters to replace in the template.
keep_stages: If True, the string is formatted in a block structure with stage names
and newlines that differentiate commands in the respective stages of the InputFile.
If False, stage names are not printed and all commands appear in a single block.
and newlines that differentiate commands in the respective stages of the InputFile.
If False, stage names are not printed and all commands appear in a single block.
/!\ This InputSet and InputGenerator implementation is based on templates and is not intended to be very flexible.
For instance, pymatgen will not detect whether a given variable should be adapted based on others
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/io/qchem/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(
Ex:
1. For a single-state calculation with two constraints:
cdft=[[
cdft=[[
{"value": 1.0, "coefficients": [1.0], "first_atoms": [1], "last_atoms": [2], "types": [None]},
{"value": 2.0, "coefficients": [1.0, -1.0], "first_atoms": [1, 17], "last_atoms": [3, 19],
"types": ["s"]}
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/io/vasp/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ def set_temperature(self, temperature: float):
self.structure.add_site_property("velocities", velocities.tolist())


with open(f"{module_dir}/incar_parameters.json") as incar_params:
incar_params = json.loads(incar_params.read())
with open(f"{module_dir}/incar_parameters.json") as json_file:
incar_params = json.loads(json_file.read())


class BadIncarWarning(UserWarning):
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/phonon/gruneisen.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
multiplicities: list of multiplicities
structure: The crystal structure (as a pymatgen Structure object) associated with the gruneisen parameters.
lattice: The reciprocal lattice as a pymatgen Lattice object. Pymatgen uses the physics convention of
reciprocal lattice vectors WITH a 2*pi coefficient.
reciprocal lattice vectors WITH a 2*pi coefficient.
"""
self.qpoints = qpoints
self.gruneisen = gruneisen
Expand Down
Loading

0 comments on commit 0ce5091

Please sign in to comment.