Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remplace les méthodes .period par une instanciation de la classe Period() #1961

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from numpy import busday_count, datetime64, logical_or as or_, logical_and as and_, timedelta64

from openfisca_core.periods import Period

from openfisca_france.model.base import *

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -535,7 +537,7 @@ def compute_allegement_anticipe(individu, period, parameters, variable_name, com
if period.start.month == 12:
cumul = individu(
variable_name,
period.start.offset('first-of', 'year').period('month', 11), options = [ADD])
Period(('month', period.start.offset('first-of', 'year'), 11)), options = [ADD])
return compute_function(
individu, period.this_year, parameters
) - cumul
Expand All @@ -546,8 +548,8 @@ def compute_allegement_progressif(individu, period, parameters, variable_name, c
return compute_function(individu, period.first_month, parameters)

if period.start.month > 1:
up_to_this_month = period.start.offset('first-of', 'year').period('month', period.start.month)
up_to_previous_month = period.start.offset('first-of', 'year').period('month', period.start.month - 1)
up_to_this_month = Period(('month', period.start.offset('first-of', 'year'), period.start.month))
up_to_previous_month = Period(('month', period.start.offset('first-of', 'year'), period.start.month - 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Une limite peut-être à notre syntaxe qui existait précédemment à cette PR :

Ici Period(('month', period.start.offset('first-of', 'year'), period.start.month - 1)) ne fonctionne que parce que period.start.month > 1.

Sinon, on obtient une AssertionError pas très claire sur la démarche à suivre. Extrait de l'erreur avec l'ancienne syntaxe :

  File "/Users/sch/.local/share/virtualenvs/fr1961/lib/python3.7/site-packages/openfisca_core/periods/instant_.py", line 97, in period
    assert isinstance(size, int) and size >= 1, 'Invalid size: {} of type {}'.format(size, type(size))
AssertionError: Invalid size: 0 of type <class 'int'>

cumul = individu(variable_name, up_to_previous_month, options = [ADD])
return compute_function(individu, up_to_this_month, parameters) - cumul

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from openfisca_core.periods import Period

from openfisca_france.model.base import *


Expand Down Expand Up @@ -371,8 +373,10 @@ def compute_cotisation_anticipee(individu, period, parameters, cotisation_type =
bareme_name = bareme_name,
)
if period.start.month == 12:
cumul = individu(variable_name, period.start.offset('first-of', 'month').offset(
-11, 'month').period('month', 11), options = [ADD])
cumul = individu(
variable_name,
Period(('month', period.start.offset('first-of', 'month').offset(-11, 'month'), 11)),
options = [ADD])
# December variable_name depends on variable_name in the past 11 months.
# We need to explicitely allow this recursion.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from openfisca_core.periods import Period

from openfisca_france.model.base import *


Expand Down Expand Up @@ -333,7 +335,7 @@ def formula_2017_01_01(menage, period, parameters):
taux_th_commune = menage('taux_th_commune', period)
taux_th_epci = menage('taux_th_epci', period)
ecart_avec_2000 = period.start.offset('first-of', 'year').year - 2000
annee_2000 = period.start.offset('first-of', 'year').period('year').offset(-ecart_avec_2000)
annee_2000 = Period(('year', period.start.offset('first-of', 'year'), 1)).offset(-ecart_avec_2000)
taux_th_commune_2000 = menage('taux_th_commune', annee_2000)
taux_th_epci_2000 = menage('taux_th_epci', annee_2000)

Expand Down
4 changes: 3 additions & 1 deletion openfisca_france/model/prestations/agepi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from numpy import fabs, timedelta64

from openfisca_core.periods import Period

from openfisca_france.model.base import Famille, Individu, Variable, Enum, MONTH, ADD,\
set_input_dispatch_by_period, set_input_divide_by_period, date, min_, not_
from openfisca_france.model.revenus.activite.salarie import TypesContrat, TypesLieuEmploiFormation,\
Expand Down Expand Up @@ -83,7 +85,7 @@ def formula_2014_01_20(individu, period, parameters):
condition_nb_enfants = individu.famille('agepi_nbenf', period) > 0

# L'individu n'a pas touché l'AGEPI dans les 12 derniers mois (condition de durée entre faits générateurs)
annee_glissante = period.start.period('year').offset(-1).offset(-1, 'month')
annee_glissante = Period(('year', period.start, 1)).offset(-1).offset(-1, 'month')
agepi_non_percues = not_(individu('agepi', annee_glissante, options=[ADD]))

# L'individu est inscrit en catégorie 1, 2, 3, 4 "stagiaire de la formation professionnelle" ou 5 "contrat aidé"
Expand Down
5 changes: 4 additions & 1 deletion openfisca_france/model/prestations/aide_mobilite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from numpy import fabs, timedelta64

from openfisca_core.periods import Period

from openfisca_france.model.base import Individu, Variable, MONTH, Enum, not_, ADD,\
set_input_dispatch_by_period, set_input_divide_by_period, min_, date
from openfisca_france.model.caracteristiques_socio_demographiques.logement import TypesLieuResidence
Expand Down Expand Up @@ -326,7 +329,7 @@ def formula_2021_06_09(individu, period, parameters):
eligibilite_amob = individu('aide_mobilite_eligible', period)
parametres_amob = parameters(period).prestations_sociales.aide_mobilite

annee_glissante = period.start.period('year').offset(-1)
annee_glissante = Period(('year', period.start, 1)).offset(-1)

aide_mobilite_12_derniers_mois = individu('aide_mobilite', annee_glissante, options=[ADD])

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from openfisca_france.model.base import *
from openfisca_core import periods

from openfisca_france.model.base import *

from numpy import logical_or as or_, logical_and as and_

# Les éligibilités séparées de l'indemnité inflation
Expand Down
6 changes: 3 additions & 3 deletions openfisca_france/model/prestations/aides_logement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

from numpy import ceil, datetime64, fromiter, int16, logical_or as or_, logical_and as and_, logical_not as not_

import openfisca_france
from openfisca_core.periods import Instant
from openfisca_core.periods import Instant, Period

import openfisca_france
from openfisca_france.model.base import *
from openfisca_france.model.revenus.activite.salarie import TypesConges
from openfisca_france.model.prestations.prestations_familiales.base_ressource import nb_enf
Expand Down Expand Up @@ -566,7 +566,7 @@ class aide_logement_base_ressources_individu(Variable):

def formula_2021_01_01(individu, period, parameters):
period_frais = period.last_year
annee_glissante = period.start.period('year').offset(-1).offset(-1, 'month')
annee_glissante = Period(('year', period.start, 1)).offset(-1).offset(-1, 'month')

salaire_imposable = individu('salaire_imposable', annee_glissante, options=[ADD])
chomage_imposable = individu('chomage_imposable', annee_glissante, options=[ADD])
Expand Down
10 changes: 6 additions & 4 deletions openfisca_france/model/prestations/autonomie.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from openfisca_core.periods import Period

from openfisca_france.model.base import *


Expand Down Expand Up @@ -133,7 +135,7 @@ class apa_eligibilite(Variable):
set_input = set_input_dispatch_by_period

def formula_2002(individu, period, parameters):
period = period.start.offset('first-of', 'month').period('month')
period = Period(('month', period.start.offset('first-of', 'month'), 1))
parameters = parameters(period).prestations_sociales.prestations_etat_de_sante.perte_autonomie_personnes_agees
age = individu('age', period)
apa_age_min = parameters.apa_domicile.condition_age
Expand Down Expand Up @@ -172,7 +174,7 @@ class apa_domicile(Variable):
set_input = set_input_divide_by_period

def formula_2002(individu, period, parameters):
period = period.start.offset('first-of', 'month').period('month')
period = Period(('month', period.start.offset('first-of', 'month'), 1))
apa_domicile = parameters(period).prestations_sociales.prestations_etat_de_sante.perte_autonomie_personnes_agees.apa_domicile
apa_eligibilite = individu('apa_eligibilite', period)
smic_brut_horaire = parameters(period).marche_travail.salaire_minimum.smic.smic_b_horaire
Expand All @@ -193,7 +195,7 @@ class apa_etablissement(Variable):
set_input = set_input_divide_by_period

def formula_2002(individu, period, parameters):
period = period.start.offset('first-of', 'month').period('month')
period = Period(('month', period.start.offset('first-of', 'month'), 1))
perte_autonomie_personnes_agees = parameters(period).prestations_sociales.prestations_etat_de_sante.perte_autonomie_personnes_agees
smic_brut_horaire = parameters(period).marche_travail.salaire_minimum.smic.smic_b_horaire
seuil_non_versement = perte_autonomie_personnes_agees.apa_institution.seuil_versement_en_part_smic_brut_horaire * smic_brut_horaire
Expand Down Expand Up @@ -350,7 +352,7 @@ class apa_urgence_institution(Variable):
set_input = set_input_divide_by_period

def formula_2002(individu, period, parameters):
period = period.start.offset('first-of', 'month').period('month')
period = Period(('month', period.start.offset('first-of', 'month'), 1))
dependance_tarif_etablissement_gir_1_2 = individu('dependance_tarif_etablissement_gir_1_2', period)
part_urgence_institution = parameters(period).prestations_sociales.prestations_etat_de_sante.perte_autonomie_personnes_agees.apa_institution.part_tarif_dependance
apa_urgence_institution = part_urgence_institution * dependance_tarif_etablissement_gir_1_2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from openfisca_core.periods import Period

from openfisca_france.model.base import * # noqa analysis:ignore


Expand All @@ -17,7 +19,7 @@ def formula_2022_03_01(individu, period, parameters):
montant_degressivite = parameters(period).prestations_sociales.aides_jeunes.contrat_engagement_jeune.degressivite.montant
age = individu('age', period)
majeur = individu('majeur', period)
previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)
tranche = individu.foyer_fiscal('ir_tranche', previous_year)

degressivite = majeur * (tranche > 0) * montant_degressivite
Expand Down Expand Up @@ -91,7 +93,7 @@ def formula_2022_03_01(individu, period, parameters):

niveau_ressources = (niveau_ressources_individuelles_3_mois) / 3

previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)
tranche = individu.foyer_fiscal('ir_tranche', previous_year) <= 1
return (niveau_ressources <= plafond) * sans_rsa * sans_ppa * tranche

Expand Down
4 changes: 2 additions & 2 deletions openfisca_france/model/prestations/logement_social.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from numpy import isin, logical_not as not_, select

from openfisca_core.indexed_enums import Enum
from openfisca_core.periods import MONTH
from openfisca_core.periods import MONTH, Period
from openfisca_core.variables import Variable

from openfisca_france.entities import Famille, Menage
Expand Down Expand Up @@ -172,7 +172,7 @@ class logement_social_eligible(Variable):
def formula_2017(famille, period, parameters):
parent_majeur = famille.any(famille.members('majeur', period), role = Famille.PARENT)
logement_social_plafond_ressources = famille('logement_social_plafond_ressources', period)
rfr_n0 = famille.demandeur.foyer_fiscal('rfr', period.start.offset('first-of', 'year').period('year'))
rfr_n0 = famille.demandeur.foyer_fiscal('rfr', Period(('year', period.start.offset('first-of', 'year'), 1)))
rfr_n1 = famille.demandeur.foyer_fiscal('rfr', period.last_year)
rfr_n2 = famille.demandeur.foyer_fiscal('rfr', period.n_2)

Expand Down
16 changes: 9 additions & 7 deletions openfisca_france/model/prestations/minima_sociaux/aah.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from openfisca_core.periods import Period

from openfisca_france.model.base import *

from numpy import datetime64
Expand Down Expand Up @@ -105,7 +107,7 @@ def assiette_revenu_activite_demandeur(revenus_demandeur):
return (1 - aah.travail_ordinaire.abattement_30) * total_tranche1 + (1 - aah.travail_ordinaire.abattement_sup) * total_tranche2

def base_ressource_eval_trim():
three_previous_months = period.first_month.start.period('month', 3).offset(-3)
three_previous_months = Period(('month', period.first_month.start, 3)).offset(-3)
base_ressource_activite = individu('aah_base_ressources_activite_eval_trimestrielle', period) - individu('aah_base_ressources_activite_milieu_protege', three_previous_months, options = [ADD])
base_ressource_hors_activite = individu('aah_base_ressources_hors_activite_eval_trimestrielle', period) + individu('aah_base_ressources_activite_milieu_protege', three_previous_months, options = [ADD])

Expand Down Expand Up @@ -151,7 +153,7 @@ def assiette_revenu_activite_demandeur(revenus_demandeur):
return (1 - aah.travail_ordinaire.abattement_30) * total_tranche1 + (1 - aah.travail_ordinaire.abattement_sup) * total_tranche2

def base_ressource_eval_trim():
three_previous_months = period.first_month.start.period('month', 3).offset(-3)
three_previous_months = Period(('month', period.first_month.start, 3)).offset(-3)
base_ressource_activite = individu('aah_base_ressources_activite_eval_trimestrielle', period) - individu('aah_base_ressources_activite_milieu_protege', three_previous_months, options = [ADD])
base_ressource_hors_activite = individu('aah_base_ressources_hors_activite_eval_trimestrielle', period) + individu('aah_base_ressources_activite_milieu_protege', three_previous_months, options = [ADD])

Expand Down Expand Up @@ -192,7 +194,7 @@ def assiette_revenu_activite_demandeur(revenus_demandeur):
return (1 - aah.travail_ordinaire.abattement_30) * total_tranche1 + (1 - aah.travail_ordinaire.abattement_sup) * total_tranche2

def base_ressource_eval_trim():
three_previous_months = period.first_month.start.period('month', 3).offset(-3)
three_previous_months = Period(('month', period.first_month.start, 3)).offset(-3)
base_ressource_activite = individu('aah_base_ressources_activite_eval_trimestrielle', period) - individu('aah_base_ressources_activite_milieu_protege', three_previous_months, options = [ADD])
base_ressource_hors_activite = individu('aah_base_ressources_hors_activite_eval_trimestrielle', period) + individu('aah_base_ressources_activite_milieu_protege', three_previous_months, options = [ADD])

Expand Down Expand Up @@ -239,7 +241,7 @@ class aah_base_ressources_activite_eval_trimestrielle(Variable):

def formula(individu, period):
period = period.first_month
three_previous_months = period.start.period('month', 3).offset(-3)
three_previous_months = Period(('month', period.start, 3)).offset(-3)
last_year = period.last_year

ressources_a_inclure = [
Expand Down Expand Up @@ -299,7 +301,7 @@ class aah_base_ressources_hors_activite_eval_trimestrielle(Variable):

def formula(individu, period):
period = period.first_month
three_previous_months = period.start.period('month', 3).offset(-3)
three_previous_months = Period(('month', period.start, 3)).offset(-3)

ressources_a_inclure = [
'asi',
Expand Down Expand Up @@ -564,7 +566,7 @@ class eligibilite_caah(Variable):
set_input = set_input_dispatch_by_period

def formula_2015_07_01(individu, period, parameters):
annee_precedente = period.start.period('year').offset(-1)
annee_precedente = Period(('year', period.start, 1)).offset(-1)
prestations = parameters(period).prestations_sociales
taux_incapacite_min = prestations.prestations_etat_de_sante.invalidite.aah.taux_capacite.taux_incapacite
aah = individu('aah', period)
Expand Down Expand Up @@ -606,7 +608,7 @@ def formula_2015_07_01(individu, period, parameters):

def formula_2005_07_01(individu, period, parameters):
invalidite = parameters(period).prestations_sociales.prestations_etat_de_sante.invalidite
annee_precedente = period.start.period('year').offset(-1)
annee_precedente = Period(('year', period.start, 1)).offset(-1)
activite_12_mois = individu('salaire_imposable', annee_precedente, options = [ADD]) + individu('rpns_imposables', annee_precedente)

garantie_ressources = invalidite.caah.garantie_ressources
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from numpy import abs as abs_, logical_or as or_

from openfisca_core.periods import Period

from openfisca_france.model.base import *


Expand Down Expand Up @@ -147,7 +149,7 @@ class asi_eligibilite(Variable):
set_input = set_input_dispatch_by_period

def formula(individu, period):
last_month = period.start.period('month').offset(-1)
last_month = Period(('month', period.start, 1)).offset(-1)

non_eligible_aspa = not_(individu('aspa_eligibilite', period))
touche_pension_invalidite = individu('pensions_invalidite', period) > 0
Expand Down
12 changes: 7 additions & 5 deletions openfisca_france/model/prestations/minima_sociaux/ass.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from numpy import absolute as abs_, logical_and as and_

from openfisca_core.periods import Period

from openfisca_france.model.base import *


Expand Down Expand Up @@ -79,7 +81,7 @@ class ass_base_ressources_individu(Variable):

def formula(individu, period, parameters):
# Rolling year
previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)
# N-1
last_year = period.last_year

Expand Down Expand Up @@ -130,9 +132,9 @@ class ass_base_ressources_conjoint(Variable):

def formula(individu, period, parameters):
# Rolling year
previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)

last_month = period.start.period('month').offset(-1)
last_month = Period(('month', period.start, 1)).offset(-1)

ass_base_ressources_individu = individu('ass_base_ressources_individu', period)
chomage_net_interrompue = individu('chomage_net', last_month) == 0
Expand All @@ -147,14 +149,14 @@ def formula(individu, period, parameters):


def calculateWithAbatement(individu, parameters, period, ressourceName):
last_month = period.start.period('month').offset(-1)
last_month = Period(('month', period.start, 1)).offset(-1)
has_ressources_substitution = (
individu('chomage_net', last_month)
+ individu('indemnites_journalieres', last_month)
+ individu('retraite_nette', last_month)
) > 0
# Rolling year
previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)

ressource_year = individu(ressourceName, previous_year, options=[ADD])
ressource_last_month = individu(ressourceName, last_month)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from numpy import logical_not as not_

from openfisca_core.periods import Period

from openfisca_france.model.base import Variable, Famille, MONTH, ADD, set_input_dispatch_by_period


Expand All @@ -10,7 +13,7 @@ class css_cmu_acs_eligibilite(Variable):
set_input = set_input_dispatch_by_period

def formula(famille, period, parameters):
previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)
this_year = period.this_year
age_min = parameters(period).prestations_sociales.solidarite_insertion.minima_sociaux.cs.cmu.age_limite_pac
nb_enfants = famille('cmu_nb_pac', period)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from numpy import absolute as abs_, logical_or as or_, logical_not as not_

from openfisca_core.periods import Period

from openfisca_france.model.base import (
Variable,
Individu,
Expand Down Expand Up @@ -29,7 +31,7 @@ class css_cmu_base_ressources_individu(Variable):

def formula(individu, period, parameters):
# Rolling year
previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)
# N-1
last_year = period.last_year
last_month = period.last_month
Expand Down Expand Up @@ -93,7 +95,7 @@ class css_cmu_base_ressources(Variable):
set_input = set_input_divide_by_period

def formula(famille, period, parameters):
previous_year = period.start.period('year').offset(-1)
previous_year = Period(('year', period.start, 1)).offset(-1)

ressources_famille_a_inclure = [
'af',
Expand Down
Loading