Skip to content

Breaking: Overhaul class PymatgenTest #3014

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

Merged
merged 8 commits into from
May 25, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_coordination_geometry(self):
[[-1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, -1.0, 0.0]],
[[-1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, -1.0]],
]
self.assertArrayAlmostEqual(cg_oct.faces(sites=sites, permutation=[0, 3, 2, 4, 5, 1]), faces)
self.assert_all_close(cg_oct.faces(sites=sites, permutation=[0, 3, 2, 4, 5, 1]), faces)

faces = [
[[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
Expand All @@ -118,7 +118,7 @@ def test_coordination_geometry(self):
[[0.0, 0.0, -1.0], [-1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
[[0.0, 0.0, -1.0], [-1.0, 0.0, 0.0], [0.0, -1.0, 0.0]],
]
self.assertArrayAlmostEqual(cg_oct.faces(sites=sites), faces)
self.assert_all_close(cg_oct.faces(sites=sites), faces)

edges = [
[[0.0, 0.0, 1.0], [1.0, 0.0, 0.0]],
Expand All @@ -134,7 +134,7 @@ def test_coordination_geometry(self):
[[0.0, 1.0, 0.0], [0.0, -1.0, 0.0]],
[[0.0, 1.0, 0.0], [0.0, 0.0, -1.0]],
]
self.assertArrayAlmostEqual(cg_oct.edges(sites=sites, permutation=[0, 3, 2, 4, 5, 1]), edges)
self.assert_all_close(cg_oct.edges(sites=sites, permutation=[0, 3, 2, 4, 5, 1]), edges)

edges = [
[[0.0, 0.0, 1.0], [1.0, 0.0, 0.0]],
Expand All @@ -150,9 +150,9 @@ def test_coordination_geometry(self):
[[-1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
[[-1.0, 0.0, 0.0], [0.0, -1.0, 0.0]],
]
self.assertArrayAlmostEqual(cg_oct.edges(sites=sites), edges)
self.assert_all_close(cg_oct.edges(sites=sites), edges)

self.assertArrayAlmostEqual(
self.assert_all_close(
cg_oct.solid_angles(),
[2.0943951, 2.0943951, 2.0943951, 2.0943951, 2.0943951, 2.0943951],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def test_abstract_geometry(self):
cg_ts3 = self.lgf.allcg["TS:3"]
cg_tet = self.lgf.allcg["T:4"]
abstract_geom = AbstractGeometry.from_cg(cg=cg_ts3, centering_type="central_site")
self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.0])
self.assert_all_close(abstract_geom.centre, [0.0, 0.0, 0.0])
abstract_geom = AbstractGeometry.from_cg(cg=cg_ts3, centering_type="centroid")
self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.33333333333])
self.assert_all_close(abstract_geom.centre, [0.0, 0.0, 0.33333333333])
with pytest.raises(ValueError) as exc_info:
AbstractGeometry.from_cg(
cg=cg_ts3,
Expand All @@ -55,7 +55,7 @@ def test_abstract_geometry(self):
abstract_geom = AbstractGeometry.from_cg(
cg=cg_ts3, centering_type="centroid", include_central_site_in_centroid=True
)
self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.25])
self.assert_all_close(abstract_geom.centre, [0.0, 0.0, 0.25])

# WHY ARE WE TESTING STRINGS????
# self.assertEqual(str(abstract_geom)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
import pytest
from numpy.testing import assert_array_almost_equal

from pymatgen.analysis.chemenv.coordination_environments.chemenv_strategies import (
AngleNbSetWeight,
Expand Down Expand Up @@ -115,8 +116,8 @@ def test_structure_environments_neighbors_sets(self):

neighb_coords = nb_set.coords

np.testing.assert_array_almost_equal(coords, neighb_coords[1:])
np.testing.assert_array_almost_equal(nb_set.structure[nb_set.isite].coords, neighb_coords[0])
assert_array_almost_equal(coords, neighb_coords[1:])
assert_array_almost_equal(nb_set.structure[nb_set.isite].coords, neighb_coords[0])

norm_dist = nb_set.normalized_distances
assert sorted(norm_dist) == pytest.approx(sorted([1.001792, 1.001792, 1, 1.0]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import pytest
from monty.tempfile import ScratchDir
from numpy.testing import assert_array_almost_equal

from pymatgen.analysis.chemenv.coordination_environments.chemenv_strategies import (
MultiWeightsChemenvStrategy,
Expand All @@ -23,7 +24,7 @@

__author__ = "waroquiers"

se_files_dir = os.path.join(
struct_env_files_dir = os.path.join(
PymatgenTest.TEST_FILES_DIR,
"chemenv",
"structure_environments_files",
Expand All @@ -33,24 +34,18 @@
class StructureEnvironmentsTest(PymatgenTest):
def test_structure_environments(self):
with ScratchDir("."):
with open(f"{se_files_dir}/se_mp-7000.json") as f:
with open(f"{struct_env_files_dir}/se_mp-7000.json") as f:
dd = json.load(f)

se = StructureEnvironments.from_dict(dd)
isite = 6
csm_and_maps_fig, csm_and_maps_subplot = se.get_csm_and_maps(isite=isite)
np.testing.assert_array_almost_equal(
csm_and_maps_subplot.lines[0].get_xydata().flatten(), [0.0, 0.53499332]
)
np.testing.assert_array_almost_equal(
csm_and_maps_subplot.lines[1].get_xydata().flatten(), [1.0, 0.47026441]
)
np.testing.assert_array_almost_equal(
csm_and_maps_subplot.lines[2].get_xydata().flatten(), [2.0, 0.00988778]
)
assert_array_almost_equal(csm_and_maps_subplot.lines[0].get_xydata().flatten(), [0.0, 0.53499332])
assert_array_almost_equal(csm_and_maps_subplot.lines[1].get_xydata().flatten(), [1.0, 0.47026441])
assert_array_almost_equal(csm_and_maps_subplot.lines[2].get_xydata().flatten(), [2.0, 0.00988778])

environments_figure, environments_subplot = se.get_environments_figure(isite=isite)
np.testing.assert_array_almost_equal(
assert_array_almost_equal(
np.array(environments_subplot.patches[0].get_xy()),
[
[1.0, 1.0],
Expand All @@ -60,7 +55,7 @@ def test_structure_environments(self):
[1.0, 1.0],
],
)
np.testing.assert_array_almost_equal(
assert_array_almost_equal(
np.array(environments_subplot.patches[1].get_xy()),
[
[1.0, 0.99301365],
Expand All @@ -70,7 +65,7 @@ def test_structure_environments(self):
[1.0, 0.99301365],
],
)
np.testing.assert_array_almost_equal(
assert_array_almost_equal(
np.array(environments_subplot.patches[2].get_xy()),
[
[1.00179228, 1.0],
Expand All @@ -80,7 +75,7 @@ def test_structure_environments(self):
[1.00179228, 1.0],
],
)
np.testing.assert_array_almost_equal(
assert_array_almost_equal(
np.array(environments_subplot.patches[3].get_xy()),
[
[1.00179228, 0.99301365],
Expand All @@ -92,7 +87,7 @@ def test_structure_environments(self):
[1.00179228, 0.99301365],
],
)
np.testing.assert_array_almost_equal(
assert_array_almost_equal(
np.array(environments_subplot.patches[4].get_xy()),
[
[2.22376156, 0.0060837],
Expand All @@ -118,7 +113,7 @@ def test_structure_environments(self):
assert symbol == "T:4"
assert min_geometry["symmetry_measure"] == pytest.approx(0.00988778424054)

np.testing.assert_array_almost_equal(
assert_array_almost_equal(
min_geometry["other_symmetry_measures"]["rotation_matrix_wcs_csc"],
[
[-0.8433079817973094, -0.19705747216466898, 0.5000000005010193],
Expand Down Expand Up @@ -155,7 +150,7 @@ def test_structure_environments(self):

def test_light_structure_environments(self):
with ScratchDir("."):
with open(f"{se_files_dir}/se_mp-7000.json") as f:
with open(f"{struct_env_files_dir}/se_mp-7000.json") as f:
dd = json.load(f)

se = StructureEnvironments.from_dict(dd)
Expand All @@ -175,13 +170,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]]

np.testing.assert_array_almost_equal(neighb_coords, nb_set.neighb_coords)
np.testing.assert_array_almost_equal(neighb_coords, [s.coords for s in nb_set.neighb_sites])
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])
nb_sai = nb_set.neighb_sites_and_indices
np.testing.assert_array_almost_equal(neighb_coords, [sai["site"].coords for sai in nb_sai])
np.testing.assert_array_almost_equal(neighb_indices, [sai["index"] for sai in nb_sai])
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])
nb_iai = nb_set.neighb_indices_and_images
np.testing.assert_array_almost_equal(neighb_indices, [iai["index"] for iai in nb_iai])
assert_array_almost_equal(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
Expand All @@ -198,28 +193,28 @@ def test_light_structure_environments(self):
stats = lse.get_statistics()

neighbors = lse.strategy.get_site_neighbors(site=lse.structure[isite])
self.assertArrayAlmostEqual(neighbors[0].coords, np.array([0.2443798, 1.80409653, -1.13218359]))
self.assertArrayAlmostEqual(neighbors[1].coords, np.array([1.44020353, 1.11368738, 1.13218359]))
self.assertArrayAlmostEqual(neighbors[2].coords, np.array([2.75513098, 2.54465207, -0.70467298]))
self.assertArrayAlmostEqual(neighbors[3].coords, np.array([0.82616785, 3.65833945, 0.70467298]))
self.assert_all_close(neighbors[0].coords, np.array([0.2443798, 1.80409653, -1.13218359]))
self.assert_all_close(neighbors[1].coords, np.array([1.44020353, 1.11368738, 1.13218359]))
self.assert_all_close(neighbors[2].coords, np.array([2.75513098, 2.54465207, -0.70467298]))
self.assert_all_close(neighbors[3].coords, np.array([0.82616785, 3.65833945, 0.70467298]))

equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform(neighbors[0])
assert equiv_site_index_and_transform[0] == 0
self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0])
self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, -1.0])
self.assert_all_close(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0])
self.assert_all_close(equiv_site_index_and_transform[2], [0.0, 0.0, -1.0])

equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform(neighbors[1])
assert equiv_site_index_and_transform[0] == 3
self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0])
self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, 0.0])
self.assert_all_close(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0])
self.assert_all_close(equiv_site_index_and_transform[2], [0.0, 0.0, 0.0])

assert stats["atom_coordination_environments_present"] == {"Si": {"T:4": 3.0}}
assert stats["coordination_environments_atom_present"] == {"T:4": {"Si": 3.0}}
assert stats["fraction_atom_coordination_environments_present"] == {"Si": {"T:4": 1.0}}

site_info_ce = lse.get_site_info_for_specie_ce(specie=Species("Si", 4), ce_symbol="T:4")
np.testing.assert_array_almost_equal(site_info_ce["fractions"], [1.0, 1.0, 1.0])
np.testing.assert_array_almost_equal(
assert_array_almost_equal(site_info_ce["fractions"], [1.0, 1.0, 1.0])
assert_array_almost_equal(
site_info_ce["csms"],
[0.009887784240541068, 0.009887786546730826, 0.009887787384385317],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def setUp(self):

def test_factors_abcd_normal_vector(self):
factors = self.plane.coefficients / self.expected_coefficients
self.assertArrayAlmostEqual([factors[0]] * 4, list(factors))
self.assert_all_close([factors[0]] * 4, list(factors))
assert np.allclose([2.0 / 3.0, 1.0 / 3.0, -2.0 / 3.0], self.plane.normal_vector)

def test_from_npoints_plane(self):
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_distances(self):
point_8 = np.array([100.0, 0.0, 0.0], np.float_)
plist = [point_1, point_2, point_4, point_6, point_7, point_8]
distances, indices_sorted = self.plane.distances_indices_sorted(plist)
self.assertArrayAlmostEqual(
self.assert_all_close(
distances,
[
0.5,
Expand All @@ -260,23 +260,23 @@ def test_distances(self):
plane = Plane.from_coefficients(0, 2, 0, 1)
plist = [point_1, point_5, point_6, point_7]
distances, indices_sorted = plane.distances_indices_sorted(plist)
self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5])
self.assert_all_close(distances, [0.5, -1.0, 2.5, 10.5])
assert indices_sorted == [0, 1, 2, 3]
plist = [point_1, point_5, point_6, point_7]
distances, indices_sorted = plane.distances_indices_sorted(plist)
self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5])
self.assert_all_close(distances, [0.5, -1.0, 2.5, 10.5])
assert indices_sorted == [0, 1, 2, 3]
distances, indices_sorted = plane.distances_indices_sorted(plist, sign=True)
self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5])
self.assert_all_close(distances, [0.5, -1.0, 2.5, 10.5])
assert indices_sorted == [(0, 1), (1, -1), (2, 1), (3, 1)]
plist = [point_5, point_1, point_6, point_7]
distances, indices_sorted, groups = plane.distances_indices_groups(plist)
self.assertArrayAlmostEqual(distances, [-1.0, 0.5, 2.5, 10.5])
self.assert_all_close(distances, [-1.0, 0.5, 2.5, 10.5])
assert indices_sorted == [1, 0, 2, 3]
assert groups == [[1, 0], [2], [3]]
plist = [point_1, point_2, point_3, point_4, point_5, point_6, point_7, point_8]
distances, indices_sorted = plane.distances_indices_sorted(plist)
self.assertArrayAlmostEqual(distances, [0.5, 0.5, 0.5, 0.5, -1.0, 2.5, 10.5, 0.5])
self.assert_all_close(distances, [0.5, 0.5, 0.5, 0.5, -1.0, 2.5, 10.5, 0.5])
assert set(indices_sorted[:5]) == {0, 1, 2, 3, 7}
# Plane z=0 (plane xy)
plane = Plane.from_coefficients(0, 0, 1, 0)
Expand All @@ -287,30 +287,19 @@ def test_distances(self):
distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta=0.25)
assert indices_sorted == [5, 0, 1, 2, 6, 7, 9, 4, 3, 8]
assert groups == [[5, 0, 1], [2, 6, 7], [9, 4, 3], [8]]
self.assertArrayAlmostEqual(distances, zzs)
self.assert_all_close(distances, zzs)
distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta_factor=0.1)
assert indices_sorted == [5, 0, 1, 2, 6, 7, 9, 4, 3, 8]
assert groups == [[5, 0, 1, 2, 6, 7], [9, 4, 3], [8]]
self.assertArrayAlmostEqual(distances, zzs)
self.assert_all_close(distances, zzs)
distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta_factor=0.1, sign=True)
assert indices_sorted == [
(5, 0),
(0, 1),
(1, -1),
(2, 1),
(6, -1),
(7, -1),
(9, 1),
(4, -1),
(3, -1),
(8, -1),
]
assert indices_sorted == [(5, 0), (0, 1), (1, -1), (2, 1), (6, -1), (7, -1), (9, 1), (4, -1), (3, -1), (8, -1)]
assert groups == [
[(5, 0), (0, 1), (1, -1), (2, 1), (6, -1), (7, -1)],
[(9, 1), (4, -1), (3, -1)],
[(8, -1)],
]
self.assertArrayAlmostEqual(distances, zzs)
self.assert_all_close(distances, zzs)

def test_projections(self):
# Projections of points that are already on the plane
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/analysis/chemenv/utils/tests/test_math_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_power3_step(self):
def test_cosinus_step(self):
vals = np.linspace(5.0, 12.0, num=8)
assert cosinus_step(vals, edges=[0.0, 1.0]).tolist() == [1.0] * 8
self.assertArrayAlmostEqual(
self.assert_all_close(
cosinus_step(vals, edges=[7.0, 11.0]).tolist(),
[0.0, 0.0, 0.0, 0.14644660940672616, 0.5, 0.8535533905932737, 1.0, 1.0],
5,
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/analysis/diffraction/tests/test_tem.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def test_get_plot_coeffs(self):
# Test if x * p1 + y * p2 yields p3.
c = TEMCalculator()
coeffs = c.get_plot_coeffs((1, 1, 0), (1, -1, 0), (2, 0, 0))
self.assertArrayAlmostEqual(np.array([1.0, 1.0]), coeffs)
self.assert_all_close(np.array([1.0, 1.0]), coeffs)

def test_get_positions(self):
c = TEMCalculator()
Expand All @@ -234,7 +234,7 @@ def test_get_positions(self):
assert [0, 0] == positions[(0, 0, 0)].tolist()
# Test silicon diffraction data spot rough positions:
# see https://www.doitpoms.ac.uk/tlplib/diffraction-patterns/printall.php
self.assertArrayAlmostEqual([1, 0], positions[(-1, 0, 0)], 0)
self.assert_all_close([1, 0], positions[(-1, 0, 0)], 0)

def test_TEM_dots(self):
# All dependencies in TEM_dots method are tested. Only make sure each object created is
Expand Down
Loading