Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder committed Oct 3, 2024
1 parent 5003db9 commit a12cf30
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 36 deletions.
120 changes: 95 additions & 25 deletions tests/test_temporal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import warnings

import cftime
import numpy as np
Expand Down Expand Up @@ -620,7 +621,7 @@ def test_weighted_seasonal_averages_with_DJF_without_dropping_incomplete_seasons
},
)

assert result.identical(expected)
xr.testing.assert_identical(result, expected)

def test_weighted_seasonal_averages_with_DJF_and_drop_incomplete_seasons(self):
ds = generate_dataset(decode_times=True, cf_compliant=True, has_bounds=True)
Expand Down Expand Up @@ -872,7 +873,7 @@ def test_weighted_custom_seasonal_averages_drops_incomplete_seasons(self):
},
)

assert result.identical(expected)
xr.testing.assert_identical(result, expected)

def test_weighted_custom_seasonal_averages_with_seasons_spanning_calendar_years(
self,
Expand Down Expand Up @@ -1151,15 +1152,15 @@ def test_raises_error_if_reference_period_arg_is_incorrect(self):
ds.temporal.climatology(
"ts",
"season",
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
reference_period=("01-01-2000", "01-01-2000"),
)

with pytest.raises(ValueError):
ds.temporal.climatology(
"ts",
"season",
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
reference_period=("01-01-2000"),
)

Expand All @@ -1169,7 +1170,7 @@ def test_subsets_climatology_based_on_reference_period(self):
result = ds.temporal.climatology(
"ts",
"season",
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
reference_period=("2000-01-01", "2000-06-01"),
)

Expand Down Expand Up @@ -1201,7 +1202,7 @@ def test_subsets_climatology_based_on_reference_period(self):
"freq": "season",
"weighted": "True",
"dec_mode": "DJF",
"drop_incomplete_djf": "True",
"drop_incomplete_seasons": "True",
},
)

Expand Down Expand Up @@ -1261,6 +1262,70 @@ def test_weighted_seasonal_climatology_with_DJF(self):

xr.testing.assert_identical(result, expected)

def test_raises_deprecation_warning_with_drop_incomplete_djf_season_config(self):
# NOTE: This will test will also cover the other public APIs that
# have drop_incomplete_djf as a season_config arg.
ds = self.ds.copy()

with warnings.catch_warnings(record=True) as w:
result = ds.temporal.climatology(
"ts",
"season",
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
)

assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert str(w[0].message) == (
"The `season_config` argument 'drop_incomplete_djf' is being deprecated. "
"Please use 'drop_incomplete_seasons' instead."
)

expected = ds.copy()
expected = expected.drop_dims("time")
expected_time = xr.DataArray(
data=np.array(
[
cftime.DatetimeGregorian(1, 1, 1),
cftime.DatetimeGregorian(1, 4, 1),
cftime.DatetimeGregorian(1, 7, 1),
cftime.DatetimeGregorian(1, 10, 1),
],
),
coords={
"time": np.array(
[
cftime.DatetimeGregorian(1, 1, 1),
cftime.DatetimeGregorian(1, 4, 1),
cftime.DatetimeGregorian(1, 7, 1),
cftime.DatetimeGregorian(1, 10, 1),
],
),
},
attrs={
"axis": "T",
"long_name": "time",
"standard_name": "time",
"bounds": "time_bnds",
},
)
expected["ts"] = xr.DataArray(
name="ts",
data=np.ones((4, 4, 4)),
coords={"lat": expected.lat, "lon": expected.lon, "time": expected_time},
dims=["time", "lat", "lon"],
attrs={
"operation": "temporal_avg",
"mode": "climatology",
"freq": "season",
"weighted": "True",
"drop_incomplete_seasons": "True",
"dec_mode": "DJF",
},
)

xr.testing.assert_identical(result, expected)

@requires_dask
def test_chunked_weighted_seasonal_climatology_with_DJF(self):
ds = self.ds.copy().chunk({"time": 2})
Expand Down Expand Up @@ -1468,7 +1533,7 @@ def test_weighted_custom_seasonal_climatology_with_seasons_spanning_calendar_yea

expected["ts"] = xr.DataArray(
name="ts",
data=np.ones((4, 4, 4)),
data=np.ones((1, 4, 4)),
coords={"lat": expected.lat, "lon": expected.lon, "time": expected_time},
dims=["time", "lat", "lon"],
attrs={
Expand All @@ -1481,7 +1546,7 @@ def test_weighted_custom_seasonal_climatology_with_seasons_spanning_calendar_yea
},
)

assert result.identical(expected)
xr.testing.assert_identical(result, expected)

def test_weighted_monthly_climatology(self):
result = self.ds.temporal.climatology("ts", "month")
Expand Down Expand Up @@ -1902,15 +1967,15 @@ def test_raises_error_if_reference_period_arg_is_incorrect(self):
ds.temporal.departures(
"ts",
"season",
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
reference_period=("01-01-2000", "01-01-2000"),
)

with pytest.raises(ValueError):
ds.temporal.departures(
"ts",
"season",
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
reference_period=("01-01-2000"),
)

Expand All @@ -1921,21 +1986,22 @@ def test_seasonal_departures_relative_to_climatology_reference_period(self):
"ts",
"season",
weighted=True,
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": False},
reference_period=("2000-01-01", "2000-06-01"),
)

expected = ds.copy()
expected = expected.drop_dims("time")
expected["ts"] = xr.DataArray(
name="ts",
data=np.array([[[0.0]], [[np.nan]], [[np.nan]], [[np.nan]]]),
data=np.array([[[0.0]], [[0.0]], [[np.nan]], [[np.nan]], [[0.0]]]),
coords={
"lat": expected.lat,
"lon": expected.lon,
"time": xr.DataArray(
data=np.array(
[
cftime.DatetimeGregorian(2000, 1, 1),
cftime.DatetimeGregorian(2000, 4, 1),
cftime.DatetimeGregorian(2000, 7, 1),
cftime.DatetimeGregorian(2000, 10, 1),
Expand All @@ -1959,7 +2025,7 @@ def test_seasonal_departures_relative_to_climatology_reference_period(self):
"freq": "season",
"weighted": "True",
"dec_mode": "DJF",
"drop_incomplete_djf": "True",
"drop_incomplete_seasons": "False",
},
)

Expand All @@ -1974,7 +2040,7 @@ def test_monthly_departures_relative_to_climatology_reference_period_with_same_o
"ts",
"month",
weighted=True,
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
reference_period=("2000-01-01", "2000-06-01"),
)

Expand Down Expand Up @@ -2057,20 +2123,21 @@ def test_weighted_seasonal_departures_with_DJF(self):
"ts",
"season",
weighted=True,
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": False},
)

expected = ds.copy()
expected = expected.drop_dims("time")
expected["ts"] = xr.DataArray(
name="ts",
data=np.array([[[0.0]], [[0.0]], [[0.0]], [[0.0]]]),
data=np.array([[[0.0]], [[0.0]], [[0.0]], [[0.0]], [[0.0]]]),
coords={
"lat": expected.lat,
"lon": expected.lon,
"time": xr.DataArray(
data=np.array(
[
cftime.DatetimeGregorian(2000, 1, 1),
cftime.DatetimeGregorian(2000, 4, 1),
cftime.DatetimeGregorian(2000, 7, 1),
cftime.DatetimeGregorian(2000, 10, 1),
Expand All @@ -2094,7 +2161,7 @@ def test_weighted_seasonal_departures_with_DJF(self):
"freq": "season",
"weighted": "True",
"dec_mode": "DJF",
"drop_incomplete_seasons": "True",
"drop_incomplete_seasons": "False",
},
)

Expand All @@ -2108,20 +2175,21 @@ def test_weighted_seasonal_departures_with_DJF_and_keep_weights(self):
"season",
weighted=True,
keep_weights=True,
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": False},
)

expected = ds.copy()
expected = expected.drop_dims("time")
expected["ts"] = xr.DataArray(
name="ts",
data=np.array([[[0.0]], [[0.0]], [[0.0]], [[0.0]]]),
data=np.array([[[0.0]], [[0.0]], [[0.0]], [[0.0]], [[0.0]]]),
coords={
"lat": expected.lat,
"lon": expected.lon,
"time": xr.DataArray(
data=np.array(
[
cftime.DatetimeGregorian(2000, 1, 1),
cftime.DatetimeGregorian(2000, 4, 1),
cftime.DatetimeGregorian(2000, 7, 1),
cftime.DatetimeGregorian(2000, 10, 1),
Expand All @@ -2145,16 +2213,17 @@ def test_weighted_seasonal_departures_with_DJF_and_keep_weights(self):
"freq": "season",
"weighted": "True",
"dec_mode": "DJF",
"drop_incomplete_seasons": "True",
"drop_incomplete_seasons": "False",
},
)
expected["time_wts"] = xr.DataArray(
name="ts",
data=np.array([1.0, 1.0, 1.0, 1.0]),
data=np.array([0.52542373, 1.0, 1.0, 1.0, 0.47457627]),
coords={
"time_original": xr.DataArray(
data=np.array(
[
"2000-01-16T12:00:00.000000000",
"2000-03-16T12:00:00.000000000",
"2000-06-16T00:00:00.000000000",
"2000-09-16T00:00:00.000000000",
Expand All @@ -2174,7 +2243,7 @@ def test_weighted_seasonal_departures_with_DJF_and_keep_weights(self):
dims=["time_original"],
)

xr.testing.assert_identical(result, expected)
xr.testing.assert_allclose(result, expected)

def test_unweighted_seasonal_departures_with_DJF(self):
ds = self.ds.copy()
Expand All @@ -2183,20 +2252,21 @@ def test_unweighted_seasonal_departures_with_DJF(self):
"ts",
"season",
weighted=False,
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": False},
)

expected = ds.copy()
expected = expected.drop_dims("time")
expected["ts"] = xr.DataArray(
name="ts",
data=np.array([[[0.0]], [[0.0]], [[0.0]], [[0.0]]]),
data=np.array([[[0.0]], [[0.0]], [[0.0]], [[0.0]], [[0.0]]]),
coords={
"lat": expected.lat,
"lon": expected.lon,
"time": xr.DataArray(
data=np.array(
[
cftime.DatetimeGregorian(2000, 1, 1),
cftime.DatetimeGregorian(2000, 4, 1),
cftime.DatetimeGregorian(2000, 7, 1),
cftime.DatetimeGregorian(2000, 10, 1),
Expand All @@ -2219,7 +2289,7 @@ def test_unweighted_seasonal_departures_with_DJF(self):
"mode": "departures",
"freq": "season",
"weighted": "False",
"drop_incomplete_seasons": "True",
"drop_incomplete_seasons": "False",
"dec_mode": "DJF",
},
)
Expand Down
Loading

0 comments on commit a12cf30

Please sign in to comment.