Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace warning by assertion for no weights if weighted is True for compute_aggregate #298

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
benjello marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 3 additions & 0 deletions openfisca_survey_manager/tests/test_compute_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,4 +35,5 @@ def test_compute_aggregate():
"social_security_contribution",
period = period,
filter_by = "3000 < salary < 10000",
weighted = False,
).astype(int)
Loading