Skip to content

Conversation

benoit-cty
Copy link
Contributor

@benoit-cty benoit-cty commented Sep 20, 2024

New features

  • Add an option force_keep_cache in simulation.calculate to override the deletion of some cached variable by purge_cache_of_invalid_values. For example in OpenFisca-France if you calculate the rfr variable and then what to compute quantile : the rfr will be computed again because it has been deleted from the cache.

Sample code to use this with OpenFiscaFrance:

import os
import shutil
import time
import numpy as np
from openfisca_core.simulation_builder import SimulationBuilder
from openfisca_core.tools.simulation_dumper import dump_simulation
from openfisca_france import FranceTaxBenefitSystem
from openfisca_core.periods import Period, DateUnit, Instant

dump_path = "/tmp/dump_openfisca_lqzkgsdj"

tbs = FranceTaxBenefitSystem()

sb = SimulationBuilder()
simulation = sb.build_default_simulation(tbs, count=3)
instant = Instant((2023, 1, 1))
        
period = Period((DateUnit.YEAR, instant, 1))
simulation.set_input("salaire_de_base", period, np.array([10_000, 2500, 3500]))
force_keep_cache = [  {"variable": "rfr", "period": period}]
rfr = simulation.calculate("rfr", period, force_keep_cache=force_keep_cache)
if os.path.exists(dump_path):
    # Remove folder
    shutil.rmtree(dump_path)

dump_simulation(simulation, dump_path)

if os.path.exists(dump_path + "/rfr_plus_values_hors_rni/2023.npy"):
    print("Le rfr_plus_values_hors_rni est bien dans le dump.")
    if os.path.exists(dump_path + "/rfr/2023.npy"):
        print("OK, le rfr est aussi dans le dump.")
    else:
        print("ERROR : Le RFR n'est pas dans le dump alors qu'il vient dêtre calculé.")
        exit(1)

More information here in French : https://openfisca.slack.com/archives/C47KV33QF/p1726588042968229

@coveralls
Copy link

Coverage Status

coverage: 74.728% (-0.008%) from 74.736%
when pulling 869ea35 on feat/override_purge_cache_of_invalid_values
into 00fe69a on master.

@sandcha sandcha assigned sandcha and benoit-cty and unassigned sandcha Sep 20, 2024
@sandcha sandcha self-requested a review September 20, 2024 14:38
@bonjourmauko
Copy link
Member

Thanks @benoit-cty ! Could you add some tests? I can help you with that if need be.

@benoit-cty
Copy link
Contributor Author

Thanks @benoit-cty ! Could you add some tests? I can help you with that if need be.

Hello Mauko, I definitly need some help on testing as I'm not familiar with Core. How can I use the default TBS to add revelant testing ?

@bonjourmauko
Copy link
Member

Thanks @benoit-cty ! Could you add some tests? I can help you with that if need be.

Hello Mauko, I definitly need some help on testing as I'm not familiar with Core. How can I use the default TBS to add revelant testing ?

In core we use the country package as a mean of testing. Look at the following test:

def test_calculate_full_tracer(tax_benefit_system) -> None:
    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

The tax_benefix_system injected to the test is already available for you to use.

These are called fixtures, and are available for every test. You can see all the ones already defined in tests/fixtures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants