From bcaca28a6a14cf62f30e034db5b6aa02e1029303 Mon Sep 17 00:00:00 2001 From: Martin Yeo Date: Tue, 19 Mar 2024 13:52:07 +0000 Subject: [PATCH] Remove np.testing. --- lib/iris/tests/test_analysis.py | 66 ++++++++++++------------ lib/iris/tests/test_analysis_calculus.py | 30 +++++------ 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/lib/iris/tests/test_analysis.py b/lib/iris/tests/test_analysis.py index 258ba66f0d..abc68c06b5 100644 --- a/lib/iris/tests/test_analysis.py +++ b/lib/iris/tests/test_analysis.py @@ -278,7 +278,7 @@ def test_weighted_mean(self): # check it's a 0d, scalar cube assert g.shape == () # check the value - pp_area_avg's result of 287.927 differs by factor of 1.00002959 - np.testing.assert_approx_equal(g.data, 287.935, significant=5) + _shared_utils.assert_array_almost_equal(g.data, 287.935, decimal=2) # check we get summed weights even if we don't give any h, summed_weights = e.collapsed("latitude", iris.analysis.MEAN, returned=True) @@ -413,24 +413,24 @@ def _setup(self): def test_max(self): cube = self.cube_with_nan.collapsed("foo", iris.analysis.MAX) - np.testing.assert_array_equal(cube.data, np.array([3, np.nan, np.nan])) + _shared_utils.assert_array_equal(cube.data, np.array([3, np.nan, np.nan])) cube = self.cube_with_mask.collapsed("foo", iris.analysis.MAX) - np.testing.assert_array_equal(cube.data, np.array([3, 7, 9])) + _shared_utils.assert_array_equal(cube.data, np.array([3, 7, 9])) def test_min(self): cube = self.cube_with_nan.collapsed("foo", iris.analysis.MIN) - np.testing.assert_array_equal(cube.data, np.array([0, np.nan, np.nan])) + _shared_utils.assert_array_equal(cube.data, np.array([0, np.nan, np.nan])) cube = self.cube_with_mask.collapsed("foo", iris.analysis.MIN) - np.testing.assert_array_equal(cube.data, np.array([0, 5, 8])) + _shared_utils.assert_array_equal(cube.data, np.array([0, 5, 8])) def test_sum(self): cube = self.cube_with_nan.collapsed("foo", iris.analysis.SUM) - np.testing.assert_array_equal(cube.data, np.array([6, np.nan, np.nan])) + _shared_utils.assert_array_equal(cube.data, np.array([6, np.nan, np.nan])) cube = self.cube_with_mask.collapsed("foo", iris.analysis.SUM) - np.testing.assert_array_equal(cube.data, np.array([6, 18, 17])) + _shared_utils.assert_array_equal(cube.data, np.array([6, 18, 17])) class TestAuxCoordCollapse: @@ -444,12 +444,12 @@ def _setup(self): def test_max(self): cube = self.cube_with_aux_coord.collapsed("grid_latitude", iris.analysis.MAX) - np.testing.assert_array_equal( + _shared_utils.assert_array_equal( cube.coord("surface_altitude").points, np.array([112, 113, 114, 115, 116, 117]), ) - np.testing.assert_array_equal( + _shared_utils.assert_array_equal( cube.coord("surface_altitude").bounds, np.array( [ @@ -466,22 +466,22 @@ def test_max(self): # Check collapsing over the whole coord still works cube = self.cube_with_aux_coord.collapsed("altitude", iris.analysis.MAX) - np.testing.assert_array_equal( + _shared_utils.assert_array_equal( cube.coord("surface_altitude").points, np.array([114]) ) - np.testing.assert_array_equal( + _shared_utils.assert_array_equal( cube.coord("surface_altitude").bounds, np.array([[100, 129]]) ) cube = self.cube_with_aux_coord.collapsed("grid_longitude", iris.analysis.MAX) - np.testing.assert_array_equal( + _shared_utils.assert_array_equal( cube.coord("surface_altitude").points, np.array([102, 108, 114, 120, 126]), ) - np.testing.assert_array_equal( + _shared_utils.assert_array_equal( cube.coord("surface_altitude").bounds, np.array([[100, 105], [106, 111], [112, 117], [118, 123], [124, 129]]), ) @@ -567,7 +567,7 @@ def _check_collapsed_percentile( percent=percents, **kwargs, ) - np.testing.assert_array_almost_equal(result.data, expected_result) + _shared_utils.assert_array_almost_equal(result.data, expected_result) assert type(result.data) is cube_data_type if CML_filename is not None: _shared_utils.assert_CML( @@ -576,7 +576,7 @@ def _check_collapsed_percentile( def _check_percentile(self, data, axis, percents, expected_result, **kwargs): result = iris.analysis._percentile(data, axis, percents, **kwargs) - np.testing.assert_array_almost_equal(result, expected_result) + _shared_utils.assert_array_almost_equal(result, expected_result) assert type(result) is type(expected_result) def test_percentile_1d_25_percent(self): @@ -842,7 +842,7 @@ def test_proportion(self): gt5 = cube.collapsed( "foo", iris.analysis.PROPORTION, function=lambda val: val >= 5 ) - np.testing.assert_array_almost_equal(gt5.data, np.array([6 / 11.0])) + _shared_utils.assert_array_almost_equal(gt5.data, np.array([6 / 11.0])) _shared_utils.assert_CML( self.request, gt5, ("analysis", "proportion_foo_1d.cml"), checksum=False ) @@ -853,7 +853,7 @@ def test_proportion_2d(self): gt6 = cube.collapsed( "foo", iris.analysis.PROPORTION, function=lambda val: val >= 6 ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( gt6.data, np.array([0, 0.5, 1], dtype=np.float32) ) _shared_utils.assert_CML( @@ -863,7 +863,7 @@ def test_proportion_2d(self): gt6 = cube.collapsed( "bar", iris.analysis.PROPORTION, function=lambda val: val >= 6 ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( gt6.data, np.array([1 / 3, 1 / 3, 2 / 3, 2 / 3], dtype=np.float32) ) _shared_utils.assert_CML( @@ -875,7 +875,7 @@ def test_proportion_2d(self): iris.analysis.PROPORTION, function=lambda val: val >= 6, ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( gt6.data, np.array([0.5], dtype=np.float32) ) _shared_utils.assert_CML( @@ -888,7 +888,7 @@ def test_proportion_2d(self): gt6_masked = cube.collapsed( "bar", iris.analysis.PROPORTION, function=lambda val: val >= 6 ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( gt6_masked.data, ma.array( [1 / 3, None, 1 / 2, None], @@ -906,7 +906,7 @@ def test_proportion_2d(self): def test_count(self): cube = stock.simple_1d() gt5 = cube.collapsed("foo", iris.analysis.COUNT, function=lambda val: val >= 5) - np.testing.assert_array_almost_equal(gt5.data, np.array([6])) + _shared_utils.assert_array_almost_equal(gt5.data, np.array([6])) gt5.data = gt5.data.astype("i8") _shared_utils.assert_CML( self.request, gt5, ("analysis", "count_foo_1d.cml"), checksum=False @@ -916,7 +916,7 @@ def test_count_2d(self): cube = stock.simple_2d() gt6 = cube.collapsed("foo", iris.analysis.COUNT, function=lambda val: val >= 6) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( gt6.data, np.array([0, 2, 4], dtype=np.float32) ) gt6.data = gt6.data.astype("i8") @@ -925,7 +925,7 @@ def test_count_2d(self): ) gt6 = cube.collapsed("bar", iris.analysis.COUNT, function=lambda val: val >= 6) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( gt6.data, np.array([1, 1, 2, 2], dtype=np.float32) ) gt6.data = gt6.data.astype("i8") @@ -936,7 +936,9 @@ def test_count_2d(self): gt6 = cube.collapsed( ("foo", "bar"), iris.analysis.COUNT, function=lambda val: val >= 6 ) - np.testing.assert_array_almost_equal(gt6.data, np.array([6], dtype=np.float32)) + _shared_utils.assert_array_almost_equal( + gt6.data, np.array([6], dtype=np.float32) + ) gt6.data = gt6.data.astype("i8") _shared_utils.assert_CML( self.request, gt6, ("analysis", "count_foo_bar_2d.cml"), checksum=False @@ -1738,28 +1740,28 @@ def test_init_with_str_dim_coord(self): # DimCoord always realizes points assert isinstance(weights.array, np.ndarray) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, [[0, 0, 0], [1, 1, 1]]) + _shared_utils.assert_array_equal(weights.array, [[0, 0, 0], [1, 1, 1]]) assert weights.units == "degrees" def test_init_with_str_aux_coord(self): weights = _Weights("auxcoord", self.cube) assert isinstance(weights.array, self.target_type) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, [[3, 3, 3], [4, 4, 4]]) + _shared_utils.assert_array_equal(weights.array, [[3, 3, 3], [4, 4, 4]]) assert weights.units == "s" def test_init_with_str_ancillary_variable(self): weights = _Weights("ancvar", self.cube) assert isinstance(weights.array, self.target_type) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, [[5, 6, 7], [5, 6, 7]]) + _shared_utils.assert_array_equal(weights.array, [[5, 6, 7], [5, 6, 7]]) assert weights.units == "kg" def test_init_with_str_cell_measure(self): weights = _Weights("cell_area", self.cube) assert isinstance(weights.array, self.target_type) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, self.data) + _shared_utils.assert_array_equal(weights.array, self.data) assert weights.units == "m2" def test_init_with_dim_coord(self): @@ -1767,28 +1769,28 @@ def test_init_with_dim_coord(self): # DimCoord always realizes points assert isinstance(weights.array, np.ndarray) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, [[0, 0, 0], [1, 1, 1]]) + _shared_utils.assert_array_equal(weights.array, [[0, 0, 0], [1, 1, 1]]) assert weights.units == "degrees" def test_init_with_aux_coord(self): weights = _Weights(self.aux_coord, self.cube) assert isinstance(weights.array, self.target_type) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, [[3, 3, 3], [4, 4, 4]]) + _shared_utils.assert_array_equal(weights.array, [[3, 3, 3], [4, 4, 4]]) assert weights.units == "s" def test_init_with_ancillary_variable(self): weights = _Weights(self.ancillary_variable, self.cube) assert isinstance(weights.array, self.target_type) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, [[5, 6, 7], [5, 6, 7]]) + _shared_utils.assert_array_equal(weights.array, [[5, 6, 7], [5, 6, 7]]) assert weights.units == "kg" def test_init_with_cell_measure(self): weights = _Weights(self.cell_measure, self.cube) assert isinstance(weights.array, self.target_type) assert isinstance(weights.units, cf_units.Unit) - np.testing.assert_array_equal(weights.array, self.data) + _shared_utils.assert_array_equal(weights.array, self.data) assert weights.units == "m2" def test_init_with_list(self): diff --git a/lib/iris/tests/test_analysis_calculus.py b/lib/iris/tests/test_analysis_calculus.py index 256b3839a5..74e0f90d8e 100644 --- a/lib/iris/tests/test_analysis_calculus.py +++ b/lib/iris/tests/test_analysis_calculus.py @@ -202,18 +202,18 @@ def test_sin(self): sin_of_coord_radians = iris.analysis.calculus._coord_sin(self.rlat) # Check the values are correct (within a tolerance) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( np.sin(self.rlat.points), sin_of_coord.points ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( np.sin(self.rlat.bounds), sin_of_coord.bounds ) # Check that the results of the sin function are almost equal when operating on a coord with degrees and radians - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( sin_of_coord.points, sin_of_coord_radians.points ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( sin_of_coord.bounds, sin_of_coord_radians.bounds ) @@ -225,18 +225,18 @@ def test_cos(self): cos_of_coord_radians = iris.analysis.calculus._coord_cos(self.rlat) # Check the values are correct (within a tolerance) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( np.cos(self.rlat.points), cos_of_coord.points ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( np.cos(self.rlat.bounds), cos_of_coord.bounds ) # Check that the results of the cos function are almost equal when operating on a coord with degrees and radians - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( cos_of_coord.points, cos_of_coord_radians.points ) - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( cos_of_coord.bounds, cos_of_coord_radians.bounds ) @@ -590,7 +590,7 @@ def test_contrived_differential1(self): data = -sin_x_pts * y_ones result = df_dlon.copy(data=data) - np.testing.assert_array_almost_equal(result.data, df_dlon.data, decimal=3) + _shared_utils.assert_array_almost_equal(result.data, df_dlon.data, decimal=3) def test_contrived_differential2(self): # testing : @@ -607,7 +607,7 @@ def test_contrived_differential2(self): x_pts, x_ones, y_pts, y_ones, z_pts, z_ones = self.get_coord_pts(r) result = r.copy(data=y_pts * 2.0 * x_ones * z_ones) - np.testing.assert_array_almost_equal(result.data, r.data, decimal=6) + _shared_utils.assert_array_almost_equal(result.data, r.data, decimal=6) def test_contrived_non_spherical_curl1(self): # testing : @@ -662,16 +662,16 @@ def test_contrived_non_spherical_curl2(self): # result.data = y_pts * 2. * x_ones * z_ones # print(repr(r[0].data[0:1, 0:5, 0:25:5])) # print(repr(result.data[0:1, 0:5, 0:25:5])) - # np.testing.assert_array_almost_equal(result.data, r[0].data, decimal=2) + # _shared_utils.assert_array_almost_equal(result.data, r[0].data, decimal=2) # # result = r[1].copy(data=True) # x_pts, x_ones, y_pts, y_ones, z_pts, z_ones = self.get_coord_pts(result) # result.data = pow(z_pts, 2) * x_ones * y_ones - # np.testing.assert_array_almost_equal(result.data, r[1].data, decimal=6) + # _shared_utils.assert_array_almost_equal(result.data, r[1].data, decimal=6) result = r[2].copy() result.data = result.data * 0 + 1 - np.testing.assert_array_almost_equal(result.data, r[2].data, decimal=4) + _shared_utils.assert_array_almost_equal(result.data, r[2].data, decimal=4) _shared_utils.assert_CML( self.request, @@ -705,7 +705,7 @@ def test_contrived_spherical_curl1(self): result = r.copy(data=r.data * 0) # Note: This numerical comparison was created when the radius was 1000 times smaller - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( result.data[5:-5], r.data[5:-5] / 1000.0, decimal=1 ) _shared_utils.assert_CML( @@ -757,7 +757,7 @@ def test_contrived_spherical_curl2(self): result = r.copy(data=-2 * cos_x_pts * cos_y_pts) # Note: This numerical comparison was created when the radius was 1000 times smaller - np.testing.assert_array_almost_equal( + _shared_utils.assert_array_almost_equal( result.data[30:-30, :], r.data[30:-30, :] / 1000.0, decimal=1 ) _shared_utils.assert_CML(