Skip to content

Commit

Permalink
Implemented unit test changes suggested in the latest first review
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwhitemet committed Feb 17, 2025
1 parent a751771 commit b42be72
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions improver/utilities/mathematical_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def _add_reference_epoch_metadata(output_cube, diagnostic_cube, mean_cube) -> Cu
reference_epoch = iris.coords.AuxCoord(
points=mean_cube.coord("time").points,
bounds=mean_cube.coord("time").bounds
if diagnostic_cube.coord("time").has_bounds()
if mean_cube.coord("time").has_bounds()
else None,
long_name="reference_epoch",
units=mean_cube.coord("time").units,
Expand All @@ -566,7 +566,7 @@ def _add_reference_epoch_metadata(output_cube, diagnostic_cube, mean_cube) -> Cu
output_cube.add_aux_coord(reference_epoch)

cell_method = CellMethod(
method="anomaly", coords="reference_epoch"
method="anomaly", coords=["reference_epoch"]
) # Create a cell method to describe the operation that was performed
output_cube.add_cell_method(cell_method)

Expand Down
51 changes: 43 additions & 8 deletions improver_tests/utilities/test_CalculateClimateAnomalies.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ def test_calculate_unstandardised_anomalies_gridded_data(
np.testing.assert_allclose(result.data, expected_anomalies, rtol=1e-5)
assert result.name() == diagnostic_cube.name() + "_anomaly"
assert result.units == "K"
assert "reference_epoch" in [coord.name() for coord in result.coords()]
assert result.coord("reference_epoch")
assert result.coord("reference_epoch").points == mean_cube.coord("time").points
assert np.array_equal(
result.coord("reference_epoch").bounds, mean_cube.coord("time").bounds
)
assert result.coord("reference_epoch").units == mean_cube.coord("time").units
assert (
"(CellMethod(method='anomaly', coord_names=('reference_epoch',), intervals=(), comments=())"
in str(result.cell_methods)
Expand All @@ -177,6 +182,12 @@ def test_calculate_standardised_anomalies_gridded_data(
assert result.long_name == diagnostic_cube.name() + "_standard_anomaly"
assert result.units == "1"
assert "reference_epoch" in [coord.name() for coord in result.coords()]
assert result.coord("reference_epoch")
assert result.coord("reference_epoch").points == mean_cube.coord("time").points
assert np.array_equal(
result.coord("reference_epoch").bounds, mean_cube.coord("time").bounds
)
assert result.coord("reference_epoch").units == mean_cube.coord("time").units
assert (
"(CellMethod(method='anomaly', coord_names=('reference_epoch',), intervals=(), comments=())"
in str(result.cell_methods)
Expand All @@ -193,6 +204,11 @@ def test_calculate_unstandardised_anomalies_site_data(site_cubes):
assert result.name() == site_cube_diagnostic.name() + "_anomaly"
assert result.units == "K"
assert "reference_epoch" in [coord.name() for coord in result.coords()]
assert result.coord("reference_epoch").points == site_cube_mean.coord("time").points
assert np.array_equal(
result.coord("reference_epoch").bounds, site_cube_mean.coord("time").bounds
)
assert result.coord("reference_epoch").units == site_cube_mean.coord("time").units
assert (
"(CellMethod(method='anomaly', coord_names=('reference_epoch',), intervals=(), comments=())"
in str(result.cell_methods)
Expand All @@ -202,28 +218,47 @@ def test_calculate_unstandardised_anomalies_site_data(site_cubes):
def test_calculate_standardised_anomalies_site_data(site_cubes):
"""Test that the plugin calculates standardised anomalies correctly for site data."""
plugin = CalculateClimateAnomalies()
site_cube_diagnostic, _, _ = site_cubes
result = plugin.process(*site_cubes)
site_cube_diagnostic, site_cube_mean, site_cube_variance = site_cubes
result = plugin.process(site_cube_diagnostic, site_cube_mean, site_cube_variance)
expected_anomalies = np.array([3.5], dtype=np.float32)
np.testing.assert_allclose(result.data, expected_anomalies, rtol=1e-5)
assert result.long_name == site_cube_diagnostic.name() + "_standard_anomaly"
assert result.units == "1"
assert "reference_epoch" in [coord.name() for coord in result.coords()]
assert result.coord("reference_epoch").points == site_cube_mean.coord("time").points
assert np.array_equal(
result.coord("reference_epoch").bounds, site_cube_mean.coord("time").bounds
)
assert result.coord("reference_epoch").units == site_cube_mean.coord("time").units
assert (
"(CellMethod(method='anomaly', coord_names=('reference_epoch',), intervals=(), comments=())"
in str(result.cell_methods)
)


def test_ignore_temporal_mismatch(diagnostic_cube, mean_cube, variance_cube):
"""Test that the verify_time_coords_match() function handles requests to ignore temporal mismatch
"""Test that the plugin handles requests to ignore temporal mismatch
between diagnostic cube and mean/variance cube.
"""
plugin = CalculateClimateAnomalies(ignore_temporal_mismatch=True)
diagnostic_cube.coord("time").points = (
diagnostic_cube.coord("time").points + 10 * SECONDS_IN_HOUR
) # Moves diagnostic bounds outside mean bounds
plugin.verify_time_coords_match(diagnostic_cube, mean_cube, variance_cube)

plugin = CalculateClimateAnomalies(ignore_temporal_mismatch=True)
result = plugin.process(diagnostic_cube, mean_cube, variance_cube)

assert result.long_name == diagnostic_cube.name() + "_standard_anomaly"
assert result.units == "1"
assert result.coord("reference_epoch")
assert result.coord("reference_epoch").points == mean_cube.coord("time").points
assert np.array_equal(
result.coord("reference_epoch").bounds, mean_cube.coord("time").bounds
)
assert result.coord("reference_epoch").units == mean_cube.coord("time").units
assert (
"(CellMethod(method='anomaly', coord_names=('reference_epoch',), intervals=(), comments=())"
in str(result.cell_methods)
)


## Testing the plugin's internal verification checks
Expand Down Expand Up @@ -252,10 +287,10 @@ def test_error_spatial_coords_mismatch_gridded_data(
diagnostic_cube, mean_cube, variance_cube
):
"""Test that the plugin raises a ValueError if the spatial coordinates of the diagnostic cube and another cube mismatch"""
mean_cube.coord("latitude").points = mean_cube.coord("latitude").points + 20
mean_cube.coord("longitude").points = mean_cube.coord("longitude").points + 20
plugin = CalculateClimateAnomalies()
with pytest.raises(ValueError, match="The spatial coordinates must match."):
mean_cube.coord("latitude").points = mean_cube.coord("latitude").points + 20
mean_cube.coord("longitude").points = mean_cube.coord("longitude").points + 20
plugin.verify_spatial_coords_match(diagnostic_cube, mean_cube, variance_cube)


Expand Down

0 comments on commit b42be72

Please sign in to comment.