def test_add_person_values_with_default_period_old_syntax(persons): persons_json = {'Alicia': {'salary': 3000}, 'Javier': {}} simulation_builder = SimulationBuilder() simulation_builder.set_default_period('month:2018-11') simulation_builder.add_person_entity(persons, persons_json) tools.assert_near(simulation_builder.get_input('salary', '2018-11'), [3000, 0])
def test_add_axis_on_a_non_existing_variable(persons): simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, {'Alicia': {}}) simulation_builder.add_parallel_axis({'count': 3, 'name': 'ubi', 'min': 0, 'max': 3000, 'period': '2018-11'}) with pytest.raises(KeyError): simulation_builder.expand_axes()
def test_canonicalize_period_keys(persons): persons_json = {'Alicia': {'salary': {'year:2018-01': 100}}} simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, persons_json) population = Population(persons) simulation_builder.finalize_variables_init(population) tools.assert_near( population.get_holder('salary').get_array('2018-12'), [100])
def test_add_axis_without_period(persons): simulation_builder = SimulationBuilder() simulation_builder.set_default_period('2018-11') simulation_builder.add_person_entity(persons, {'Alicia': {}}) simulation_builder.register_variable('salary', persons) simulation_builder.add_parallel_axis({'count': 3, 'name': 'salary', 'min': 0, 'max': 3000}) simulation_builder.expand_axes() assert simulation_builder.get_input('salary', '2018-11') == pytest.approx([0, 1500, 3000])
def test_add_axis_with_group_int_period(persons): simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, {'Alicia': {}, 'Javier': {}}) simulation_builder.register_variable('salary', persons) simulation_builder.add_parallel_axis({'count': 2, 'name': 'salary', 'min': 0, 'max': 3000, 'period': 2018}) simulation_builder.add_parallel_axis({'count': 2, 'name': 'salary', 'min': 0, 'max': 3000, 'period': 2018, 'index': 1}) simulation_builder.expand_axes() assert simulation_builder.get_input('salary', '2018') == pytest.approx([0, 0, 3000, 3000])
def test_add_axis_on_persons(persons): simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, {'Alicia': {}}) simulation_builder.register_variable('salary', persons) simulation_builder.add_parallel_axis({'count': 3, 'name': 'salary', 'min': 0, 'max': 3000, 'period': '2018-11'}) simulation_builder.expand_axes() assert simulation_builder.get_input('salary', '2018-11') == pytest.approx([0, 1500, 3000]) assert simulation_builder.get_count('persons') == 3 assert simulation_builder.get_ids('persons') == ['Alicia0', 'Alicia1', 'Alicia2']
def test_finalize_person_entity(persons): persons_json = {'Alicia': {'salary': {'2018-11': 3000}}, 'Javier': {}} simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, persons_json) population = Population(persons) simulation_builder.finalize_variables_init(population) tools.assert_near( population.get_holder('salary').get_array('2018-11'), [3000, 0]) assert population.count == 2 assert population.ids == ['Alicia', 'Javier']
def test_add_axis_distributes_memberships(persons, households): simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, {'Alicia': {}, 'Javier': {}, 'Tom': {}}) simulation_builder.add_group_entity('persons', ['Alicia', 'Javier', 'Tom'], households, { 'housea': {'parents': ['Alicia']}, 'houseb': {'parents': ['Tom'], 'children': ['Javier']}, }) simulation_builder.register_variable('rent', households) simulation_builder.add_parallel_axis({'count': 2, 'name': 'rent', 'min': 0, 'max': 3000, 'period': '2018-11'}) simulation_builder.expand_axes() assert simulation_builder.get_memberships('households') == [0, 1, 1, 2, 3, 3]
def test_add_axis_on_persons_distributes_roles(persons, households): simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, {'Alicia': {}, 'Javier': {}, 'Tom': {}}) simulation_builder.add_group_entity('persons', ['Alicia', 'Javier', 'Tom'], households, { 'housea': {'parents': ['Alicia']}, 'houseb': {'parents': ['Tom'], 'children': ['Javier']}, }) simulation_builder.register_variable('salary', persons) simulation_builder.add_parallel_axis({'count': 2, 'name': 'salary', 'min': 0, 'max': 3000, 'period': '2018-11'}) simulation_builder.expand_axes() assert [role.key for role in simulation_builder.get_roles('households')] == ['parent', 'child', 'parent', 'parent', 'child', 'parent']
def test_add_axis_on_households(persons, households): simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, {'Alicia': {}, 'Javier': {}, 'Tom': {}}) simulation_builder.add_group_entity('persons', ['Alicia', 'Javier', 'Tom'], households, { 'housea': {'parents': ['Alicia', 'Javier']}, 'houseb': {'parents': ['Tom']}, }) simulation_builder.register_variable('rent', households) simulation_builder.add_parallel_axis({'count': 2, 'name': 'rent', 'min': 0, 'max': 3000, 'period': '2018-11'}) simulation_builder.expand_axes() assert simulation_builder.get_count('households') == 4 assert simulation_builder.get_ids('households') == ['housea0', 'houseb1', 'housea2', 'houseb3'] assert simulation_builder.get_input('rent', '2018-11') == pytest.approx([0, 0, 3000, 0])
def test_add_perpendicular_axis_on_an_existing_variable_with_input(persons): simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, { 'Alicia': { 'salary': {'2018-11': 1000}, 'pension': {'2018-11': 1000}, }, },) simulation_builder.register_variable('salary', persons) simulation_builder.register_variable('pension', persons) simulation_builder.add_parallel_axis({'count': 3, 'name': 'salary', 'min': 0, 'max': 3000, 'period': '2018-11'}) simulation_builder.add_perpendicular_axis({'count': 2, 'name': 'pension', 'min': 0, 'max': 2000, 'period': '2018-11'}) simulation_builder.expand_axes() assert simulation_builder.get_input('salary', '2018-11') == pytest.approx([0, 1500, 3000, 0, 1500, 3000]) assert simulation_builder.get_input('pension', '2018-11') == pytest.approx([0, 0, 0, 2000, 2000, 2000])
def test_add_person_entity_with_values(persons): persons_json = {'Alicia': {'salary': {'2018-11': 3000}}, 'Javier': {}} simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, persons_json) tools.assert_near(simulation_builder.get_input('salary', '2018-11'), [3000, 0])
def test_numeric_ids(persons): persons_json = {1: {'salary': {}}, 2: {}} simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, persons_json) assert simulation_builder.get_count('persons') == 2 assert simulation_builder.get_ids('persons') == ['1', '2']
def test_add_person_entity(persons): persons_json = {'Alicia': {'salary': {}}, 'Javier': {}} simulation_builder = SimulationBuilder() simulation_builder.add_person_entity(persons, persons_json) assert simulation_builder.get_count('persons') == 2 assert simulation_builder.get_ids('persons') == ['Alicia', 'Javier']