From b498e9b1e8ca7d8cfd5832d16d0f1ff80bf90e0e Mon Sep 17 00:00:00 2001 From: Mahdi Ben Jelloul Date: Fri, 11 Aug 2023 17:45:14 +0200 Subject: [PATCH] Fix observations_threshold --- openfisca_survey_manager/scenarios.py | 4 ++-- openfisca_survey_manager/simulations.py | 8 +++++--- .../tests/test_compute_winners_loosers.py | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/openfisca_survey_manager/scenarios.py b/openfisca_survey_manager/scenarios.py index 28f537b5..be250a3a 100644 --- a/openfisca_survey_manager/scenarios.py +++ b/openfisca_survey_manager/scenarios.py @@ -356,7 +356,7 @@ def compute_winners_loosers(self, variable = None, period = None, absolute_minimal_detected_variation = 0, relative_minimal_detected_variation = .01, - observations_thershold = None, + observations_threshold = None, weighted = True, alternative_weights = None): @@ -370,7 +370,7 @@ def compute_winners_loosers(self, variable = None, period = period, absolute_minimal_detected_variation = absolute_minimal_detected_variation, relative_minimal_detected_variation = relative_minimal_detected_variation, - observations_thershold = observations_thershold, + observations_threshold = observations_threshold, weighted = weighted, alternative_weights = alternative_weights, filtering_variable_by_entity = self.filtering_variable_by_entity, diff --git a/openfisca_survey_manager/simulations.py b/openfisca_survey_manager/simulations.py index 191f16e9..e6d76131 100644 --- a/openfisca_survey_manager/simulations.py +++ b/openfisca_survey_manager/simulations.py @@ -525,7 +525,7 @@ def compute_winners_loosers( period = None, absolute_minimal_detected_variation = 0, relative_minimal_detected_variation = .01, - observations_thershold = None, + observations_threshold = None, weighted = True, alternative_weights = None, filtering_variable_by_entity = None, @@ -589,8 +589,10 @@ def compute_winners_loosers( after_value < absolute_minimal_detected_variation )[almost_zero_before] - if observations_thershold is not None: - if ((above_after).sum() < observations_thershold) | ((below_after).sum() < observations_thershold): + if observations_threshold is not None: + not_legit_below = (below_after.sum() < observations_threshold) & (below_after.sum() > 0) + not_legit_above = (above_after.sum() < observations_threshold) & (above_after.sum() > 0) + if not_legit_below | not_legit_above: raise ValueError("Not enough observations involved") above_after_count = (above_after * weight).sum() diff --git a/openfisca_survey_manager/tests/test_compute_winners_loosers.py b/openfisca_survey_manager/tests/test_compute_winners_loosers.py index 9675d592..61f706e2 100644 --- a/openfisca_survey_manager/tests/test_compute_winners_loosers.py +++ b/openfisca_survey_manager/tests/test_compute_winners_loosers.py @@ -17,7 +17,7 @@ def test_compute_winners_loosers(): absolute_minimal_detected_variation = .9 relative_minimal_detected_variation = .05 - observations_thershold = 1 + observations_threshold = 1 below, neutral, above = simulation.compute_winners_loosers( baseline_simulation, @@ -25,7 +25,7 @@ def test_compute_winners_loosers(): period = period, absolute_minimal_detected_variation = absolute_minimal_detected_variation, relative_minimal_detected_variation = relative_minimal_detected_variation, - observations_thershold = observations_thershold, + observations_threshold = observations_threshold, ) below_scenario, neutral_scenario, above_scenario = survey_scenario.compute_winners_loosers( @@ -33,7 +33,7 @@ def test_compute_winners_loosers(): period = period, absolute_minimal_detected_variation = absolute_minimal_detected_variation, relative_minimal_detected_variation = relative_minimal_detected_variation, - observations_thershold = observations_thershold, + observations_threshold = observations_threshold, ) assert (below, neutral, above) == (1, 0, 9)