Ejemplo n.º 1
0
def test_get_memory_usage():
    simulation = SimulationBuilder().build_from_entities(
        tax_benefit_system, 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)
Ejemplo n.º 2
0
def test_get_memory_usage():
    test_case = deepcopy(single)
    test_case["persons"]["Alicia"]["salary"] = {"2017-01": 0}
    simulation = SimulationBuilder().build_from_dict(tax_benefit_system, test_case)
    simulation.calculate('disposable_income', '2017-01')
    memory_usage = simulation.person.get_memory_usage(variables = ['salary'])
    assert(memory_usage['total_nb_bytes'] > 0)
    assert(len(memory_usage['by_variable']) == 1)
Ejemplo n.º 3
0
def test_get_memory_usage():
    test_case = deepcopy(single)
    test_case["persons"]["Alicia"]["salary"] = {"2017-01": 0}
    simulation = SimulationBuilder().build_from_dict(tax_benefit_system,
                                                     test_case)
    simulation.calculate('disposable_income', '2017-01')
    memory_usage = simulation.person.get_memory_usage(variables=['salary'])
    assert (memory_usage['total_nb_bytes'] > 0)
    assert (len(memory_usage['by_variable']) == 1)
Ejemplo n.º 4
0
def test_dump():
    directory = tempfile.mkdtemp(prefix = "openfisca_")
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, 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_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)
Ejemplo n.º 5
0
def test_dump():
    directory = tempfile.mkdtemp(prefix="openfisca_")
    simulation = SimulationBuilder().build_from_entities(
        tax_benefit_system, 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_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)
Ejemplo n.º 6
0
def calculate(tax_benefit_system, input_data):
    simulation = SimulationBuilder().build_from_entities(
        tax_benefit_system, 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)
        population = simulation.get_population(entity_plural)
        entity_index = population.get_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 = str(result[entity_index])
        else:
            entity_result = result.tolist()[entity_index]

        dpath.util.new(computation_results, path, entity_result)

    dpath.merge(input_data, computation_results)

    return input_data
Ejemplo n.º 7
0
def calculate(tax_benefit_system, input_data):
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, 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)
        population = simulation.get_population(entity_plural)
        entity_index = population.get_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 = str(result[entity_index])
        else:
            entity_result = result.tolist()[entity_index]

        dpath.util.new(computation_results, path, entity_result)

    dpath.merge(input_data, computation_results)

    return input_data
Ejemplo n.º 8
0
def test_calculate_with_trace():
    simulation = SimulationBuilder().build_default_simulation(tax_benefit_system)
    simulation.trace = True
    simulation.calculate('income_tax', '2017-01')

    salary_trace = simulation.tracer.trace['salary<2017-01>']
    assert salary_trace['parameters'] == {}

    income_tax_trace = simulation.tracer.trace['income_tax<2017-01>']
    assert income_tax_trace['parameters']['taxes.income_tax_rate<2017-01-01>'] == 0.15

    # Trace parameters called with indirect access
    simulation.calculate('housing_tax', '2017')
    housing_tax_trace = simulation.tracer.trace['housing_tax<2017>']
    assert 'taxes.housing_tax<2017-01-01>' not in housing_tax_trace['parameters']
    assert housing_tax_trace['parameters']['taxes.housing_tax.rate<2017-01-01>'] == 10
    assert housing_tax_trace['parameters']['taxes.housing_tax.minimal_amount<2017-01-01>'] == 200
Ejemplo n.º 9
0
def trace(tax_benefit_system, input_data):
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, input_data)
    simulation.trace = True

    requested_calculations = []
    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('/')
        requested_calculations.append(f"{variable_name}<{str(period)}>")
        simulation.calculate(variable_name, period)
    trace = simulation.tracer.get_serialized_flat_trace()

    return {
        "trace": trace,
        "entitiesDescription": simulation.describe_entities(),
        "requestedCalculations": requested_calculations
        }
Ejemplo n.º 10
0
def test_calculate_full_tracer():
    simulation = SimulationBuilder().build_default_simulation(
        tax_benefit_system)
    simulation.trace = True
    simulation.calculate('income_tax', '2017-01')

    income_tax_node = simulation.tracer.trees[0]
    assert income_tax_node.name == 'income_tax'
    assert str(income_tax_node.period) == '2017-01'
    assert income_tax_node.value == 0

    salary_node = income_tax_node.children[0]
    assert salary_node.name == 'salary'
    assert str(salary_node.period) == '2017-01'
    assert salary_node.parameters == []

    assert len(income_tax_node.parameters) == 1
    assert income_tax_node.parameters[0].name == 'taxes.income_tax_rate'
    assert income_tax_node.parameters[0].period == '2017-01-01'
    assert income_tax_node.parameters[0].value == 0.15
Ejemplo n.º 11
0
def trace(tax_benefit_system, input_data):
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, 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)
        }
Ejemplo n.º 12
0
def trace(tax_benefit_system, input_data):
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, input_data)
    simulation.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(vector_trace['value'], EnumArray):
            value = [item.name for item in vector_trace['value'].decode()]
        if isinstance(value[0], bytes):
            value = [str(item) for item in value]
        vector_trace['value'] = value

    return {
        "trace": trace,
        "entitiesDescription": simulation.describe_entities(),
        "requestedCalculations": list(simulation.tracer.requested_calculations)
        }
Ejemplo n.º 13
0
def test_get_memory_usage():
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, 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)