Skip to content

Commit

Permalink
Merge pull request #298 from openfisca/assertion-no-weithed-compute-a…
Browse files Browse the repository at this point in the history
…ggregate

Replace warning by assertion for no weights if weighted is True for compute_aggregate
  • Loading branch information
bfabre01 authored Apr 24, 2024
2 parents c1ea6c8 + 317f205 commit ecb8bbc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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 :
Expand Down
11 changes: 4 additions & 7 deletions openfisca_survey_manager/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, \
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions openfisca_survey_manager/tests/test_compute_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ def test_compute_aggregate():
"social_security_contribution",
period = period,
filter_by = "3000 < salary < 10000",
weighted = False,
).astype(int)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit ecb8bbc

Please sign in to comment.