def trace(tax_benefit_system, input_data): simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=input_data, trace=True) requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter=lambda t: t is None, yielded=True) for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split('/') simulation.calculate(variable_name, period) trace = deepcopy(simulation.tracer.trace) for vector_key, vector_trace in trace.items(): value = vector_trace['value'].tolist() if isinstance(value[0], Enum): value = [item.name for item in value] if isinstance(value[0], bytes): value = [to_unicode(item) for item in value] vector_trace['value'] = value return { "trace": trace, "entitiesDescription": {entity.plural: entity.ids for entity in simulation.entities.values()}, "requestedCalculations": list(simulation.tracer.requested_calculations) }
def trace(): tax_benefit_system = data['tax_benefit_system'] request.on_json_loading_failed = handle_invalid_json input_data = request.get_json() try: simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=input_data, trace=True) except SituationParsingError as e: abort(make_response(jsonify(e.error), e.code or 400)) requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter=lambda t: t is None, yielded=True) for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split('/') simulation.calculate(variable_name, period).tolist() return jsonify({ "trace": simulation.tracer.trace, "entitiesDescription": { entity.plural: entity.ids for entity in simulation.entities.itervalues() }, "requestedCalculations": list(simulation.tracer.requested_calculations) })
def test_get_memory_usage(): simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=single) simulation.calculate('disposable_income', '2017-01') memory_usage = simulation.get_memory_usage(variables=['salary']) assert (memory_usage['total_nb_bytes'] > 0) assert (len(memory_usage['by_variable']) == 1)
def test_dump(): directory = tempfile.mkdtemp(prefix = "openfisca_") simulation = Simulation(tax_benefit_system = tax_benefit_system, simulation_json = couple) calculated_value = simulation.calculate('disposable_income', '2018-01') dump_simulation(simulation, directory) simulation_2 = restore_simulation(directory, tax_benefit_system) # Check entities structure have been restored assert_array_equal(simulation.person.ids, simulation_2.person.ids) assert_array_equal(simulation.person.count, simulation_2.person.count) assert_array_equal(simulation.household.ids, simulation_2.household.ids) assert_array_equal(simulation.household.count, simulation_2.household.count) assert_array_equal(simulation.household.members_position, simulation_2.household.members_position) assert_array_equal(simulation.household.members_entity_id, simulation_2.household.members_entity_id) assert_array_equal(simulation.household.members_legacy_role, simulation_2.household.members_legacy_role) assert_array_equal(simulation.household.members_role, simulation_2.household.members_role) # Check calculated values are in cache disposable_income_holder = simulation_2.person.get_holder('disposable_income') cached_value = disposable_income_holder.get_array('2018-01') assert cached_value is not None assert_array_equal(cached_value, calculated_value) shutil.rmtree(directory)
def calculate(): tax_benefit_system = data['tax_benefit_system'] request.on_json_loading_failed = handle_invalid_json input_data = request.get_json() try: simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=input_data) except SituationParsingError as e: abort(make_response(jsonify(e.error), e.code or 400)) requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter=lambda t: t is None, yielded=True) for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split('/') result = simulation.calculate(variable_name, period).tolist() entity = simulation.get_entity(plural=entity_plural) entity_index = entity.ids.index(entity_id) variable = tax_benefit_system.get_variable(variable_name) if variable.value_type == Enum: entity_result = result[entity_index].name else: entity_result = result[entity_index] dpath.util.set(input_data, path, entity_result) return jsonify(input_data)
def calculate(tax_benefit_system, input_data): simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=input_data) requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter=lambda t: t is None, yielded=True) computation_results = {} for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split('/') variable = tax_benefit_system.get_variable(variable_name) result = simulation.calculate(variable_name, period) entity = simulation.get_entity(plural=entity_plural) entity_index = entity.ids.index(entity_id) if variable.value_type == Enum: entity_result = result.decode()[entity_index].name elif variable.value_type == float: entity_result = float( str(result[entity_index]) ) # To turn the float32 into a regular float without adding confusing extra decimals. There must be a better way. elif variable.value_type == str: entity_result = to_unicode( result[entity_index]) # From bytes to unicode else: entity_result = result.tolist()[entity_index] dpath.util.new(computation_results, path, entity_result) dpath.merge(input_data, computation_results) return input_data
def calculate(): tax_benefit_system = data['tax_benefit_system'] request.on_json_loading_failed = handle_invalid_json input_data = request.get_json() try: simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=input_data) except SituationParsingError as e: abort(make_response(jsonify(e.error), e.code or 400)) except UnicodeEncodeError as e: abort( make_response( jsonify({ "error": "'" + e[1] + "' is not a valid ASCII value." }), 400)) requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter=lambda t: t is None, yielded=True) results = {} try: for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split( '/') variable = tax_benefit_system.get_variable(variable_name) result = simulation.calculate(variable_name, period) entity = simulation.get_entity(plural=entity_plural) entity_index = entity.ids.index(entity_id) if variable.value_type == Enum: entity_result = result.decode()[entity_index].name elif variable.value_type == float: entity_result = float( str(result[entity_index]) ) # To turn the float32 into a regular float without adding confusing extra decimals. There must be a better way. elif variable.value_type == str: entity_result = to_unicode( result[entity_index]) # From bytes to unicode else: entity_result = result.tolist()[entity_index] dpath.util.new(results, path, entity_result) except UnicodeEncodeError as e: abort( make_response( jsonify({ "error": "'" + e[1] + "' is not a valid ASCII value." }), 400)) dpath.util.merge(input_data, results) return jsonify(input_data)
def test_set_input_enum(): simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=couple) status_occupancy = np.asarray(['owner']) period = make_period('2017-12') HousingOccupancyStatus = tax_benefit_system.get_variable( 'housing_occupancy_status').possible_values simulation.household.get_holder('housing_occupancy_status').set_input( period, status_occupancy) result = simulation.calculate('housing_occupancy_status', period) assert_equal(result, HousingOccupancyStatus.owner)
def trace(): tax_benefit_system = data['tax_benefit_system'] request.on_json_loading_failed = handle_invalid_json input_data = request.get_json() try: simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=input_data, trace=True) except SituationParsingError as e: abort(make_response(jsonify(e.error), e.code or 400)) requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter=lambda t: t is None, yielded=True) for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split('/') simulation.calculate(variable_name, period) trace = deepcopy(simulation.tracer.trace) for vector_key, vector_trace in trace.items(): value = vector_trace['value'].tolist() if isinstance(value[0], Enum): value = [item.name for item in value] if isinstance(value[0], bytes): value = [to_unicode(item) for item in value] vector_trace['value'] = value return jsonify({ "trace": trace, "entitiesDescription": { entity.plural: entity.ids for entity in simulation.entities.values() }, "requestedCalculations": list(simulation.tracer.requested_calculations) })
def run_situation(reform_name, reform, situation_json, calculs): with open(situation_json) as json_data: json_str = json_data.read() situation = json.loads(json_str) results = {"Situation": situation_json, "Legislation": reform_name} # Initialisez la simulation (rencontre des entités avec la legislation) simulation = Simulation(tax_benefit_system=reform, simulation_json=situation) # Demandez l'ensemble des calculs for calcul, period in calculs.iteritems(): results try: results[calcul] = simulation.calculate(calcul, period)[0] print('. ' + calcul) except ParameterNotFound as e: print 'x ' + calcul print e.message + os.linesep return results
def run_situation(reform_name, reform, situation_json, calculs): with open(situation_json) as json_data: json_str = json_data.read() situation = json.loads(json_str) results = {"Situation": situation_json, "Legislation": reform_name} # Initialisez la simulation (rencontre des entités avec la legislation) simulation = Simulation(tax_benefit_system=reform, simulation_json=situation) # Demandez l'ensemble des calculs for calcul, period in calculs.iteritems(): results try: results[calcul] = simulation.calculate(calcul, period)[0] print('. ' + calcul) except ParameterNotFound as e: print 'x ' + calcul print e.message + os.linesep return results
def simulate(self, variable_name, period, simulation_data): simulation = Simulation( tax_benefit_system=self.tax_benefit_system, simulation_json=simulation_data.to_openfisca_dict(period=period)) result = simulation.calculate(variable_name, period=period) return result
# Calculez la situation actuelle de la legislation française legislation_france = openfisca_france.FranceTaxBenefitSystem() simulation_actuelle = Simulation(tax_benefit_system=legislation_france, simulation_json=situation) # Insérez ci-dessous la variable que vous souhaitez calculer (ex : 'aah_base_ressources') # ainsi que la periode sur laquelle vous souaitez la calculer (ex : '2017') bases_ressources = [ 'aah_base_ressources', 'ass_base_ressources', 'cmu_base_ressources', 'prestations_familiales_base_ressources' ] for base in bases_ressources: print(base) print(simulation_actuelle.calculate(base, '2018-01')) print('---') # Calculez la situation avec la réforme print('#### Réforme ####') legislation_reforme = MaReforme(legislation_france) simulation_reforme = Simulation(tax_benefit_system=legislation_reforme, simulation_json=situation) # Insérez ci-dessous la variable que vous souhaitez calculer (ex : 'impots_directs') # ainsi que la periode sur laquelle vous souhaitez la calculer (ex : '2017') bases_ressources = [ 'base_ressource_mensuelle_individu', ] for base in bases_ressources:
from datetime import datetime from openfisca_core.simulations import Simulation import sys from pprint import pprint filename = '../doc/CMUCACS.csv' if len(sys.argv) > 1: filename = sys.argv[len(sys.argv) - 1] legislation_francaise = openfisca_france.FranceTaxBenefitSystem() with open(filename, 'r') as file: rows = csv.DictReader(file) for data in rows: period = datetime.strptime('2018-08-01', '%Y-%m-%d') situation = cpam_utils.createSituation(data, period) pprint(situation) simulation_actuelle = Simulation( tax_benefit_system=legislation_francaise, simulation_json=situation) variable = 'cmu_c' periodText = period.strftime('%Y-%m') resultat = simulation_actuelle.calculate(variable, periodText) print('{0} - {1} : {2}'.format(variable, periodText, resultat)) print('')
], "personne_de_reference": [ "Alex" ] } } } # Calculez la situation actuelle de la legislation française legislation_france = openfisca_france.FranceTaxBenefitSystem() simulation_actuelle = Simulation(tax_benefit_system=legislation_france, simulation_json=situation) # Insérez ci-dessous la variable que vous souhaitez calculer (ex : 'impots_directs') # ainsi que la periode sur laquelle vous souaitez la calculer (ex : '2017') resultat_actuel = simulation_actuelle.calculate('impots_directs', '2017') print "Résultat actuel" print resultat_actuel # Calculez la situation avec la réforme legislation_reforme = MaReforme(legislation_france) simulation_reforme = Simulation(tax_benefit_system=legislation_reforme, simulation_json=situation) # Insérez ci-dessous la variable que vous souhaitez calculer (ex : 'impots_directs') # ainsi que la periode sur laquelle vous souaitez la calculer (ex : '2017') resultat_reforme = simulation_reforme.calculate('impots_directs', '2017') print "Resultat après reforme" print resultat_reforme
'cmu_base_ressources': '2018-01', 'prestations_familiales_base_ressources': '2018-01' } # Initialisez le fichier CSV pour export Excel with open('resultats.csv', 'w') as csvfile: fieldnames = ['Situation'] + list(calculs) writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for situation_json in situations_json: with open(situation_json) as json_data: json_str = json_data.read() situation = json.loads(json_str) results = {"Situation": situation_json} # Initialisez la simulation (rencontre des entités avec la legislation) legislation_france = openfisca_france.FranceTaxBenefitSystem() simulation_actuelle = Simulation(tax_benefit_system=legislation_france, simulation_json=situation) # Demandez l'ensemble des calculs for calcul, period in calculs.iteritems(): results[calcul] = simulation_actuelle.calculate(calcul, period)[0] # Écrivez les resultats dans le CSV writer.writerow(results) print('le calcul est terminé')
tax_benefit_system = openfisca_france.CountryTaxBenefitSystem() simulation_actuelle = Simulation( tax_benefit_system=tax_benefit_system, simulation_json=situation) import numpy import scipy.stats import pandas as pd csv_input = pd.read_csv(inputpath, sep=";", nrows=n_limit) for calcul, periodes in calculs.iteritems(): print(calcul) ids = simulation_actuelle.get_variable_entity(calcul).ids for periode in periodes: valeurs = simulation_actuelle.calculate(calcul, periode) sources = simulation_actuelle.calculate(calcul, m3) csv_input['_'.join(['openfisca', calcul, periode])] = valeurs if calcul in process: process[calcul](sources, valeurs) else: print(numpy.corrcoef(valeurs, sources)[0][1]) # print(scipy.stats.spearmanr(valeurs, sources)) print (numpy.histogram(valeurs - sources)) resultat = dict(zip(ids, zip(valeurs, sources))) for matricul, valeurs in resultat.iteritems(): calculee, reelle = valeurs #print (matricul, reelle, calculee)
with open(json_path) as json_data: json1_str = json_data.read() situation = json.loads(json1_str) ####### Calculez la situation actuelle de la legislation française ############## legislation_france = openfisca_france.FranceTaxBenefitSystem() simulation_actuelle = Simulation(tax_benefit_system=legislation_france, simulation_json=situation) # Insérez ci-dessous la variable que vous souhaitez calculer (ex : 'impots_directs') # ainsi que la periode sur laquelle vous souaitez la calculer (ex : '2017') print "Revenu Fiscal de Référence" print simulation_actuelle.calculate('rfr', '2017') print '---' bases_ressources = [ 'aah_base_ressources', 'ass_base_ressources', 'cmu_base_ressources', 'prestations_familiales_base_ressources' ] for base in bases_ressources: print base print simulation_actuelle.calculate(base, '2018-01') print '---' # ####### Calcule la situation avec la réforme ############## # legislation_reforme = MaReform(legislation_france) # simulation_reforme = Simulation(tax_benefit_system=legislation_reforme, simulation_json=situation)
fieldnames = ['Situation', 'Period', 'Variable', 'Value'] # Initialisez le fichier CSV pour export Excel with open('resultats.csv', 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for situation_json in situations_json: with open(situation_json) as json_data: json_str = json_data.read() situation = json.loads(json_str) pprint(situation_json) results = {'Situation': situation_json} # Initialisez la simulation (rencontre des entités avec la legislation) legislation_france = openfisca_france.FranceTaxBenefitSystem() simulation_actuelle = Simulation(tax_benefit_system=legislation_france, simulation_json=removeComputedValues(normalize(situation))) # Demandez l'ensemble des calculs for calcul, periods in calculs.iteritems(): results['Variable'] = calcul for period in periods: results['Period'] = period results['Value'] = simulation_actuelle.calculate(calcul, period)[0] writer.writerow(results) print 'le calcul est terminé'
'af_taux_modulation': allMonths, 'prestations_familiales_base_ressources': allMonths, } calculs = { 'af': allMonths, } import experiment_af import situations situation = utils.merge(utils.prefix('7618153', situations.situation_7618153), utils.prefix('6771069', situations.situation_6771069)) simulation_actuelle = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=situation) checkPeriod = "2018-07" for calcul, periods in calculs.iteritems(): ids = simulation_actuelle.get_variable_entity(calcul).ids for period in periods: data = simulation_actuelle.calculate(calcul, period) result = dict(zip(ids, data)) for mid, value in result.iteritems(): print(mid, value, value * 0.995) # source = situation["familles"][mid]["af"][checkPeriod] # if abs(source - value)>1: # print (mid, source, value) from pprint import pprint #pprint(situation)
# -*- coding: utf-8 -*- from openfisca_france import CountryTaxBenefitSystem from openfisca_core.simulations import Simulation from situation_examples import celibataire tax_benefit_system = CountryTaxBenefitSystem() simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=celibataire) print(simulation.calculate('revenu_disponible', '2016'))