def test_shortcut_to_containing_entity_not_provided(): """ Tests that, when an entity doesn't provide a containing entity, the shortcut to that containing entity is not provided. """ person_entity = build_entity( key="person", plural="people", label="A person", is_person=True, ) family_entity = build_entity( key="family", plural="families", label="A family (all members in the same household)", containing_entities=[], roles=[{ "key": "member", "plural": "members", "label": "Member", }]) household_entity = build_entity( key="household", plural="households", label="A household, containing one or more families", roles=[{ "key": "member", "plural": "members", "label": "Member", }]) entities = [person_entity, family_entity, household_entity] system = TaxBenefitSystem(entities) simulation = SimulationBuilder().build_from_dict(system, {}) try: simulation.populations["family"].household raise AssertionError() except AttributeError: pass
from openfisca_core.entities import build_entity Famille = build_entity(key="famille", plural="familles", label='Famille', roles=[{ 'key': 'parent', 'plural': 'parents', 'label': 'Parents', 'subroles': ['demandeur', 'conjoint'] }, { 'key': 'enfant', 'plural': 'enfants', 'label': 'Enfants' }]) Individu = build_entity(key="individu", plural="individus", label='Individu', is_person=True) FoyerFiscal = build_entity(key="foyer_fiscal", plural="foyers_fiscaux", label='Déclaration d’impôts', doc=''' Le foyer fiscal désigne l'ensemble des personnes inscrites sur une même déclaration de revenus. Il peut y avoir plusieurs foyers fiscaux dans un seul ménage : par exemple, un couple non marié où chacun remplit sa propre déclaration de revenus compte pour deux foyers fiscaux. Voir https://www.insee.fr/fr/metadonnees/definition/c1735. ''', roles=[
# -*- coding: utf-8 -*- # This file defines the entities needed by our legislation. from openfisca_core.entities import build_entity Family = build_entity( key="family", plural="families", label= u'Family means all persons living in the same household who are supported by the child’s parent(s)’ or guardian(s)’ income; and are related to the child’s parent(s) or guardian(s) by blood, marriage, or adoption; or are the child’s authorized caregiver or legally responsible party.', doc=''' Family means all persons living in the same household who are supported by the child’s parent(s)’ or guardian(s)’ income; and are related to the child’s parent(s) or guardian(s) by blood, marriage, or adoption; or are the child’s authorized caregiver or legally responsible party. Reference: https://eclkc.ohs.acf.hhs.gov/policy/45-cfr-chap-xiii/1305-2-terms. ''', roles=[], is_person=True) Person = build_entity( key="person", plural="persons", label= u'An individual. The minimal legal entity on which a legislation might be applied.', doc=''' ''', is_person=True, roles=[], ) entities = [Family, Person]
# -*- coding: utf-8 -*- # This file defines the entities needed by our legislation. from openfisca_core.entities import build_entity Commune = build_entity( key="commune", plural="communes", label= "Une commune. L'entité légale la plus réduite à laquelle s'applique la législation de ce moteur de calcul.", doc=''' TODO ''', is_person=True, # entité pivot ) Etat = build_entity(key="etat", plural="etats", label='État', roles=[{ 'key': 'commune', 'plural': 'communes', 'label': 'Communes', }]) entities = [Etat, Commune]
import numpy as np import pandas as pd from openfisca_core.model_api import Variable, YEAR from openfisca_core.entities import build_entity from openfisca_core.taxbenefitsystems import TaxBenefitSystem from openfisca_survey_manager.scenarios import AbstractSurveyScenario from openfisca_survey_manager.statshelpers import mark_weighted_percentiles from openfisca_survey_manager.variables import quantile Individu = build_entity( key = "individu", plural = "individus", label = u'Individu', is_person = True, ) entities = [Individu] class salaire(Variable): value_type = float entity = Individu label = "Salaire" definition_period = YEAR class decile_salaire_from_quantile(Variable): entity = Individu
from openfisca_core.entities import build_entity Familia = build_entity(key="familia", plural="families", label=u'Familia', roles=[{ 'key': 'sustentador_i_custodia', 'plural': 'sustentadors_i_custodia', 'label': u'sustentadors i custodia', 'max': 2, 'subroles': ['primer_adult', 'segon_adult'] }, { 'key': 'sustentador', 'plural': 'sustentadors', 'label': u'sustentadors', 'max': 2 }, { 'key': 'altres_familiars', 'plural': 'altres_familiars', 'label': u'Other familiar' }, { 'key': 'menor', 'plural': 'menors', 'label': u'Menor', }, { 'key': 'altres_persones', 'plural': 'altres_persones', 'label': u'altres persones', }]) FamiliaFins2onGrau = build_entity(key="familia_fins_a_segon_grau",
# This file defines the entities needed by our legislation. from openfisca_core.entities import build_entity Titled_Property = build_entity( key="titled_property", plural="titled_properties", label=u'Titled Property', doc=''' A Titled property represents a property that is owned by a Person or group of Persons. Example usage: Check the number of individuals of a specific role: check how many persons co-own the property: `titled_properties.nb_persons(Titled_Property.OWNER)`. Calculate a variable applied to each tenant of the group entity: calculate the income of each member of the Property: `tenants_incomes = titled_properties.members('income', period = MONTH); tenants_total_income = titled_properties.sum(tenants_incomes)`. For more information on group entities, see: http://openfisca.org/doc/coding-the-legislation/50_entities.html ''', roles=[{ 'key': 'owner', 'plural': 'owners', 'label': u'Owners', 'doc': u'The one or more persons who hold title for the property.' }, { 'key': 'other', 'plural': 'others', 'label': u'Others', 'doc': u'People who are not in any other role' }]) Person = build_entity( key="person", plural="persons",
def test_group_encapsulation(): """Projects a calculation to all members of an entity. When a household contains more than one family Variables can be defined for the the household And calculations are projected to all the member families. """ from openfisca_core.taxbenefitsystems import TaxBenefitSystem from openfisca_core.entities import build_entity from openfisca_core.periods import ETERNITY person_entity = build_entity( key="person", plural="people", label="A person", is_person=True, ) family_entity = build_entity( key="family", plural="families", label="A family (all members in the same household)", containing_entities=["household"], roles=[{ "key": "member", "plural": "members", "label": "Member", }]) household_entity = build_entity( key="household", plural="households", label="A household, containing one or more families", roles=[{ "key": "member", "plural": "members", "label": "Member", }]) entities = [person_entity, family_entity, household_entity] system = TaxBenefitSystem(entities) class household_level_variable(Variable): value_type = int entity = household_entity definition_period = ETERNITY class projected_family_level_variable(Variable): value_type = int entity = family_entity definition_period = ETERNITY def formula(family, period): return family.household("household_level_variable", period) system.add_variables(household_level_variable, projected_family_level_variable) simulation = SimulationBuilder().build_from_dict( system, { "people": { "person1": {}, "person2": {}, "person3": {} }, "families": { "family1": { "members": ["person1", "person2"] }, "family2": { "members": ["person3"] }, }, "households": { "household1": { "members": ["person1", "person2", "person3"], "household_level_variable": { "eternity": 5 } } } }) assert (simulation.calculate("projected_family_level_variable", "2021-01-01") == 5).all()
# -*- coding: utf-8 -*- # This file defines the entities needed by our legislation. from openfisca_core.entities import build_entity Person = build_entity( key="person", plural="persons", label= u'An individual. The minimal legal entity on which a legislation might be applied.', doc=''' Variables like 'salary' and 'income_tax' are usually defined for the entity 'Person'. Usage: Calculate a variable applied to a 'Person' (e.g. access the 'salary' of a specific month with person('salary', "2017-05")). Check the role of a 'Person' in a group entity (e.g. check if a the 'Person' is a 'first_parent' in a 'Household' entity with person.has_role(Household.FIRST_PARENT)). For more information, see: https://openfisca.org/doc/coding-the-legislation/50_entities.html ''', is_person=True, ) Vehicle = build_entity(key="vehicle", plural="vehicles", label=u'.', doc=''' Variables like 'salary' and 'income_tax' are usually defined for the entity 'Person'. Usage:
# -*- coding: utf-8 -*- from openfisca_core.entities import build_entity Famille = build_entity( key = "famille", plural = "familles", label = u'Famille', roles = [ { 'key': 'parent', 'plural': 'parents', 'label': u'Parents', 'subroles': ['demandeur', 'conjoint'] }, { 'key': 'enfant', 'plural': 'enfants', 'label': u'Enfants' } ] ) Individu = build_entity( key = "individu", plural = "individus", label = u'Individu', is_person = True ) FoyerFiscal = build_entity(
# -*- coding: utf-8 -*- import os from openfisca_core.taxbenefitsystems import TaxBenefitSystem from openfisca_core.model_api import Variable, MONTH from openfisca_core.entities import build_entity Person = build_entity( key="person", plural="persons", label=u'Person', is_person=True, ) COUNTRY_DIR = os.path.dirname(os.path.abspath(__file__)) class dummy_variable(Variable): entity = Person value_type = float definition_period = MONTH class CountryTaxBenefitSystem(TaxBenefitSystem): def __init__(self): super(CountryTaxBenefitSystem, self).__init__([Person]) param_path = os.path.join(COUNTRY_DIR, 'parameters') self.load_parameters(param_path) self.add_variable(dummy_variable)
# -*- coding: utf-8 -*- # This file defines the entities needed by our legislation. from openfisca_core.entities import build_entity Familia = build_entity(key="familia", plural="families", label=u'Familia', roles=[{ 'key': 'adult', 'plural': 'adults', 'label': u'Adults', 'max': 2, 'subroles': ['primer_adult', 'segon_adult'] }, { 'key': 'altre_adult', 'plural': 'altres_adults', 'label': u'Other adults' }, { 'key': 'menor', 'plural': 'menors', 'label': u'Menor', }]) Persona = build_entity( key="persona", plural="persones", label=u'Persona', is_person=True, )
Household = build_entity( key = "household", plural = "households", label = u'All the people in a family or group who live together in the same place.', doc = ''' Household is an example of a group entity. A group entity contains one or more individual·s. Each individual in a group entity has a role (e.g. parent or children). Some roles can only be held by a limited number of individuals (e.g. a 'first_parent' can only be held by one individual), while others can have an unlimited number of individuals (e.g. 'children'). Example: Housing variables (e.g. housing_tax') are usually defined for a group entity such as 'Household'. Usage: Check the number of individuals of a specific role (e.g. check if there is a 'second_parent' with household.nb_persons(Household.SECOND_PARENT)). Calculate a variable applied to each individual of the group entity (e.g. calculate the 'salary' of each member of the 'Household' with salaries = household.members('salary', period = MONTH); sum_salaries = household.sum(salaries)). For more information, see: https://openfisca.org/doc/coding-the-legislation/50_entities.html ''', roles = [ { 'key': 'parent', 'plural': 'parents', 'label': u'Parents', 'max': 2, 'subroles': ['first_parent', 'second_parent'], 'doc': u'The one or two adults in charge of the household.' }, { 'key': 'child', 'plural': 'children', 'label': u'Child', 'doc': u'Other individuals living in the household.' } ] )
import numpy as np import pandas as pd from openfisca_core.model_api import Variable, YEAR from openfisca_core.entities import build_entity from openfisca_core.taxbenefitsystems import TaxBenefitSystem from openfisca_survey_manager.scenarios import AbstractSurveyScenario from openfisca_survey_manager.statshelpers import mark_weighted_percentiles from openfisca_survey_manager.variables import quantile Individu = build_entity( key="individu", plural="individus", label='Individu', is_person=True, ) entities = [Individu] class salaire(Variable): value_type = float entity = Individu label = "Salaire" definition_period = YEAR class decile_salaire_from_quantile(Variable): entity = Individu value_type = int label = "Décile de salaire nouveau calcul"
from openfisca_core.entities import build_entity Article = build_entity( key="article", plural="articles", label='Article', is_person=True, # = est entité pivot doc=''' Un article est une part d'un unique titre. Il est sur une seule commune. Il traite d'une seule substance fiscale. ''') Titre = build_entity( # il n'y a qu'une Titre par article key="titre", plural="titres", label='Titre', roles=[{ 'key': 'article', 'plural': 'articles', 'label': 'Articles' }]) Commune = build_entity( # il n'y a qu'une commune par article key="commune", plural="communes", label='Commune', roles=[{ 'key': 'article',
# -*- coding: utf-8 -*- # This file defines the entities needed by our legislation. from openfisca_core.entities import build_entity Titled_Property = build_entity( key="titled_property", plural="titled_properties", label=u'Titled Property', doc=''' A Titled property represents a property that is owned by a Person or group of Persons. Example usage: Check the number of individuals of a specific role: check how many persons co-own the property: `titled_properties.nb_persons(Titled_Property.OWNER)`. Calculate a variable applied to each tenant of the group entity: calculate the income of each member of the Property: `tenants_incomes = titled_properties.members('income', period = MONTH); tenants_total_income = titled_properties.sum(tenants_incomes)`. For more information on group entities, see: http://openfisca.org/doc/coding-the-legislation/50_entities.html ''', roles=[{ 'key': 'owner', 'plural': 'owners', 'label': u'Owners', 'doc': u'The one or more persons who hold title for the property.' }]) Person = build_entity( key="person", plural="persons", label=u'Person', doc=''' A Person represents an individual, the minimal legal entity on which a legislation might be applied.
Menage = build_entity(key="menage", plural="menages", label=u"Occupants d'un logement principal", doc=''' Ménage est un exemple d'entité groupe. Une entité groupe contient un ou plusieurs individus. Chaque individu dans une entité groupe a un rôle (e.g. parent ou enfant). Certains rôles ne peuvent être tenus que par un nombre limité d'individus (e.g. a 'premier_parent' ne peut être tenu que par un individu), alors que d'autres peuvent avoir un nombre illimité d'individus (e.g. 'enfant'). Exemple : Les variables de logement (e.g. 'taxe_habitation') sont généralement définies pour une entité groupe telle que 'Menage'. Utilisation : Vérifier le nombre d'individus d'un rôle spécifique (e.g. vérifier s'il y a un 'second_parent') avec menage.nb_persons(Menage.PREMIER_PARENT). Calculer une variable appliquée à chaque individu de l'entité groupe (e.g. calculer le 'salaire' de chaque membre du 'Menage') avec salaires = menage.members('salaire', period = MONTH); sum_salaries = menage.sum(salaires). Pour en savoir plus, consulter ce lien : https://openfisca.org/doc/coding-the-legislation/50_entities.html ''', roles=[{ 'key': 'parent', 'plural': 'parents', 'label': u'Parents', 'max': 2, 'subroles': ['premier_parent', 'second_parent'], 'doc': u'Le ou les deux adultes en charge du ménage.' }, { 'key': 'enfant', 'plural': 'enfants', 'label': u'Enfant', 'doc': u'Autres individus vivant au sein du ménage.' }])
# -*- coding: utf-8 -*- import collections import itertools from openfisca_core.entities import build_entity Famille = build_entity( key = "famille", plural = "familles", label = u'Famille', roles = [ { 'key': 'parent', 'plural': 'parents', 'label': u'Parents', 'subroles': ['demandeur', 'conjoint'] }, { 'key': 'enfant', 'plural': 'enfants', 'label': u'Enfants' } ] ) Individu = build_entity( key = "individu", plural = "individus", label = u'Individu', is_person = True )
Household = build_entity( key="household", plural="households", label='Household', doc=''' Household is an example of a group entity. A group entity contains one or more individual·s. For more information, see: https://openfisca.org/doc/coding-the-legislation/50_entities.html ''', roles=[ { 'key': 'personne_de_reference', 'label': 'Personne de reference (Chef-fe de ménage)', 'doc': 'La personne de référence dans le ménage.', 'max': 1, }, { 'key': 'conjoint', 'label': 'Conjoint de la personne de référence', 'doc': 'Le/la conjoint-e de la personne de référence.', # 'max': 1, }, { 'key': 'enfant', 'plural': 'enfants', 'label': 'Enfant', 'doc': '''Enfant à la charge de la personne de référence et de son conjoint - il peut y avoir d'autres enfant dans le ménage ''' }, { 'key': 'autre_membre', 'plural': 'autres_membres', 'label': 'Autres membres du ménage', 'doc': 'Membres du ménage différents de la personne de référence, de son/sa conjoint-e et de leurs enfants' } ] # 'Chef du menage', # 'Conjoint du CM', # 'Enfant du chef/conjoint du CM', # autre rassemble: # 'Pere/mere du CM/conjoint du CM', # 'Autre parent du CM/conjoint du CM', # 'Autres personnes non apparentees', # 'Domestique' )
from openfisca_core.entities import build_entity Societe = build_entity( key="societe", plural="societes", label='Société', is_person=True # = est entité pivot ) Commune = build_entity(key="commune", plural="communes", label='Commune', roles=[{ 'key': 'societe', 'plural': 'societes', 'label': 'Sociétés' }]) entities = [Societe, Commune]
# -*- coding: utf-8 -*- from openfisca_core.entities import build_entity Famille = build_entity(key="famille", plural="familles", label=u'Famille', roles=[{ 'key': 'parent', 'plural': 'parents', 'label': u'Parents', 'subroles': ['demandeur', 'conjoint'] }, { 'key': 'enfant', 'plural': 'enfants', 'label': u'Enfants' }]) Individu = build_entity(key="individu", plural="individus", label=u'Individu', is_person=True) FoyerFiscal = build_entity(key="foyer_fiscal", plural="foyers_fiscaux", label=u'Déclaration d’impôts', roles=[ { 'key': 'declarant', 'plural': 'declarants', 'label': u'Déclarants',
def test_enum_projects_between_containing_groups(): """ Test that an Enum-type person-level variable projects values onto its household (from the first person) correctly. """ person_entity = build_entity( key="person", plural="people", label="A person", is_person=True, ) family_entity = build_entity( key="family", plural="families", label="A family (all members in the same household)", containing_entities=["household"], roles=[{ "key": "member", "plural": "members", "label": "Member", }]) household_entity = build_entity( key="household", plural="households", label="A household, containing one or more families", roles=[{ "key": "member", "plural": "members", "label": "Member", }]) entities = [person_entity, family_entity, household_entity] system = TaxBenefitSystem(entities) class enum(Enum): FIRST_OPTION = "First option" SECOND_OPTION = "Second option" class household_level_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = household_entity definition_period = ETERNITY class projected_family_level_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = family_entity definition_period = ETERNITY def formula(family, period): return family.household("household_level_variable", period) system.add_variables(household_level_variable, projected_family_level_variable) simulation = SimulationBuilder().build_from_dict( system, { "people": { "person1": {}, "person2": {}, "person3": {} }, "families": { "family1": { "members": ["person1", "person2"] }, "family2": { "members": ["person3"] }, }, "households": { "household1": { "members": ["person1", "person2", "person3"], "household_level_variable": { "eternity": "SECOND_OPTION" } } } }) assert (simulation.calculate("projected_family_level_variable", "2021-01-01").decode_to_str() == np.array( ["SECOND_OPTION"])).all()
# -*- coding: utf-8 -*- # This file defines the entities needed by our legislation. from openfisca_core.entities import build_entity Household = build_entity( key="household", plural="households", label= u"All the people in a family or group who live together in the same place.", doc=u"Each household may contain multiple families, or benefit units.", roles=[ { "key": "adult", "plural": "adults", "label": u"Adult", "doc": u"The adults in the household.", }, { "key": "child", "plural": "children", "label": u"Child", "doc": u"Dependent children in the household.", }, ], ) BenUnit = build_entity( key="benunit", plural="benunits", label=
def test_enum_projects_downwards(): """ Test that an Enum-type household-level variable projects values onto its members correctly. """ person = build_entity( key="person", plural="people", label="A person", is_person=True, ) household = build_entity(key="household", plural="households", label="A household", roles=[{ "key": "member", "plural": "members", "label": "Member", }]) entities = [person, household] system = TaxBenefitSystem(entities) class enum(Enum): FIRST_OPTION = "First option" SECOND_OPTION = "Second option" class household_enum_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = household definition_period = ETERNITY class projected_enum_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = person definition_period = ETERNITY def formula(person, period): return person.household("household_enum_variable", period) system.add_variables(household_enum_variable, projected_enum_variable) simulation = SimulationBuilder().build_from_dict( system, { "people": { "person1": {}, "person2": {}, "person3": {} }, "households": { "household1": { "members": ["person1", "person2", "person3"], "household_enum_variable": { "eternity": "SECOND_OPTION" } } } }) assert (simulation.calculate("projected_enum_variable", "2021-01-01").decode_to_str() == np.array( ["SECOND_OPTION"] * 3)).all()
# -*- coding: utf-8 -*- from openfisca_core.entities import build_entity Famille = build_entity( key = "famille", plural = "familles", label = u'Famille', roles = [ { 'key': 'parent', 'plural': 'parents', 'label': u'Parents', 'subroles': ['demandeur', 'conjoint'] }, { 'key': 'enfant', 'plural': 'enfants', 'label': u'Enfants', } ] ) Individu = build_entity( key = "individu", plural = "individus", label = u'Individu', is_person = True, )
""" This file defines the entities needed by our legislation. """ from openfisca_core.entities import build_entity Building = build_entity( key="building", plural="buildings", label=u'Building', doc=''' ''', roles=[{ 'key': 'representative', 'plural': 'representatives', 'label': u'Representative', 'doc': u'A person authorised to represent the building' }, { 'key': 'other', 'plural': 'others', 'label': u'Other', 'doc': u'Other representatives who are not a person - such as Strata bodies' }]) Organisation = build_entity( key="organisation", plural="organisations", label=u'Organisation',
# doc = ''' # Variables like 'salary' and 'income_tax' are usually defined for the entity 'Person'. # Usage: # Calculate a variable applied to a 'Person' (e.g. access the 'salary' of a specific month with person('salary', "2017-05")). # Check the role of a 'Person' in a group entity (e.g. check if a the 'Person' is a 'first_parent' in a 'Household' entity with person.has_role(Household.FIRST_PARENT)). # For more information, see: https://openfisca.org/doc/coding-the-legislation/50_entities.html # ''', # is_person = True, # ) Personne = build_entity( key="personne", plural="personnes", label=u'Une personne physique ou morale', is_person=True, ) Pays = build_entity(key="pays", plural="pays", label=u'Le pays', doc=''' TODO. ''', roles=[{ 'key': 'Contribuables', 'plural': 'contribuables', 'label': u'Contribuables', 'doc': u'Les contribuables du pays.'