Skip to content
Merged
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

### 35.10.1 [#1143](https://github.com/openfisca/openfisca-core/pull/1143)

#### Bug fix

- Reintroduce support for the ``day`` date unit in `holders.set_input_dispatch_by_period` and `holders.
set_input_divide_by_period`
- Allows for dispatching values per day, for example, to provide a daily (week, fortnight) to an yearly variable.
- Inversely, allows for calculating the daily (week, fortnight) value of a yearly input.

## 35.10.0 [#1151](https://github.com/openfisca/openfisca-core/pull/1151)

#### New features
Expand Down
18 changes: 6 additions & 12 deletions openfisca_core/holders/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ def set_input_dispatch_by_period(holder, period, array):
period_size = period.size
period_unit = period.unit

if holder.variable.definition_period == periods.MONTH:
cached_period_unit = periods.MONTH
elif holder.variable.definition_period == periods.YEAR:
cached_period_unit = periods.YEAR
else:
raise ValueError('set_input_dispatch_by_period can be used only for yearly or monthly variables.')
if holder.variable.definition_period == periods.ETERNITY:
raise ValueError("set_input_dispatch_by_period can't be used for eternal variables.")

cached_period_unit = holder.variable.definition_period
after_instant = period.start.offset(period_size, period_unit)

# Cache the input data, skipping the existing cached months
Expand Down Expand Up @@ -55,13 +52,10 @@ def set_input_divide_by_period(holder, period, array):
period_size = period.size
period_unit = period.unit

if holder.variable.definition_period == periods.MONTH:
cached_period_unit = periods.MONTH
elif holder.variable.definition_period == periods.YEAR:
cached_period_unit = periods.YEAR
else:
raise ValueError('set_input_divide_by_period can be used only for yearly or monthly variables.')
if holder.variable.definition_period == periods.ETERNITY:
raise ValueError("set_input_divide_by_period can't be used for eternal variables.")

cached_period_unit = holder.variable.definition_period
after_instant = period.start.offset(period_size, period_unit)

# Count the number of elementary periods to change, and the difference with what is already known.
Expand Down
98 changes: 98 additions & 0 deletions openfisca_core/holders/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import pytest

from openfisca_core import holders, periods, tools
from openfisca_core.entities import Entity
from openfisca_core.holders import Holder
from openfisca_core.periods import Instant, Period
from openfisca_core.populations import Population
from openfisca_core.variables import Variable


@pytest.fixture
def people():
return Entity(
key = "person",
plural = "people",
label = "An individual member of a larger group.",
doc = "People have the particularity of not being someone else.",
)


@pytest.fixture
def Income(people):
return type(
"Income",
(Variable,), {
"value_type": float,
"entity": people
},
)


@pytest.fixture
def population(people):
population = Population(people)
population.count = 1
return population


@pytest.mark.parametrize("dispatch_unit, definition_unit, values, expected", [
[periods.YEAR, periods.YEAR, [1.], [3.]],
[periods.YEAR, periods.MONTH, [1.], [36.]],
[periods.YEAR, periods.DAY, [1.], [1096.]],
[periods.MONTH, periods.YEAR, [1.], [1.]],
[periods.MONTH, periods.MONTH, [1.], [3.]],
[periods.MONTH, periods.DAY, [1.], [90.]],
[periods.DAY, periods.YEAR, [1.], [1.]],
[periods.DAY, periods.MONTH, [1.], [1.]],
[periods.DAY, periods.DAY, [1.], [3.]],
])
def test_set_input_dispatch_by_period(
Income,
population,
dispatch_unit,
definition_unit,
values,
expected,
):
Income.definition_period = definition_unit
income = Income()
holder = Holder(income, population)
instant = Instant((2022, 1, 1))
dispatch_period = Period((dispatch_unit, instant, 3))

holders.set_input_dispatch_by_period(holder, dispatch_period, values)
total = sum(map(holder.get_array, holder.get_known_periods()))

tools.assert_near(total, expected, absolute_error_margin = 0.001)


@pytest.mark.parametrize("divide_unit, definition_unit, values, expected", [
[periods.YEAR, periods.YEAR, [3.], [1.]],
[periods.YEAR, periods.MONTH, [36.], [1.]],
[periods.YEAR, periods.DAY, [1095.], [1.]],
[periods.MONTH, periods.YEAR, [1.], [1.]],
[periods.MONTH, periods.MONTH, [3.], [1.]],
[periods.MONTH, periods.DAY, [90.], [1.]],
[periods.DAY, periods.YEAR, [1.], [1.]],
[periods.DAY, periods.MONTH, [1.], [1.]],
[periods.DAY, periods.DAY, [3.], [1.]],
])
def test_set_input_divide_by_period(
Income,
population,
divide_unit,
definition_unit,
values,
expected,
):
Income.definition_period = definition_unit
income = Income()
holder = Holder(income, population)
instant = Instant((2022, 1, 1))
divide_period = Period((divide_unit, instant, 3))

holders.set_input_divide_by_period(holder, divide_period, values)
last = holder.get_array(holder.get_known_periods()[-1])

tools.assert_near(last, expected, absolute_error_margin = 0.001)
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ non_interactive = True
[mypy-openfisca_core.commons.tests.*]
ignore_errors = True

[mypy-openfisca_core.holders.tests.*]
ignore_errors = True

[mypy-openfisca_core.scripts.*]
ignore_errors = True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

setup(
name = 'OpenFisca-Core',
version = '35.10.0',
version = '35.10.1',
author = 'OpenFisca Team',
author_email = '[email protected]',
classifiers = [
Expand Down