def get_brut_from_net(net, type_sal=0, hsup=0, csg_rempl=0, rev='sal', year=2011): from openfisca_core.simulations import ScenarioSimulation import openfisca_france openfisca_france.init_country() simulation = ScenarioSimulation() simulation.set_config(year=2011, nmen=2, x_axis="sali", maxrev=100) simulation.set_param() simulation.compute() net = [net] _defaultP = simulation.P if rev == 'sal': output = _salbrut_from_salnet(net, hsup, type_sal, _defaultP) elif rev == 'cho': output = _chobrut_from_chonet(net, csg_rempl, _defaultP) elif rev == 'rst': output = _rstbrut_from_rstnet(net, csg_rempl, _defaultP) return output
def run_OF(ipp2of_input_variables, path_dta_input, param_scenario = None, dic = None, datesim = None, option = 'test_dta'): ''' Lance le calculs sur OF à partir des cas-types issues de TaxIPP input : base .dta issue de l'étape précédente ''' def _test_of_dta(dta_input, dic): ''' Cette fonction teste que la table .dta trouvée correspond au bon scénario ''' data = read_stata(dta_input) dic_dta = data.loc[0, 'dic_scenar'] if str(dic) != str(dic_dta) : print "La base .dta permettant de lancer la simulation OF est absente " print "La base s'en rapprochant le plus a été construite avec les paramètres : ", dic_dta pdb.set_trace() else : data = data.drop('dic_scenar', 1).sort(['id_foyf', 'id_indiv'], ascending = [True, False]) return data def _scenar_dta(dta_input) : ''' cette fonction identifie le scenario enregistré dans la table d'input ''' data = read_stata(dta_input) dic_dta = data.loc[0, 'dic_scenar'] data = data.drop('dic_scenar', 1).sort(['id_foyf', 'id_indiv'], ascending = [True, False]) return dic_dta, data if option == 'test_dta': data_IPP = _test_of_dta(path_dta_input, dic) if option == 'list_dta': dic_scenar, data_IPP = _scenar_dta(path_dta_input) dict_scenar = dict() expression = "dict_scenar.update(" + dic_scenar + ")" eval(expression) datesim = dict_scenar['datesim'] param_scenario = dict_scenar if 'option' in param_scenario.keys() and param_scenario['option'] == 'brut': TaxBenefitSystem = openfisca_france.init_country(start_from = "brut") tax_benefit_system = TaxBenefitSystem() del ipp2of_input_variables['sal_irpp_old'] ipp2of_input_variables['sal_brut'] = 'salbrut' ipp2of_input_variables['chom_brut'] = 'chobrut' ipp2of_input_variables['pension_brut'] = 'rstbrut' else : TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() openfisca_survey = build_input_OF(data_IPP, ipp2of_input_variables, tax_benefit_system) openfisca_survey = openfisca_survey.fillna(0) # .sort(['idfoy','noi']) # print datesim simulation = surveys.new_simulation_from_survey_data_frame( # debug = True, survey = openfisca_survey, tax_benefit_system = tax_benefit_system, year = datesim, ) if option == 'list_dta': return simulation, param_scenario else: return simulation
def simulation_from_test(test, verbose = False, monthly_amount = False, default_error_margin = 1, forced_error_margin = None): TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() year = test["year"] parent1 = dict(birth = datetime.date(year - 40, 1, 1)) menage = dict() foyer_fiscal = dict() for variable, value in test['input_vars'].iteritems(): if variable == "age": parent1['birth'] = datetime.date(year - value, 1, 1) elif tax_benefit_system.column_by_name[variable].entity == 'men': menage[variable] = value elif tax_benefit_system.column_by_name[variable].entity == 'ind': parent1[variable] = value #TODO: if the person is a child elif tax_benefit_system.column_by_name[variable].entity == 'foy': foyer_fiscal[variable] = value simulation = tax_benefit_system.new_scenario().init_single_entity( parent1 = parent1, menage = menage, foyer_fiscal = foyer_fiscal, year = year, ).new_simulation(debug = True) return simulation
def test_survey_simulation(): year = 2006 input_data_frame = get_input_data_frame(year) TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system_class = TaxBenefitSystem survey_scenario = SurveyScenario().init_from_data_frame( input_data_frame = input_data_frame, tax_benefit_system_class = tax_benefit_system_class, year = year, ) simulation = survey_scenario.new_simulation() try: from pandas import DataFrame revdisp = DataFrame({"revdisp": simulation.calculate('revdisp')}) except NaNCreationError as error: index = error.index entity = error.entity column_name = error.column_name input_data_frame_debug = filter_input_data_frame( simulation.input_data_frame, entity, index[:10], ) survey_scenario_debug = SurveyScenario() simulation_debug = survey_scenario_debug.new_simulation( debug = True, input_data_frame = input_data_frame_debug, tax_benefit_system_class = tax_benefit_system_class, year = year, ) simulation_debug.calculate(column_name) print revdisp.info() print 'finished'
def test_check_rsa(): '''test rsa pour un célibataire avec différent sali''' tests_list = [ {"year": 2014, "amount": 0, "rsa": 439}, {"year": 2014, "amount": 5000, "rsa": 281}, {"year": 2014, "amount": 10000, "rsa": 123}, {"year": 2014, "amount": 12000, "rsa": 59}, {"year": 2014, "amount": 13000, "rsa": 28}, {"year": 2014, "amount": 14000, "rsa": 0}, {"year": 2014, "amount": 15000, "rsa": 0}, {"year": 2014, "amount": 20000, "rsa": 0}, ] error_margin = 1 year = 2014 age = 29 revenu = "sali" for test in tests_list: print test amount = test["amount"] TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() simulation = tax_benefit_system.new_scenario().init_single_entity( period = periods.period('year', year), parent1 = { 'birth': datetime.date(year - age + 1, 1, 1), revenu: amount, }, ).new_simulation(debug = True) calculated_rsa = simulation.calculate('rsa') / 12 assert abs(calculated_rsa - test['rsa']) < error_margin, \ 'calculated_rsa {} is not equal to expected value {}'.format(test['rsa'], calculated_rsa)
def test_survey_simulation(): year = 2011 input_data_frame = get_input_data_frame(year) TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system_class = TaxBenefitSystem survey_scenario = SurveyScenario().init_from_data_frame( input_data_frame = input_data_frame, tax_benefit_system_class = tax_benefit_system_class, year = year, ) simulation = survey_scenario.new_simulation() try: from pandas import DataFrame revdisp = DataFrame({"revdisp": simulation.calculate('revdisp')}) except NaNCreationError as error: index = error.index entity = error.entity column_name = error.column_name input_data_frame_debug = filter_input_data_frame( simulation.input_data_frame, entity, index[:10], ) survey_scenario_debug = SurveyScenario() simulation_debug = survey_scenario_debug.new_simulation( debug = True, input_data_frame = input_data_frame_debug, tax_benefit_system_class = tax_benefit_system_class, year = year, ) simulation_debug.calculate(column_name) print revdisp.info() print 'finished'
def set_variables_default_value(dataframe, year): import openfisca_france TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() for column_name, column in tax_benefit_system.column_by_name.iteritems(): if column_name in dataframe.columns: dataframe[column_name].fillna(column.default, inplace = True) dataframe[column_name] = dataframe[column_name].astype(column.dtype)
def test_weights_building(): year = 2006 input_data_frame = get_input_data_frame(year) TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system_class = TaxBenefitSystem survey_scenario = SurveyScenario().init_from_data_frame( input_data_frame = input_data_frame, tax_benefit_system_class = tax_benefit_system_class, year = year, ) simulation = survey_scenario.new_simulation()
def test_weights_building(): year = 2011 input_data_frame = get_input_data_frame(year) TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system_class = TaxBenefitSystem survey_scenario = SurveyScenario().init_from_data_frame( input_data_frame = input_data_frame, tax_benefit_system_class = tax_benefit_system_class, year = year, ) simulation = survey_scenario.new_simulation()
def get_brut_from_net(net, type_sal = 0, hsup = 0, csg_rempl = 0, rev = 'sal', year = 2011): from openfisca_core.simulations import ScenarioSimulation import openfisca_france openfisca_france.init_country() simulation = ScenarioSimulation() simulation.set_config(year = 2011, nmen = 2, x_axis = "sali", maxrev = 100) simulation.set_param() simulation.compute() net = [net] _defaultP = simulation.P if rev == 'sal': output = _salbrut_from_salnet(net, hsup, type_sal, _defaultP) elif rev == 'cho': output = _chobrut_from_chonet(net, csg_rempl, _defaultP) elif rev == 'rst': output = _rstbrut_from_rstnet(net, csg_rempl, _defaultP) return output
def build_cerfa_fields_by_column_name(year, sections_cerfa): TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() cerfa_fields_by_column_name = dict() for name, column in tax_benefit_system.column_by_name.iteritems(): for section_cerfa in sections_cerfa: if name.startswith('f{}'.format(section_cerfa)): start = column.start or None end = column.end or None if (start is None or start.year <= year) and (end is None or end.year >= year): if column.entity == 'ind': cerfa_field = ['f' + x.lower().encode('ascii', 'ignore') for x in column.cerfa_field.values()] elif column.entity == 'foy': cerfa_field = ['f' + column.cerfa_field.lower().encode('ascii', 'ignore')] cerfa_fields_by_column_name[name.encode('ascii', 'ignore')] = cerfa_field return cerfa_fields_by_column_name
def process_tests_list(tests_list, verbose = False, monthly_amount = False, default_error_margin = 1, forced_error_margin = None): TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() for test in tests_list: if forced_error_margin: error_margin = forced_error_margin else: error_margin = test.pop("error_margin", default_error_margin) simulation = simulation_from_test(test) for variable, expected_value in test['output_vars'].iteritems(): calculated_value = (simulation.calculate(variable)).sum() / (1 * (not monthly_amount) + 12 * monthly_amount) assert abs(calculated_value - expected_value) < error_margin, u'Variable "{} = {}. Expected: {}'.format( variable, calculated_value, expected_value)
def find_ultimate_dependancies(variable_name, date, tax_benefit_system=None, input_variables=None): if input_variables is None: input_variables = set() elif isinstance(input_variables, list): input_variables = set(input_variables) if tax_benefit_system is None: TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() if variable_name not in TaxBenefitSystem.prestation_by_name: input_variables.add(variable_name) pass else: column = TaxBenefitSystem.prestation_by_name[variable_name] column_formula_type = column.formula_constructor.__bases__ if AlternativeFormula in column_formula_type: formula = column.formula_constructor.alternative_formulas_constructor[0] formula.extract_parameters() elif DatedFormula in column_formula_type: dated_formula_classes = column.formula_constructor.dated_formulas_class formula = [ dated_formula_class["formula_class"] for dated_formula_class in dated_formula_classes if dated_formula_class["start"] <= date <= dated_formula_class["end"] ][0] elif SelectFormula in column_formula_type: formula = column.formula_constructor.formula_constructor_by_main_variable.items()[0][1] elif SimpleFormula in column_formula_type: formula = column.formula_constructor formula.extract_parameters() formula.set_dependencies(column, tax_benefit_system) for variable_name in formula.parameters: find_ultimate_dependancies(variable_name, date, input_variables=input_variables) return list(input_variables)
def adaptation_eipp_to_OF(years = None, filename = "test", check = False): """ Adapation des bases ERFS FPR de l'IPP pour OF """ if years is None: log.error("A list of years to process is needed") for year in years: # load data eipp_survey_collection = SurveyCollection.load(collection = 'eipp') survey = eipp_survey_collection.surveys['eipp_{}'.format(year)] base = survey.get_values(table = "base") ipp2of_input_variables, ipp2of_output_variables = build_ipp2of_variables() # print 'avant',list(base.columns.values) # base.rename(columns = ipp2of_input_variables, inplace = True) # print 'après',list(base.columns.values) TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() data_frame = build_input_OF(base, ipp2of_input_variables, tax_benefit_system) return data_frame
def main(): TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() scenario = tax_benefit_system.new_scenario() scenario.period = periods.period('2014') individu = read_data(data_dir) year_birth = (2014 - individu['age']).astype(int).astype(str) individu['birth'] = year_birth + '-10-02' individu['birth'] = pd.to_datetime(individu['birth']) del individu['age'] scenario.input_variables = { variable_name: {periods.period('2014'): serie.values} for variable_name, serie in individu.iterkv() } simulation = scenario.new_simulation() ars = simulation.calculate('ars') print ars print len(ars)
# # OpenFisca is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import datetime import json import xml.etree.ElementTree import openfisca_france openfisca_france.init_country() from openfisca_core import conv, legislations, legislationsxml, model def test_legislation_xml_file(): legislation_tree = xml.etree.ElementTree.parse(model.PARAM_FILE) legislation_xml_json = conv.check(legislationsxml.xml_legislation_to_json)(legislation_tree.getroot(), state = conv.default_state) legislation_xml_json, errors = legislationsxml.validate_node_xml_json(legislation_xml_json, state = conv.default_state) if errors is not None: errors = conv.embed_error(legislation_xml_json, 'errors', errors) if errors is None: raise ValueError(unicode(json.dumps(legislation_xml_json, ensure_ascii = False,
def compute(self): """ Computing the test case """ self.starting_long_process(_("Computing test case ...")) # TODO # Consistency check on scenario # msg = self.scenario.check_consistency() # if msg: # QMessageBox.critical(self, u"Ménage non valide", # msg, # QMessageBox.Ok, QMessageBox.NoButton) # return False # If it is consistent starts the computation self.action_compute.setEnabled(False) # P, P_default = self.main.parameters.getParam(), self.main.parameters.getParam(defaut = True) # self.simulation.set_param(P, P_default) import openfisca_france TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() axes = [ dict( count = self.nmen, name = 'salaire_imposable', max = self.maxrev, min = 0, ), ] test_case = { "familles": { "fam0": { "parents": ['ind0'], "enfants": [], }, }, "foyers_fiscaux": { "foy0": { "declarants": ['ind0'], "personnes_a_charge": [], } }, "individus": { "ind0": { "activite": 0, "birth": '1970-01-01', "statmarit": 2, }, }, "menages": { "men0": { "personne_de_reference": 'ind0', "conjoint": None, "enfants": [], "autres": [], }, } } self.scenario = tax_benefit_system.new_scenario().init_from_attributes( axes = axes, test_case = test_case, year = 2013, ) print self.scenario.test_case self.scenario.simulation = self.scenario.new_simulation(debug = True) self.scenario.simulation.compute('revdisp') self.main.scenario = self.scenario self.main.refresh_test_case_plugins() self.ending_long_process(_("Test case results are updated"))
# -*- coding: utf-8 -*- import datetime import numpy as np import openfisca_france TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() def check_1_parent(year = 2013): simulation = tax_benefit_system.new_scenario().init_single_entity( axes = [ dict( count = 3, name = 'salaire_imposable', max = 100000, min = 0, ), ], period = year, parent1 = dict(date_naissance = datetime.date(year - 40, 1, 1)), ).new_simulation(debug = True) simulation.calculate('revdisp') salaire_imposable = simulation.get_holder('salaire_imposable').new_test_case_array(simulation.period) assert (salaire_imposable - np.linspace(0, 100000, 3) == 0).all(), 'salaire_imposable: {}'.format(salaire_imposable) def test_1_parent():
def test_rsa_couple(): '''test pour un célibataire avec son age variant entre 18 et 25 ans''' tests_list = [ { "year": 2014, "parent1": { "sali": 0, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 629, }, { "year": 2014, "parent1": { "sali": 5000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 471, }, { "year": 2014, "parent1": { "sali": 10000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 312, }, { "year": 2014, "parent1": { "sali": 12000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 249, }, { "year": 2014, "parent1": { "sali": 13000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 217, }, { "year": 2014, "parent1": { "sali": 14000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 186, }, { "year": 2014, "parent1": { "sali": 15000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 154, }, { "year": 2014, "parent1": { "sali": 16000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 122, }, { "year": 2014, "parent1": { "sali": 17000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 91, }, { "year": 2014, "parent1": { "sali": 18000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 59, }, { "year": 2014, "parent1": { "sali": 19000, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "rsa": 27, }, { "year": 2014, "parent1": { "sali": 9000, "age": 29, }, "parent2": { "sali": 11000, "age": 29, }, "rsa": 0, }, # test si le déclarant a moins 25 ans et le conjoint plus de 25 { "year": 2014, "parent1": { "sali": 400, "age": 21, }, "parent2": { "sali": 3000, "age": 21, }, "rsa": 0, }, # test pour moins de 25 ans pour les deux { "year": 2014, "parent1": { "sali": 400, "age": 21, }, "parent2": { "sali": 3000, "age": 21, }, "rsa": 0, }, # test pour un parent isolé avec 1 enfant de 2 ans # { # "year": 2014, # "parent1": { # "sali": 5000, # "age": 29, # }, # "enfants": [ # dict(age = 2), # ], # "rsa": 550, # }, # test pour un couple avec un enfant de moins de 3 ans # Problème avec site de la cnaf pour changer les revenus { "year": 2014, "parent1": { "sali": 0, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "enfants": [ dict(age = 2), ], "rsa": 565, # "paje": 184.62, }, # TODO: Do not pass # { # "year": 2014, # "parent1": { # "sali": 5000, # "age": 29, # }, # "parent2": { # "sali": 0, # "age": 29, # }, # "enfants": [ # dict(age = 2), # ], # "rsa": 444, # Unsure about the value # }, # TODO: do not pass # { # "year": 2014, # "parent1": { # "sali": 10000, # "age": 29, # }, # "parent2": { # "sali": 0, # "age": 29, # }, # "enfants": [ # dict(age = 2), # ], # "rsa": 565, # TODO: check that value # }, # "revenu": [ # {"year": 2014, "amountdéclarant": 0, "amountconjoint":0, "rsa": 750 }, # {"year": 2014, "amountdéclarant": 5000, "amountconjoint":0, "rsa": 592 }, # {"year": 2014, "amountdéclarant": 10000, "amountconjoint":0, "rsa": 434 }, # {"year": 2014, "amountdéclarant": 12000, "amountconjoint":0, "rsa": 370 }, # {"year": 2014, "amountdéclarant": 13000, "amountconjoint":0, "rsa": 339 }, # {"year": 2014, "amountdéclarant": 14000, "amountconjoint":0, "rsa": 307 }, # {"year": 2014, "amountdéclarant": 15000, "amountconjoint":0, "rsa": 275 }, # {"year": 2014, "amountdéclarant": 16000, "amountconjoint":0, "rsa": 244 }, # {"year": 2014, "amountdéclarant": 17000, "amountconjoint":0, "rsa": 212 }, # {"year": 2014, "amountdéclarant": 18000, "amountconjoint":0, "rsa": 180 }, # {"year": 2014, "amountdéclarant": 19000, "amountconjoint":0, "rsa": 149 }, # {"year": 2014, "amountdéclarant": 20000, "amountconjoint":0, "rsa": 117 }, # {"year": 2014, "amountdéclarant": 21000, "amountconjoint":0, "rsa": 85 }, # {"year": 2014, "amountdéclarant": 22000, "amountconjoint":0, "rsa": 54 }, # {"year": 2014, "amountdéclarant": 23000, "amountconjoint":0, "rsa": 22 }, # {"year": 2014, "amountdéclarant": 24000, "amountconjoint":0, "rsa": 0 }, # ], # test pour enfants ayant entre 3 et 13 ans { "year": 2014, "parent1": { "sali": 0, "age": 29, }, "parent2": { "sali": 0, "age": 29, }, "enfants": [ dict(age = 10), ], "rsa": 750, }, ] error_margin = 1 TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() for test in tests_list: print test test = test.copy() test['period'] = periods.period('year', test.pop('year')) target_rsa = test.pop("rsa") # enlève rsa du dictionnaire et l'assigne a calculated_rsa scenario = tax_benefit_system.new_scenario().init_single_entity(**test) scenario.suggest() simulation = scenario.new_simulation(debug = True) calculated_rsa = simulation.calculate('rsa') / 12 # if test['rsa'] != calculated_rsa : assert abs(calculated_rsa - target_rsa) < error_margin, \ 'calculated_rsa {} is not equal to expected value {}'.format(target_rsa, calculated_rsa) # u'Variable "{} = {}. Expected: {}'.format( # variable, calculated_value, expected_value) print calculated_rsa
from datetime import date # module nécessaire pour la définition des dates, dont notamment les dates de naissances from openfisca_core import periods import openfisca_france # module décrivant le système socio-fiscal français from openfisca_france.tests import base #from modulation_allocations_familiales.reforms import af_modulation from openfisca_core import rates TaxBenefitSystem = openfisca_france.init_country() # Initialisation de la classe décrivant le système socio-fiscal français print TaxBenefitSystem tax_benefit_system = TaxBenefitSystem() # Création d'une instance du système socio-fiscal français import cPickle from openfisca_utils.make_ready_to_use_scenario import make_couple_with_child_scenario salaire_de_base_min = 0 salaire_de_base_maximal = 2*10**5 count = 10000 year_range = range(2012,2016) child_range = range (0, 5) axes_variable = 'salaire_de_base' simulation_var = ['rsa','af','salaire_net','rni','br_pf','aide_logement','decote_gain_fiscal', 'salaire_imposable']+\ ['revdisp', 'irpp','avantage_qf','impo', 'decote', 'salaire_de_base', 'ir_plaf_qf','salaire_de_base','salcho_imp'] def make_result_dict_single(tax_benefit_system = tax_benefit_system, pickle = False, year_range = range(2012,2016), simulation_var = simulation_var, child_range = range (0, 5) ): legislation_var_year = dict() for year in year_range: simulation = dict() for nb_enf in child_range: simulation[nb_enf] = make_couple_with_child_scenario(nombre_enfants = nb_enf, year = year,
def main(liam, annee_leg=None,annee_base=None, mode_output='array'): ''' Send data from the simulation to openfisca - annee_base: si rempli alors on tourne sur cette année-là, sinon sur toute la base mais à voir - annee_leg pour donner les paramètres ''' print "annee base", annee_base ## on recupere la liste des annees en entree if annee_base is not None: if isinstance(annee_base,int): annee_base = [annee_base] else: #TODO: ? peut-être updater pour qaund get_years = HDFStore(path_til + "/output/to_run_leg.h5") years = [x[-4:] for x in dir(get_years.root) if x[0]!='_' ] get_years.close() TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() column_by_name = tax_benefit_system.column_by_name simulation = simulations.Simulation( compact_legislation = None, date = dt.date(2009, 5, 1), debug = None, debug_all = None, tax_benefit_system = tax_benefit_system, trace = None, ) entities = liam.entities entities_name = map( lambda e: e.name, liam.entities) def _get_entity(name): position = entities_name.index(name) return liam.entities[position] input = dict() required = dict() # pour conserver les valeurs que l'on va vouloir sortir de of. #pour chaque entité d'open fisca ## load data : selected_rows = {} for of_ent_name, of_entity in tax_benefit_system.entity_class_by_key_plural.iteritems(): input[of_ent_name] = [] required[of_ent_name] = [] # on cherche l'entité corrspondante dans liam til_ent_name = traduc_entities[of_ent_name] til_entity = _get_entity(til_ent_name) til_entity = til_entity.array.columns selected = np.ones(len(til_entity['id']), dtype=bool) if of_entity.is_persons_entity: selected = (til_entity['men'] > -1) & (til_entity['foy'] > -1) til_entity['quimen'][selected] = deal_with_qui(til_entity['quimen'][selected], til_entity['men'][selected]) til_entity['quifoy'][selected] = deal_with_qui(til_entity['quifoy'][selected], til_entity['foy'][selected]) selected_rows[of_ent_name] = selected of_entity.count = of_entity.step_size = sum(selected) of_entity.roles_count = 10 #TODO: faire une fonction # pour toutes les variables de l'entité of for column in of_entity.column_by_name: # on regarde si on les a dans til sous un nom ou un autre if column in rename_variables.keys() + til_entity.keys() + new_ident.keys(): holder = simulation.get_or_new_holder(column) #on selectionne les valeurs d'entrée if holder.formula is None or column in input_even_if_formula: input[of_ent_name].append(column) if column in til_entity: holder.array = til_entity[column][selected] elif column in new_ident: ident = til_entity[new_ident[column]][selected] holder.array = rankdata(ident, 'dense').astype(int) - 2 else: holder.array = til_entity[rename_variables[column]][selected] # sinon, on conserve la liste des variable pour tout à l'heure else: required[of_ent_name].append(column) # print(of_ent_name) # print(required[of_ent_name]) # print(input[of_ent_name]) ### load of outputs for entity, entity_vars in required.iteritems(): til_ent_name = traduc_entities[entity] til_entity = _get_entity(til_ent_name) til_column = til_entity.array.columns selected = selected_rows[entity] for var in entity_vars: til_column[var][selected] = simulation.calculate(var) #TODO: check incidence of following line til_column[var][~selected] = 0
def compute(self): """ Computing the test case """ self.starting_long_process(_("Computing test case ...")) # TODO # Consistency check on scenario # msg = self.scenario.check_consistency() # if msg: # QMessageBox.critical(self, u"Ménage non valide", # msg, # QMessageBox.Ok, QMessageBox.NoButton) # return False # If it is consistent starts the computation self.action_compute.setEnabled(False) # P, P_default = self.main.parameters.getParam(), self.main.parameters.getParam(defaut = True) # self.simulation.set_param(P, P_default) import openfisca_france TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() axes = [ dict( count=self.nmen, name='sali', max=self.maxrev, min=0, ), ] test_case = { "familles": { "fam0": { "parents": ['ind0'], "enfants": [], }, }, "foyers_fiscaux": { "foy0": { "declarants": ['ind0'], "personnes_a_charge": [], } }, "individus": { "ind0": { "activite": 0, "birth": '1970-01-01', "statmarit": 2, }, }, "menages": { "men0": { "personne_de_reference": 'ind0', "conjoint": None, "enfants": [], "autres": [], }, } } self.scenario = tax_benefit_system.new_scenario().init_from_attributes( axes=axes, test_case=test_case, year=2013, ) print self.scenario.test_case self.scenario.simulation = self.scenario.new_simulation(debug=True) self.scenario.simulation.compute('revdisp') self.main.scenario = self.scenario self.main.refresh_test_case_plugins() self.ending_long_process(_("Test case results are updated"))
# -*- coding: utf-8 -*- ''' Created on 13 févr. 2014 @author: p702207 ''' import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib.pyplot as plt from datetime import datetime from openfisca_core import model import openfisca_france openfisca_france.init_country() from openfisca_core.simulations import ScenarioSimulation def case_study(year=2013): simulation = ScenarioSimulation( ) #on instancie on prends une instance de ScenarioSimulation, on prends un exemple de cette classe là simulation.set_config( year=year, nmen=500, #nombre de ménages maxrev=100000, x_axis='sali') #salaire imposable (une seule personne dans le ménage) simulation.set_param() #prends les paramètres par défaut de la législation df = simulation.get_results_dataframe( ) #renvoie la dataframe avec tout calculé #print df.to_string() return df
def run_OF(dic_input, path_dta_input, param_scenario = None, dic = None, datesim = None, option = 'test_dta'): ''' Lance le calculs sur OF à partir des cas-types issues de TaxIPP input : base .dta issue de l'étape précédente ''' def _test_of_dta(dta_input, dic): ''' Cette fonction teste que la table .dta trouvée correspond au bon scénario ''' data = read_stata(dta_input) dic_dta = data.loc[0, 'dic_scenar'] if str(dic) != str(dic_dta) : print "La base .dta permettant de lancer la simulation OF est absente " print "La base s'en rapprochant le plus a été construite avec les paramètres : ", dic_dta pdb.set_trace() else : data = data.drop('dic_scenar', 1).sort(['id_foyf', 'id_indiv'], ascending = [True, False]) return data def _scenar_dta(dta_input) : ''' cette fonction identifie le scenario enregistré dans la table d'input ''' data = read_stata(dta_input) dic_dta = data.loc[0, 'dic_scenar'] data = data.drop('dic_scenar', 1).sort(['id_foyf', 'id_indiv'], ascending = [True, False]) return dic_dta, data if option == 'test_dta': data_IPP = _test_of_dta(path_dta_input, dic) if option == 'list_dta': dic_scenar, data_IPP = _scenar_dta(path_dta_input) dict_scenar = dict() expression = "dict_scenar.update(" + dic_scenar + ")" eval(expression) datesim = dict_scenar['datesim'] param_scenario = dict_scenar if 'salbrut' in param_scenario.items() : if param_scenario['option'] == 'salbrut': TaxBenefitSystem = openfisca_france.init_country(start_from = "brut") tax_benefit_system = TaxBenefitSystem() del dic_input['sal_irpp_old'] dic_input['sal_brut'] = 'salbrut' else : TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() else : TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() openfisca_survey = build_input_OF(data_IPP, dic_input) openfisca_survey = openfisca_survey.fillna(0) # .sort(['idfoy','noi']) simulation = SurveySimulation() simulation.set_config(year = datesim, survey_filename = openfisca_survey, param_file = os.path.join(os.path.dirname(model.PARAM_FILE), 'param.xml')) simulation.set_param() simulation.compute() if option == 'list_dta': return simulation, simulation.output_table.table, param_scenario else: return simulation, simulation.output_table.table
# GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from __future__ import division import datetime import openfisca_france from openfisca_france.model.cotisations_sociales.travail import CAT, TAUX_DE_PRIME from pandas import DataFrame TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() def test_sal(year = 2013, verbose = False): ''' Tests that _salbrut which computes "salaire brut" from "imposable" yields an amount compatbe with the one obtained from running openfisca satrting with a "salaire brut" ''' maxrev = 50000 for type_sal_category in ['prive_non_cadre', 'prive_cadre']: # , 'public_titulaire_etat']: simulation = tax_benefit_system.new_scenario().init_single_entity( axes = [ dict(name = 'salbrut', max = maxrev, min = 0, count = 11) ], parent1 = dict(
def main(): parser = argparse.ArgumentParser() parser.add_argument( '-s', '--source-dir', default='yaml-clean', help='path of source directory containing clean IPP YAML files') parser.add_argument( '-t', '--target', default='ipp-tax-and-benefit-tables-to-openfisca-parameters.yaml', help= 'path of generated YAML file containing the association between IPP fields to OpenFisca parameters' ) parser.add_argument('-v', '--verbose', action='store_true', default=False, help="increase output verbosity") args = parser.parse_args() logging.basicConfig( level=logging.DEBUG if args.verbose else logging.WARNING, stream=sys.stdout) file_system_encoding = sys.getfilesystemencoding() ipp_infos_by_value = {} for source_dir_encoded, directories_name_encoded, filenames_encoded in os.walk( args.source_dir): directories_name_encoded.sort() for filename_encoded in sorted(filenames_encoded): if not filename_encoded.endswith('.yaml'): continue filename = filename_encoded.decode(file_system_encoding) sheet_name = os.path.splitext(filename)[0] source_file_path_encoded = os.path.join(source_dir_encoded, filename_encoded) relative_file_path_encoded = source_file_path_encoded[ len(args.source_dir):].lstrip(os.sep) relative_file_path = relative_file_path_encoded.decode( file_system_encoding) if sheet_name.isupper(): continue assert sheet_name.islower(), sheet_name log.info(u'Loading file {}'.format(relative_file_path)) with open(source_file_path_encoded) as source_file: data = yaml.load(source_file) rows = data.get(u"Valeurs") if rows is None: log.info(u' Skipping file {} without "Valeurs"'.format( relative_file_path)) continue for row in rows: start = row.get(u"Date d'effet") if start is None: for date_name in date_names: start = row.get(date_name) if start is not None: break else: # No date found. Skip row. continue elif not isinstance(start, datetime.date): start = start[u"Année Revenus"] for name, child in row.iteritems(): if name in date_names: continue for path, value in iter_ipp_values(child): if isinstance(value, basestring): split_value = value.split() if len(split_value) == 2 and split_value[1] in ( u'%', u'AF', # anciens francs u'CFA', # francs CFA u'COTISATIONS', u'EUR', u'FRF', ): value = float(split_value[0]) if isinstance(value, float) and value == int(value): value = int(value) full_path = tuple( relative_file_path.split(os.sep)[:-1]) + ( sheet_name, name) + tuple(path) ipp_infos_by_value.setdefault(value, []).append( dict( path=full_path, start=start, )) # print yaml.dump(ipp_infos_by_value, allow_unicode = True, default_flow_style = False, indent = 2, width = 120) TaxBenefitSystem = init_country() tax_benefit_system = TaxBenefitSystem() # print yaml.dump(tax_benefit_system.legislation_json, allow_unicode = True, default_flow_style = False, indent = 2, # width = 120) # openfisca_infos_by_value = {} # for path, start, value in iter_openfisca_values(tax_benefit_system.legislation_json): # openfisca_infos_by_value.setdefault(value, []).append(dict( # path = tuple(path), # start = start, # )) # print yaml.dump(openfisca_infos_by_value, allow_unicode = True, default_flow_style = False, indent = 2, width = 120) # ipp_count = {} # for path, start, value in iter_openfisca_values(tax_benefit_system.legislation_json): # ipp_infos = ipp_infos_by_value.get(value) # if ipp_infos is None: # # OpenFisca parameter doesn't exit in IPP. # continue # for ipp_info in ipp_infos: # if ipp_info['start'] == start: # ipp_child = ipp_count # ipp_path = ipp_info['path'] # for name in path: # ipp_child = ipp_child.setdefault(name, {}) # ipp_child_count = ipp_child.setdefault('count_by_path', {}) # for ipp_index in range(len(ipp_path)): # ipp_sub_path = ipp_path[:ipp_index + 1] # ipp_child_count[ipp_sub_path] = ipp_child_count.get(ipp_sub_path, 0) + 1 # print yaml.dump(ipp_count, allow_unicode = True, default_flow_style = False, indent = 2, width = 120) starts_by_ipp_path_by_openfisca_path = {} starts_by_openfisca_path_by_ipp_path = {} for path, start, value in iter_openfisca_values( tax_benefit_system.legislation_json): ipp_infos = ipp_infos_by_value.get(value) if ipp_infos is None: # OpenFisca parameter doesn't exit in IPP. continue same_start_ipp_paths = [ ipp_info['path'] for ipp_info in ipp_infos if ipp_info['start'] == start ] if len(same_start_ipp_paths) == 1: ipp_path = same_start_ipp_paths[0] starts_by_ipp_path_by_openfisca_path.setdefault( tuple(path), {}).setdefault(ipp_path, set()).add(start) starts_by_openfisca_path_by_ipp_path.setdefault( ipp_path, {}).setdefault(tuple(path), set()).add(start) # for openfisca_path, starts_by_ipp_path in sorted(starts_by_ipp_path_by_openfisca_path.iteritems()): ## if len(starts_by_ipp_path) == 1: ## print u'.'.join(openfisca_path), '->', u' / '.join(starts_by_ipp_path.keys()[0]) # if len(starts_by_ipp_path) > 1: # print u'.'.join(openfisca_path), '->', starts_by_ipp_path # for ipp_path, starts_by_openfisca_path in sorted(starts_by_openfisca_path_by_ipp_path.iteritems()): # if len(starts_by_openfisca_path) == 1: # print u' / '.join(ipp_path), '->', u'.'.join( # unicode(fragment) # for fragment in starts_by_openfisca_path.keys()[0] # ) ## if len(starts_by_openfisca_path) > 1: ## print u' / '.join(ipp_path), '->', u'.'.join( ## unicode(fragment) ## for fragment in starts_by_openfisca_path.keys()[0] ## ) openfisca_path_by_ipp_tree = collections.OrderedDict() for ipp_path, starts_by_openfisca_path in sorted( starts_by_openfisca_path_by_ipp_path.iteritems()): openfisca_path_by_ipp_sub_tree = openfisca_path_by_ipp_tree for ipp_name in ipp_path[:-1]: openfisca_path_by_ipp_sub_tree = openfisca_path_by_ipp_sub_tree.setdefault( ipp_name, collections.OrderedDict()) ipp_name = ipp_path[-1] openfisca_path_by_ipp_sub_tree[ipp_name] = [ u'.'.join(unicode(fragment) for fragment in openfisca_name) for openfisca_name in sorted(starts_by_openfisca_path) ] with open(args.target, 'w') as target_file: yaml.dump(openfisca_path_by_ipp_tree, target_file, allow_unicode=True, default_flow_style=False, indent=2, width=120) return 0
def test_irpp(): """ test pour un célibataire pour un revenu de 20 000, 50 000 € et 150 000 € et des revenus de différentes origines """ dico = { # test pour un célibataire ayant un revenu salarial (1AJ) "sali": [ {"year": 2010, "amount": 20000, "irpp": -1181}, {"year": 2010, "amount": 50000, "irpp": -7934}, {"year": 2010, "amount": 150000, "irpp": -42338}, {"year": 2011, "amount": 20000, "irpp": -1181}, {"year": 2011, "amount": 50000, "irpp": -7934}, {"year": 2011, "amount": 150000, "irpp": -42338}, {"year": 2012, "amount": 20000, "irpp": -1181}, {"year": 2012, "amount": 50000, "irpp": -7934}, {"year": 2012, "amount": 150000, "irpp": -43222}, {"year": 2013, "amount": 20000, "irpp": -1170}, {"year": 2013, "amount": 50000, "irpp": -7889}, {"year": 2013, "amount": 150000, "irpp": -43076}, ], # test pour un retraité célibataire ayant une pension (1AS) # TODO: test mal spécifié passe toujours au dessus du seuil # qui est d'environ 3500 euros (e.g.faire test pour amount de 500 # 2000 5000, ou faire un seul test au dessus du seuil par année.) "rsti": [ {"year": 2010, "amount": 20000, "irpp": -1181}, {"year": 2010, "amount": 50000, "irpp": -8336}, {"year": 2010, "amount": 150000, "irpp": -46642}, {"year": 2011, "amount": 20000, "irpp": -1181}, {"year": 2011, "amount": 50000, "irpp": -8336}, {"year": 2011, "amount": 150000, "irpp": -46642}, {"year": 2012, "amount": 20000, "irpp": -1181}, {"year": 2012, "amount": 50000, "irpp": -8336}, {"year": 2012, "amount": 150000, "irpp": -46642}, {"year": 2013, "amount": 20000, "irpp": -1170}, {"year": 2013, "amount": 50000, "irpp": -8283}, {"year": 2013, "amount": 150000, "irpp": -46523}, ], # test sur un revenu des actions soumises à un prélèvement libératoire de 21 % (2DA) "f2da": [ {"year": 2010, "amount": 20000, "irpp": 0}, {"year": 2010, "amount": 50000, "irpp": 0}, {"year": 2010, "amount": 150000, "irpp": 0}, {"year": 2011, "amount": 20000, "irpp": 0}, {"year": 2011, "amount": 50000, "irpp": 0}, {"year": 2011, "amount": 150000, "irpp": 0}, {"year": 2012, "amount": 20000, "irpp": 0}, {"year": 2012, "amount": 50000, "irpp": 0}, {"year": 2012, "amount": 150000, "irpp": 0}, # {"year": 2013, "amount": 20000, "irpp": 0}, # {"year": 2013, "amount": 50000, "irpp": 0}, # {"year": 2013, "amount": 150000, "irpp": 0},TODO: check mahdi if 2DA exist in 2013 ], # test sur un revenu (2DH) issu des produits d'assurance vie et de capitalisation soumis au prélèvement # libératoire de 7.5 % "f2dh": [ {"year": 2010, "amount": 20000, "irpp": 345}, {"year": 2010, "amount": 50000, "irpp": 345}, {"year": 2010, "amount": 150000, "irpp": 345}, {"year": 2011, "amount": 20000, "irpp": 345}, {"year": 2011, "amount": 50000, "irpp": 345}, {"year": 2011, "amount": 150000, "irpp": 345}, {"year": 2012, "amount": 20000, "irpp": 345}, {"year": 2012, "amount": 50000, "irpp": 345}, {"year": 2012, "amount": 150000, "irpp": 345}, {"year": 2013, "amount": 20000, "irpp": 345}, {"year": 2013, "amount": 50000, "irpp": 345}, {"year": 2013, "amount": 150000, "irpp": 345} ], # test sur un revenu des actions et parts (2DC) "f2dc": [ {"year": 2010, "amount": 20000, "irpp": 0}, {"year": 2010, "amount": 50000, "irpp": -2976}, {"year": 2010, "amount": 150000, "irpp": -22917}, {"year": 2011, "amount": 20000, "irpp": 0}, {"year": 2011, "amount": 50000, "irpp": -2976}, {"year": 2011, "amount": 150000, "irpp": -22917}, {"year": 2012, "amount": 20000, "irpp": 0}, {"year": 2012, "amount": 50000, "irpp": -3434}, {"year": 2012, "amount": 150000, "irpp": -23542}, # {"year": 2013, "amount": 20000, "irpp": -}, # {"year": 2013, "amount": 50000, "irpp": -}, # {"year": 2013, "amount": 150000, "irpp": -}, TODO: check with mahdi ], # test sur le revenu de valeurs mobilières (2TS) "f2ts": [ {"year": 2010, "amount": 20000, "irpp": -1461}, {"year": 2010, "amount": 50000, "irpp": -9434}, {"year": 2010, "amount": 150000, "irpp": -48142}, {"year": 2011, "amount": 20000, "irpp": -1461}, {"year": 2011, "amount": 50000, "irpp": -9434}, {"year": 2011, "amount": 150000, "irpp": -48142}, {"year": 2012, "amount": 20000, "irpp": -1461}, {"year": 2012, "amount": 50000, "irpp": -9434}, {"year": 2012, "amount": 150000, "irpp": -48142}, {"year": 2013, "amount": 20000, "irpp": -1450}, {"year": 2013, "amount": 50000, "irpp": -9389}, {"year": 2013, "amount": 150000, "irpp": -48036}, ], # test sur les intérêts (2TR) "f2tr": [ {"year": 2010, "amount": 20000, "irpp": -1461}, {"year": 2010, "amount": 50000, "irpp": -9434}, {"year": 2010, "amount": 150000, "irpp": -48142}, {"year": 2011, "amount": 20000, "irpp": -1461}, {"year": 2011, "amount": 50000, "irpp": -9434}, {"year": 2011, "amount": 150000, "irpp": -48142}, {"year": 2012, "amount": 20000, "irpp": -1461}, {"year": 2012, "amount": 50000, "irpp": -9434}, {"year": 2012, "amount": 150000, "irpp": -48142}, {"year": 2013, "amount": 20000, "irpp": -1450}, {"year": 2013, "amount": 50000, "irpp": -9389}, {"year": 2013, "amount": 150000, "irpp": -48036}, ], # test sur les revenus fonciers (4BA) "f4ba": [ {"year": 2010, "amount": 20000, "irpp": -1461}, {"year": 2010, "amount": 50000, "irpp": -9434}, {"year": 2010, "amount": 150000, "irpp": -48142}, {"year": 2011, "amount": 20000, "irpp": -1461}, {"year": 2011, "amount": 50000, "irpp": -9434}, {"year": 2011, "amount": 150000, "irpp": -48142}, {"year": 2012, "amount": 20000, "irpp": -1461}, {"year": 2012, "amount": 50000, "irpp": -9434}, {"year": 2012, "amount": 150000, "irpp": -48142}, {"year": 2013, "amount": 20000, "irpp": -1450}, {"year": 2013, "amount": 50000, "irpp": -9389}, {"year": 2013, "amount": 150000, "irpp": -48036}, ], # test sur les plus-values mobilières (3VG) "f3vg": [ {"year": 2010, "amount": 20000, "irpp": -3600}, {"year": 2010, "amount": 50000, "irpp": -9000}, {"year": 2010, "amount": 150000, "irpp": -27000}, {"year": 2011, "amount": 20000, "irpp": -3800}, {"year": 2011, "amount": 50000, "irpp": -9500}, {"year": 2011, "amount": 150000, "irpp": -28500}, {"year": 2012, "amount": 20000, "irpp": -4800}, {"year": 2012, "amount": 50000, "irpp": -12000}, {"year": 2012, "amount": 150000, "irpp": -36000}, {"year": 2013, "amount": 20000, "irpp": -1450}, {"year": 2013, "amount": 50000, "irpp": -9389}, {"year": 2013, "amount": 150000, "irpp": -48036}, ], # test sur les plus-values immobilières (3VZ) "f3vz": [ # TODO: voir pour tests 2010 {"year": 2011, "amount": 20000, "irpp": 0}, {"year": 2011, "amount": 50000, "irpp": 0}, {"year": 2011, "amount": 150000, "irpp": 0}, {"year": 2012, "amount": 20000, "irpp": 0}, {"year": 2012, "amount": 50000, "irpp": 0}, {"year": 2012, "amount": 150000, "irpp": 0}, {"year": 2013, "amount": 20000, "irpp": 0}, {"year": 2013, "amount": 50000, "irpp": 0}, {"year": 2013, "amount": 150000, "irpp": 0}, ], } for revenu, test_list in dico.iteritems(): for item in test_list: year = item["year"] amount = item["amount"] irpp = item["irpp"] fiscal_values = ["f2da", "f2dh", "f2dc", "f2ts", "f2tr", "f4ba", "f3vg", "f3vz"] TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() # if revenu != "f2dc": # continue if revenu in ["rsti", "sali"]: simulation = tax_benefit_system.new_scenario().init_single_entity( period = periods.period('year', year), parent1 = { 'birth': datetime.date(year - 40, 1, 1), revenu: amount, }, ).new_simulation(debug = True) elif revenu in fiscal_values: simulation = tax_benefit_system.new_scenario().init_single_entity( period = periods.period('year', year), parent1 = { 'birth': datetime.date(year - 40, 1, 1), }, foyer_fiscal = {revenu: amount}, ).new_simulation(debug = True) yield check_irpp, amount, irpp, revenu, simulation, year
def test_couple(verbose = False): """Couple de retraités""" tests_list = [ { "year": 2013, "input_vars": { "age": 73, "activite": 3, "rsti": 12500, }, "output_vars": { "rst": 2 * 12500, } }, ] TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() passed = True for test in tests_list: year = test["year"] parent1 = dict(birth = datetime.date(year - 40, 1, 1)) parent2 = dict(birth = datetime.date(year - 40, 1, 1)) menage = dict() foyer_fiscal = dict() for variable, value in test['input_vars'].iteritems(): if variable == "age": parent1['birth'] = datetime.date(year - value, 1, 1) parent2['birth'] = datetime.date(year - value, 1, 1) elif tax_benefit_system.column_by_name[variable].entity == 'men': menage[variable] = value elif tax_benefit_system.column_by_name[variable].entity == 'ind': parent1[variable] = value parent2[variable] = value elif tax_benefit_system.column_by_name[variable].entity == 'foy': foyer_fiscal[variable] = value simulation = tax_benefit_system.new_scenario().init_single_entity( period = periods.period('year', year), parent1 = parent1, parent2 = parent2, menage = menage, foyer_fiscal = foyer_fiscal, ).new_simulation(debug = True) for variable, value in test['output_vars'].iteritems(): computed_value = (simulation.calculate(variable)).sum() test_assertion = abs(abs(computed_value) - value) < 1 expression = "Test failed for variable %s on year %i and case %s:\n" \ "OpenFisca value: %s\n" \ " Real value: %s \n" % (variable, year, test['input_vars'], abs(computed_value), value) if not test_assertion: print expression passed = False else: if verbose: expression = "Test passed for variable %s on year %i and case %s:\n" \ "OpenFisca value: %s\n" \ " Real value: %s \n" % (variable, year, test['input_vars'], abs(computed_value), value) print expression assert passed, "Test failed for some variables"
# -*- coding: utf-8 -*- import datetime import json from openfisca_core import conv, legislations, legislationsxml from openfisca_france import init_country # Exceptionally for this test do not import TaxBenefitSystem from tests.base. TaxBenefitSystem = init_country() def check_legislation_xml_file(year): legislation_tree = conv.check( legislationsxml.make_xml_legislation_info_list_to_xml_element(False))( TaxBenefitSystem.legislation_xml_info_list, state=conv.default_state) legislation_xml_json = conv.check(legislationsxml.xml_legislation_to_json)( legislation_tree, state=conv.default_state, ) legislation_xml_json, errors = legislationsxml.validate_legislation_xml_json( legislation_xml_json, state=conv.default_state) if errors is not None: errors = conv.embed_error(legislation_xml_json, 'errors', errors) if errors is None: raise ValueError( unicode( json.dumps(legislation_xml_json,
def test_cotsoc(): """ Cotisations sur les revenus du capital """ cotsoc_cap = { # test sur un revenu des actions soumises à un prélèvement libératoire de 21 % (2DA) "f2da" : [ {"year" : 2012, "amount": 20000, "vars" : {"prelsoc_cap_lib":-(4.5 + 2 + 0.3) * 0.01 * 20000, "csg_cap_lib":-.082 * 20000, "crds_cap_lib":-.005 * 20000 } }, {"year" : 2011, "amount": 20000, "vars" : {"prelsoc_cap_lib":-(3.4 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_lib":-.082 * 20000, "crds_cap_lib":-.005 * 20000 } }, {"year" : 2010, "amount": 20000, "vars" : {"prelsoc_cap_lib":-(2.2 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_lib":-.082 * 20000, "crds_cap_lib":-.005 * 20000 } } ], # Célibataire sans enfant # test sur un revenu des actions et parts (2DC) "f2dc" :[ {"year" : 2013, "amount": 20000, "vars" : {"prelsoc_cap_bar":-1360, "csg_cap_bar":-1640, "crds_cap_bar":-100, "ir_plaf_qf": 330, "irpp":-0} }, {"year" : 2012, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(4.5 + 2 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, {"year" : 2011, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(3.4 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, {"year" : 2010, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(2.2 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, ], # # test sur le Revenus imposables des titres non côtés détenus dans le PEA et distributions perçues via votre entreprise # ## donnant droit à abattement (2fu) "f2fu" :[ {"year" : 2013, "amount": 20000, "vars" : {"prelsoc_cap_bar":-1360, "csg_cap_bar":-1640, "crds_cap_bar":-100, "ir_plaf_qf": 330, "irpp":0} }, ], # Autres revenus distribués et revenus des structures soumises hors de France à un régime fiscal privilégié (2Go) "f2go" :[ {"year" : 2013, "amount": 20000, "vars" : {"rev_cat_rvcm" : 25000, "prelsoc_cap_bar":-1700, "csg_cap_bar":-2050, "crds_cap_bar":-125, "ir_plaf_qf": 2150, "irpp":-2150 } }, ], "f2ts" :[ {"year" : 2013, "amount": 20000, "vars" : {"rev_cat_rvcm" : 20000, "prelsoc_cap_bar":-1360, "csg_cap_bar":-1640, "crds_cap_bar":-100, "ir_plaf_qf": 1450, "irpp":-1450 } }, {"year" : 2012, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(4.5 + 2 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, {"year" : 2011, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(3.4 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, {"year" : 2010, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(2.2 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, ], # # test sur les intérêts (2TR) "f2tr" :[ {"year" : 2013, "amount": 20000, "vars" : {"prelsoc_cap_bar":-1360, "csg_cap_bar":-1640, "crds_cap_bar":-100, "ir_plaf_qf": 1450, "irpp":-1450 } }, {"year" : 2012, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(4.5 + 2 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000, } }, {"year" : 2011, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(3.4 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, {"year" : 2010, "amount": 20000, "vars" : {"prelsoc_cap_bar":-(2.2 + 1.1 + 0.3) * 0.01 * 20000, "csg_cap_bar":-.082 * 20000, "crds_cap_bar":-.005 * 20000 } }, ], # # test sur les revenus fonciers (4BA) "f4ba":[ {"year" : 2013, "amount": 20000, "vars" : {"prelsoc_fon":-1360, "csg_fon":-1640, "crds_fon":-100, "ir_plaf_qf": 1450, "irpp":-1450} }, {"year" : 2012, "amount": 20000, "vars" : {"prelsoc_fon":-(4.5 + 2 + 0.3) * 0.01 * 20000, "csg_fon":-.082 * 20000, "crds_fon":-.005 * 20000, "irpp" :-1461 } }, {"year" : 2011, "amount": 20000, "vars" : {"prelsoc_fon":-(3.4 + 1.1 + 0.3) * 0.01 * 20000, "csg_fon":-.082 * 20000, "crds_fon":-.005 * 20000 } }, {"year" : 2010, "amount": 20000, "vars" : {"prelsoc_fon":-(2.2 + 1.1 + 0.3) * 0.01 * 20000, "csg_fon":-.082 * 20000, "crds_fon":-.005 * 20000 } }, ], # # test (3VG) Plus-values de cession de valeurs mobilières, droits sociaux et gains assimilés "f3vg" :[ {"year" : 2013, "amount": 20000, "vars" : {"prelsoc_pv_mo":-1360, "csg_pv_mo":-1640, "crds_pv_mo":-100, "ir_plaf_qf": 1450, "irpp":-1450} }, {"year" : 2012, "amount": 20000, "vars" : {"prelsoc_pv_mo":-(4.5 + 2 + 0.3) * 0.01 * 20000, "csg_pv_mo":-.082 * 20000, "crds_pv_mo":-.005 * 20000 } }, {"year" : 2011, "amount": 20000, "vars" : {"prelsoc_pv_mo":-(3.4 + 1.1 + 0.3) * 0.01 * 20000, "csg_pv_mo":-.082 * 20000, "crds_pv_mo":-.005 * 20000 } }, {"year" : 2010, "amount": 20000, "vars" : {"prelsoc_pv_mo":-(2.2 + 1.1 + 0.3) * 0.01 * 20000, "csg_pv_mo":-.082 * 20000, "crds_pv_mo":-.005 * 20000 } }, {"year" : 2006, "amount": 20000, "vars" : {"prelsoc_pv_mo":-460 , "csg_pv_mo":-1640, "crds_pv_mo":-100} }, ], # # test sur les plus-values immobilières (3VZ) "f3vz" :[ {"year" : 2012, "amount": 20000, "vars" : {"prelsoc_pv_immo":-(4.5 + 2 + 0.3) * 0.01 * 20000, "csg_pv_immo":-.082 * 20000, "crds_pv_immo":-.005 * 20000 } }, {"year" : 2011, "amount": 20000, "vars" : {"prelsoc_pv_immo":-(3.4 + 1.1 + 0.3) * 0.01 * 20000, "csg_pv_immo":-.082 * 20000, "crds_pv_immo":-.005 * 20000 } }, {"year" : 2010, "amount": 20000, "vars" : {"prelsoc_pv_immo":-(2.2 + 1.1 + 0.3) * 0.01 * 20000, "csg_pv_immo":-.082 * 20000, "crds_pv_immo":-.005 * 20000 } }, ], } for revenu, test_list in cotsoc_cap.iteritems(): for item in test_list: year = item["year"] amount = item["amount"] for var, value in item["vars"].iteritems(): parent1 = dict(birth = datetime.date(year - 40, 1, 1)) foyer_fiscal = dict() if revenu in ["rsti", "sali"]: parent1[revenu] = value elif revenu in ["f2da", "f2dh", "f2dc", "f2ts", "f2tr", "f4ba", "f3vg", "f3vz", "f2fu", "f4ba", "f2go"]: foyer_fiscal[revenu] = amount else: print revenu assert False TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() simulation = tax_benefit_system.new_scenario().init_single_entity( period = periods.period('year', year), parent1 = parent1, foyer_fiscal = foyer_fiscal, ).new_simulation(debug = True) val = simulation.calculate(var) difference = abs(val - value) passed = (difference < 1) if not passed: expression = "Test failed for variable %s on year %i : \n OpenFisca value : %s \n Real value : %s \n" % (var, year, abs(val), value) assert passed, expression
# GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import datetime import json import xml.etree.ElementTree from openfisca_core import conv, legislations, legislationsxml from openfisca_france import init_country TaxBenefitSystem = init_country() def check_legislation_xml_file(year): legislation_tree = xml.etree.ElementTree.parse(TaxBenefitSystem.legislation_xml_file_path) legislation_xml_json = conv.check(legislationsxml.xml_legislation_to_json)(legislation_tree.getroot(), state = conv.default_state) legislation_xml_json, errors = legislationsxml.validate_legislation_xml_json(legislation_xml_json, state = conv.default_state) if errors is not None: errors = conv.embed_error(legislation_xml_json, 'errors', errors) if errors is None: raise ValueError(unicode(json.dumps(legislation_xml_json, ensure_ascii = False, indent = 2)).encode('utf-8')) raise ValueError(u'{0} for: {1}'.format(
def main(): parser = argparse.ArgumentParser() parser.add_argument('-s', '--source-dir', default = 'yaml-clean', help = 'path of source directory containing clean IPP YAML files') parser.add_argument('-t', '--target', default = 'ipp-tax-and-benefit-tables-to-openfisca-parameters.yaml', help = 'path of generated YAML file containing the association between IPP fields to OpenFisca parameters') parser.add_argument('-v', '--verbose', action = 'store_true', default = False, help = "increase output verbosity") args = parser.parse_args() logging.basicConfig(level = logging.DEBUG if args.verbose else logging.WARNING, stream = sys.stdout) file_system_encoding = sys.getfilesystemencoding() ipp_infos_by_value = {} for source_dir_encoded, directories_name_encoded, filenames_encoded in os.walk(args.source_dir): directories_name_encoded.sort() for filename_encoded in sorted(filenames_encoded): if not filename_encoded.endswith('.yaml'): continue filename = filename_encoded.decode(file_system_encoding) sheet_name = os.path.splitext(filename)[0] source_file_path_encoded = os.path.join(source_dir_encoded, filename_encoded) relative_file_path_encoded = source_file_path_encoded[len(args.source_dir):].lstrip(os.sep) relative_file_path = relative_file_path_encoded.decode(file_system_encoding) if sheet_name.isupper(): continue assert sheet_name.islower(), sheet_name log.info(u'Loading file {}'.format(relative_file_path)) with open(source_file_path_encoded) as source_file: data = yaml.load(source_file) rows = data.get(u"Valeurs") if rows is None: log.info(u' Skipping file {} without "Valeurs"'.format(relative_file_path)) continue for row in rows: start = row.get(u"Date d'effet") if start is None: for date_name in date_names: start = row.get(date_name) if start is not None: break else: # No date found. Skip row. continue elif not isinstance(start, datetime.date): start = start[u"Année Revenus"] for name, child in row.iteritems(): if name in date_names: continue for path, value in iter_ipp_values(child): if isinstance(value, basestring): split_value = value.split() if len(split_value) == 2 and split_value[1] in ( u'%', u'AF', # anciens francs u'CFA', # francs CFA u'COTISATIONS', u'EUR', u'FRF', ): value = float(split_value[0]) if isinstance(value, float) and value == int(value): value = int(value) full_path = tuple(relative_file_path.split(os.sep)[:-1]) + (sheet_name, name) + tuple(path) ipp_infos_by_value.setdefault(value, []).append(dict( path = full_path, start = start, )) # print yaml.dump(ipp_infos_by_value, allow_unicode = True, default_flow_style = False, indent = 2, width = 120) TaxBenefitSystem = init_country() tax_benefit_system = TaxBenefitSystem() # print yaml.dump(tax_benefit_system.legislation_json, allow_unicode = True, default_flow_style = False, indent = 2, # width = 120) # openfisca_infos_by_value = {} # for path, start, value in iter_openfisca_values(tax_benefit_system.legislation_json): # openfisca_infos_by_value.setdefault(value, []).append(dict( # path = tuple(path), # start = start, # )) # print yaml.dump(openfisca_infos_by_value, allow_unicode = True, default_flow_style = False, indent = 2, width = 120) # ipp_count = {} # for path, start, value in iter_openfisca_values(tax_benefit_system.legislation_json): # ipp_infos = ipp_infos_by_value.get(value) # if ipp_infos is None: # # OpenFisca parameter doesn't exit in IPP. # continue # for ipp_info in ipp_infos: # if ipp_info['start'] == start: # ipp_child = ipp_count # ipp_path = ipp_info['path'] # for name in path: # ipp_child = ipp_child.setdefault(name, {}) # ipp_child_count = ipp_child.setdefault('count_by_path', {}) # for ipp_index in range(len(ipp_path)): # ipp_sub_path = ipp_path[:ipp_index + 1] # ipp_child_count[ipp_sub_path] = ipp_child_count.get(ipp_sub_path, 0) + 1 # print yaml.dump(ipp_count, allow_unicode = True, default_flow_style = False, indent = 2, width = 120) starts_by_ipp_path_by_openfisca_path = {} starts_by_openfisca_path_by_ipp_path = {} for path, start, value in iter_openfisca_values(tax_benefit_system.legislation_json): ipp_infos = ipp_infos_by_value.get(value) if ipp_infos is None: # OpenFisca parameter doesn't exit in IPP. continue same_start_ipp_paths = [ ipp_info['path'] for ipp_info in ipp_infos if ipp_info['start'] == start ] if len(same_start_ipp_paths) == 1: ipp_path = same_start_ipp_paths[0] starts_by_ipp_path_by_openfisca_path.setdefault(tuple(path), {}).setdefault(ipp_path, set()).add(start) starts_by_openfisca_path_by_ipp_path.setdefault(ipp_path, {}).setdefault(tuple(path), set()).add(start) # for openfisca_path, starts_by_ipp_path in sorted(starts_by_ipp_path_by_openfisca_path.iteritems()): ## if len(starts_by_ipp_path) == 1: ## print u'.'.join(openfisca_path), '->', u' / '.join(starts_by_ipp_path.keys()[0]) # if len(starts_by_ipp_path) > 1: # print u'.'.join(openfisca_path), '->', starts_by_ipp_path # for ipp_path, starts_by_openfisca_path in sorted(starts_by_openfisca_path_by_ipp_path.iteritems()): # if len(starts_by_openfisca_path) == 1: # print u' / '.join(ipp_path), '->', u'.'.join( # unicode(fragment) # for fragment in starts_by_openfisca_path.keys()[0] # ) ## if len(starts_by_openfisca_path) > 1: ## print u' / '.join(ipp_path), '->', u'.'.join( ## unicode(fragment) ## for fragment in starts_by_openfisca_path.keys()[0] ## ) openfisca_path_by_ipp_tree = collections.OrderedDict() for ipp_path, starts_by_openfisca_path in sorted(starts_by_openfisca_path_by_ipp_path.iteritems()): openfisca_path_by_ipp_sub_tree = openfisca_path_by_ipp_tree for ipp_name in ipp_path[:-1]: openfisca_path_by_ipp_sub_tree = openfisca_path_by_ipp_sub_tree.setdefault(ipp_name, collections.OrderedDict()) ipp_name = ipp_path[-1] openfisca_path_by_ipp_sub_tree[ipp_name] = [ u'.'.join( unicode(fragment) for fragment in openfisca_name ) for openfisca_name in sorted(starts_by_openfisca_path) ] with open(args.target, 'w') as target_file: yaml.dump(openfisca_path_by_ipp_tree, target_file, allow_unicode = True, default_flow_style = False, indent = 2, width = 120) return 0
# # OpenFisca is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from __future__ import division import sys import logging import openfisca_france openfisca_france.init_country(start_from = "brut") from openfisca_core.simulations import ScenarioSimulation from openfisca_france.model.cotisations_sociales.travail import CAT def test_case_study(year = 2013, verbose = False): for type_sal_category in ['prive_non_cadre', 'prive_cadre', 'public_titulaire_etat']: simulation = ScenarioSimulation() maxrev = 24000 simulation.set_config(year = year, reforme = False, nmen = 1, x_axis = 'salbrut') # Add husband/wife on the same tax sheet (foyer). # simulation.scenario.addIndiv(1, datetime.date(1975, 1, 1), 'conj', 'part') simulation.scenario.indiv[0]['salbrut'] = maxrev simulation.scenario.indiv[0]['type_sal'] = CAT[type_sal_category]
# GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from __future__ import division import sys import datetime import logging import nose import pdb import openfisca_france openfisca_france.init_country(start_from = "brut") from openfisca_core.simulations import ScenarioSimulation from openfisca_france.model.cotisations_sociales.travail import CAT def test_cotsoc_celib(): """ test pour un célibataire """ # Comparaison avec la fiche de paie IPP calculé avec une cotisation transport correspondant à Paris ((.026) # alors qu'Openfisca la cacule pour Lyon (.0175) tests_list = [ # Célibataires (pas de supplément familial de traitement {"year" : 2012, "input_vars":