From 38b9e4b69c86112fcfb8e85d83353a810c5d8bff Mon Sep 17 00:00:00 2001 From: Brice Fabre Date: Wed, 17 Apr 2024 13:37:25 +0200 Subject: [PATCH 1/4] Replace warning by assertion for no weights if weighted is True --- openfisca_survey_manager/simulations.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/openfisca_survey_manager/simulations.py b/openfisca_survey_manager/simulations.py index 87b65d71..2d131f9d 100644 --- a/openfisca_survey_manager/simulations.py +++ b/openfisca_survey_manager/simulations.py @@ -2,6 +2,7 @@ import logging import numpy as np +from numpy import logical_or as or_ import pandas as pd import re from typing import Callable, Dict, List, Optional, Union @@ -165,6 +166,7 @@ def compute_aggregate(simulation: Simulation, variable: str = None, aggfunc: str uniform_weight = np.array(1.0) weight_variable = None if weighted: + assert or_(alternative_weights, weight_variable_by_entity), "The weighted option is set at True but there is no weight variable for entity {} nor alternative weights. Either define a weight variable or switch to unweighted".format(entity_key) if alternative_weights: if isinstance(alternative_weights, str): assert alternative_weights in tax_benefit_system.variables, \ @@ -174,13 +176,8 @@ def compute_aggregate(simulation: Simulation, variable: str = None, aggfunc: str elif (type(alternative_weights) is int) or (type(alternative_weights) is float): weight_variable = None uniform_weight = float(alternative_weights) - - else: - if weight_variable_by_entity: - weight_variable = weight_variable_by_entity[entity_key] - - else: - log.warn('There is no weight variable for entity {} nor alternative weights. Switch to unweighted'.format(entity_key)) + elif weight_variable_by_entity: + weight_variable = weight_variable_by_entity[entity_key] if variable in simulation.tax_benefit_system.variables: value = simulation.adaptative_calculate_variable(variable = variable, period = period) From 4d47b6ba8884222936e45352003b1605af93dacb Mon Sep 17 00:00:00 2001 From: Brice Fabre Date: Wed, 17 Apr 2024 15:14:57 +0200 Subject: [PATCH 2/4] Ajuste test_compute_aggregate --- openfisca_survey_manager/tests/test_compute_aggregate.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openfisca_survey_manager/tests/test_compute_aggregate.py b/openfisca_survey_manager/tests/test_compute_aggregate.py index db71b76e..f761fdb3 100644 --- a/openfisca_survey_manager/tests/test_compute_aggregate.py +++ b/openfisca_survey_manager/tests/test_compute_aggregate.py @@ -19,12 +19,14 @@ def test_compute_aggregate(): "social_security_contribution", period = period, filter_by = "salary < 3000", + weighted = False, ) assert 34489 == survey_scenario.compute_aggregate( "social_security_contribution", period = period, filter_by = "3000 < salary < 10000", + weighted = False, ).astype(int) del survey_scenario.weight_variable_by_entity @@ -33,4 +35,5 @@ def test_compute_aggregate(): "social_security_contribution", period = period, filter_by = "3000 < salary < 10000", + weighted = False, ).astype(int) From 33843d6ffba1f3492952265a841188b68d73fcb5 Mon Sep 17 00:00:00 2001 From: Brice Fabre Date: Tue, 23 Apr 2024 14:12:43 +0200 Subject: [PATCH 3/4] Corrige test --- openfisca_survey_manager/tests/test_compute_aggregate.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/openfisca_survey_manager/tests/test_compute_aggregate.py b/openfisca_survey_manager/tests/test_compute_aggregate.py index f761fdb3..b06e8f39 100644 --- a/openfisca_survey_manager/tests/test_compute_aggregate.py +++ b/openfisca_survey_manager/tests/test_compute_aggregate.py @@ -19,14 +19,12 @@ def test_compute_aggregate(): "social_security_contribution", period = period, filter_by = "salary < 3000", - weighted = False, ) assert 34489 == survey_scenario.compute_aggregate( "social_security_contribution", period = period, filter_by = "3000 < salary < 10000", - weighted = False, ).astype(int) del survey_scenario.weight_variable_by_entity From 317f2059295085d445cfb57ee0c3fc8ed61c5c01 Mon Sep 17 00:00:00 2001 From: Brice Fabre Date: Tue, 23 Apr 2024 16:26:18 +0200 Subject: [PATCH 4/4] Update the changelog and the setup --- CHANGELOG.md | 4 ++++ setup.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbdd1b6f..014c835c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.1 [#298](https://github.com/openfisca/openfisca-survey-manager/pull/298) + +* For `compute_aggregate`, replace the warning by an assert when `weighted = True` (the default) and no weight is defined for the variable. Before, a warning was displayed and the aggregate was computed using uniform weights. + ## 2.2.0 [#295](https://github.com/openfisca/openfisca-survey-manager/pull/295) * Improve support for parquet file format : diff --git a/setup.py b/setup.py index d89d039f..d8b7739c 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ setup( name = 'OpenFisca-Survey-Manager', - version = '2.2.0', + version = '2.2.1', author = 'OpenFisca Team', author_email = 'contact@openfisca.fr', classifiers = [classifier for classifier in classifiers.split('\n') if classifier],