def test_decomposition(print_decomposition = False): simulation = tax_benefit_system.new_scenario().init_single_entity( period = "2013-01", parent1 = dict( effectif_entreprise = 3000, exposition_accident = 3, code_postal_entreprise = "75001", ratio_alternants = .025, salaire_de_base = {"2013": 12 * 3000}, taille_entreprise = 3, type_sal = 0, ), menage = dict( zone_apl = 1, ), ).new_simulation(debug = True) xml_file_path = os.path.join( tax_benefit_system.DECOMP_DIR, "fiche_de_paie_decomposition.xml" ) decomposition_json = decompositions.get_decomposition_json(tax_benefit_system, xml_file_path = xml_file_path) simulations = [simulation] response = decompositions.calculate(simulations, decomposition_json) if print_decomposition: print unicode( json.dumps(response, encoding = 'utf-8', ensure_ascii = False, indent = 2) )
def data_frame_from_decomposition_json(simulation, decomposition_json = None, reference_simulation = None, remove_null = False, label = True, name = False): # currency = simulation.tax_benefit_system.CURRENCY # TODO : put an option to add currency, for now useless assert label or name, "At least label or name should be True" if decomposition_json is None: decomposition_json = decompositions.get_decomposition_json(simulation.tax_benefit_system) data = OutNode.init_from_decomposition_json(simulation, decomposition_json) index = [row.desc for row in data if row.desc not in ('root')] data_frame = None for row in data: if row.desc not in ('root'): if data_frame is None: value_columns = ['value_' + str(i) for i in range(len(row.vals))] if len(row.vals) > 1 else ['value'] data_frame = pandas.DataFrame(index = index, columns = ['name'] + value_columns) data_frame['name'][row.desc] = row.code data_frame.loc[row.desc, value_columns] = row.vals data_frame.index.name = "label" if remove_null: variables_to_remove = [] for variable in data_frame.index: if (data_frame.loc[variable, value_columns] == 0).all(): variables_to_remove.append(variable) data_frame.drop(variables_to_remove, inplace = True) data_frame.reset_index(inplace = True) return data_frame
def test_decomposition(print_decomposition=False): simulation = base.tax_benefit_system.new_scenario().init_single_entity( period="2013-01", parent1=dict( effectif_entreprise=3000, exposition_accident=3, code_postal_entreprise="75001", ratio_alternants=.025, salaire_de_base={"2013": 12 * 3000}, taille_entreprise=3, categorie_salarie=0, ), menage=dict(zone_apl=1, ), ).new_simulation() xml_file_path = os.path.join(decompositions_directory, "fiche_de_paie_decomposition.xml") decomposition_json = decompositions.get_decomposition_json( base.tax_benefit_system, xml_file_path=xml_file_path) simulations = [simulation] response = decompositions.calculate(simulations, decomposition_json) if print_decomposition: print unicode( json.dumps(response, encoding='utf-8', ensure_ascii=False, indent=2))
def test_decomposition(print_decomposition = False): simulation = tax_benefit_system.new_scenario().init_single_entity( period = "2013-01", parent1 = dict( effectif_entreprise = 3000, exposition_accident = "tres_eleve", code_postal_entreprise = "75001", ratio_alternants = .025, salaire_de_base = {"2013": 12 * 3000}, taille_entreprise = "de_20_a_249", categorie_salarie = "prive_non_cadre", ), menage = dict( zone_apl = "zone_1", ), ).new_simulation() xml_file_path = os.path.join( decompositions_directory, "fiche_de_paie_decomposition.xml" ) decomposition_json = decompositions.get_decomposition_json( tax_benefit_system, xml_file_path = xml_file_path) simulations = [simulation] response = decompositions.calculate(simulations, decomposition_json) if print_decomposition: logger.debug(to_unicode( json.dumps(response, encoding = 'utf-8', ensure_ascii = False, indent = 2) ))
def test_decomposition(print_decomposition=False): from openfisca_core.decompositions import calculate, get_decomposition_json import json import os simulation = tax_benefit_system.new_scenario().init_single_entity( period="2013-01", parent1=dict( effectif_entreprise=3000, exposition_accident=3, localisation_entreprise="75001", ratio_alternants=.025, salaire_de_base={"2013": 12 * 3000}, taille_entreprise=3, type_sal=0, ), menage=dict(zone_apl=1, ), ).new_simulation(debug=True) xml_file_path = os.path.join(tax_benefit_system.DECOMP_DIR, "fiche_de_paie_decomposition.xml") decomposition_json = get_decomposition_json(xml_file_path, tax_benefit_system) response = calculate(simulation, decomposition_json) if print_decomposition: print unicode( json.dumps(response, encoding='utf-8', ensure_ascii=False, indent=2))
def get_cached_or_new_decomposition_json(tax_benefit_system): xml_file_path = tax_benefit_system.decomposition_file_path global decomposition_json_by_file_path_cache decomposition_json = decomposition_json_by_file_path_cache.get(xml_file_path) if decomposition_json is None: decomposition_json = decompositions.get_decomposition_json(tax_benefit_system, xml_file_path) decomposition_json_by_file_path_cache[xml_file_path] = decomposition_json return decomposition_json
def get_cached_or_new_decomposition_json(tax_benefit_system): xml_file_path = tax_benefit_system.decomposition_file_path global decomposition_json_by_file_path_cache decomposition_json = decomposition_json_by_file_path_cache.get(xml_file_path) if decomposition_json is None: decomposition_json = decompositions.get_decomposition_json(tax_benefit_system, xml_file_path) decomposition_json_by_file_path_cache[xml_file_path] = decomposition_json return decomposition_json
def get_cached_or_new_decomposition_json(tax_benefit_system, xml_file_name = None): if xml_file_name is None: xml_file_name = tax_benefit_system.DEFAULT_DECOMP_FILE global decomposition_json_by_file_path_cache decomposition_json = decomposition_json_by_file_path_cache.get(xml_file_name) if decomposition_json is None: xml_file_path = os.path.join(tax_benefit_system.DECOMP_DIR, xml_file_name) decomposition_json = decompositions.get_decomposition_json(tax_benefit_system, xml_file_path) decomposition_json_by_file_path_cache[xml_file_name] = decomposition_json return decomposition_json
def get_cached_or_new_decomposition_json(tax_benefit_system, xml_file_name = None): if xml_file_name is None: xml_file_name = tax_benefit_system.DEFAULT_DECOMP_FILE global decomposition_json_by_file_path_cache decomposition_json = decomposition_json_by_file_path_cache.get(xml_file_name) if decomposition_json is None: xml_file_path = os.path.join(tax_benefit_system.DECOMP_DIR, xml_file_name) decomposition_json = decompositions.get_decomposition_json(tax_benefit_system, xml_file_path) decomposition_json_by_file_path_cache[xml_file_name] = decomposition_json return decomposition_json
def test_decomposition_calculate(): xml_file_path = tax_benefit_system.decomposition_file_path decomposition_json = decompositions.get_decomposition_json( tax_benefit_system, xml_file_path) year = 2013 simulation = tax_benefit_system.new_scenario().init_single_entity( period=year, parent1={}, ).new_simulation() decomposition = decompositions.calculate([simulation], decomposition_json) assert isinstance(decomposition, dict)
def test_decomposition_calculate(): decompositions_directory = base.tax_benefit_system.DECOMP_DIR xml_file_path = os.path.join(decompositions_directory, base.tax_benefit_system.DEFAULT_DECOMP_FILE) decomposition_json = decompositions.get_decomposition_json(base.tax_benefit_system, xml_file_path) year = 2013 simulation = base.tax_benefit_system.new_scenario().init_single_entity( period = year, parent1 = {}, ).new_simulation(debug = True) decomposition = decompositions.calculate([simulation], decomposition_json) assert isinstance(decomposition, dict)
def test_fiche_de_paie_bareme(bareme=True): reform_simulation, reference_simulation = create_simulation(bareme=bareme) xml_file_path = os.path.join( tax_benefit_system.DECOMP_DIR, "fiche_de_paie_decomposition.xml" ) decomposition_json = get_decomposition_json(tax_benefit_system, xml_file_path) data_frame = data_frame_from_decomposition_json( reference_simulation, decomposition_json = decomposition_json, remove_null = True) return data_frame
def test_decomposition_calculate(): decompositions_directory = base.tax_benefit_system.DECOMP_DIR xml_file_path = os.path.join(decompositions_directory, base.tax_benefit_system.DEFAULT_DECOMP_FILE) decomposition_json = decompositions.get_decomposition_json( base.tax_benefit_system, xml_file_path) year = 2013 simulation = base.tax_benefit_system.new_scenario().init_single_entity( period=year, parent1={}, ).new_simulation() decomposition = decompositions.calculate([simulation], decomposition_json) assert isinstance(decomposition, dict)
def draw_waterfall(simulation, axes = None, decomposition_json = None, visible = None): if axes is None: fig = plt.figure() axes = fig.gca() if decomposition_json is None: decomposition_json = decompositions.get_decomposition_json(simulation.tax_benefit_system) currency = simulation.tax_benefit_system.CURRENCY data = OutNode.init_from_decomposition_json(simulation, decomposition_json) data.setLeavesVisible() if visible is not None: for code in visible: data[code].visible = 1 axes.clear() draw_waterfall_from_node_data(data, axes, currency)
def draw_bareme(simulation, axes = None, x_axis = None, reference_simulation = None, decomposition_json = None, visible_lines = None, hide_all = False, legend = True, legend_position = None): if axes is None: fig = plt.figure() axes = fig.gca() if decomposition_json is None: decomposition_json = decompositions.get_decomposition_json(simulation.tax_benefit_system) currency = simulation.tax_benefit_system.CURRENCY if legend_position is None: legend_position = 2 is_reform = False if simulation is not None and reference_simulation is not None: data = OutNode.init_from_decomposition_json(simulation, decomposition_json) reference_data = OutNode.init_from_decomposition_json(reference_simulation, decomposition_json) is_reform = True data.difference(reference_data) else: data = OutNode.init_from_decomposition_json(simulation, decomposition_json) reference_data = None data.setLeavesVisible() if visible_lines is not None: for code in visible_lines: data[code].visible = 1 data[code].typevar = 2 data[x_axis].setHidden(changeParent = True) if is_reform and hide_all is True: data.hideAll() axes.clear() draw_bareme_from_node_data( data, axes, x_axis, reform = is_reform, legend = legend, reference_data = reference_data, currency = currency, legend_position = legend_position, )
def precalculate_decomposition_json(tbs): period = 2018 scenario_params = { "period": period, "parent1": { "age": 30, }, # "enfants": [ # {"age": 6}, # {"age": 8}, # {"age": 10} # ], "axes": [ dict( count=COUNT, min=MIN, max=MAX, name='salaire_de_base', ), ], } scenario = tbs.new_scenario().init_single_entity(**scenario_params) simulation = scenario.new_simulation() decomposition_json = decompositions.get_decomposition_json(tbs) decomposition_tree = decompositions.calculate([simulation], decomposition_json) # def serialize(x): # if isinstance(x, collections.Iterable): # return list(x) # return x # with Path("decomposition.json").open('w') as fd: # json.dump(decomposition_tree, fd, indent=2, default=serialize) return decomposition_tree
code, desc, color = color, typevar = typv, shortname = shortname) out_node.addChild(child_out_node) convert_to_out_node(child_out_node, child) else: out_node.setVals(np.array(node['values'])) return if __name__ == '__main__': import openfisca_france import datetime from openfisca_core import periods year = 2013 period = periods.period("year", year) TaxBenefitSystem = openfisca_france.init_country() tax_benefit_system = TaxBenefitSystem() scenario = tax_benefit_system.new_scenario().init_single_entity( parent1 = dict( birth = datetime.date(year - 40, 1, 1), sali = 0, ), period = period, ) simulation = scenario.new_simulation() decomposition_json = decompositions.get_decomposition_json(tax_benefit_system) tree = OutNode.init_from_decomposition_json(simulation, decomposition_json)