metoppv / improver

IMPROVER is a library of algorithms for meteorological post-processing.
http://improver.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
103 stars 85 forks source link

Fix unit test failures for NumPy 1.21 #1522

Open dmentipl opened 3 years ago

dmentipl commented 3 years ago

As part of the update to using Iris 3 (see PR #1507) I discovered new unit test failures in changing from NumPy 1.20.* to 1.21 (see here for where they were originally raised). There are four unit test failures in improver_tests.nbhood.circular_kernel.

These test failures stop us from using the latest NumPy (1.21). I did some investigation (see here). However, they require further investigation.

Related issues: #1437, #1507, #1517

Here is the pytest output (for the Python 3.8 env; the errors are the same for the Python 3.7 env):

```Python traceback ================================================ FAILURES ================================================= ___________________________ Test_pad_and_unpad_cube.test_single_point_on_corner ___________________________ [gw0] linux -- Python 3.8.10 /home/dmentipl/conda/envs/improver_py38_iris30/bin/python self = def test_single_point_on_corner(self): """Test behaviour for a single non-zero grid cell on the corner.""" expected = np.array( [ [ [0.0, 0.4, 1.0, 1.0, 1.0], [0.4, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], [ [0.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], ] ) self.cube.data[0, 0] = 0 # Point is right on the corner. percentiles = np.array([10, 50, 90]) kernel = np.array([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]]) result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).pad_and_unpad_cube(self.cube, kernel) > self.assertArrayAlmostEqual(result.data, expected) improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py:347: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = a = array([[[0. , 0.4, 1. , 1. , 1. ], [0.4, 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ], [1. ,... [1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ]]], dtype=float32) b = array([[[0. , 0.4, 1. , 1. , 1. ], [0.4, 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ], [1. ,..., 1. , 1. ], [1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ]]]) decimal = 6 def assertArrayAlmostEqual(self, a, b, decimal=6): > np.testing.assert_array_almost_equal(a, b, decimal=decimal) E AssertionError: E Arrays are not almost equal to 6 decimals E E Mismatched elements: 1 / 75 (1.33%) E Max absolute difference: 1. E Max relative difference: 0.00000001 E x: array([[[0. , 0.4, 1. , 1. , 1. ], E [0.4, 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 1. , 1. ],... E y: array([[[0. , 0.4, 1. , 1. , 1. ], E [0.4, 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 1. , 1. ],... ../../conda/envs/improver_py38_iris30/lib/python3.8/site-packages/iris/tests/__init__.py:670: AssertionError _______________________ Test_run.test_number_of_percentiles_equals_number_of_points _______________________ [gw0] linux -- Python 3.8.10 /home/dmentipl/conda/envs/improver_py38_iris30/bin/python self = def test_number_of_percentiles_equals_number_of_points(self): """Test when the number of percentiles is equal to the number of points used to construct the percentiles.""" expected = np.array( [ [ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 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, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], ], ] ) data = np.ones((7, 7), dtype=np.float32) data[3, 3] = 0 cube = set_up_variable_cube(data, spatial_grid="equalarea",) percentiles = np.array([5, 10, 15, 20, 25]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).run(cube, radius) > self.assertArrayAlmostEqual(result.data, expected) improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py:715: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = a = array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 0.2,..., 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ]]], dtype=float32) b = array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 0.2,..., 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ]]]) decimal = 6 def assertArrayAlmostEqual(self, a, b, decimal=6): > np.testing.assert_array_almost_equal(a, b, decimal=decimal) E AssertionError: E Arrays are not almost equal to 6 decimals E E Mismatched elements: 1 / 245 (0.408%) E Max absolute difference: 0.6 E Max relative difference: 1.5 E x: array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 0.2, 1. , 1. , 1. ],... E y: array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 0.2, 1. , 1. , 1. ],... ../../conda/envs/improver_py38_iris30/lib/python3.8/site-packages/iris/tests/__init__.py:670: AssertionError ______________________ Test_run.test_number_of_points_half_of_number_of_percentiles _______________________ [gw0] linux -- Python 3.8.10 /home/dmentipl/conda/envs/improver_py38_iris30/bin/python self = def test_number_of_points_half_of_number_of_percentiles(self): """Test when the number of points is half the number of percentiles.""" expected = np.array( [ [ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 0.1, 1.0, 1.0, 1.0], [1.0, 1.0, 0.1, 0.1, 0.1, 1.0, 1.0], [1.0, 1.0, 1.0, 0.1, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], ], [ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 0.2, 1.0, 1.0, 1.0], [1.0, 1.0, 0.2, 0.2, 0.2, 1.0, 1.0], [1.0, 1.0, 1.0, 0.2, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], ], [ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 0.3, 1.0, 1.0, 1.0], [1.0, 1.0, 0.3, 0.3, 0.3, 1.0, 1.0], [1.0, 1.0, 1.0, 0.3, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0,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, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 0.8, 1.0, 1.0, 1.0], [1.0, 1.0, 0.8, 0.8, 0.8, 1.0, 1.0], [1.0, 1.0, 1.0, 0.8, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], ], [ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 0.9, 1.0, 1.0, 1.0], [1.0, 1.0, 0.9, 0.9, 0.9, 1.0, 1.0], [1.0, 1.0, 1.0, 0.9, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], ], [ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], ], ] ) data = np.ones((7, 7), dtype=np.float32) data[3, 3] = 0 cube = set_up_variable_cube(data, spatial_grid="equalarea",) percentiles = np.array([2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).run(cube, radius) > self.assertArrayAlmostEqual(result.data, expected) improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py:824: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = a = array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 0.1,..., 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ]]], dtype=float32) b = array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 0.1,..., 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. , 1. , 1. ]]]) decimal = 6 def assertArrayAlmostEqual(self, a, b, decimal=6): > np.testing.assert_array_almost_equal(a, b, decimal=decimal) E AssertionError: E Arrays are not almost equal to 6 decimals E E Mismatched elements: 9 / 490 (1.84%) E Max absolute difference: 0.8 E Max relative difference: 4. E x: array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 0.1, 1. , 1. , 1. ],... E y: array([[[1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 1. , 0.1, 1. , 1. , 1. ],... ../../conda/envs/improver_py38_iris30/lib/python3.8/site-packages/iris/tests/__init__.py:670: AssertionError _______________________________ Test_run.test_single_point_low_percentiles ________________________________ [gw0] linux -- Python 3.8.10 /home/dmentipl/conda/envs/improver_py38_iris30/bin/python self = def test_single_point_low_percentiles(self): """Test behaviour with low percentiles.""" expected = np.array( [ [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 0.2, 1.0, 1.0], [1.0, 0.2, 0.2, 0.2, 1.0], [1.0, 1.0, 0.2, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 0.4, 1.0, 1.0], [1.0, 0.4, 0.4, 0.4, 1.0], [1.0, 1.0, 0.4, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 0.8, 1.0, 1.0], [1.0, 0.8, 0.8, 0.8, 1.0], [1.0, 1.0, 0.8, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], ] ) self.cube.data[2, 2] = 0 percentiles = np.array([5, 10, 20]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).run(self.cube, radius) > self.assertArrayAlmostEqual(result.data, expected) improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py:613: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = a = array([[[1. , 1. , 1. , 1. , 1. ], [1. , 1. , 0.2, 1. , 1. ], [1. , 0.2, 0.2, 0.2, 1. ], [1. ,... [1. , 0.8, 0.8, 0.8, 1. ], [1. , 1. , 0.8, 1. , 1. ], [1. , 1. , 1. , 1. , 1. ]]], dtype=float32) b = array([[[1. , 1. , 1. , 1. , 1. ], [1. , 1. , 0.2, 1. , 1. ], [1. , 0.2, 0.2, 0.2, 1. ], [1. ,..., 1. , 1. ], [1. , 0.8, 0.8, 0.8, 1. ], [1. , 1. , 0.8, 1. , 1. ], [1. , 1. , 1. , 1. , 1. ]]]) decimal = 6 def assertArrayAlmostEqual(self, a, b, decimal=6): > np.testing.assert_array_almost_equal(a, b, decimal=decimal) E AssertionError: E Arrays are not almost equal to 6 decimals E E Mismatched elements: 2 / 75 (2.67%) E Max absolute difference: 0.6 E Max relative difference: 1.5 E x: array([[[1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 0.2, 1. , 1. ], E [1. , 0.2, 0.2, 0.2, 1. ],... E y: array([[[1. , 1. , 1. , 1. , 1. ], E [1. , 1. , 0.2, 1. , 1. ], E [1. , 0.2, 0.2, 0.2, 1. ],... ../../conda/envs/improver_py38_iris30/lib/python3.8/site-packages/iris/tests/__init__.py:670: AssertionError ========================================= short test summary info ========================================= FAILED improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py::Test_pad_and_unpad_cube::test_single_point_on_corner FAILED improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py::Test_run::test_number_of_percentiles_equals_number_of_points FAILED improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py::Test_run::test_number_of_points_half_of_number_of_percentiles FAILED improver_tests/nbhood/circular_kernel/test_GeneratePercentilesFromACircularNeighbourhood.py::Test_run::test_single_point_low_percentiles ================= 4 failed, 2428 passed, 319 skipped, 1 xfailed, 3568 warnings in 48.88s ================== ```
cpelley commented 12 months ago

Is this issue fixed?