diff --git a/pymatgen/analysis/graphs.py b/pymatgen/analysis/graphs.py index b92194699ca..2460a06c9c2 100644 --- a/pymatgen/analysis/graphs.py +++ b/pymatgen/analysis/graphs.py @@ -1086,13 +1086,13 @@ def __mul__(self, scaling_matrix): raise NotImplementedError("Not tested with 3x3 scaling matrices yet.") new_lattice = Lattice(np.dot(scale_matrix, self.structure.lattice.matrix)) - f_lat = lattice_points_in_supercell(scale_matrix) - c_lat = new_lattice.get_cartesian_coords(f_lat) + frac_lattice = lattice_points_in_supercell(scale_matrix) + cart_lattice = new_lattice.get_cartesian_coords(frac_lattice) new_sites = [] new_graphs = [] - for v in c_lat: + for v in cart_lattice: # create a map of nodes from original graph to its image mapping = {n: n + len(new_sites) for n in range(len(self.structure))} diff --git a/pymatgen/command_line/critic2_caller.py b/pymatgen/command_line/critic2_caller.py index 561e7fc6e05..b9dd7fa289c 100644 --- a/pymatgen/command_line/critic2_caller.py +++ b/pymatgen/command_line/critic2_caller.py @@ -353,7 +353,7 @@ def __init__( coords=None, field_hessian=None, ): - """Class to characterise a critical point from a topological + """Class to characterize a critical point from a topological analysis of electron charge density. Note this class is usually associated with a Structure, so @@ -388,7 +388,7 @@ def __str__(self): return f"Critical Point: {self.type.name} ({self.frac_coords})" @property - def laplacian(self): + def laplacian(self) -> float: """Returns: The Laplacian of the field at the critical point.""" return np.trace(self.field_hessian) @@ -408,7 +408,15 @@ def ellipticity(self): class Critic2Analysis(MSONable): """Class to process the standard output from critic2 into pymatgen-compatible objects.""" - def __init__(self, structure: Structure, stdout=None, stderr=None, cpreport=None, yt=None, zpsp=None): + def __init__( + self, + structure: Structure, + stdout: str | None = None, + stderr: str | None = None, + cpreport: dict | None = None, + yt: dict | None = None, + zpsp: dict | None = None, + ) -> None: """This class is used to store results from the Critic2Caller. To explore the bond graph, use the "structure_graph" @@ -443,6 +451,18 @@ class with bonding information. By default, this returns :param zpsp (dict): Dict of element/symbol name to number of electrons (ZVAL in VASP pseudopotential), with which to calculate charge transfer. Optional. + + Args: + structure (Structure): Associated Structure. + stdout (str, optional): stdout from running critic2 in automatic mode. + stderr (str, optional): stderr from running critic2 in automatic mode. + cpreport (dict, optional): JSON output from CPREPORT command. Either this or stdout required. + yt (dict, optional): JSON output from YT command. + zpsp (dict, optional): Dict of element/symbol name to number of electrons (ZVAL in VASP pseudopotential), + with which to calculate charge transfer. Optional. + + Raises: + ValueError: If one of cpreport or stdout is not provided. """ self.structure = structure @@ -471,10 +491,8 @@ def structure_graph(self, include_critical_points=("bond", "ring", "cage")): """A StructureGraph object describing bonding information in the crystal. Args: - include_critical_points: add DummySpecies for - the critical points themselves, a list of - "nucleus", "bond", "ring", "cage", set to None - to disable + include_critical_points: add DummySpecies for the critical points themselves, a list of + "nucleus", "bond", "ring", "cage", set to None to disable Returns: StructureGraph diff --git a/pymatgen/command_line/gulp_caller.py b/pymatgen/command_line/gulp_caller.py index 17fb7f6cdd6..d0718b24c5e 100644 --- a/pymatgen/command_line/gulp_caller.py +++ b/pymatgen/command_line/gulp_caller.py @@ -23,6 +23,8 @@ __status__ = "Production" __date__ = "Jun 22, 2013M" +module_dir = os.path.dirname(os.path.abspath(__file__)) + _anions = set(map(Element, ["O", "S", "F", "Cl", "Br", "N", "P"])) _cations = set( map( @@ -861,7 +863,6 @@ class TersoffPotential: def __init__(self): """Init TersoffPotential.""" - module_dir = os.path.dirname(os.path.abspath(__file__)) with open(f"{module_dir}/OxideTersoffPotentials") as f: data = {} for row in f: diff --git a/pymatgen/core/composition.py b/pymatgen/core/composition.py index f45b6a22717..6e872e5be26 100644 --- a/pymatgen/core/composition.py +++ b/pymatgen/core/composition.py @@ -25,6 +25,7 @@ from collections.abc import Generator, Iterator SpeciesLike = Union[str, Element, Species, DummySpecies] +module_dir = os.path.dirname(os.path.abspath(__file__)) @total_ordering @@ -928,7 +929,6 @@ def _get_oxi_state_guesses(self, all_oxi_states, max_sites, oxi_states_override, # Load prior probabilities of oxidation states, used to rank solutions if not Composition.oxi_prob: - module_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) all_data = loadfn(f"{module_dir}/../analysis/icsd_bv.yaml") Composition.oxi_prob = {Species.from_str(sp): data for sp, data in all_data["occurrence"].items()} oxi_states_override = oxi_states_override or {} diff --git a/pymatgen/core/structure.py b/pymatgen/core/structure.py index 4f6dd1938ca..3efa3902025 100644 --- a/pymatgen/core/structure.py +++ b/pymatgen/core/structure.py @@ -1357,12 +1357,12 @@ def __mul__(self, scaling_matrix: int | Sequence[int] | Sequence[Sequence[int]]) scale_matrix = scale_matrix * np.eye(3) new_lattice = Lattice(np.dot(scale_matrix, self.lattice.matrix)) - f_lat = lattice_points_in_supercell(scale_matrix) - c_lat = new_lattice.get_cartesian_coords(f_lat) + frac_lattice = lattice_points_in_supercell(scale_matrix) + cart_lattice = new_lattice.get_cartesian_coords(frac_lattice) new_sites = [] for site in self: - for vec in c_lat: + for vec in cart_lattice: periodic_site = PeriodicSite( site.species, site.coords + vec, @@ -2228,11 +2228,13 @@ def interpolate( for x in images: if interpolate_lattices: l_a = np.dot(np.identity(3) + x * lvec, lstart).T - lat = Lattice(l_a) + lattice = Lattice(l_a) else: - lat = self.lattice + lattice = self.lattice fcoords = start_coords + x * vec - structs.append(self.__class__(lat, sp, fcoords, site_properties=self.site_properties, labels=self.labels)) + structs.append( + self.__class__(lattice, sp, fcoords, site_properties=self.site_properties, labels=self.labels) + ) return structs def get_miller_index_from_site_indexes(self, site_ids, round_dp=4, verbose=True): diff --git a/pymatgen/io/vasp/inputs.py b/pymatgen/io/vasp/inputs.py index 85c24f2518b..b9e92646457 100644 --- a/pymatgen/io/vasp/inputs.py +++ b/pymatgen/io/vasp/inputs.py @@ -1202,16 +1202,16 @@ def automatic_density(structure: Structure, kppa: float, force_gamma: bool = Fal comment = f"pymatgen with grid density = {kppa:.0f} / number of atoms" if math.fabs((math.floor(kppa ** (1 / 3) + 0.5)) ** 3 - kppa) < 1: kppa += kppa * 0.01 - latt = structure.lattice - lengths = latt.abc + lattice = structure.lattice + lengths = lattice.abc ngrid = kppa / len(structure) mult = (ngrid * lengths[0] * lengths[1] * lengths[2]) ** (1 / 3) num_div = [int(math.floor(max(mult / length, 1))) for length in lengths] - is_hexagonal = latt.is_hexagonal() + is_hexagonal = lattice.is_hexagonal() is_face_centered = structure.get_space_group_info()[0][0] == "F" - has_odd = any(i % 2 == 1 for i in num_div) + has_odd = any(idx % 2 == 1 for idx in num_div) if has_odd or is_hexagonal or is_face_centered or force_gamma: style = Kpoints.supported_modes.Gamma else: diff --git a/pymatgen/io/xr.py b/pymatgen/io/xr.py index 3e0402ff872..3da07100c8e 100644 --- a/pymatgen/io/xr.py +++ b/pymatgen/io/xr.py @@ -81,11 +81,11 @@ def from_str(cls, string, use_cores=True, thresh=1.0e-4): Args: string (str): string representation of an Xr object. use_cores (bool): use core positions and discard shell - positions if set to True (default). Otherwise, - use shell positions and discard core positions. + positions if set to True (default). Otherwise, + use shell positions and discard core positions. thresh (float): relative threshold for consistency check - between cell parameters (lengths and angles) from - header information and cell vectors, respectively. + between cell parameters (lengths and angles) from + header information and cell vectors, respectively. Returns: xr (Xr): Xr object corresponding to the input @@ -106,18 +106,18 @@ def from_str(cls, string, use_cores=True, thresh=1.0e-4): if item != tokens_2[j]: raise RuntimeError("expected both matrices to be the same in xr file") mat[i] = np.array([float(w) for w in tokens]) - lat = Lattice(mat) + lattice = Lattice(mat) if ( - fabs(lat.a - lengths[0]) / fabs(lat.a) > thresh - or fabs(lat.b - lengths[1]) / fabs(lat.b) > thresh - or fabs(lat.c - lengths[2]) / fabs(lat.c) > thresh - or fabs(lat.alpha - angles[0]) / fabs(lat.alpha) > thresh - or fabs(lat.beta - angles[1]) / fabs(lat.beta) > thresh - or fabs(lat.gamma - angles[2]) / fabs(lat.gamma) > thresh + fabs(lattice.a - lengths[0]) / fabs(lattice.a) > thresh + or fabs(lattice.b - lengths[1]) / fabs(lattice.b) > thresh + or fabs(lattice.c - lengths[2]) / fabs(lattice.c) > thresh + or fabs(lattice.alpha - angles[0]) / fabs(lattice.alpha) > thresh + or fabs(lattice.beta - angles[1]) / fabs(lattice.beta) > thresh + or fabs(lattice.gamma - angles[2]) / fabs(lattice.gamma) > thresh ): raise RuntimeError( f"cell parameters in header ({lengths}, {angles}) are not consistent with Cartesian " - f"lattice vectors ({lat.abc}, {lat.angles})" + f"lattice vectors ({lattice.abc}, {lattice.angles})" ) # Ignore line w/ index 3. sp = [] @@ -138,7 +138,7 @@ def from_str(cls, string, use_cores=True, thresh=1.0e-4): else: sp.append(tmp_sp) coords.append([float(m.group(i)) for i in range(2, 5)]) - return cls(Structure(lat, sp, coords, coords_are_cartesian=True)) + return cls(Structure(lattice, sp, coords, coords_are_cartesian=True)) @classmethod def from_file(cls, filename, use_cores=True, thresh=1.0e-4): diff --git a/tests/analysis/chemenv/connectivity/test_connected_components.py b/tests/analysis/chemenv/connectivity/test_connected_components.py index 3861e920721..71fc9923675 100644 --- a/tests/analysis/chemenv/connectivity/test_connected_components.py +++ b/tests/analysis/chemenv/connectivity/test_connected_components.py @@ -83,19 +83,19 @@ def test_init(self): assert len(cc2.graph) == 6 def test_serialization(self): - lat = Lattice.hexagonal(a=2.0, c=2.5) + lattice = Lattice.hexagonal(a=2.0, c=2.5) en1 = EnvironmentNode( - central_site=PeriodicSite("Si", coords=np.array([0.0, 0.0, 0.0]), lattice=lat), + central_site=PeriodicSite("Si", coords=np.array([0.0, 0.0, 0.0]), lattice=lattice), i_central_site=3, ce_symbol="T:4", ) en2 = EnvironmentNode( - central_site=PeriodicSite("Ag", coords=np.array([0.0, 0.0, 0.5]), lattice=lat), + central_site=PeriodicSite("Ag", coords=np.array([0.0, 0.0, 0.5]), lattice=lattice), i_central_site=5, ce_symbol="T:4", ) en3 = EnvironmentNode( - central_site=PeriodicSite("Ag", coords=np.array([0.0, 0.5, 0.5]), lattice=lat), + central_site=PeriodicSite("Ag", coords=np.array([0.0, 0.5, 0.5]), lattice=lattice), i_central_site=8, ce_symbol="O:6", ) diff --git a/tests/analysis/chemenv/coordination_environments/test_read_write.py b/tests/analysis/chemenv/coordination_environments/test_read_write.py index 8d572867012..50c5b1749ef 100644 --- a/tests/analysis/chemenv/coordination_environments/test_read_write.py +++ b/tests/analysis/chemenv/coordination_environments/test_read_write.py @@ -2,7 +2,7 @@ import json -from numpy.testing import assert_allclose, assert_array_almost_equal +from numpy.testing import assert_allclose from pytest import approx from pymatgen.analysis.chemenv.coordination_environments.chemenv_strategies import ( @@ -103,8 +103,8 @@ def test_structure_environments_neighbors_sets(self): neighb_coords = nb_set.coords - assert_array_almost_equal(coords, neighb_coords[1:]) - assert_array_almost_equal(nb_set.structure[nb_set.isite].coords, neighb_coords[0]) + assert_allclose(coords, neighb_coords[1:], atol=1e-6) + assert_allclose(nb_set.structure[nb_set.isite].coords, neighb_coords[0]) norm_dist = nb_set.normalized_distances assert sorted(norm_dist) == approx(sorted([1.001792, 1.001792, 1, 1])) diff --git a/tests/analysis/chemenv/coordination_environments/test_structure_environments.py b/tests/analysis/chemenv/coordination_environments/test_structure_environments.py index ee06ae7486e..b5d06649ed8 100644 --- a/tests/analysis/chemenv/coordination_environments/test_structure_environments.py +++ b/tests/analysis/chemenv/coordination_environments/test_structure_environments.py @@ -4,7 +4,7 @@ import os import numpy as np -from numpy.testing import assert_allclose, assert_array_almost_equal +from numpy.testing import assert_allclose from pytest import approx from pymatgen.analysis.chemenv.coordination_environments.chemenv_strategies import ( @@ -32,24 +32,24 @@ def test_structure_environments(self): struct_envs = StructureEnvironments.from_dict(dd) isite = 6 _csm_and_maps_fig, csm_and_maps_ax = struct_envs.get_csm_and_maps(isite=isite) - assert_array_almost_equal(csm_and_maps_ax.lines[0].get_xydata().flatten(), [0, 0.53499332]) - assert_array_almost_equal(csm_and_maps_ax.lines[1].get_xydata().flatten(), [1, 0.47026441]) - assert_array_almost_equal(csm_and_maps_ax.lines[2].get_xydata().flatten(), [2, 0.00988778]) + assert_allclose(csm_and_maps_ax.lines[0].get_xydata().flatten(), [0, 0.53499332]) + assert_allclose(csm_and_maps_ax.lines[1].get_xydata().flatten(), [1, 0.47026441]) + assert_allclose(csm_and_maps_ax.lines[2].get_xydata().flatten(), [2, 0.00988778], atol=1e-8) _envs_fig, envs_ax = struct_envs.get_environments_figure(isite=isite) - assert_array_almost_equal( + assert_allclose( np.array(envs_ax.patches[0].get_xy()), [[1, 1], [1, 0.99301365], [1.00179228, 0.99301365], [1.00179228, 1], [1, 1]], ) - assert_array_almost_equal( + assert_allclose( np.array(envs_ax.patches[1].get_xy()), [[1, 0.99301365], [1, 0], [1.00179228, 0], [1.00179228, 0.99301365], [1, 0.99301365]], ) - assert_array_almost_equal( + assert_allclose( np.array(envs_ax.patches[2].get_xy()), [[1.00179228, 1], [1.00179228, 0.99301365], [2.25, 0.99301365], [2.25, 1], [1.00179228, 1]], ) - assert_array_almost_equal( + assert_allclose( np.array(envs_ax.patches[3].get_xy()), [ [1.00179228, 0.99301365], @@ -60,10 +60,12 @@ def test_structure_environments(self): [2.25, 0.99301365], [1.00179228, 0.99301365], ], + atol=1e-8, ) - assert_array_almost_equal( + assert_allclose( np.array(envs_ax.patches[4].get_xy()), [[2.22376156, 0.0060837], [2.22376156, 0], [2.25, 0], [2.25, 0.0060837], [2.22376156, 0.0060837]], + atol=1e-8, ) struct_envs.save_environments_figure(isite=isite, imagename="image.png") @@ -81,7 +83,7 @@ def test_structure_environments(self): assert symbol == "T:4" assert min_geometry["symmetry_measure"] == approx(0.00988778424054) - assert_array_almost_equal( + assert_allclose( min_geometry["other_symmetry_measures"]["rotation_matrix_wcs_csc"], [ [-0.8433079817973094, -0.19705747216466898, 0.5000000005010193], @@ -137,13 +139,13 @@ def test_light_structure_environments(self): neighb_indices = [0, 3, 5, 1] neighb_images = [[0, 0, -1], [0, 0, 0], [0, 0, -1], [0, 0, 0]] - assert_array_almost_equal(neighb_coords, nb_set.neighb_coords) - assert_array_almost_equal(neighb_coords, [s.coords for s in nb_set.neighb_sites]) + assert_allclose(neighb_coords, nb_set.neighb_coords) + assert_allclose(neighb_coords, [s.coords for s in nb_set.neighb_sites]) nb_sai = nb_set.neighb_sites_and_indices - assert_array_almost_equal(neighb_coords, [sai["site"].coords for sai in nb_sai]) - assert_array_almost_equal(neighb_indices, [sai["index"] for sai in nb_sai]) + assert_allclose(neighb_coords, [sai["site"].coords for sai in nb_sai]) + assert_allclose(neighb_indices, [sai["index"] for sai in nb_sai]) nb_iai = nb_set.neighb_indices_and_images - assert_array_almost_equal(neighb_indices, [iai["index"] for iai in nb_iai]) + assert_allclose(neighb_indices, [iai["index"] for iai in nb_iai]) np.testing.assert_array_equal(neighb_images, [iai["image_cell"] for iai in nb_iai]) assert len(nb_set) == 4 @@ -182,8 +184,8 @@ def test_light_structure_environments(self): assert stats["fraction_atom_coordination_environments_present"] == {"Si": {"T:4": 1}} site_info_ce = lse.get_site_info_for_specie_ce(specie=Species("Si4+"), ce_symbol="T:4") - assert_array_almost_equal(site_info_ce["fractions"], [1, 1, 1]) - assert_array_almost_equal( + assert_allclose(site_info_ce["fractions"], [1, 1, 1]) + assert_allclose( site_info_ce["csms"], [0.009887784240541068, 0.009887786546730826, 0.009887787384385317], ) diff --git a/tests/analysis/interfaces/test_zsl.py b/tests/analysis/interfaces/test_zsl.py index d85541f20a3..3cc7732eb73 100644 --- a/tests/analysis/interfaces/test_zsl.py +++ b/tests/analysis/interfaces/test_zsl.py @@ -39,7 +39,7 @@ def test_zsl(self): assert fast_norm(np.array([3, 2, 1])) == approx(3.74165738) assert_array_equal(reduce_vectors(np.array([1, 0, 0]), np.array([2, 2, 0])), [[1, 0, 0], [0, 2, 0]]) assert vec_area(np.array([1, 0, 0]), np.array([0, 2, 0])) == 2 - assert_array_equal(list(get_factors(18)), [1, 2, 3, 6, 9, 18]) + assert list(get_factors(18)) == [1, 2, 3, 6, 9, 18] assert is_same_vectors( np.array([[1.01, 0, 0], [0, 2, 0]], dtype=float), np.array([[1, 0, 0], [0, 2.01, 0]], dtype=float) ) diff --git a/tests/analysis/structure_prediction/test_volume_predictor.py b/tests/analysis/structure_prediction/test_volume_predictor.py index 169d5fd1ac1..c9d02fd7eb0 100644 --- a/tests/analysis/structure_prediction/test_volume_predictor.py +++ b/tests/analysis/structure_prediction/test_volume_predictor.py @@ -9,7 +9,7 @@ from pymatgen.core import Structure from pymatgen.util.testing import PymatgenTest -module_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) +module_dir = os.path.dirname(os.path.abspath(__file__)) class TestRLSVolumePredictor(PymatgenTest): diff --git a/tests/analysis/test_bond_dissociation.py b/tests/analysis/test_bond_dissociation.py index d847df8b6b9..704ae46f3dd 100644 --- a/tests/analysis/test_bond_dissociation.py +++ b/tests/analysis/test_bond_dissociation.py @@ -8,7 +8,7 @@ from pymatgen.analysis.bond_dissociation import BondDissociationEnergies -module_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) +module_dir = os.path.dirname(os.path.abspath(__file__)) class TestBondDissociation(unittest.TestCase): diff --git a/tests/analysis/test_graphs.py b/tests/analysis/test_graphs.py index 5aaf96ddb7c..997f02b7e26 100644 --- a/tests/analysis/test_graphs.py +++ b/tests/analysis/test_graphs.py @@ -43,7 +43,7 @@ __status__ = "Beta" __date__ = "August 2017" -module_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) +module_dir = os.path.dirname(os.path.abspath(__file__)) molecule_dir = f"{TEST_FILES_DIR}/molecules" @@ -52,7 +52,7 @@ def setUp(self): self.maxDiff = None # trivial example, simple square lattice for testing - structure = Structure(Lattice.tetragonal(5.0, 50.0), ["H"], [[0, 0, 0]]) + structure = Structure(Lattice.tetragonal(5, 50), ["H"], [[0, 0, 0]]) self.square_sg = StructureGraph.with_empty_graph(structure, edge_weight_name="", edge_weight_units="") self.square_sg.add_edge(0, 0, from_jimage=(0, 0, 0), to_jimage=(1, 0, 0)) self.square_sg.add_edge(0, 0, from_jimage=(0, 0, 0), to_jimage=(-1, 0, 0)) @@ -62,7 +62,7 @@ def setUp(self): # self.square_sg.decorate_structure_with_ce_info() # body-centered square lattice for testing - structure = Structure(Lattice.tetragonal(5.0, 50.0), ["H", "He"], [[0, 0, 0], [0.5, 0.5, 0.5]]) + structure = Structure(Lattice.tetragonal(5, 50), ["H", "He"], [[0, 0, 0], [0.5, 0.5, 0.5]]) self.bc_square_sg = StructureGraph.with_empty_graph(structure, edge_weight_name="", edge_weight_units="") self.bc_square_sg.add_edge(0, 0, from_jimage=(0, 0, 0), to_jimage=(1, 0, 0)) self.bc_square_sg.add_edge(0, 0, from_jimage=(0, 0, 0), to_jimage=(-1, 0, 0)) @@ -75,7 +75,7 @@ def setUp(self): # body-centered square lattice for testing # directions reversed, should be equivalent to bc_square - structure = Structure(Lattice.tetragonal(5.0, 50.0), ["H", "He"], [[0, 0, 0], [0.5, 0.5, 0.5]]) + structure = Structure(Lattice.tetragonal(5, 50), ["H", "He"], [[0, 0, 0], [0.5, 0.5, 0.5]]) self.bc_square_sg_r = StructureGraph.with_empty_graph(structure, edge_weight_name="", edge_weight_units="") self.bc_square_sg_r.add_edge(0, 0, from_jimage=(0, 0, 0), to_jimage=(1, 0, 0)) self.bc_square_sg_r.add_edge(0, 0, from_jimage=(0, 0, 0), to_jimage=(-1, 0, 0)) @@ -89,8 +89,8 @@ def setUp(self): # MoS2 example, structure graph obtained from critic2 # (not ground state, from mp-1023924, single layer) stdout_file = f"{TEST_FILES_DIR}/critic2/MoS2_critic2_stdout.txt" - with open(stdout_file) as f: - reference_stdout = f.read() + with open(stdout_file) as txt_file: + reference_stdout = txt_file.read() self.structure = Structure.from_file(f"{TEST_FILES_DIR}/critic2/MoS2.cif") c2o = Critic2Analysis(self.structure, reference_stdout) self.mos2_sg = c2o.structure_graph(include_critical_points=False) diff --git a/tests/analysis/test_nmr.py b/tests/analysis/test_nmr.py index 125967c7ead..14490ec7a0c 100644 --- a/tests/analysis/test_nmr.py +++ b/tests/analysis/test_nmr.py @@ -15,7 +15,7 @@ def test_construction(self): cs = ChemicalShielding([1, 2, 3]) assert cs.shape == (3, 3) - assert_array_equal(np.diag(cs), [1, 2, 3]) + assert np.diag(cs).tolist() == [1, 2, 3] def test_principal_axis_system(self): cs = ChemicalShielding([1, 2, 3]) diff --git a/tests/analysis/test_phase_diagram.py b/tests/analysis/test_phase_diagram.py index 3782e5b2fbc..5ab300f46bc 100644 --- a/tests/analysis/test_phase_diagram.py +++ b/tests/analysis/test_phase_diagram.py @@ -4,7 +4,6 @@ import os import unittest from numbers import Number -from pathlib import Path import numpy as np import pytest @@ -31,7 +30,7 @@ from pymatgen.entries.entry_tools import EntrySet from pymatgen.util.testing import PymatgenTest -module_dir = Path(__file__).absolute().parent +module_dir = os.path.dirname(os.path.abspath(__file__)) class TestPDEntry(unittest.TestCase): @@ -97,7 +96,7 @@ def test_str(self): assert str(pde) == "PDEntry : Li1 Fe1 O2 with energy = 53.0000" def test_read_csv(self): - entries = EntrySet.from_csv(module_dir / "pd_entries_test.csv") + entries = EntrySet.from_csv(f"{module_dir}/pd_entries_test.csv") assert entries.chemsys == {"Li", "Fe", "O"}, "Wrong elements!" assert len(entries) == 490, "Wrong number of entries!" @@ -161,7 +160,7 @@ def test_normalize(self): class TestPhaseDiagram(PymatgenTest): def setUp(self): - self.entries = EntrySet.from_csv(module_dir / "pd_entries_test.csv") + self.entries = EntrySet.from_csv(f"{module_dir}/pd_entries_test.csv") self.pd = PhaseDiagram(self.entries) def test_init(self): @@ -630,7 +629,7 @@ def test_val_err_on_no_entries(self): class TestGrandPotentialPhaseDiagram(unittest.TestCase): def setUp(self): - self.entries = EntrySet.from_csv(module_dir / "pd_entries_test.csv") + self.entries = EntrySet.from_csv(f"{module_dir}/pd_entries_test.csv") self.pd = GrandPotentialPhaseDiagram(self.entries, {Element("O"): -5}) self.pd6 = GrandPotentialPhaseDiagram(self.entries, {Element("O"): -6}) @@ -664,7 +663,7 @@ def test_str(self): class TestCompoundPhaseDiagram(unittest.TestCase): def setUp(self): - self.entries = EntrySet.from_csv(module_dir / "pd_entries_test.csv") + self.entries = EntrySet.from_csv(f"{module_dir}/pd_entries_test.csv") self.pd = CompoundPhaseDiagram(self.entries, [Composition("Li2O"), Composition("Fe2O3")]) def test_stable_entries(self): @@ -690,7 +689,7 @@ def test_str(self): class TestPatchedPhaseDiagram(unittest.TestCase): def setUp(self): - self.entries = EntrySet.from_csv(module_dir / "reaction_entries_test.csv") + self.entries = EntrySet.from_csv(f"{module_dir}/reaction_entries_test.csv") # NOTE add He to test for correct behavior despite no patches involving He self.no_patch_entry = he_entry = PDEntry("He", -1.23) self.entries.add(he_entry) @@ -821,7 +820,6 @@ def test_setitem_and_delitem(self): class TestReactionDiagram(unittest.TestCase): def setUp(self): - module_dir = os.path.dirname(os.path.abspath(__file__)) self.entries = list(EntrySet.from_csv(f"{module_dir}/reaction_entries_test.csv").entries) for e in self.entries: if e.composition.reduced_formula == "VPO5": @@ -834,12 +832,12 @@ def test_get_compound_pd(self): self.rd.get_compound_pd() def test_formula(self): - for e in self.rd.rxn_entries: - assert Element.V in e.composition - assert Element.O in e.composition - assert Element.C in e.composition - assert Element.P in e.composition - assert Element.H in e.composition + for entry in self.rd.rxn_entries: + assert Element.V in entry.composition + assert Element.O in entry.composition + assert Element.C in entry.composition + assert Element.P in entry.composition + assert Element.H in entry.composition # formed_formula = [e.composition.reduced_formula for e in self.rd.rxn_entries] # expected_formula = [ # "V0.12707182P0.12707182H0.0441989C0.03314917O0.66850829", diff --git a/tests/analysis/test_wulff.py b/tests/analysis/test_wulff.py index da9a468e5ee..d009df41463 100644 --- a/tests/analysis/test_wulff.py +++ b/tests/analysis/test_wulff.py @@ -3,7 +3,6 @@ import json import os -from numpy.testing import assert_array_equal from pytest import approx from pymatgen.analysis.wulff import WulffShape @@ -20,10 +19,11 @@ __email__ = "zix009@eng.ucsd.edu" __date__ = "May 05 2016" +module_dir = os.path.dirname(os.path.abspath(__file__)) + class TestWulffShape(PymatgenTest): def setUp(self): - module_dir = os.path.dirname(os.path.abspath(__file__)) with open(f"{module_dir}/surface_samples.json") as data_file: surface_properties = json.load(data_file) @@ -128,10 +128,7 @@ def consistency_tests(self): Nb_area_fraction_dict = self.wulff_Nb.area_fraction_dict for hkl in Nb_area_fraction_dict: - if hkl == (3, 1, 0): - assert Nb_area_fraction_dict[hkl] == 1 - else: - assert Nb_area_fraction_dict[hkl] == 0 + assert Nb_area_fraction_dict[hkl] == (1 if hkl == (3, 1, 0) else 0) assert self.wulff_Nb.miller_energy_dict[(3, 1, 0)] == self.wulff_Nb.weighted_surface_energy @@ -169,7 +166,7 @@ def test_properties(self): def test_corner_and_edges(self): # Test if it is returning the correct number of corner and edges - assert_array_equal(self.cube.tot_corner_sites, 8) - assert_array_equal(self.cube.tot_edges, 12) - assert_array_equal(self.hex_prism.tot_corner_sites, 12) - assert_array_equal(self.hex_prism.tot_edges, 18) + assert self.cube.tot_corner_sites == 8 + assert self.cube.tot_edges == 12 + assert self.hex_prism.tot_corner_sites == 12 + assert self.hex_prism.tot_edges == 18 diff --git a/tests/core/test_lattice.py b/tests/core/test_lattice.py index baaa08222b2..534557497b4 100644 --- a/tests/core/test_lattice.py +++ b/tests/core/test_lattice.py @@ -4,7 +4,7 @@ import numpy as np import pytest -from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal +from numpy.testing import assert_allclose, assert_array_equal from pytest import approx from pymatgen.core.lattice import Lattice, get_points_in_spheres @@ -553,17 +553,17 @@ def test_selling_dist(self): def test_selling_vector(self): a1 = 10 - assert_array_almost_equal( + assert_allclose( Lattice.cubic(a1).selling_vector.round(4), np.array([0, 0, 0, -(a1**2), -(a1**2), -(a1**2)]), ) a2, c2 = 5, 8 - assert_array_almost_equal( + assert_allclose( Lattice.tetragonal(a2, c2).selling_vector.round(4), np.array([0, 0, 0, -(a2**2), -(a2**2), -(c2**2)]), ) a3, b3, c3 = 4, 6, 7 - assert_array_almost_equal( + assert_allclose( Lattice.orthorhombic(a3, b3, c3).selling_vector.round(4), np.array([0, 0, 0, -(a3**2), -(b3**2), -(c3**2)]), ) diff --git a/tests/io/lammps/test_data.py b/tests/io/lammps/test_data.py index 960fc977882..ea5f7744258 100644 --- a/tests/io/lammps/test_data.py +++ b/tests/io/lammps/test_data.py @@ -10,7 +10,7 @@ import pandas as pd import pytest from monty.json import MontyDecoder, MontyEncoder -from numpy.testing import assert_array_almost_equal +from numpy.testing import assert_allclose from pytest import approx from ruamel.yaml import YAML @@ -60,16 +60,16 @@ def test_get_box_shift(self): assert peptide.get_box_shift([1, 0, 0])[0] == 64.211560 - 36.840194 assert peptide.get_box_shift([0, 0, -1])[-1] == 29.768095 - 57.139462 quartz = self.quartz - assert_array_almost_equal(quartz.get_box_shift([0, 0, 1]), [0, 0, 5.4052], 4) - assert_array_almost_equal(quartz.get_box_shift([0, 1, -1]), [-2.4567, 4.2551, -5.4052], 4) - assert_array_almost_equal(quartz.get_box_shift([1, -1, 0]), [4.9134 + 2.4567, -4.2551, 0], 4) + assert_allclose(quartz.get_box_shift([0, 0, 1]), [0, 0, 5.4052], 4) + assert_allclose(quartz.get_box_shift([0, 1, -1]), [-2.4567, 4.2551, -5.4052], 4) + assert_allclose(quartz.get_box_shift([1, -1, 0]), [4.9134 + 2.4567, -4.2551, 0], 4) def test_to_lattice(self): peptide = self.peptide.to_lattice() - assert_array_almost_equal(peptide.abc, [27.371367] * 3) + assert_allclose(peptide.abc, [27.371367] * 3) assert peptide.is_orthogonal quartz = self.quartz.to_lattice() - assert_array_almost_equal( + assert_allclose( quartz.matrix, [[4.913400, 0, 0], [-2.456700, 4.255129, 0], [0, 0, 5.405200]], ) @@ -86,7 +86,7 @@ def setUpClass(cls): def test_structure(self): quartz = self.quartz.structure - assert_array_almost_equal( + assert_allclose( quartz.lattice.matrix, [[4.913400, 0, 0], [-2.456700, 4.255129, 0], [0, 0, 5.405200]], ) @@ -94,15 +94,15 @@ def test_structure(self): assert "molecule-ID" not in self.quartz.atoms.columns ethane = self.ethane.structure - assert_array_almost_equal(ethane.lattice.matrix, np.diag([10.0] * 3)) + assert_allclose(ethane.lattice.matrix, np.diag([10.0] * 3)) l_bounds = np.array(self.ethane.box.bounds)[:, 0] coords = self.ethane.atoms[["x", "y", "z"]] - l_bounds - assert_array_almost_equal(ethane.cart_coords, coords) - assert_array_almost_equal(ethane.site_properties["charge"], self.ethane.atoms["q"]) + assert_allclose(ethane.cart_coords, coords) + assert_allclose(ethane.site_properties["charge"], self.ethane.atoms["q"]) tatb = self.tatb.structure frac_coords = tatb.frac_coords[381] real_frac_coords = frac_coords - np.floor(frac_coords) - assert_array_almost_equal(real_frac_coords, [0.01553397, 0.71487872, 0.14134139]) + assert_allclose(real_frac_coords, [0.01553397, 0.71487872, 0.14134139], atol=1e-8) co = Structure.from_spacegroup(194, Lattice.hexagonal(2.50078, 4.03333), ["Co"], [[1 / 3, 2 / 3, 1 / 4]]) ld_co = LammpsData.from_structure(co) @@ -313,7 +313,7 @@ def test_disassemble(self): np.testing.assert_array_equal(sample_coeff["coeffs"], c.force_field[ff_kw].iloc[i].values, ff_kw) topo = topos[-1] atoms = c.atoms[c.atoms["molecule-ID"] == 46] - assert_array_almost_equal(topo.sites.cart_coords, atoms[["x", "y", "z"]]) + assert_allclose(topo.sites.cart_coords, atoms[["x", "y", "z"]]) np.testing.assert_array_equal(topo.charges, atoms["q"]) atom_labels = [m[0] for m in mass_info] assert topo.sites.site_properties["ff_map"] == [atom_labels[i - 1] for i in atoms["type"]] @@ -487,7 +487,7 @@ def test_from_structure(self): va = velocities[i].dot(a) / np.linalg.norm(a) assert va == approx(ld.velocities.loc[i + 1, "vx"]) assert velocities[i, 1] == approx(ld.velocities.loc[i + 1, "vy"]) - assert_array_almost_equal(ld.masses["mass"], [22.989769, 190.23, 15.9994]) + assert_allclose(ld.masses["mass"], [22.989769, 190.23, 15.9994]) np.testing.assert_array_equal(ld.atoms["type"], [2] * 4 + [3] * 16) def test_set_charge_atom(self): @@ -774,11 +774,11 @@ def test_lattice_2_lmpbox(self): origin = np.random.rand(3) * 10 - 5 box, symmop = lattice_2_lmpbox(lattice=init_latt, origin=origin) boxed_latt = box.to_lattice() - assert_array_almost_equal(init_latt.abc, boxed_latt.abc) - assert_array_almost_equal(init_latt.angles, boxed_latt.angles) + assert_allclose(init_latt.abc, boxed_latt.abc) + assert_allclose(init_latt.angles, boxed_latt.angles) cart_coords = symmop.operate_multi(init_structure.cart_coords) - origin boxed_structure = Structure(boxed_latt, ["H"] * 10, cart_coords, coords_are_cartesian=True) - assert_array_almost_equal(boxed_structure.frac_coords, frac_coords) + assert_allclose(boxed_structure.frac_coords, frac_coords) tetra_latt = Lattice.tetragonal(5, 5) tetra_box, _ = lattice_2_lmpbox(tetra_latt) assert tetra_box.tilt is None @@ -787,7 +787,7 @@ def test_lattice_2_lmpbox(self): assert ortho_box.tilt is None rot_tetra_latt = Lattice([[5, 0, 0], [0, 2, 2], [0, -2, 2]]) _, rotop = lattice_2_lmpbox(rot_tetra_latt) - assert_array_almost_equal( + assert_allclose( rotop.rotation_matrix, [ [1, 0, 0], @@ -1005,22 +1005,22 @@ def test_get_str(self): def test_structure(self): li_ec_structure = self.li_ec.structure - assert_array_almost_equal( + assert_allclose( li_ec_structure.lattice.matrix, [[38.698274, 0, 0], [0, 38.698274, 0], [0, 0, 38.698274]], ) - assert_array_almost_equal( + assert_allclose( li_ec_structure.lattice.angles, (90.0, 90.0, 90.0), ) assert li_ec_structure.formula == "Li1 H4 C3 O3" lbounds = np.array(self.li_ec.box.bounds)[:, 0] coords = self.li_ec.atoms[["x", "y", "z"]] - lbounds - assert_array_almost_equal(li_ec_structure.cart_coords, coords) - assert_array_almost_equal(li_ec_structure.site_properties["charge"], self.li_ec.atoms["q"]) + assert_allclose(li_ec_structure.cart_coords, coords) + assert_allclose(li_ec_structure.site_properties["charge"], self.li_ec.atoms["q"]) frac_coords = li_ec_structure.frac_coords[0] real_frac_coords = frac_coords - np.floor(frac_coords) - assert_array_almost_equal(real_frac_coords, [0.01292047, 0.01292047, 0.01292047]) + assert_allclose(real_frac_coords, [0.01292047, 0.01292047, 0.01292047], atol=1e-8) def test_from_ff_and_topologies(self): with pytest.raises(AttributeError, match="Unsupported constructor for CombinedData objects"): @@ -1043,7 +1043,7 @@ def test_disassemble(self): topo = topos[-1] atoms = ld.atoms[ld.atoms["molecule-ID"] == 1] - assert_array_almost_equal(topo.sites.cart_coords, atoms[["x", "y", "z"]]) + assert_allclose(topo.sites.cart_coords, atoms[["x", "y", "z"]]) np.testing.assert_array_equal(topo.charges, atoms["q"]) atom_labels = [m[0] for m in mass_info] assert topo.sites.site_properties["ff_map"] == [atom_labels[i - 1] for i in atoms["type"]] diff --git a/tests/io/lammps/test_outputs.py b/tests/io/lammps/test_outputs.py index 0bbbda1fe88..a2c072aeb42 100644 --- a/tests/io/lammps/test_outputs.py +++ b/tests/io/lammps/test_outputs.py @@ -6,7 +6,7 @@ import numpy as np import pandas as pd -from numpy.testing import assert_array_almost_equal +from numpy.testing import assert_allclose from pymatgen.io.lammps.outputs import LammpsDump, parse_lammps_dumps, parse_lammps_log from pymatgen.util.testing import TEST_FILES_DIR @@ -31,18 +31,18 @@ def test_from_string(self): np.testing.assert_array_equal(self.rdx.data.columns, ["id", "type", "xs", "ys", "zs"]) rdx_data = self.rdx.data.iloc[-1] rdx_data_target = [19, 2, 0.42369, 0.47347, 0.555425] - assert_array_almost_equal(rdx_data, rdx_data_target) + assert_allclose(rdx_data, rdx_data_target) assert self.tatb.timestep == 0 assert self.tatb.natoms == 384 bounds = [[0, 13.624], [0, 17.1149153805], [0, 15.1826391451]] - assert_array_almost_equal(self.tatb.box.bounds, bounds) + assert_allclose(self.tatb.box.bounds, bounds) tilt = [-5.75315630927, -6.325466, 7.4257288] - assert_array_almost_equal(self.tatb.box.tilt, tilt) + assert_allclose(self.tatb.box.tilt, tilt) np.testing.assert_array_equal(self.tatb.data.columns, ["id", "type", "q", "x", "y", "z"]) tatb_data = self.tatb.data.iloc[-1] tatb_data_target = [356, 3, -0.482096, 2.58647, 12.9577, 14.3143] - assert_array_almost_equal(tatb_data, tatb_data_target) + assert_allclose(tatb_data, tatb_data_target) def test_json_dict(self): encoded = json.dumps(self.rdx.as_dict()) @@ -81,7 +81,7 @@ def test_parse_lammps_log(self): [0, 1, -4.6295947, -4.6297237, -4.6297237, 0], [5, 1, -4.6295965, -4.6297255, -4.6297255, 0], ] - assert_array_almost_equal(comb0.iloc[[0, -1]], comb0_data) + assert_allclose(comb0.iloc[[0, -1]], comb0_data) # final comb run comb_1 = comb[-1] np.testing.assert_array_equal( @@ -104,7 +104,7 @@ def test_parse_lammps_log(self): ) assert len(comb_1) == 11 comb_1_data = [[36, 5.1293854e-06], [46, 2192.8256]] - assert_array_almost_equal(comb_1.iloc[[0, -1], [0, -3]], comb_1_data) + assert_allclose(comb_1.iloc[[0, -1], [0, -3]], comb_1_data) ehex_file = "log.13Oct16.ehex.g++.8.gz" ehex = parse_lammps_log(filename=os.path.join(test_dir, ehex_file)) @@ -117,7 +117,7 @@ def test_parse_lammps_log(self): [0, 1.35, -4.1241917, 0, -2.0994448, -3.1961612], [1000, 1.3732017, -3.7100044, 0, -1.6504594, 0.83982701], ] - assert_array_almost_equal(ehex0.iloc[[0, -1]], ehex0_data) + assert_allclose(ehex0.iloc[[0, -1]], ehex0_data) # ehex run #2 np.testing.assert_array_equal(["Step", "Temp", "c_Thot", "c_Tcold"], ehex1.columns) assert len(ehex1) == 11 @@ -125,7 +125,7 @@ def test_parse_lammps_log(self): [1000, 1.35, 1.431295, 1.2955644], [11000, 1.3794051, 1.692299, 1.0515688], ] - assert_array_almost_equal(ehex1.iloc[[0, -1]], ehex1_data) + assert_allclose(ehex1.iloc[[0, -1]], ehex1_data) # ehex run #3 np.testing.assert_array_equal(["Step", "Temp", "c_Thot", "c_Tcold", "v_tdiff", "f_ave"], ehex2.columns) assert len(ehex2) == 21 @@ -133,7 +133,7 @@ def test_parse_lammps_log(self): [11000, 1.3794051, 1.6903393, 1.0515688, 0, 0], [31000, 1.3822489, 1.8220413, 1.0322271, -0.7550338, -0.76999077], ] - assert_array_almost_equal(ehex2.iloc[[0, -1]], ehex2_data) + assert_allclose(ehex2.iloc[[0, -1]], ehex2_data) peptide_file = "log.5Oct16.peptide.g++.1.gz" peptide = parse_lammps_log(filename=os.path.join(test_dir, peptide_file)) @@ -159,4 +159,4 @@ def test_parse_lammps_log(self): assert len(peptide0) == 7 peptide0_select = peptide0.loc[[0, 6], ["Step", "TotEng", "Press"]] peptide0_data = [[0, -5237.4580, -837.0112], [300, -5251.3637, -471.5505]] - assert_array_almost_equal(peptide0_select, peptide0_data) + assert_allclose(peptide0_select, peptide0_data) diff --git a/tests/io/qchem/test_inputs.py b/tests/io/qchem/test_inputs.py index f9a37b95aad..bbf456d47a3 100644 --- a/tests/io/qchem/test_inputs.py +++ b/tests/io/qchem/test_inputs.py @@ -18,7 +18,7 @@ __email__ = "samblau1@gmail.com" __credits__ = "Xiaohui Qu" -module_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) +module_dir = os.path.dirname(os.path.abspath(__file__)) logger = logging.getLogger(__name__)