예제 #1
0
def test_known_periods():
    simulation = get_simulation(
        single, memory_config=force_storage_on_disk)  # Force using disk
    month = make_period('2017-01')
    month_2 = make_period('2017-02')
    holder = simulation.person.get_holder('salary')
    data = np.asarray([2000, 3000, 0, 500])
    holder.put_in_cache(data, month)
    holder._memory_storage.put(data, month_2)
    assert_items_equal(holder.get_known_periods(), [month, month_2])
예제 #2
0
def test_known_periods(couple):
    simulation = couple
    simulation.memory_config = force_storage_on_disk
    month = make_period('2017-01')
    month_2 = make_period('2017-02')
    holder = simulation.person.get_holder('disposable_income')
    data = np.asarray([2000, 3000])
    holder.put_in_cache(data, month)
    holder._memory_storage.put(data, month_2)

    assert sorted(holder.get_known_periods()), [month == month_2]
예제 #3
0
def test_known_periods():
    simulation = get_simulation(
        couple, memory_config=force_storage_on_disk)  # Force using disk
    month = make_period('2017-01')
    month_2 = make_period('2017-02')
    holder = simulation.person.get_holder('disposable_income')
    data = np.asarray([2000, 3000])
    holder.put_in_cache(data, month)
    holder._memory_storage.put(data, month_2)

    assert_equal(sorted(holder.get_known_periods()), [month, month_2])
예제 #4
0
def test_delete_arrays(single):
    simulation = single
    salary_holder = simulation.person.get_holder('salary')
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    salary_holder.set_input(make_period(2018), np.asarray([60000]))
    assert simulation.person('salary', '2017-01') == 2500
    assert simulation.person('salary', '2018-01') == 5000
    salary_holder.delete_arrays(period = 2018)
    salary_holder.set_input(make_period(2018), np.asarray([15000]))
    assert simulation.person('salary', '2017-01') == 2500
    assert simulation.person('salary', '2018-01') == 1250
예제 #5
0
def test_delete_arrays():
    simulation = get_simulation(single)
    salary_holder = simulation.person.get_holder('salary')
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    salary_holder.set_input(make_period(2018), np.asarray([60000]))
    assert_equal(simulation.person('salary', '2017-01'), 2500)
    assert_equal(simulation.person('salary', '2018-01'), 5000)
    salary_holder.delete_arrays(period=2018)
    salary_holder.set_input(make_period(2018), np.asarray([15000]))
    assert_equal(simulation.person('salary', '2017-01'), 2500)
    assert_equal(simulation.person('salary', '2018-01'), 1250)
예제 #6
0
def test_delete_arrays_on_disk(single):
    simulation = single
    simulation.memory_config = force_storage_on_disk
    salary_holder = simulation.person.get_holder('salary')
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    salary_holder.set_input(make_period(2018), np.asarray([60000]))
    assert simulation.person('salary', '2017-01') == 2500
    assert simulation.person('salary', '2018-01') == 5000
    salary_holder.delete_arrays(period = 2018)
    salary_holder.set_input(make_period(2018), np.asarray([15000]))
    assert simulation.person('salary', '2017-01') == 2500
    assert simulation.person('salary', '2018-01') == 1250
예제 #7
0
def test_delete_arrays_on_disk():
    simulation = get_simulation(
        single, memory_config=force_storage_on_disk)  # Force using disk
    salary_holder = simulation.person.get_holder('salary')
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    salary_holder.set_input(make_period(2018), np.asarray([60000]))
    assert_equal(simulation.person('salary', '2017-01'), 2500)
    assert_equal(simulation.person('salary', '2018-01'), 5000)
    salary_holder.delete_arrays(period=2018)
    salary_holder.set_input(make_period(2018), np.asarray([15000]))
    assert_equal(simulation.person('salary', '2017-01'), 2500)
    assert_equal(simulation.person('salary', '2018-01'), 1250)
예제 #8
0
def test_cache_disk_with_extra_params():
    simulation = get_simulation(
        single, memory_config=force_storage_on_disk)  # Force using disk
    month = make_period('2017-01')
    extra_param_1 = make_period('2017-02')
    extra_param_2 = make_period('2017-03')
    holder = simulation.person.get_holder('salary')
    data_1 = np.asarray([2000, 3000, 0, 500])
    data_2 = np.asarray([1000, 4000, 200, 200])
    holder.put_in_cache(data_1, month, extra_params=[extra_param_1])
    holder.put_in_cache(data_2, month, extra_params=[extra_param_2])
    stored_data_1 = holder.get_array(month, extra_params=[extra_param_1])
    stored_data_2 = holder.get_array(month, extra_params=[extra_param_2])
    assert_near(data_1, stored_data_1)
    assert_near(data_2, stored_data_2)
예제 #9
0
def test_cache_enum_on_disk(single):
    simulation = single
    simulation.memory_config = force_storage_on_disk
    month = make_period('2017-01')
    simulation.calculate('housing_occupancy_status', month)  # First calculation
    housing_occupancy_status = simulation.calculate('housing_occupancy_status', month)  # Read from cache
    assert housing_occupancy_status == HousingOccupancyStatus.tenant
예제 #10
0
def test_permanent_variable_filled():
    simulation = get_simulation(single)
    holder = simulation.person.get_holder('birth')
    value = np.asarray(['1980-01-01'], dtype=holder.variable.dtype)
    holder.set_input(make_period(ETERNITY), value)
    assert_equal(holder.get_array(None), value)
    assert_equal(holder.get_array(ETERNITY), value)
    assert_equal(holder.get_array('2016-01'), value)
예제 #11
0
def test_set_input_dispatch_by_period(single):
    simulation = single
    variable = simulation.tax_benefit_system.get_variable('housing_occupancy_status')
    entity = simulation.household
    holder = Holder(variable, entity)
    set_input_dispatch_by_period(holder, make_period(2019), 'owner')
    assert holder.get_array('2019-01') == holder.get_array('2019-12')  # Check the feature
    assert holder.get_array('2019-01') is holder.get_array('2019-12')  # Check that the vectors are the same in memory, to avoid duplication
예제 #12
0
def test_permanent_variable_filled(single):
    simulation = single
    holder = simulation.person.get_holder('birth')
    value = np.asarray(['1980-01-01'], dtype = holder.variable.dtype)
    holder.set_input(make_period(ETERNITY), value)
    assert holder.get_array(None) == value
    assert holder.get_array(ETERNITY) == value
    assert holder.get_array('2016-01') == value
예제 #13
0
def test_cache_disk():
    simulation = get_simulation(
        single, memory_config=force_storage_on_disk)  # Force using disk
    month = make_period('2017-01')
    holder = simulation.person.get_holder('salary')
    data = np.asarray([2000, 3000, 0, 500])
    holder.put_in_cache(data, month)
    stored_data = holder.get_array(month)
    assert_near(data, stored_data)
예제 #14
0
def test_cache_disk(couple):
    simulation = couple
    simulation.memory_config = force_storage_on_disk
    month = make_period('2017-01')
    holder = simulation.person.get_holder('disposable_income')
    data = np.asarray([2000, 3000])
    holder.put_in_cache(data, month)
    stored_data = holder.get_array(month)
    assert_near(data, stored_data)
예제 #15
0
def test_cache_enum_on_disk():
    simulation = get_simulation(
        single, memory_config=force_storage_on_disk)  # Force using disk
    month = make_period('2017-01')
    simulation.calculate('housing_occupancy_status',
                         month)  # First calculation
    housing_occupancy_status = simulation.calculate('housing_occupancy_status',
                                                    month)  # Read from cache
    assert_equal(housing_occupancy_status, HousingOccupancyStatus.tenant)
    def init_variable_values(self, entity_object, entity_id):
        entity_index = self.ids.index(entity_id)
        for variable_name, variable_values in entity_object.items():
            path_in_json = [self.plural, entity_id, variable_name]
            try:
                self.check_variable_defined_for_entity(variable_name)
            except ValueError as e:  # The variable is defined for another entity
                raise SituationParsingError(path_in_json, e.args[0])
            except VariableNotFound as e:  # The variable doesn't exist
                raise SituationParsingError(path_in_json, e.message, code=404)

            if not isinstance(variable_values, dict):
                raise SituationParsingError(
                    path_in_json,
                    "Invalid type: must be of type object. Input variables must be set for specific periods. For instance: {'salary': {'2017-01': 2000, '2017-02': 2500}}, or {'birth_date': {'ETERNITY': '1980-01-01'}}."
                )

            holder = self.get_holder(variable_name)
            for period_str, value in variable_values.items():
                path_in_json.append(period_str)
                try:
                    period = make_period(period_str)
                except ValueError as e:
                    raise SituationParsingError(path_in_json, e.args[0])
                if value is not None:
                    array = holder.buffer.get(period)
                    if array is None:
                        array = holder.default_array()
                    if holder.variable.value_type == Enum and isinstance(
                            value, basestring_type):
                        try:
                            value = holder.variable.possible_values[
                                value].index
                        except KeyError:
                            possible_values = [
                                item.name
                                for item in holder.variable.possible_values
                            ]
                            raise SituationParsingError(
                                path_in_json,
                                "'{}' is not a valid value for '{}'. Possible values are ['{}']."
                                .format(value, variable_name,
                                        "', '".join(possible_values)))
                    try:
                        array[entity_index] = value
                    except (ValueError, TypeError):
                        if holder.variable.value_type == date:
                            error_message = "Invalid date: '{}'.".format(value)
                        else:
                            error_message = "Invalid value: must be of type {}, received '{}'.".format(
                                holder.variable.json_type, value)
                        raise SituationParsingError(path_in_json,
                                                    error_message)

                    holder.buffer[period] = array
예제 #17
0
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)
예제 #18
0
def test_get_memory_usage_with_trace(single):
    simulation = single
    simulation.trace = True
    salary_holder = simulation.person.get_holder('salary')
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    simulation.calculate('salary', '2017-01')
    simulation.calculate('salary', '2017-01')
    simulation.calculate('salary', '2017-02')
    simulation.calculate_add('salary', '2017')  # 12 calculations
    memory_usage = salary_holder.get_memory_usage()
    assert memory_usage['nb_requests'] == 15
    assert memory_usage['nb_requests_by_array'] == 1.25  # 15 calculations / 12 arrays
예제 #19
0
def test_get_memory_usage_with_trace():
    simulation = get_simulation(single, trace=True)
    salary_holder = simulation.person.get_holder('salary')
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    simulation.calculate('salary', '2017-01')
    simulation.calculate('salary', '2017-01')
    simulation.calculate('salary', '2017-02')
    simulation.calculate_add('salary', '2017')  # 12 calculations
    memory_usage = salary_holder.get_memory_usage()
    assert_equal(memory_usage['nb_requests'], 15)
    assert_equal(memory_usage['nb_requests_by_array'],
                 1.25)  # 15 calculations / 12 arrays
예제 #20
0
def test_get_memory_usage(single):
    simulation = single
    salary_holder = simulation.person.get_holder('salary')
    memory_usage = salary_holder.get_memory_usage()
    assert memory_usage['total_nb_bytes'] == 0
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    memory_usage = salary_holder.get_memory_usage()
    assert memory_usage['nb_cells_by_array'] == 1
    assert memory_usage['cell_size'] == 4  # float 32
    assert memory_usage['nb_cells_by_array'] == 1  # one person
    assert memory_usage['nb_arrays'] == 12  # 12 months
    assert memory_usage['total_nb_bytes'] == 4 * 12 * 1
예제 #21
0
def test_get_memory_usage():
    simulation = get_simulation(single)
    salary_holder = simulation.person.get_holder('salary')
    memory_usage = salary_holder.get_memory_usage()
    assert_equal(memory_usage['total_nb_bytes'], 0)
    salary_holder.set_input(make_period(2017), np.asarray([30000]))
    memory_usage = salary_holder.get_memory_usage()
    assert_equal(memory_usage['nb_cells_by_array'], 1)
    assert_equal(memory_usage['cell_size'], 4)  # float 32
    assert_equal(memory_usage['nb_cells_by_array'], 1)  # one person
    assert_equal(memory_usage['nb_arrays'], 12)  # 12 months
    assert_equal(memory_usage['total_nb_bytes'], 4 * 12 * 1)
예제 #22
0
    def init_variable_value(self, entity, entity_id, variable_name, period_str,
                            value):
        path_in_json = [entity.plural, entity_id, variable_name, period_str]
        entity_index = entity.ids.index(entity_id)
        holder = entity.get_holder(variable_name)
        try:
            period = make_period(period_str)
        except ValueError as e:
            raise SituationParsingError(path_in_json, e.args[0])
        if value is None:
            return
        array = holder.buffer.get(period)
        if array is None:
            array = holder.default_array()
        if holder.variable.value_type == Enum and isinstance(
                value, basestring_type):
            try:
                value = holder.variable.possible_values[value].index
            except KeyError:
                possible_values = [
                    item.name for item in holder.variable.possible_values
                ]
                raise SituationParsingError(
                    path_in_json,
                    "'{}' is not a known value for '{}'. Possible values are ['{}']."
                    .format(value, variable_name,
                            "', '".join(possible_values)))
        if holder.variable.value_type in (float, int) and isinstance(
                value, basestring_type):
            value = eval_expression(value)
        try:
            array[entity_index] = value
        except (OverflowError):
            error_message = "Can't deal with value: '{}', it's too large for type '{}'.".format(
                value, holder.variable.json_type)
            raise SituationParsingError(path_in_json, error_message)
        except (ValueError, TypeError):
            if holder.variable.value_type == date:
                error_message = "Can't deal with date: '{}'.".format(value)
            else:
                error_message = "Can't deal with value: expected type {}, received '{}'.".format(
                    holder.variable.json_type, value)
            raise SituationParsingError(path_in_json, error_message)

        if not variable_name in self.input:
            self.input[variable_name] = {}

        self.input[variable_name][period_str] = array

        holder.buffer[period] = array
예제 #23
0
from .test_countries import tax_benefit_system

from pytest import fixture


@fixture
def single():
    return SimulationBuilder().build_from_entities(tax_benefit_system, openfisca_country_template.situation_examples.single)


@fixture
def couple():
    return SimulationBuilder().build_from_entities(tax_benefit_system, openfisca_country_template.situation_examples.couple)


period = make_period('2017-12')


def test_set_input_enum_string(couple):
    simulation = couple
    status_occupancy = np.asarray(['free_lodger'])
    simulation.household.get_holder('housing_occupancy_status').set_input(period, status_occupancy)
    result = simulation.calculate('housing_occupancy_status', period)
    assert result == HousingOccupancyStatus.free_lodger


def test_set_input_enum_int(couple):
    simulation = couple
    status_occupancy = np.asarray([2], dtype = np.int16)
    simulation.household.get_holder('housing_occupancy_status').set_input(period, status_occupancy)
    result = simulation.calculate('housing_occupancy_status', period)
예제 #24
0
from openfisca_country_template.situation_examples import couple, single
from openfisca_core.simulations import Simulation
from openfisca_core.periods import period as make_period, ETERNITY
from openfisca_core.tools import assert_near
from openfisca_core.memory_config import MemoryConfig
from .test_countries import tax_benefit_system


def get_simulation(json, **kwargs):
    return Simulation(tax_benefit_system=tax_benefit_system,
                      simulation_json=json,
                      **kwargs)


period = make_period('2017-12')
HousingOccupancyStatus = tax_benefit_system.get_variable(
    'housing_occupancy_status').possible_values


def test_set_input_enum_string():
    simulation = get_simulation(couple)
    status_occupancy = np.asarray(['free_lodger'])
    simulation.household.get_holder('housing_occupancy_status').set_input(
        period, status_occupancy)
    result = simulation.calculate('housing_occupancy_status', period)
    assert_equal(result, HousingOccupancyStatus.free_lodger)


def test_set_input_enum_int():
    simulation = get_simulation(couple)