Download tasks/data/forward/agentoxygen__hdp.a0d206f/tests/test_patch.diff from Lego-X/ProgramGen: direct link, hf CLI and curl.
- Browser
- Download file 20.9 kB
-
https://huggingface.co/datasets/Lego-X/ProgramGen/resolve/main/tasks/data/forward/agentoxygen__hdp.a0d206f/tests/test_patch.diff
- Command line
-
hf download hf://datasets/Lego-X/ProgramGen/tasks/data/forward/agentoxygen__hdp.a0d206f/tests/test_patch.diff
-
curl -L -o test_patch.diff https://huggingface.co/datasets/Lego-X/ProgramGen/resolve/main/tasks/data/forward/agentoxygen__hdp.a0d206f/tests/test_patch.diff
20.9 kB
| diff --git a/hdp/tests/__init__.py b/hdp/tests/__init__.py | |
| new file mode 100644 | |
| index 0000000..e69de29 | |
| diff --git a/hdp/tests/test_heatwave_average.py b/hdp/tests/test_heatwave_average.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/hdp/tests/test_heatwave_average.py | |
| +from hdp.metric import heatwave_average | |
| +import numpy as np | |
| + | |
| + | |
| +class TestHeatwaveAverage: | |
| + def test_heatwave_average_null_case(self): | |
| + hot_day_null = np.zeros(100, dtype=int) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_average(hot_day_null, season_range), np.array([0])) | |
| + | |
| + def test_heatwave_average_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_average(hot_day_full, season_range), np.array([100])) | |
| + | |
| + def test_heatwave_average_case1(self): | |
| + hot_day_case1 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case1.size]], dtype=int) | |
| + assert np.array_equal(heatwave_average(hot_day_case1, season_range), np.array([5.5])) | |
| + | |
| + def test_heatwave_average_case2(self): | |
| + hot_day_case2 = np.array( | |
| + [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case2.size]], dtype=int) | |
| + assert np.allclose(heatwave_average(hot_day_case2, season_range), np.array([3.333333])) | |
| + | |
| + def test_heatwave_average_case3(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_average(hot_day_case3, season_range), np.array([1.75])) | |
| + | |
| + def test_heatwave_average_season_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int) | |
| + assert np.array_equal(heatwave_average(hot_day_full, season_range), np.array([5, 10, 10, 8])) | |
| + | |
| + def test_heatwave_average_season_case1(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_average(hot_day_case3, season_range), np.array([6, 2.5])) | |
| + | |
| + def test_heatwave_average_season_case2(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_average(hot_day_case3, season_range), np.array([1.5, 2])) | |
| diff --git a/hdp/tests/test_heatwave_duration.py b/hdp/tests/test_heatwave_duration.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/hdp/tests/test_heatwave_duration.py | |
| +from hdp.metric import heatwave_duration | |
| +import numpy as np | |
| + | |
| + | |
| +class TestHeatwaveDuration: | |
| + def test_heatwave_duration_null_case(self): | |
| + hot_day_null = np.zeros(100, dtype=int) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_null, season_range), np.array([0])) | |
| + | |
| + def test_heatwave_duration_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_full, season_range), np.array([100])) | |
| + | |
| + def test_heatwave_duration_case1(self): | |
| + hot_day_case1 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case1.size]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_case1, season_range), np.array([7])) | |
| + | |
| + def test_heatwave_duration_case2(self): | |
| + hot_day_case2 = np.array( | |
| + [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case2.size]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_case2, season_range), np.array([5])) | |
| + | |
| + def test_heatwave_duration_case3(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_case3, season_range), np.array([3])) | |
| + | |
| + def test_heatwave_duration_season_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_full, season_range), np.array([5, 10, 10, 8])) | |
| + | |
| + def test_heatwave_duration_season_case1(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_case3, season_range), np.array([6, 4])) | |
| + | |
| + def test_heatwave_duration_season_case2(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_duration(hot_day_case3, season_range), np.array([2, 3])) | |
| diff --git a/hdp/tests/test_heatwave_frequency.py b/hdp/tests/test_heatwave_frequency.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/hdp/tests/test_heatwave_frequency.py | |
| +from hdp.metric import heatwave_frequency | |
| +import numpy as np | |
| + | |
| + | |
| +class TestHeatwaveFrequency: | |
| + def test_heatwave_frequency_null_case(self): | |
| + hot_day_null = np.zeros(100, dtype=int) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_null, season_range), np.array([0])) | |
| + | |
| + def test_heatwave_frequency_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_full, season_range), np.array([100])) | |
| + | |
| + def test_heatwave_frequency_case1(self): | |
| + hot_day_case1 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case1.size]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_case1, season_range), np.array([11])) | |
| + | |
| + def test_heatwave_frequency_case2(self): | |
| + hot_day_case2 = np.array( | |
| + [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case2.size]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_case2, season_range), np.array([10])) | |
| + | |
| + def test_heatwave_frequency_case3(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_case3, season_range), np.array([7])) | |
| + | |
| + def test_heatwave_frequency_season_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_full, season_range), np.array([5, 10, 10, 8])) | |
| + | |
| + def test_heatwave_frequency_season_case1(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_case3, season_range), np.array([6, 5])) | |
| + | |
| + def test_heatwave_frequency_season_case2(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_frequency(hot_day_case3, season_range), np.array([3, 4])) | |
| diff --git a/hdp/tests/test_heatwave_number.py b/hdp/tests/test_heatwave_number.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/hdp/tests/test_heatwave_number.py | |
| +from hdp.metric import heatwave_number | |
| +import numpy as np | |
| + | |
| + | |
| +class TestHeatwaveNumber: | |
| + def test_heatwave_number_null_case(self): | |
| + hot_day_null = np.zeros(100, dtype=int) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_null, season_range), np.array([0])) | |
| + | |
| + def test_heatwave_number_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 100]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_full, season_range), np.array([1])) | |
| + | |
| + def test_heatwave_number_case1(self): | |
| + hot_day_case1 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case1.size]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_case1, season_range), np.array([2])) | |
| + | |
| + def test_heatwave_number_case2(self): | |
| + hot_day_case2 = np.array( | |
| + [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case2.size]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_case2, season_range), np.array([3])) | |
| + | |
| + def test_heatwave_number_case3(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_case3, season_range), np.array([4])) | |
| + | |
| + def test_heatwave_number_season_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_full, season_range), np.array([1, 1, 1, 1])) | |
| + | |
| + def test_heatwave_number_season_case1(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_case3, season_range), np.array([1, 2])) | |
| + | |
| + def test_heatwave_number_season_case2(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0], | |
| + dtype=int) | |
| + season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int) | |
| + assert np.array_equal(heatwave_number(hot_day_case3, season_range), np.array([2, 2])) | |
| diff --git a/hdp/tests/test_index_heatwaves.py b/hdp/tests/test_index_heatwaves.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/hdp/tests/test_index_heatwaves.py | |
| +from hdp.metric import index_heatwaves | |
| +import numpy as np | |
| +import pytest | |
| + | |
| + | |
| +class TestIndexHeatwave: | |
| + def test_index_heatwaves_null_case(self): | |
| + hot_day_null = np.zeros(100, dtype=bool) | |
| + assert np.array_equal(index_heatwaves(hot_day_null, 1, 1, 1), np.zeros(hot_day_null.size)) | |
| + assert np.array_equal(index_heatwaves(hot_day_null, 1, 0, 1), np.zeros(hot_day_null.size)) | |
| + assert np.array_equal(index_heatwaves(hot_day_null, 0, 0, 1), np.zeros(hot_day_null.size)) | |
| + | |
| + def test_index_heatwaves_full_case(self): | |
| + hot_day_full = np.ones(100, dtype=bool) | |
| + assert np.array_equal(index_heatwaves(hot_day_full, 1, 1, 1), np.ones(hot_day_full.size)) | |
| + assert np.array_equal(index_heatwaves(hot_day_full, 1, 0, 1), np.ones(hot_day_full.size)) | |
| + assert np.array_equal(index_heatwaves(hot_day_full, 0, 0, 1), np.ones(hot_day_full.size)) | |
| + | |
| + def test_index_heatwaves_case1(self): | |
| + hot_day_case1 = np.array( | |
| + [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0], | |
| + dtype=bool) | |
| + assert np.array_equal(index_heatwaves(hot_day_case1, 1, 1, 1), [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0]) | |
| + assert np.array_equal(index_heatwaves(hot_day_case1, 1, 0, 1), [0, 1, 1, 1, 1, 0, 2, 2, 2, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0]) | |
| + assert np.array_equal(index_heatwaves(hot_day_case1, 0, 0, 1), [0, 1, 1, 1, 1, 0, 2, 2, 2, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0]) | |
| + | |
| + def test_index_heatwaves_case2(self): | |
| + hot_day_case2 = np.array( | |
| + [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1], | |
| + dtype=bool) | |
| + assert np.array_equal(index_heatwaves(hot_day_case2, 1, 1, 1), [0, 1, 0, 1, 1, 0, 2, 2, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 4]) | |
| + assert np.array_equal(index_heatwaves(hot_day_case2, 1, 0, 1), [0, 1, 0, 2, 2, 0, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 5]) | |
| + assert np.array_equal(index_heatwaves(hot_day_case2, 0, 0, 1), [0, 1, 0, 2, 2, 0, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 5]) | |
| + | |
| + def test_index_heatwaves_case3(self): | |
| + hot_day_case3 = np.array( | |
| + [0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], | |
| + dtype=bool) | |
| + assert np.array_equal(index_heatwaves(hot_day_case3, 1, 1, 1), [0, 0, 0, 1, 1, 0, 1, 0, 2, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0]) | |
| + assert np.array_equal(index_heatwaves(hot_day_case3, 1, 0, 1), [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0]) | |
| + assert np.array_equal(index_heatwaves(hot_day_case3, 0, 0, 1), [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0]) | |
| diff --git a/hdp/tests/test_utils.py b/hdp/tests/test_utils.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/hdp/tests/test_utils.py | |
| +from hdp.utils import * | |
| +import numpy as np | |
| +from math import isclose | |
| +from xarray import DataArray | |
| + | |
| +def test_get_time_stamp(): | |
| + assert type(get_time_stamp()) is str | |
| + | |
| + | |
| +def test_get_version(): | |
| + assert type(get_version()) is str | |
| + | |
| + | |
| +def test_control_dataarray(): | |
| + control_da = generate_test_control_dataarray(grid_shape=(1,1), start_date="2000", end_date="2100") | |
| + assert type(control_da) is DataArray | |
| + var = control_da.name | |
| + assert control_da.attrs["units"] == "degC" | |
| + assert control_da.dims == ("lon", "lat", "time") | |
| + assert control_da.dtype == float | |
| + assert control_da.time.values[0].calendar == "noleap" | |
| + assert control_da.time.values.size >= 365 | |
| + assert np.sum(np.isnan(control_da)) == 0 | |
| + | |
| + control_slope = np.polyfit(np.arange(control_da["time"].size), control_da.mean(dim=["lat", "lon"]).values, 1)[0] | |
| + assert np.abs(control_slope) < 0.01 | |
| + | |
| + | |
| +def test_warming_dataarray(): | |
| + warming_da = generate_test_warming_dataarray(grid_shape=(1,1), start_date="2000", end_date="2100") | |
| + avg_da = warming_da.mean(dim=["lat", "lon"]).values | |
| + warm_slope = np.polyfit(np.arange(avg_da.size), avg_da, 1)[0] | |
| + | |
| + assert warm_slope > 0 | |
| + assert not isclose(warm_slope, 0) | |
| + | |
| + | |
| +def test_rh_dataarray(): | |
| + rh_da = generate_test_rh_dataarray() | |
| + | |
| + assert rh_da.max() <= 1 | |
| + assert rh_da.min() >= 0 | |
| + | |
| + | |
| +def test_defaults_compatibility(): | |
| + control_da = generate_test_control_dataarray() | |
| + warming_da = generate_test_warming_dataarray() | |
| + rh_da = generate_test_rh_dataarray() | |
| + | |
| + assert control_da.shape == warming_da.shape | |
| + assert warming_da.shape == rh_da.shape | |
| + | |
| + assert control_da.dims == warming_da.dims | |
| + assert warming_da.dims == rh_da.dims | |
| + | |
| + assert control_da.name == warming_da.name | |
| + assert control_da.units == warming_da.units | |
| diff --git a/hdp/tests/test_workflow.py b/hdp/tests/test_workflow.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/hdp/tests/test_workflow.py | |
| +from hdp.graphics.notebook import create_notebook | |
| +import pytest | |
| +import hdp.utils | |
| +import hdp.measure | |
| +import hdp.threshold | |
| +import hdp.metric | |
| +import numpy as np | |
| + | |
| + | |
| +@pytest.fixture(scope="function") | |
| +def temp_output_dir(tmp_path_factory): | |
| + return tmp_path_factory.mktemp("output_dir") | |
| + | |
| + | |
| +def test_full_data_workflow(temp_output_dir): | |
| + grid_shape = (2, 3) | |
| + | |
| + baseline_temp = hdp.utils.generate_test_control_dataarray(grid_shape=grid_shape).rename("temp") | |
| + baseline_rh = hdp.utils.generate_test_rh_dataarray(grid_shape=grid_shape).rename("rh") | |
| + baseline_measures = hdp.measure.format_standard_measures([baseline_temp], rh=baseline_rh) | |
| + | |
| + percentiles = np.arange(0.9, 1, 0.01) | |
| + | |
| + thresholds = hdp.threshold.compute_thresholds(baseline_measures, percentiles=percentiles) | |
| + | |
| + test_temp = hdp.utils.generate_test_warming_dataarray(grid_shape=grid_shape).rename("temp") | |
| + test_rh = baseline_rh | |
| + | |
| + hw_definitions = [[3,0,0], [3,1,1], [4,2,0], [4,1,3], [5,0,1], [5,1,4]] | |
| + | |
| + test_measures = hdp.measure.format_standard_measures([test_temp], rh=test_rh) | |
| + metrics = hdp.metric.compute_group_metrics(test_measures, thresholds, hw_definitions) | |
| + | |
| + metrics = metrics.compute() | |
| + thresholds = thresholds.compute() | |
| + | |
| + assert (thresholds.percentile.values == percentiles).all() | |
| + assert len(thresholds.data_vars) == 2 | |
| + | |
| + assert metrics.definition.values[0] == "3-0-0" | |
| + assert metrics.definition.values[1] == "3-1-1" | |
| + assert metrics.definition.values[2] == "4-2-0" | |
| + assert metrics.definition.values[3] == "4-1-3" | |
| + assert metrics.definition.values[4] == "5-0-1" | |
| + assert metrics.definition.values[5] == "5-1-4" | |
| + assert (metrics.percentile.values == percentiles).all() | |
| + | |
| + metric_means = metrics.mean() | |
| + | |
| + assert metric_means["temp.temp_threshold.HWF"] >= metric_means["temp.temp_threshold.HWD"] | |
| + assert metric_means["temp.temp_threshold.HWD"] >= metric_means["temp.temp_threshold.HWA"] | |
| + | |
| + for var in metrics: | |
| + assert metrics[var].shape == (metrics.percentile.size, metrics.definition.size, metrics.lon.size, metrics.lat.size, metrics.time.size) | |
| + assert metrics[var].dtype == int | |
| + if "HWF" in var or "HWD" in var: | |
| + assert metrics[var].attrs["units"] == 'heatwave days', f"Variable '{var}' has incorrect units '{metrics[var].attrs["units"]}'" | |
| + elif "HWN" in var or "HWA" in var: | |
| + assert metrics[var].attrs["units"] == 'heatwave events', f"Variable '{var}' has incorrect units '{metrics[var].attrs["units"]}'" | |
| + else: | |
| + assert False, f"Cannot determine primary heatwave metric from variable '{var}'." | |
| + | |
| + figure_notebook = create_notebook(metrics) | |
| + figure_notebook.save_notebook(f"{temp_output_dir}/sample_hw_summary_figures.ipynb") | |
| + | |
| + | |
| +def test_include_threshold(temp_output_dir): | |
| + grid_shape = (2, 3) | |
| + | |
| + baseline_temp = hdp.utils.generate_test_control_dataarray(grid_shape=grid_shape).rename("temp") | |
| + test_temp = hdp.utils.generate_test_warming_dataarray(grid_shape=grid_shape).rename("temp") | |
| + | |
| + baseline_measures = hdp.measure.format_standard_measures([baseline_temp]) | |
| + test_measures = hdp.measure.format_standard_measures([test_temp]) | |
| + | |
| + percentiles = np.arange(0.9, 1, 0.02) | |
| + thresholds = hdp.threshold.compute_thresholds(baseline_measures, percentiles=percentiles) | |
| + | |
| + hw_definitions = [[3, 0, 0], [3, 1, 1]] | |
| + | |
| + metrics_with = hdp.metric.compute_group_metrics(test_measures, thresholds, hw_definitions, include_threshold=True).compute() | |
| + metrics_without = hdp.metric.compute_group_metrics(test_measures, thresholds, hw_definitions, include_threshold=False).compute() | |
| + thresholds = thresholds.compute() | |
| + | |
| + # The threshold variable is embedded under its own name only when include_threshold=True. | |
| + assert "temp_threshold" in metrics_with.data_vars | |
| + assert "temp_threshold" not in metrics_without.data_vars | |
| + | |
| + # The metric variables are unaffected by include_threshold. | |
| + metric_vars = [var for var in metrics_with.data_vars if var != "temp_threshold"] | |
| + assert set(metric_vars) == set(metrics_without.data_vars) | |
| + | |
| + # The embedded threshold matches the source threshold exactly. | |
| + embedded = metrics_with["temp_threshold"] | |
| + source = thresholds["temp_threshold"] | |
| + assert embedded.broadcast_like(source).equals(source.broadcast_like(embedded)) | |
| + | |
| + figure_notebook = create_notebook(metrics_with) | |
| + figure_notebook.save_notebook(f"{temp_output_dir}/sample_hw_summary_figures.ipynb") | |