Skip to content

Implement unique selection reduction - #1194

Closed
luseverin wants to merge 42 commits into
forecast-classfrom
implement_unique_selection_reduction
Closed

Implement unique selection reduction #1194
luseverin wants to merge 42 commits into
forecast-classfrom
implement_unique_selection_reduction

Adjust reduction logic with respect to members

5b46f63
Select commit
Loading
Failed to load commit list.
Sign in for the full log view
GitHub Actions / Core / Unit Test Results (3.10) failed Dec 17, 2025 in 0s

3 fail, 1 skipped, 831 pass in 5m 58s

  1 files  ± 0    1 suites  ±0   5m 58s ⏱️ +13s
835 tests  - 69  831 ✅  - 50  1 💤 ±0  3 ❌  - 19 
846 runs   - 69  842 ✅  - 50  1 💤 ±0  3 ❌  - 19 

Results for commit 5b46f63. ± Comparison against earlier commit 001f340.

Annotations

Check warning on line 0 in climada.hazard.test.test_forecast

See this annotation in the file changed.

@github-actions github-actions / Core / Unit Test Results (3.10)

test_hazard_forecast_mean_min_max_member[min] (climada.hazard.test.test_forecast) failed

tests_xml/tests.xml [took 0s]
Raw output
AssertionError: 
Arrays are not equal

Mismatched elements: 4 / 4 (100%)
Max absolute difference among violations: 4
Max relative difference among violations: 4.
 ACTUAL: array([0, 1, 2, 3])
 DESIRED: array([-1, -1, -1, -1])
haz_fc = <climada.hazard.forecast.HazardForecast object at 0x7f18e06c9c90>
attr = 'min'

    @pytest.mark.parametrize("attr", ["min", "mean", "max"])
    def test_hazard_forecast_mean_min_max_member(haz_fc, attr):
        """Check mean, min, and max methods for HazardForecast with dim argument"""
    
        for dim, unique_vals in zip(
            ["member", "lead_time"],
            [np.unique(haz_fc.member), np.unique(haz_fc.lead_time)],
        ):
            haz_fcst_reduced = getattr(haz_fc, attr)(dim=dim)
            # Assert sparse matrices
            expected_intensity = []
            expected_fraction = []
            for val in unique_vals:
                mask = getattr(haz_fc, dim) == val
                expected_intensity.append(
                    getattr(haz_fc.intensity.todense()[mask], attr)(axis=0)
                )
                expected_fraction.append(
                    getattr(haz_fc.fraction.todense()[mask], attr)(axis=0)
                )
            npt.assert_array_equal(
                haz_fcst_reduced.intensity.todense(),
                np.vstack(expected_intensity),
            )
            npt.assert_array_equal(
                haz_fcst_reduced.fraction.todense(),
                np.vstack(expected_fraction),
            )
            # Check that attributes where reduced correctly
            if dim == "lead_time":
                npt.assert_array_equal(haz_fcst_reduced.member, np.unique(haz_fc.member))
                npt.assert_array_equal(
                    haz_fcst_reduced.lead_time,
                    np.array([np.timedelta64("NaT")] * len(unique_vals)),
                )
            else:  # dim == "member"
                npt.assert_array_equal(
                    haz_fcst_reduced.lead_time, np.unique(haz_fc.lead_time)
                )
>               npt.assert_array_equal(
                    haz_fcst_reduced.member,
                    np.array([-1] * len(unique_vals)),
                )
E               AssertionError: 
E               Arrays are not equal
E               
E               Mismatched elements: 4 / 4 (100%)
E               Max absolute difference among violations: 4
E               Max relative difference among violations: 4.
E                ACTUAL: array([0, 1, 2, 3])
E                DESIRED: array([-1, -1, -1, -1])

climada/hazard/test/test_forecast.py:364: AssertionError

Check warning on line 0 in climada.hazard.test.test_forecast

See this annotation in the file changed.

@github-actions github-actions / Core / Unit Test Results (3.10)

test_hazard_forecast_mean_min_max_member[mean] (climada.hazard.test.test_forecast) failed

tests_xml/tests.xml [took 0s]
Raw output
AssertionError: 
Arrays are not equal

Mismatched elements: 4 / 4 (100%)
Max absolute difference among violations: 4
Max relative difference among violations: 4.
 ACTUAL: array([0, 1, 2, 3])
 DESIRED: array([-1, -1, -1, -1])
haz_fc = <climada.hazard.forecast.HazardForecast object at 0x7f18a7d83160>
attr = 'mean'

    @pytest.mark.parametrize("attr", ["min", "mean", "max"])
    def test_hazard_forecast_mean_min_max_member(haz_fc, attr):
        """Check mean, min, and max methods for HazardForecast with dim argument"""
    
        for dim, unique_vals in zip(
            ["member", "lead_time"],
            [np.unique(haz_fc.member), np.unique(haz_fc.lead_time)],
        ):
            haz_fcst_reduced = getattr(haz_fc, attr)(dim=dim)
            # Assert sparse matrices
            expected_intensity = []
            expected_fraction = []
            for val in unique_vals:
                mask = getattr(haz_fc, dim) == val
                expected_intensity.append(
                    getattr(haz_fc.intensity.todense()[mask], attr)(axis=0)
                )
                expected_fraction.append(
                    getattr(haz_fc.fraction.todense()[mask], attr)(axis=0)
                )
            npt.assert_array_equal(
                haz_fcst_reduced.intensity.todense(),
                np.vstack(expected_intensity),
            )
            npt.assert_array_equal(
                haz_fcst_reduced.fraction.todense(),
                np.vstack(expected_fraction),
            )
            # Check that attributes where reduced correctly
            if dim == "lead_time":
                npt.assert_array_equal(haz_fcst_reduced.member, np.unique(haz_fc.member))
                npt.assert_array_equal(
                    haz_fcst_reduced.lead_time,
                    np.array([np.timedelta64("NaT")] * len(unique_vals)),
                )
            else:  # dim == "member"
                npt.assert_array_equal(
                    haz_fcst_reduced.lead_time, np.unique(haz_fc.lead_time)
                )
>               npt.assert_array_equal(
                    haz_fcst_reduced.member,
                    np.array([-1] * len(unique_vals)),
                )
E               AssertionError: 
E               Arrays are not equal
E               
E               Mismatched elements: 4 / 4 (100%)
E               Max absolute difference among violations: 4
E               Max relative difference among violations: 4.
E                ACTUAL: array([0, 1, 2, 3])
E                DESIRED: array([-1, -1, -1, -1])

climada/hazard/test/test_forecast.py:364: AssertionError

Check warning on line 0 in climada.hazard.test.test_forecast

See this annotation in the file changed.

@github-actions github-actions / Core / Unit Test Results (3.10)

test_hazard_forecast_mean_min_max_member[max] (climada.hazard.test.test_forecast) failed

tests_xml/tests.xml [took 0s]
Raw output
AssertionError: 
Arrays are not equal

Mismatched elements: 4 / 4 (100%)
Max absolute difference among violations: 4
Max relative difference among violations: 4.
 ACTUAL: array([0, 1, 2, 3])
 DESIRED: array([-1, -1, -1, -1])
haz_fc = <climada.hazard.forecast.HazardForecast object at 0x7f18a7d0f9d0>
attr = 'max'

    @pytest.mark.parametrize("attr", ["min", "mean", "max"])
    def test_hazard_forecast_mean_min_max_member(haz_fc, attr):
        """Check mean, min, and max methods for HazardForecast with dim argument"""
    
        for dim, unique_vals in zip(
            ["member", "lead_time"],
            [np.unique(haz_fc.member), np.unique(haz_fc.lead_time)],
        ):
            haz_fcst_reduced = getattr(haz_fc, attr)(dim=dim)
            # Assert sparse matrices
            expected_intensity = []
            expected_fraction = []
            for val in unique_vals:
                mask = getattr(haz_fc, dim) == val
                expected_intensity.append(
                    getattr(haz_fc.intensity.todense()[mask], attr)(axis=0)
                )
                expected_fraction.append(
                    getattr(haz_fc.fraction.todense()[mask], attr)(axis=0)
                )
            npt.assert_array_equal(
                haz_fcst_reduced.intensity.todense(),
                np.vstack(expected_intensity),
            )
            npt.assert_array_equal(
                haz_fcst_reduced.fraction.todense(),
                np.vstack(expected_fraction),
            )
            # Check that attributes where reduced correctly
            if dim == "lead_time":
                npt.assert_array_equal(haz_fcst_reduced.member, np.unique(haz_fc.member))
                npt.assert_array_equal(
                    haz_fcst_reduced.lead_time,
                    np.array([np.timedelta64("NaT")] * len(unique_vals)),
                )
            else:  # dim == "member"
                npt.assert_array_equal(
                    haz_fcst_reduced.lead_time, np.unique(haz_fc.lead_time)
                )
>               npt.assert_array_equal(
                    haz_fcst_reduced.member,
                    np.array([-1] * len(unique_vals)),
                )
E               AssertionError: 
E               Arrays are not equal
E               
E               Mismatched elements: 4 / 4 (100%)
E               Max absolute difference among violations: 4
E               Max relative difference among violations: 4.
E                ACTUAL: array([0, 1, 2, 3])
E                DESIRED: array([-1, -1, -1, -1])

climada/hazard/test/test_forecast.py:364: AssertionError