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

Conversation

janosh
Copy link
Member

@janosh janosh commented Aug 18, 2023

Reason: significantly more helpful error messages on failed tests.

Example before with assert np.allclose:

tests/analysis/gb/test_grain.py:100: AssertionError

    def test_as_dict_and_from_dict(self):
        d1 = self.Cu_GB1.as_dict()
        d2 = self.Cu_GB2.as_dict()
        Cu_GB1_new = GrainBoundary.from_dict(d1)
        Cu_GB2_new = GrainBoundary.from_dict(d2)
        assert Cu_GB1_new.sigma == approx(self.Cu_GB1.sigma)
        assert Cu_GB1_new.rotation_angle == approx(self.Cu_GB1.rotation_angle)
        assert Cu_GB1_new.rotation_axis == self.Cu_GB1.rotation_axis
        assert Cu_GB1_new.gb_plane == self.Cu_GB1.gb_plane
        assert np.allclose(Cu_GB1_new.init_cell.lattice.matrix, self.Cu_GB1.init_cell.lattice.matrix)
>       assert np.allclose(
            Cu_GB1_new.oriented_unit_cell.lattice.matrix + 1e-3,
            self.Cu_GB1.oriented_unit_cell.lattice.matrix,
        )
E       assert False
E        +  where False = <function allclose at 0x1102feab0>((array([[-10.84922172,   0.        ,  10.84922172],\n       [-14.46562896,  10.84922172, -18.0820362 ],\n       [ -7.23281448,   3.61640724,  -7.23281448]]) + 0.001), array([[-10.84922172,   0.        ,  10.84922172],\n       [-14.46562896,  10.84922172, -18.0820362 ],\n       [ -7.23281448,   3.61640724,  -7.23281448]]))
E        +    where <function allclose at 0x1102feab0> = np.allclose
E        +    and   array([[-10.84922172,   0.        ,  10.84922172],\n       [-14.46562896,  10.84922172, -18.0820362 ],\n       [ -7.23281448,   3.61640724,  -7.23281448]]) = Lattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.130102354156051 90.0 95.7391704...72\n      B : -14.46562896 10.84922172 -18.0820362\n      C : -7.23281448 3.61640724 -7.23281448\n    pbc : True True True.matrix
E        +      where Lattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.130102354156051 90.0 95.7391704...72\n      B : -14.46562896 10.84922172 -18.0820362\n      C : -7.23281448 3.61640724 -7.23281448\n    pbc : True True True = Structure Summary\nLattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.1301023541560...cSite: Cu (-16.27, 9.041, -10.85) [0.3889, 0.8333, 0.0]\nPeriodicSite: Cu (-12.66, 9.041, -14.47) [0.05556, 0.8333, 0.0].lattice
E        +        where Structure Summary\nLattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.1301023541560...cSite: Cu (-16.27, 9.041, -10.85) [0.3889, 0.8333, 0.0]\nPeriodicSite: Cu (-12.66, 9.041, -14.47) [0.05556, 0.8333, 0.0] = Structure Summary\nLattice\n    abc : 15.343116497616757 25.571860829361263 87.14641575163476\n angles : 9.61799283329482...e: Cu (-45.26, 18.33, -34.41) [0.5261, 0.1568, 0.6344]\nPeriodicSite: Cu (-45.26, 20.14, -36.21) [0.508, 0.5483, 0.5413].oriented_unit_cell
E        +    and   array([[-10.84922172,   0.        ,  10.84922172],\n       [-14.46562896,  10.84922172, -18.0820362 ],\n       [ -7.23281448,   3.61640724,  -7.23281448]]) = Lattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.130102354156051 90.0 95.7391704...72\n      B : -14.46562896 10.84922172 -18.0820362\n      C : -7.23281448 3.61640724 -7.23281448\n    pbc : True True True.matrix
E        +      where Lattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.130102354156051 90.0 95.7391704...72\n      B : -14.46562896 10.84922172 -18.0820362\n      C : -7.23281448 3.61640724 -7.23281448\n    pbc : True True True = Structure Summary\nLattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.1301023541560...cSite: Cu (-16.27, 9.041, -10.85) [0.3889, 0.8333, 0.0]\nPeriodicSite: Cu (-12.66, 9.041, -14.47) [0.05556, 0.8333, 0.0].lattice
E        +        where Structure Summary\nLattice\n    abc : 15.343116497616757 25.571860829361263 10.849221720000001\n angles : 8.1301023541560...cSite: Cu (-16.27, 9.041, -10.85) [0.3889, 0.8333, 0.0]\nPeriodicSite: Cu (-12.66, 9.041, -14.47) [0.05556, 0.8333, 0.0] = Structure Summary\nLattice\n    abc : 15.343116497616757 25.571860829361263 87.14641575163476\n angles : 9.61799283329482...e: Cu (-45.26, 18.33, -34.41) [0.5261, 0.1568, 0.6344]\nPeriodicSite: Cu (-45.26, 20.14, -36.21) [0.508, 0.5483, 0.5413].oriented_unit_cell
E        +          where Structure Summary\nLattice\n    abc : 15.343116497616757 25.571860829361263 87.14641575163476\n angles : 9.61799283329482...e: Cu (-45.26, 18.33, -34.41) [0.5261, 0.1568, 0.6344]\nPeriodicSite: Cu (-45.26, 20.14, -36.21) [0.508, 0.5483, 0.5413] = <tests.analysis.gb.test_grain.TestGrainBoundary testMethod=test_as_dict_and_from_dict>.Cu_GB1

After changing to numpy.testing.assert_allclose:

    def test_as_dict_and_from_dict(self):
        d1 = self.Cu_GB1.as_dict()
        d2 = self.Cu_GB2.as_dict()
        Cu_GB1_new = GrainBoundary.from_dict(d1)
        Cu_GB2_new = GrainBoundary.from_dict(d2)
        assert Cu_GB1_new.sigma == approx(self.Cu_GB1.sigma)
        assert Cu_GB1_new.rotation_angle == approx(self.Cu_GB1.rotation_angle)
        assert Cu_GB1_new.rotation_axis == self.Cu_GB1.rotation_axis
        assert Cu_GB1_new.gb_plane == self.Cu_GB1.gb_plane
        assert np.allclose(Cu_GB1_new.init_cell.lattice.matrix, self.Cu_GB1.init_cell.lattice.matrix)
>       assert_allclose(
            Cu_GB1_new.oriented_unit_cell.lattice.matrix + 1e-3,
            self.Cu_GB1.oriented_unit_cell.lattice.matrix,
        )

kwds = {'equal_nan': True, 'err_msg': '', 'header': 'Not equal to tolerance rtol=1e-07, atol=0', 'verbose': True}

E           AssertionError: 
E           Not equal to tolerance rtol=1e-07, atol=0
E           
E           Mismatched elements: 9 / 9 (100%)
E           Max absolute difference: 0.001
E           Max relative difference: 0.00027652
E            x: array([[-1.084822e+01,  1.000000e-03,  1.085022e+01],
E                  [-1.446463e+01,  1.085022e+01, -1.808104e+01],
E                  [-7.231814e+00,  3.617407e+00, -7.231814e+00]])
E            y: array([[-10.849222,   0.      ,  10.849222],
E                  [-14.465629,  10.849222, -18.082036],
E                  [ -7.232814,   3.616407,  -7.232814]])

aa84b65 fix return type doc str indentation
6f4bb28 don't use pytest.approx() with assert len(...)
b12e7eb use numpy.testing.assert_allclose over assert np.allclose
e025ffb np.allclose(np.array(...), ...) to np.allclose(..., ...)

@janosh janosh added tests Issues with or changes to the pymatgen test suite dx Developer experience labels Aug 18, 2023
@janosh janosh force-pushed the np-assert-allcose branch from fc526a3 to b12e7eb Compare August 18, 2023 16:35
@janosh janosh enabled auto-merge (squash) August 18, 2023 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dx Developer experience tests Issues with or changes to the pymatgen test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant