Skip to content

Commit

Permalink
convert tests to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer committed Jan 16, 2024
1 parent b49f7e2 commit a678716
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions lib/iris/tests/unit/analysis/stats/test_pearsonr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import numpy as np
import numpy.ma as ma
import pytest

import iris
import iris._lazy_data
Expand All @@ -18,7 +19,7 @@


class Mixin:
def setUp(self):
def setup_method(self):
# 3D cubes:
cube_temp = iris.load_cube(
tests.get_data_path(
Expand All @@ -35,18 +36,18 @@ def setUp(self):


@tests.skip_data
class TestLazy(Mixin, tests.IrisTest):
class TestLazy(Mixin):
def test_perfect_corr(self):
r = stats.pearsonr(self.cube_a, self.cube_a, ["latitude", "longitude"])
self.assertArrayEqual(r.data, np.array([1.0] * 6))
np.testing.assert_array_equal(r.data, np.array([1.0] * 6))

def test_perfect_corr_all_dims(self):
r = stats.pearsonr(self.cube_a, self.cube_a)
self.assertArrayEqual(r.data, np.array([1.0]))
np.testing.assert_array_equal(r.data, np.array([1.0]))

def test_compatible_cubes(self):
r = stats.pearsonr(self.cube_a, self.cube_b, ["latitude", "longitude"])
self.assertArrayAlmostEqual(
np.testing.assert_array_almost_equal(
r.data,
[
0.81114936,
Expand All @@ -73,14 +74,14 @@ def test_broadcast_cubes(self):
).data
for i in range(6)
]
self.assertArrayEqual(r1.data, np.array(r_by_slice))
self.assertArrayEqual(r2.data, np.array(r_by_slice))
np.testing.assert_array_equal(r1.data, np.array(r_by_slice))
np.testing.assert_array_equal(r2.data, np.array(r_by_slice))

def test_compatible_cubes_weighted(self):
r = stats.pearsonr(
self.cube_a, self.cube_b, ["latitude", "longitude"], self.weights
)
self.assertArrayAlmostEqual(
np.testing.assert_array_almost_equal(
r.data,
[
0.79105429,
Expand Down Expand Up @@ -108,7 +109,7 @@ def test_broadcast_cubes_weighted(self):
).data
for i in range(6)
]
self.assertArrayAlmostEqual(r.data, np.array(r_by_slice))
np.testing.assert_array_almost_equal(r.data, np.array(r_by_slice))

def test_broadcast_transpose_cubes_weighted(self):
# Reference is calculated with no transposition.
Expand All @@ -128,10 +129,10 @@ def test_broadcast_transpose_cubes_weighted(self):
)

# Should get the same result, but transposed.
self.assertArrayAlmostEqual(r_test.data, r_ref.data.T)
np.testing.assert_array_almost_equal(r_test.data, r_ref.data.T)

def test_weight_error(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
stats.pearsonr(
self.cube_a,
self.cube_b[0, :, :],
Expand All @@ -144,14 +145,14 @@ def test_mdtol(self):
cube_small_masked = iris.util.mask_cube(cube_small, [0, 0, 0, 1, 1, 1])
r1 = stats.pearsonr(cube_small, cube_small_masked)
r2 = stats.pearsonr(cube_small, cube_small_masked, mdtol=0.49)
self.assertArrayAlmostEqual(r1.data, np.array([0.74586593]))
self.assertMaskedArrayEqual(r2.data, ma.array([0], mask=[True]))
np.testing.assert_array_almost_equal(r1.data, np.array([0.74586593]))
tests.assert_masked_array_equal(r2.data, ma.array([0], mask=[True]))

def test_common_mask_simple(self):
cube_small = self.cube_a[:, 0, 0]
cube_small_masked = iris.util.mask_cube(cube_small, [0, 0, 0, 1, 1, 1])
r = stats.pearsonr(cube_small, cube_small_masked, common_mask=True)
self.assertArrayAlmostEqual(r.data, np.array([1.0]))
np.testing.assert_array_almost_equal(r.data, np.array([1.0]))

def test_common_mask_broadcast(self):
cube_small = iris.util.mask_cube(self.cube_a[:, 0, 0], [0, 0, 0, 0, 0, 1])
Expand All @@ -173,11 +174,11 @@ def test_common_mask_broadcast(self):
weights=self.weights[:, 0, 0],
common_mask=True,
)
self.assertArrayAlmostEqual(r.data, np.array([1.0, 1.0]))
np.testing.assert_array_almost_equal(r.data, np.array([1.0, 1.0]))
# 2d mask does not vary on unshared coord:
cube_small_2d.data.mask[0, 0] = 1
r = stats.pearsonr(cube_small, cube_small_2d, common_mask=True)
self.assertArrayAlmostEqual(r.data, np.array([1.0, 1.0]))
np.testing.assert_array_almost_equal(r.data, np.array([1.0, 1.0]))


class TestReal(TestLazy):
Expand All @@ -187,24 +188,20 @@ def setUp(self):
_ = cube.data


class TestCoordHandling(Mixin, tests.IrisTest):
class TestCoordHandling(Mixin):
def test_lenient_handling(self):
# Smoke test that mismatched var_name does not prevent operation.
self.cube_a.coord("time").var_name = "wibble"
stats.pearsonr(self.cube_a, self.cube_b)

def test_incompatible_cubes(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
stats.pearsonr(self.cube_a[:, 0, :], self.cube_b[0, :, :], "longitude")

def test_single_coord(self):
# Smoke test that single coord can be passed as single string.
stats.pearsonr(self.cube_a, self.cube_b, "latitude")

def test_non_existent_coord(self):
with self.assertRaises(CoordinateNotFoundError):
with pytest.raises(CoordinateNotFoundError):
stats.pearsonr(self.cube_a, self.cube_b, "bad_coord")


if __name__ == "__main__":
tests.main()

0 comments on commit a678716

Please sign in to comment.