Skip to content

Commit

Permalink
Remove np.testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
trexfeathers committed Mar 19, 2024
1 parent 49f7cdb commit bcaca28
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
66 changes: 34 additions & 32 deletions lib/iris/tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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(
[
Expand All @@ -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]]),
)
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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
)
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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],
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -1738,57 +1740,57 @@ 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):
weights = _Weights(self.lat, self.cube)
# 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):
Expand Down
30 changes: 15 additions & 15 deletions lib/iris/tests/test_analysis_calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)

Expand Down Expand Up @@ -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 :
Expand All @@ -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 :
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit bcaca28

Please sign in to comment.