Skip to content
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

Use numpy.testing.assert_allclose over assert np.allclose #3253

Merged
merged 12 commits into from
Aug 18, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def draw_network(env_graph, pos, ax, sg=None, periodicity_vectors=None):
color=color,
)
else:
ecolor = color if np.allclose(np.array(delta), np.zeros(3)) else periodic_color
ecolor = color if np.allclose(delta, np.zeros(3)) else periodic_color
e = FancyArrowPatch(
n1center,
n2center,
Expand Down
17 changes: 9 additions & 8 deletions pymatgen/analysis/interfaces/coherent_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING, Iterator, Sequence

import numpy as np
from numpy.testing import assert_allclose
from scipy.linalg import polar

from pymatgen.analysis.elasticity.strain import Deformation
Expand Down Expand Up @@ -87,14 +88,14 @@ def _find_matches(self) -> None:
for match in self.zsl_matches:
xform = get_2d_transform(film_vectors, match.film_vectors)
strain, rot = polar(xform)
assert np.allclose(
strain, np.round(strain)
assert_allclose(
strain, np.round(strain), atol=1e-12
), "Film lattice vectors changed during ZSL match, check your ZSL Generator parameters"

xform = get_2d_transform(substrate_vectors, match.substrate_vectors)
strain, rot = polar(xform)
assert np.allclose(
strain, strain.astype(int)
assert_allclose(
strain, strain.astype(int), atol=1e-12
), "Substrate lattice vectors changed during ZSL match, check your ZSL Generator parameters"

def _find_terminations(self):
Expand Down Expand Up @@ -195,10 +196,10 @@ def get_interfaces(
).astype(int)
film_sl_slab = film_slab.copy()
film_sl_slab.make_supercell(super_film_transform)
assert np.allclose(
assert_allclose(
film_sl_slab.lattice.matrix[2], film_slab.lattice.matrix[2]
), "2D transformation affected C-axis for Film transformation"
assert np.allclose(
assert_allclose(
film_sl_slab.lattice.matrix[:2], match.film_sl_vectors
), "Transformation didn't make proper supercell for film"

Expand All @@ -208,10 +209,10 @@ def get_interfaces(
).astype(int)
sub_sl_slab = sub_slab.copy()
sub_sl_slab.make_supercell(super_sub_transform)
assert np.allclose(
assert_allclose(
sub_sl_slab.lattice.matrix[2], sub_slab.lattice.matrix[2]
), "2D transformation affected C-axis for Film transformation"
assert np.allclose(
assert_allclose(
sub_sl_slab.lattice.matrix[:2], match.substrate_sl_vectors
), "Transformation didn't make proper supercell for substrate"

Expand Down
26 changes: 15 additions & 11 deletions pymatgen/core/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,10 @@ def from_weight_dict(cls, weight_dict) -> Composition:
return cls(comp_dict)

def get_el_amt_dict(self) -> dict[str, float]:
"""Returns:
dict[str, float]: element symbol and (unreduced) amount. E.g.
{"Fe": 4.0, "O":6.0} or {"Fe3+": 4.0, "O2-":6.0}.
"""
Returns:
dict[str, float]: element symbol and (unreduced) amount. E.g.
{"Fe": 4.0, "O":6.0} or {"Fe3+": 4.0, "O2-":6.0}.
"""
dic: dict[str, float] = collections.defaultdict(float)
for el, amt in self.items():
Expand All @@ -652,24 +653,27 @@ def as_dict(self) -> dict[str, float]:

@property
def to_reduced_dict(self) -> dict[str, float]:
"""Returns:
dict[str, float]: element symbols mapped to reduced amount e.g. {"Fe": 2.0, "O":3.0}.
"""
Returns:
dict[str, float]: element symbols mapped to reduced amount e.g. {"Fe": 2.0, "O":3.0}.
"""
return self.reduced_composition.as_dict()

@property
def to_weight_dict(self) -> dict[str, float]:
"""Returns:
dict[str, float] with weight fraction of each component {"Ti": 0.90, "V": 0.06, "Al": 0.04}.
"""
Returns:
dict[str, float] with weight fraction of each component {"Ti": 0.90, "V": 0.06, "Al": 0.04}.
"""
return {str(el): self.get_wt_fraction(el) for el in self.elements}

@property
def to_data_dict(self) -> dict:
"""Returns:
A dict with many keys and values relating to Composition/Formula,
including reduced_cell_composition, unit_cell_composition,
reduced_cell_formula, elements and nelements.
"""
Returns:
A dict with many keys and values relating to Composition/Formula,
including reduced_cell_composition, unit_cell_composition,
reduced_cell_formula, elements and nelements.
"""
return {
"reduced_cell_composition": self.reduced_composition,
Expand Down
17 changes: 9 additions & 8 deletions pymatgen/core/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from itertools import chain, combinations, product

import numpy as np
from numpy.testing import assert_allclose
from scipy.cluster.hierarchy import fcluster, linkage
from scipy.spatial.distance import squareform

Expand Down Expand Up @@ -171,8 +172,9 @@ def film(self) -> Structure:
return Structure.from_sites(self.film_sites)

def copy(self):
"""Returns:
Interface: A copy of the Interface.
"""
Returns:
Interface: A copy of the Interface.
"""
return Interface.from_dict(self.as_dict())

Expand All @@ -199,8 +201,7 @@ def get_shifts_based_on_adsorbate_sites(self, tolerance: float = 0.1) -> list[tu
tolerance: tolerance for "uniqueness" for shifts in Cartesian unit
This is usually Angstroms.
"""
substrate = self.substrate
film = self.film
substrate, film = self.substrate, self.film

substrate_surface_sites = np.dot(
list(chain.from_iterable(AdsorbateSiteFinder(substrate).find_adsorption_sites().values())),
Expand Down Expand Up @@ -339,10 +340,10 @@ def from_slabs(
substrate_slab = substrate_slab.get_orthogonal_c_slab()
if isinstance(film_slab, Slab):
film_slab = film_slab.get_orthogonal_c_slab()
assert np.allclose(film_slab.lattice.alpha, 90, 0.1)
assert np.allclose(film_slab.lattice.beta, 90, 0.1)
assert np.allclose(substrate_slab.lattice.alpha, 90, 0.1)
assert np.allclose(substrate_slab.lattice.beta, 90, 0.1)
assert_allclose(film_slab.lattice.alpha, 90, 0.1)
assert_allclose(film_slab.lattice.beta, 90, 0.1)
assert_allclose(substrate_slab.lattice.alpha, 90, 0.1)
assert_allclose(substrate_slab.lattice.beta, 90, 0.1)

# Ensure sub is right-handed
# IE sub has surface facing "up"
Expand Down
10 changes: 6 additions & 4 deletions pymatgen/core/ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ def charge(self) -> float:
return self._charge

def as_dict(self) -> dict[str, float]:
"""Returns:
dict with composition, as well as charge.
"""
Returns:
dict with composition, as well as charge.
"""
d = super().as_dict()
d["charge"] = self.charge
Expand All @@ -214,8 +215,9 @@ def from_dict(cls, d) -> Ion:

@property
def to_reduced_dict(self) -> dict:
"""Returns:
dict with element symbol and reduced amount e.g.,
"""
Returns:
dict with element symbol and reduced amount e.g.,
{"Fe": 2.0, "O":3.0}.
"""
d = self.composition.to_reduced_dict
Expand Down
15 changes: 8 additions & 7 deletions pymatgen/core/molecular_orbitals.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ def __init__(self, formula):
self.band_edges = self.obtain_band_edges()

def max_electronegativity(self):
"""Returns:
The maximum pairwise electronegativity difference.
"""
Returns:
The maximum pairwise electronegativity difference.
"""
maximum = 0
for e1, e2 in combinations(self.elements, 2):
Expand All @@ -66,13 +67,13 @@ def max_electronegativity(self):
return maximum

def aos_as_list(self):
"""Returns:
A list of atomic orbitals, sorted from lowest to highest energy.

The orbitals energies in eV are represented as
"""The orbitals energies in eV are represented as
[['O', '1s', -18.758245], ['O', '2s', -0.871362], ['O', '2p', -0.338381]]
Data is obtained from
https://www.nist.gov/pml/data/atomic-reference-data-electronic-structure-calculations
https://www.nist.gov/pml/data/atomic-reference-data-electronic-structure-calculations.

Returns:
A list of atomic orbitals, sorted from lowest to highest energy.
"""
return sorted(
chain.from_iterable([self.aos[el] * int(self.composition[el]) for el in self.elements]),
Expand Down
12 changes: 7 additions & 5 deletions pymatgen/core/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,18 @@ def X(self) -> float:

@property
def atomic_radius(self) -> FloatWithUnit | None:
"""Returns:
float | None: The atomic radius of the element in Ångstroms. Can be None for
some elements like noble gases.
"""
Returns:
float | None: The atomic radius of the element in Ångstroms. Can be None for
some elements like noble gases.
"""
return self._atomic_radius

@property
def atomic_mass(self) -> FloatWithUnit:
"""Returns:
float: The atomic mass of the element in amu.
"""
Returns:
float: The atomic mass of the element in amu.
"""
return self._atomic_mass

Expand Down
5 changes: 3 additions & 2 deletions pymatgen/core/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ def get_interpolated_value(self, x: float) -> list[float]:
return [get_linear_interpolated_value(self.x, self.y[:, k], x) for k in range(self.ydim[1])]

def copy(self):
"""Returns:
Copy of Spectrum object.
"""
Returns:
Copy of Spectrum object.
"""
return self.__class__(self.x, self.y, *self._args, **self._kwargs)

Expand Down
34 changes: 18 additions & 16 deletions pymatgen/entries/computed_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,9 @@ def __init__(

@property
def uncorrected_energy(self) -> float:
"""Returns:
float: the *uncorrected* energy of the entry.
"""
Returns:
float: the *uncorrected* energy of the entry.
"""
return self._energy

Expand All @@ -338,17 +339,17 @@ def energy(self) -> float:

@property
def uncorrected_energy_per_atom(self) -> float:
"""Returns:
float: the *uncorrected* energy of the entry, normalized by atoms
(units of eV/atom).
"""
Returns:
float: the *uncorrected* energy of the entry, normalized by atoms in eV/atom.
"""
return self.uncorrected_energy / self.composition.num_atoms

@property
def correction(self) -> float:
"""Returns:
float: the total energy correction / adjustment applied to the entry,
in eV.
"""
Returns:
float: the total energy correction / adjustment applied to the entry in eV.
"""
# adds to ufloat(0.0, 0.0) to ensure that no corrections still result in ufloat object
corr = ufloat(0.0, 0.0) + sum(ufloat(ea.value, ea.uncertainty) for ea in self.energy_adjustments)
Expand All @@ -361,16 +362,17 @@ def correction(self, x: float) -> None:

@property
def correction_per_atom(self) -> float:
"""Returns:
float: the total energy correction / adjustment applied to the entry,
normalized by atoms (units of eV/atom).
"""
Returns:
float: the total energy correction / adjustment applied to the entry in eV/atom.
"""
return self.correction / self.composition.num_atoms

@property
def correction_uncertainty(self) -> float:
"""Returns:
float: the uncertainty of the energy adjustments applied to the entry, in eV.
"""
Returns:
float: the uncertainty of the energy adjustments applied to the entry in eV.
"""
# adds to ufloat(0.0, 0.0) to ensure that no corrections still result in ufloat object
unc = ufloat(0.0, 0.0) + sum(
Expand All @@ -385,9 +387,9 @@ def correction_uncertainty(self) -> float:

@property
def correction_uncertainty_per_atom(self) -> float:
"""Returns:
float: the uncertainty of the energy adjustments applied to the entry,
normalized by atoms (units of eV/atom).
"""
Returns:
float: the uncertainty of the energy adjustments applied to the entry in eV/atom.
"""
return self.correction_uncertainty / self.composition.num_atoms

Expand Down
5 changes: 3 additions & 2 deletions pymatgen/entries/entry_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ def discard(self, element):

@property
def chemsys(self) -> set:
"""Returns:
set representing the chemical system, e.g., {"Li", "Fe", "P", "O"}.
"""
Returns:
set representing the chemical system, e.g., {"Li", "Fe", "P", "O"}.
"""
chemsys = set()
for e in self.entries:
Expand Down
Loading